PageRenderTime 38ms CodeModel.GetById 29ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

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