PageRenderTime 25ms CodeModel.GetById 13ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/razweb/src/com/razie/pub/comms/POSTActionToInvoke.java

http://razpub.googlecode.com/
Java | 99 lines | 45 code | 11 blank | 43 comment | 0 complexity | 1a7db2827e52b3903059562a663ac032 MD5 | raw file
 1/**
 2 * Razvan's public code. Copyright 2008 based on Apache license (share alike) see LICENSE.txt for
 3 * details.
 4 */
 5package com.razie.pub.comms;
 6
 7import java.net.MalformedURLException;
 8import java.net.URL;
 9
10import com.razie.pub.comms.Agents;
11
12import razie.base.ActionToInvoke;
13import razie.base.ActionItem;
14import razie.base.AttrAccess;
15import razie.base.ActionContext;
16import razie.base.BaseActionToInvoke;
17import razie.draw.Drawable;
18
19/**
20 * same semantics as ActionToInvike, except it is done via POST, not GET
21 * 
22 * this cannot be printed to a web page...since you can't
23 * 
24 * @author razvanc99
25 */
26public class POSTActionToInvoke extends BaseActionToInvoke implements Cloneable, AttrAccess, Drawable {
27   /**
28    * constructor
29    * 
30    * @param target the prefix used depending on the drawing technology - for http, it's the URL to
31    *        append to
32    * @param item this is the action, contains the actual command name and label to display
33    * @param pairs
34    */
35   public POSTActionToInvoke(String target, ActionItem item, AttrAccess postArgs, Object... pairs) {
36      super(target, item, pairs);
37      this.postArgs = postArgs;
38   }
39
40   /**
41    * constructor
42    * 
43    * @param target the prefix used depending on the drawing technology - for http, it's the URL to
44    *        append to
45    * @param item this is the action, contains the actual command name and label to display
46    * @param pairs
47    */
48   public POSTActionToInvoke(ActionItem item, AttrAccess postArgs, Object... pairs) {
49      super(Agents.me().url, item, pairs);
50      this.postArgs = postArgs;
51   }
52
53   // TODO 3-1 CODE clone args and postargs
54   public POSTActionToInvoke clone() {
55      return new POSTActionToInvoke(this.target, this.actionItem.clone(), this.postArgs, this.toPairs());
56   }
57
58   /**
59    * should not tie this to actual technology, but URLs are the most common form of invoking
60    * actions
61    */
62   public String makeActionUrl() {
63      String url = target.endsWith("/") ? target : target + "/";
64      url += actionItem.name;
65      url = addToUrl(url);
66      return LightAuth.wrapUrl(url);
67   }
68
69   /**
70    * this will take an URL and parse it into the same action object it created it... could be
71    * useful, so we'll implement then - for now it documents the idea
72    */
73   public static ActionToInvoke fromActionUrl(String url) {
74      // TODO FUTURE-implement
75      return null;
76   }
77
78   /**
79    * execute this action in a given context. The context must include me as well?
80    * 
81    * default implementation assumes i need to call an url and get the first line of response
82    */
83   public Object act(ActionContext ctx) {
84      try {
85         URL url = new URL(this.makeActionUrl());
86         // TODO implement the POST
87         return Comms.readUrl(url.toExternalForm());
88      } catch (MalformedURLException e) {
89         throw new RuntimeException("while getting the command url: " + this.makeActionUrl(), e);
90      }
91   }
92
93   @Override
94   public ActionToInvoke args(Object... args) {
95      return new POSTActionToInvoke(this.target, this.actionItem.clone(), this.postArgs, args);
96   }
97
98   public AttrAccess postArgs;
99}