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