Re: json-rpc behind a proxy server

31 views
Skip to first unread message

Sasha Ovsankin

unread,
Jan 11, 2008, 1:19:19 AM1/11/08
to jabsorb-user, json-rp...@googlegroups.com
Great!

Please note though that this scheme wouldn't support cookie-based authorization (yet).

Getting rid of the Apache HTTP library also removed dependencies on
commons-codec and commons-logging, so this makes a huge difference to
the client which would normally need to download these JAR files.
Indeed, that was one of the reasons to use URLConnection in the first place.

Dominic wrote:
Ok, never mind, I worked it out (it is much simpler now):

var session = JsonRpc.TransportRegistry.i().createSession(hostRoot +
"JSON-RPC");
client = new JsonRpc.Client(session);
var userManager = client.openProxy("userManager",
Uidl.example.UserManager);

Getting rid of the Apache HTTP library also removed dependencies on
commons-codec and commons-logging, so this makes a huge difference to
the client which would normally need to download these JAR files.

Thanks!
Dominic,

On Jan 11, 5:00 pm, Dominic <dominic.cioccare...@gmail.com> wrote:
  
Hi Sasha,

On Jan 11, 4:33 pm, Sasha Ovsankin <sa...@codebistro.com> wrote:

    
OK, so basically the built-in client supports proxies better than commons-httpclient? This is cool.
      
Yep, exactly.

    
Actually, the client does not need the dependency on the commons-httpclient unless you register it explicitly, so all is well :-)
      
How does one do do this? My current code (sorry, in JavaScript) is:

Client = Packages.org.apache.commons.httpclient;
JsonRpc = Packages.org.jabsorb.client;

JsonRpc.HTTPSession.register(JsonRpc.TransportRegistry.i());
var httpSession = JsonRpc.TransportRegistry.i().createSession(hostRoot
+ "JSON-RPC");
setProxy(httpSession);

var state = new Client.HttpState();
httpSession.setState(state);
client = new JsonRpc.Client(httpSession);

var userManager = client.openProxy("userManager",
Uidl.example.UserManager);

So you are saying that I don't need to call httpSession.setState()?

I could only find examples which used the Apache HTTP client.

Regards,
Dominic.
    
  

Stanley Knutson

unread,
Jan 13, 2008, 5:52:09 PM1/13/08
to jabsor...@googlegroups.com

If the class specified by javaClass is missing, the error message does not include the name of the missing class.

 

It’s a simple change to JSONSerializer to print the missing class name.

 

  private Class getClassFromHint(Object o) throws UnmarshallException

  {

    if (o == null)

    {

      return null;

    }

    if (o instanceof JSONObject)

    {

      String class_name = "(unknown)";

      try

      {

        class_name = ((JSONObject) o).getString("javaClass");

        Class clazz = Class.forName(class_name);

        return clazz;

      }

      catch (Exception e)

      {

        throw new UnmarshallException("Class specified in hint not found: " + class_name, e);

      }

    }

    if (o instanceof JSONArray)

    {

      JSONArray arr = (JSONArray) o;

      if (arr.length() == 0)

      {

        throw new UnmarshallException("no type for empty array");

      }

      // return type of first element

      Class compClazz;

      try

      {

        compClazz = getClassFromHint(arr.get(0));

      }

      catch (JSONException e)

      {

        throw (NoSuchElementException) new NoSuchElementException(e.getMessage()).initCause(e);

      }

      try

      {

        if (compClazz.isArray())

        {

          return Class.forName("[" + compClazz.getName());

        }

        return Class.forName("[L" + compClazz.getName() + ";");

      }

      catch (ClassNotFoundException e)

      {

        throw new UnmarshallException("problem getting array type", e);

      }

    }

    return o.getClass();

  }


Arthur Blake

unread,
Jan 13, 2008, 7:53:11 PM1/13/08
to jabsor...@googlegroups.com

On Jan 13, 2008 5:52 PM, Stanley Knutson <sta...@stanleyknutson.com> wrote:

If the class specified by javaClass is missing, the error message does not include the name of the missing class.

 

It's a simple change to JSONSerializer to print the missing class name.


Good idea!  How about this as a slight improvement ? (added extra null check)

      String className = null;
      try
      {
        className = ((JSONObject) o).getString("javaClass");
        if (className == null)
        {
          throw new UnmarshallException("javaClass hint not provided");
        }
        return Class.forName(className);
      }
      catch (Exception e)
      {
        throw new UnmarshallException("Class specified in javaClass hint not found: " + className, e);
      }

Arthur Blake

unread,
Jan 13, 2008, 8:03:17 PM1/13/08
to jabsor...@googlegroups.com
On second thought, my "improvement" wouldn't be good because getString would throw an exception before the null check occurs...
Reply all
Reply to author
Forward
0 new messages