/razpub/src/com/razie/pub/lightsoa/HttpAssetSoaBinding.java

http://razpub.googlecode.com/ · Java · 107 lines · 63 code · 17 blank · 27 comment · 12 complexity · 0dddadf6084590fa17c16ae5766b9b40 MD5 · raw file

  1. /**
  2. * Razvan's code. Copyright 2008 based on Apache (share alike) see LICENSE.txt for details.
  3. */
  4. package com.razie.pub.lightsoa;
  5. import java.util.HashMap;
  6. import java.util.Map;
  7. import java.util.Properties;
  8. import razie.assets.AssetKey;
  9. import razie.assets.AssetLocation;
  10. import razie.assets.AssetMap;
  11. import razie.assets.Meta;
  12. import razie.base.AttrAccess;
  13. import razie.draw.DrawStream;
  14. import razie.draw.Technology;
  15. import com.razie.pub.assets.JavaAssetMgr;
  16. import com.razie.pub.base.data.HttpUtils;
  17. import com.razie.pub.comms.MyServerSocket;
  18. import com.razie.pub.http.StreamConsumedReply;
  19. /**
  20. * special binding for assets.
  21. *
  22. * there is only one instance of this, registered as the service name "asset"
  23. *
  24. * @author razvanc99
  25. */
  26. public class HttpAssetSoaBinding extends HttpSoaBinding {
  27. Map<String, HttpSoaBinding> bindings = new HashMap<String, HttpSoaBinding>();
  28. // hack: null has no methods and, in HttpSoaBinding, will default to delegating to AssetMgr
  29. static HttpSoaBinding defaultAssetBinding = new HttpSoaBinding((Class<?>) null, "");
  30. /**
  31. * create a simple binding - you then have to register it with the server
  32. */
  33. public HttpAssetSoaBinding() {
  34. // dummy - i have no soa methods
  35. super(HttpAssetSoaBinding.class, "asset");
  36. // now should register all asset types as listeners...
  37. }
  38. /** if meta not passed in, must be annotated with SoaAsset and have the meta set */
  39. public void register(Class<?> c, Meta... meta) {
  40. String m = meta.length > 0 && meta[0] != null ? meta[0].getId().name : ((SoaAsset) c
  41. .getAnnotation(SoaAsset.class)).meta();
  42. if (!bindings.containsKey(m))
  43. bindings.put(m, new HttpSoaBinding(c, m));
  44. }
  45. public boolean has(String type) {
  46. return bindings.containsKey(type);
  47. }
  48. @Override
  49. public Object execServer(String actionName, String protocol, String cmdargs, Properties parms,
  50. MyServerSocket socket) {
  51. // two ways to specify an asset: /asset/TYPE/KEY/ (localassets) or /asset/razie.uri....(local
  52. // and remote)
  53. AssetKey key = null;
  54. if (actionName.startsWith(razie.assets.AssetKey$.MODULE$.PREFIX()))
  55. // url is /asset/KEY/cmd
  56. key = razie.assets.AssetKey$.MODULE$.fromString(HttpUtils.fromUrlEncodedString(actionName));
  57. else if (cmdargs.length() == 0) {
  58. // url is /asset/TYPE - by convention, list all of type
  59. listLocal (actionName, "", true, makeDrawStream(socket, protocol));
  60. return new StreamConsumedReply();
  61. } else {
  62. // url is /asset/TYPE/KEY/cmd
  63. String[] xx = cmdargs.split("/", 2);
  64. key = new AssetKey(actionName, HttpUtils.fromUrlEncodedString(xx[0]), null);
  65. }
  66. HttpSoaBinding binding = bindings.get(key.getType());
  67. if (binding == null) {
  68. // throw new IllegalArgumentException("ERR_HTTPSOAASSET type is not bound: " +
  69. // key.getType());
  70. // try to call a generic asset via its inventory or other injection mechanism...
  71. return defaultAssetBinding.execServer(actionName, protocol, cmdargs, parms, socket);
  72. } else
  73. return binding.execServer(actionName, protocol, cmdargs, parms, socket);
  74. }
  75. /** list some assets directly to the output stream */
  76. public static void listLocal(String ttype, String location, boolean recurse, DrawStream out) {
  77. AssetLocation env = razie.assets.AssetLocation$.MODULE$.mutantEnv(location);
  78. AssetMap movies = JavaAssetMgr.find(ttype, env, recurse);
  79. if (Technology.TEXT.equals(out.getTechnology())) {
  80. out.write(movies.toString());
  81. } else {
  82. JavaAssetMgr.pres().toDrawable(movies.jvalues(), out, null);
  83. }
  84. }
  85. /** invoke a lightsoa method on a given service */
  86. public Object invokeLocal(AssetKey key, String action, AttrAccess inparms) {
  87. HttpSoaBinding binding = bindings.get(key.getType());
  88. return binding.invoke(key, action, inparms);
  89. }
  90. }