首页

关于jabsorb源码包中session会话定义及HTTPSession、URLConnectionSession源码实现说明

标签:jabsorb,session,会话定义,HTTPSession,URLConnectionSession     发布时间:2018-07-10   

一、前言

关于jabsorb源码包定义org.jabsorb.client.Session会话接口、org.jabsorb.client.HTTPSession及URLConnectionSession实现类,针对同一URL的会话的作用域范围不同实现源码说明。

二、源码说明

1.Session定义i接口

package org.jabsorb.client;@b@@b@import org.json.JSONObject;@b@@b@public abstract interface Session@b@{@b@  public abstract JSONObject sendAndReceive(JSONObject paramJSONObject);@b@@b@  public abstract void close();@b@}
package org.json;@b@@b@import java.io.IOException;@b@import java.io.Writer;@b@import java.lang.reflect.Field;@b@import java.lang.reflect.Method;@b@import java.util.Collection;@b@import java.util.HashMap;@b@import java.util.Iterator;@b@import java.util.Map;@b@import java.util.Set;@b@import java.util.TreeSet;@b@@b@public class JSONObject@b@{@b@  private HashMap myHashMap;@b@  public static final Object NULL = new Null(null);@b@@b@  public JSONObject()@b@  {@b@    this.myHashMap = new HashMap();@b@  }@b@@b@  public JSONObject(JSONObject jo, String[] names)@b@    throws JSONException@b@  {@b@    for (int i = 0; i < names.length; ++i)@b@      putOpt(names[i], jo.opt(names[i]));@b@  }@b@@b@  public JSONObject(JSONTokener x)@b@    throws JSONException@b@  {@b@    if (x.nextClean() != '{')@b@      throw x.syntaxError("A JSONObject text must begin with '{'");@b@    while (true)@b@    {@b@      char c = x.nextClean();@b@      switch (c)@b@      {@b@      case '\0':@b@        throw x.syntaxError("A JSONObject text must end with '}'");@b@      case '}':@b@        return;@b@      }@b@      x.back();@b@      String key = x.nextValue().toString();@b@@b@      c = x.nextClean();@b@      if (c == '=')@b@        if (x.next() != '>')@b@          x.back();@b@@b@      else if (c != ':')@b@        throw x.syntaxError("Expected a ':' after a key");@b@@b@      put(key, x.nextValue());@b@@b@      switch (x.nextClean())@b@      {@b@      case ',':@b@      case ';':@b@        if (x.nextClean() == '}')@b@          return; @b@x.back();@b@      case '}':@b@      }@b@    }@b@    return;@b@@b@    throw x.syntaxError("Expected a ',' or '}'");@b@  }@b@@b@  public JSONObject(Map map)@b@  {@b@    this.myHashMap = new HashMap(map);@b@  }@b@@b@  public JSONObject(Object bean)@b@  {@b@    Class klass = bean.getClass();@b@    Method[] methods = klass.getMethods();@b@    for (int i = 0; i < methods.length; ++i)@b@      try {@b@        Method method = methods[i];@b@        String name = method.getName();@b@        String key = "";@b@        if (name.startsWith("get"))@b@          key = name.substring(3);@b@        else if (name.startsWith("is"))@b@          key = name.substring(2);@b@@b@        if ((key.length() > 0) && (Character.isUpperCase(key.charAt(0))) && (method.getParameterTypes().length == 0))@b@        {@b@          if (key.length() == 1)@b@            key = key.toLowerCase();@b@          else if (!(Character.isUpperCase(key.charAt(1)))) {@b@            key = key.substring(0, 1).toLowerCase() + key.substring(1);@b@          }@b@@b@          put(key, method.invoke(bean, null));@b@        }@b@      }@b@      catch (Exception e)@b@      {@b@      }@b@  }@b@@b@  public JSONObject(Object object, String[] names)@b@  {@b@    Class c = object.getClass();@b@    for (int i = 0; i < names.length; ++i) {@b@      String name = names[i];@b@      try {@b@        Field field = c.getField(name);@b@        Object value = field.get(object);@b@        put(name, value);@b@      }@b@      catch (Exception e)@b@      {@b@      }@b@    }@b@  }@b@@b@  public JSONObject(String source)@b@    throws JSONException@b@  {@b@    this(new JSONTokener(source));@b@  }@b@@b@  public JSONObject accumulate(String key, Object value)@b@    throws JSONException@b@  {@b@    testValidity(value);@b@    Object o = opt(key);@b@    if (o == null) {@b@      put(key, (value instanceof JSONArray) ? new JSONArray().put(value) : value);@b@    }@b@    else if (o instanceof JSONArray)@b@      ((JSONArray)o).put(value);@b@    else@b@      put(key, new JSONArray().put(o).put(value));@b@@b@    return this;@b@  }@b@@b@  public JSONObject append(String key, Object value)@b@    throws JSONException@b@  {@b@    testValidity(value);@b@    Object o = opt(key);@b@    if (o == null)@b@      put(key, new JSONArray().put(value));@b@    else if (o instanceof JSONArray)@b@      put(key, ((JSONArray)o).put(value));@b@    else {@b@      throw new JSONException("JSONObject[" + key + "] is not a JSONArray.");@b@    }@b@@b@    return this;@b@  }@b@@b@  public static String doubleToString(double d)@b@  {@b@    if ((Double.isInfinite(d)) || (Double.isNaN(d))) {@b@      return "null";@b@    }@b@@b@    String s = Double.toString(d);@b@    if ((s.indexOf(46) > 0) && (s.indexOf(101) < 0) && (s.indexOf(69) < 0)) {@b@      while (s.endsWith("0"))@b@        s = s.substring(0, s.length() - 1);@b@@b@      if (s.endsWith("."))@b@        s = s.substring(0, s.length() - 1);@b@    }@b@@b@    return s;@b@  }@b@@b@  public Object get(String key)@b@    throws JSONException@b@  {@b@    Object o = opt(key);@b@    if (o == null) {@b@      throw new JSONException("JSONObject[" + quote(key) + "] not found.");@b@    }@b@@b@    return o;@b@  }@b@@b@  public boolean getBoolean(String key)@b@    throws JSONException@b@  {@b@    Object o = get(key);@b@    if ((o.equals(Boolean.FALSE)) || ((o instanceof String) && (((String)o).equalsIgnoreCase("false"))))@b@    {@b@      return false; }@b@    if ((o.equals(Boolean.TRUE)) || ((o instanceof String) && (((String)o).equalsIgnoreCase("true"))))@b@    {@b@      return true;@b@    }@b@    throw new JSONException("JSONObject[" + quote(key) + "] is not a Boolean.");@b@  }@b@@b@  public double getDouble(String key)@b@    throws JSONException@b@  {@b@    Object o = get(key);@b@    try {@b@      return ((o instanceof Number) ? ((Number)o).doubleValue() : Double.valueOf((String)o).doubleValue());@b@    }@b@    catch (Exception e)@b@    {@b@      throw new JSONException("JSONObject[" + quote(key) + "] is not a number.");@b@    }@b@  }@b@@b@  public int getInt(String key)@b@    throws JSONException@b@  {@b@    Object o = get(key);@b@    return ((o instanceof Number) ? ((Number)o).intValue() : (int)getDouble(key));@b@  }@b@@b@  public JSONArray getJSONArray(String key)@b@    throws JSONException@b@  {@b@    Object o = get(key);@b@    if (o instanceof JSONArray)@b@      return ((JSONArray)o);@b@@b@    throw new JSONException("JSONObject[" + quote(key) + "] is not a JSONArray.");@b@  }@b@@b@  public JSONObject getJSONObject(String key)@b@    throws JSONException@b@  {@b@    Object o = get(key);@b@    if (o instanceof JSONObject)@b@      return ((JSONObject)o);@b@@b@    throw new JSONException("JSONObject[" + quote(key) + "] is not a JSONObject.");@b@  }@b@@b@  public long getLong(String key)@b@    throws JSONException@b@  {@b@    Object o = get(key);@b@    return ((o instanceof Number) ? ((Number)o).longValue() : ()getDouble(key));@b@  }@b@@b@  public static String[] getNames(JSONObject jo)@b@  {@b@    int length = jo.length();@b@    if (length == 0)@b@      return null;@b@@b@    Iterator i = jo.keys();@b@    String[] names = new String[length];@b@    int j = 0;@b@    while (i.hasNext()) {@b@      names[j] = ((String)i.next());@b@      ++j;@b@    }@b@    return names;@b@  }@b@@b@  public static String[] getNames(Object object)@b@  {@b@    if (object == null)@b@      return null;@b@@b@    Class klass = object.getClass();@b@    Field[] fields = klass.getFields();@b@    int length = fields.length;@b@    if (length == 0)@b@      return null;@b@@b@    String[] names = new String[length];@b@    for (int i = 0; i < length; ++i)@b@      names[i] = fields[i].getName();@b@@b@    return names;@b@  }@b@@b@  public String getString(String key)@b@    throws JSONException@b@  {@b@    return get(key).toString();@b@  }@b@@b@  public boolean has(String key)@b@  {@b@    return this.myHashMap.containsKey(key);@b@  }@b@@b@  public boolean isNull(String key)@b@  {@b@    return NULL.equals(opt(key));@b@  }@b@@b@  public Iterator keys()@b@  {@b@    return this.myHashMap.keySet().iterator();@b@  }@b@@b@  public int length()@b@  {@b@    return this.myHashMap.size();@b@  }@b@@b@  public JSONArray names()@b@  {@b@    JSONArray ja = new JSONArray();@b@    Iterator keys = keys();@b@    while (keys.hasNext())@b@      ja.put(keys.next());@b@@b@    return ((ja.length() == 0) ? null : ja);@b@  }@b@@b@  public static String numberToString(Number n)@b@    throws JSONException@b@  {@b@    if (n == null)@b@      throw new JSONException("Null pointer");@b@@b@    testValidity(n);@b@@b@    String s = n.toString();@b@    if ((s.indexOf(46) > 0) && (s.indexOf(101) < 0) && (s.indexOf(69) < 0)) {@b@      while (s.endsWith("0"))@b@        s = s.substring(0, s.length() - 1);@b@@b@      if (s.endsWith("."))@b@        s = s.substring(0, s.length() - 1);@b@    }@b@@b@    return s;@b@  }@b@@b@  public Object opt(String key)@b@  {@b@    return ((key == null) ? null : this.myHashMap.get(key));@b@  }@b@@b@  public boolean optBoolean(String key)@b@  {@b@    return optBoolean(key, false);@b@  }@b@@b@  public boolean optBoolean(String key, boolean defaultValue)@b@  {@b@    try@b@    {@b@      return getBoolean(key); } catch (Exception e) {@b@    }@b@    return defaultValue;@b@  }@b@@b@  public JSONObject put(String key, Collection value)@b@    throws JSONException@b@  {@b@    put(key, new JSONArray(value));@b@    return this;@b@  }@b@@b@  public double optDouble(String key)@b@  {@b@    return optDouble(key, (0.0D / 0.0D));@b@  }@b@@b@  public double optDouble(String key, double defaultValue)@b@  {@b@    Object o;@b@    try@b@    {@b@      o = opt(key);@b@      return ((o instanceof Number) ? ((Number)o).doubleValue() : new Double((String)o).doubleValue());@b@    } catch (Exception e) {@b@    }@b@    return defaultValue;@b@  }@b@@b@  public int optInt(String key)@b@  {@b@    return optInt(key, 0);@b@  }@b@@b@  public int optInt(String key, int defaultValue)@b@  {@b@    try@b@    {@b@      return getInt(key); } catch (Exception e) {@b@    }@b@    return defaultValue;@b@  }@b@@b@  public JSONArray optJSONArray(String key)@b@  {@b@    Object o = opt(key);@b@    return ((o instanceof JSONArray) ? (JSONArray)o : null);@b@  }@b@@b@  public JSONObject optJSONObject(String key)@b@  {@b@    Object o = opt(key);@b@    return ((o instanceof JSONObject) ? (JSONObject)o : null);@b@  }@b@@b@  public long optLong(String key)@b@  {@b@    return optLong(key, 0L);@b@  }@b@@b@  public long optLong(String key, long defaultValue)@b@  {@b@    try@b@    {@b@      return getLong(key); } catch (Exception e) {@b@    }@b@    return defaultValue;@b@  }@b@@b@  public String optString(String key)@b@  {@b@    return optString(key, "");@b@  }@b@@b@  public String optString(String key, String defaultValue)@b@  {@b@    Object o = opt(key);@b@    return ((o != null) ? o.toString() : defaultValue);@b@  }@b@@b@  public JSONObject put(String key, boolean value)@b@    throws JSONException@b@  {@b@    put(key, (value) ? Boolean.TRUE : Boolean.FALSE);@b@    return this;@b@  }@b@@b@  public JSONObject put(String key, double value)@b@    throws JSONException@b@  {@b@    put(key, new Double(value));@b@    return this;@b@  }@b@@b@  public JSONObject put(String key, int value)@b@    throws JSONException@b@  {@b@    put(key, new Integer(value));@b@    return this;@b@  }@b@@b@  public JSONObject put(String key, long value)@b@    throws JSONException@b@  {@b@    put(key, new Long(value));@b@    return this;@b@  }@b@@b@  public JSONObject put(String key, Map value)@b@    throws JSONException@b@  {@b@    put(key, new JSONObject(value));@b@    return this;@b@  }@b@@b@  public JSONObject put(String key, Object value)@b@    throws JSONException@b@  {@b@    if (key == null)@b@      throw new JSONException("Null key.");@b@@b@    if (value != null) {@b@      testValidity(value);@b@      this.myHashMap.put(key, value);@b@    } else {@b@      remove(key);@b@    }@b@    return this;@b@  }@b@@b@  public JSONObject putOpt(String key, Object value)@b@    throws JSONException@b@  {@b@    if ((key != null) && (value != null))@b@      put(key, value);@b@@b@    return this;@b@  }@b@@b@  public static String quote(String string)@b@  {@b@    if ((string == null) || (string.length() == 0)) {@b@      return "\"\"";@b@    }@b@@b@    char c = ';@b@@b@    int len = string.length();@b@    StringBuffer sb = new StringBuffer(len + 4);@b@@b@    sb.append('"');@b@    for (int i = 0; i < len; ++i) {@b@      char b = c;@b@      c = string.charAt(i);@b@      switch (c)@b@      {@b@      case '"':@b@      case '\\':@b@        sb.append('\\');@b@        sb.append(c);@b@        break;@b@      case '/':@b@        if (b == '<')@b@          sb.append('\\');@b@@b@        sb.append(c);@b@        break;@b@      case '\b':@b@        sb.append("\\b");@b@        break;@b@      case '\t':@b@        sb.append("\\t");@b@        break;@b@      case '\n':@b@        sb.append("\\n");@b@        break;@b@      case '\f':@b@        sb.append("\\f");@b@        break;@b@      case '\r':@b@        sb.append("\\r");@b@        break;@b@      default:@b@        if ((c < ' ') || ((c >= 128) && (c < 160)) || ((c >= 8192) && (c < 8448)))@b@        {@b@          String t = "000" + Integer.toHexString(c);@b@          sb.append("\\u" + t.substring(t.length() - 4));@b@        } else {@b@          sb.append(c);@b@        }@b@      }@b@    }@b@    sb.append('"');@b@    return sb.toString();@b@  }@b@@b@  public Object remove(String key)@b@  {@b@    return this.myHashMap.remove(key);@b@  }@b@@b@  public Iterator sortedKeys()@b@  {@b@    return new TreeSet(this.myHashMap.keySet()).iterator();@b@  }@b@@b@  static void testValidity(Object o)@b@    throws JSONException@b@  {@b@    if (o != null) {@b@      if (o instanceof Double) {@b@        if ((!(((Double)o).isInfinite())) && (!(((Double)o).isNaN()))) return;@b@        throw new JSONException("JSON does not allow non-finite numbers.");@b@      }@b@@b@      if ((o instanceof Float) && ((@b@        (((Float)o).isInfinite()) || (((Float)o).isNaN()))))@b@        throw new JSONException("JSON does not allow non-finite numbers.");@b@    }@b@  }@b@@b@  public JSONArray toJSONArray(JSONArray names)@b@    throws JSONException@b@  {@b@    if ((names == null) || (names.length() == 0))@b@      return null;@b@@b@    JSONArray ja = new JSONArray();@b@    for (int i = 0; i < names.length(); ++i)@b@      ja.put(opt(names.getString(i)));@b@@b@    return ja;@b@  }@b@@b@  public String toString()@b@  {@b@    Iterator keys;@b@    try@b@    {@b@      keys = keys();@b@      StringBuffer sb = new StringBuffer("{");@b@@b@      while (keys.hasNext()) {@b@        if (sb.length() > 1)@b@          sb.append(',');@b@@b@        Object o = keys.next();@b@        sb.append(quote(o.toString()));@b@        sb.append(':');@b@        sb.append(valueToString(this.myHashMap.get(o)));@b@      }@b@      sb.append('}');@b@      return sb.toString(); } catch (Exception e) {@b@    }@b@    return null;@b@  }@b@@b@  public String toString(int indentFactor)@b@    throws JSONException@b@  {@b@    return toString(indentFactor, 0);@b@  }@b@@b@  String toString(int indentFactor, int indent)@b@    throws JSONException@b@  {@b@    int j;@b@    Object o;@b@    int n = length();@b@    if (n == 0)@b@      return "{}";@b@@b@    Iterator keys = sortedKeys();@b@    StringBuffer sb = new StringBuffer("{");@b@    int newindent = indent + indentFactor;@b@@b@    if (n == 1) {@b@      o = keys.next();@b@      sb.append(quote(o.toString()));@b@      sb.append(": ");@b@      sb.append(valueToString(this.myHashMap.get(o), indentFactor, indent));@b@    }@b@    else {@b@      while (keys.hasNext()) {@b@        o = keys.next();@b@        if (sb.length() > 1)@b@          sb.append(",\n");@b@        else@b@          sb.append('\n');@b@@b@        for (j = 0; j < newindent; ++j)@b@          sb.append(' ');@b@@b@        sb.append(quote(o.toString()));@b@        sb.append(": ");@b@        sb.append(valueToString(this.myHashMap.get(o), indentFactor, newindent));@b@      }@b@@b@      if (sb.length() > 1) {@b@        sb.append('\n');@b@        for (j = 0; j < indent; ++j)@b@          sb.append(' ');@b@      }@b@    }@b@@b@    sb.append('}');@b@    return sb.toString();@b@  }@b@@b@  static String valueToString(Object value)@b@    throws JSONException@b@  {@b@    if ((value == null) || (value.equals(null)))@b@      return "null";@b@@b@    if (value instanceof JSONString) {@b@      Object o;@b@      try {@b@        o = ((JSONString)value).toJSONString();@b@      } catch (Exception e) {@b@        throw new JSONException(e);@b@      }@b@      if (o instanceof String)@b@        return ((String)o);@b@@b@      throw new JSONException("Bad value from toJSONString: " + o);@b@    }@b@    if (value instanceof Number)@b@      return numberToString((Number)value);@b@@b@    if ((value instanceof Boolean) || (value instanceof JSONObject) || (value instanceof JSONArray))@b@    {@b@      return value.toString();@b@    }@b@    if (value instanceof Map)@b@      return new JSONObject((Map)value).toString();@b@@b@    if (value instanceof Collection)@b@      return new JSONArray((Collection)value).toString();@b@@b@    if (value.getClass().isArray())@b@      return new JSONArray(value).toString();@b@@b@    return quote(value.toString());@b@  }@b@@b@  static String valueToString(Object value, int indentFactor, int indent)@b@    throws JSONException@b@  {@b@    if ((value == null) || (value.equals(null)))@b@      return "null";@b@    try@b@    {@b@      if (value instanceof JSONString) {@b@        Object o = ((JSONString)value).toJSONString();@b@        if (o instanceof String)@b@          return ((String)o);@b@      }@b@    }@b@    catch (Exception e)@b@    {@b@    }@b@    if (value instanceof Number)@b@      return numberToString((Number)value);@b@@b@    if (value instanceof Boolean)@b@      return value.toString();@b@@b@    if (value instanceof JSONObject)@b@      return ((JSONObject)value).toString(indentFactor, indent);@b@@b@    if (value instanceof JSONArray)@b@      return ((JSONArray)value).toString(indentFactor, indent);@b@@b@    if (value instanceof Map)@b@      return new JSONObject((Map)value).toString(indentFactor, indent);@b@@b@    if (value instanceof Collection)@b@      return new JSONArray((Collection)value).toString(indentFactor, indent);@b@@b@    if (value.getClass().isArray())@b@      return new JSONArray(value).toString(indentFactor, indent);@b@@b@    return quote(value.toString());@b@  }@b@@b@  public Writer write(Writer writer)@b@    throws JSONException@b@  {@b@    boolean b;@b@    try@b@    {@b@      b = false;@b@      Iterator keys = keys();@b@      writer.write(123);@b@@b@      while (keys.hasNext()) {@b@        if (b)@b@          writer.write(44);@b@@b@        Object k = keys.next();@b@        writer.write(quote(k.toString()));@b@        writer.write(58);@b@        Object v = this.myHashMap.get(k);@b@        if (v instanceof JSONObject)@b@          ((JSONObject)v).write(writer);@b@        else if (v instanceof JSONArray)@b@          ((JSONArray)v).write(writer);@b@        else@b@          writer.write(valueToString(v));@b@@b@        b = true;@b@      }@b@      writer.write(125);@b@      return writer;@b@    } catch (IOException e) {@b@      throw new JSONException(e);@b@    }@b@  }@b@@b@  private static final class Null@b@  {@b@    private Null()@b@    {@b@    }@b@@b@    protected final Object clone()@b@    {@b@      return this;@b@    }@b@@b@    public boolean equals(Object object)@b@    {@b@      return ((object == null) || (object == this));@b@    }@b@@b@    public String toString()@b@    {@b@      return "null";@b@    }@b@@b@    Null(JSONObject.1 x0)@b@    {@b@    }@b@  }@b@}

2.HTTPSession、URLConnectionSession实现类

package org.jabsorb.client;@b@@b@import java.io.IOException;@b@import java.net.URI;@b@import org.apache.commons.httpclient.HostConfiguration;@b@import org.apache.commons.httpclient.HttpClient;@b@import org.apache.commons.httpclient.HttpException;@b@import org.apache.commons.httpclient.HttpState;@b@import org.apache.commons.httpclient.HttpStatus;@b@import org.apache.commons.httpclient.methods.PostMethod;@b@import org.apache.commons.httpclient.methods.RequestEntity;@b@import org.apache.commons.httpclient.methods.StringRequestEntity;@b@import org.json.JSONException;@b@import org.json.JSONObject;@b@import org.json.JSONTokener;@b@import org.slf4j.Logger;@b@import org.slf4j.LoggerFactory;@b@@b@public class HTTPSession@b@  implements Session@b@{@b@  private static final Logger log = LoggerFactory.getLogger(HTTPSession.class);@b@  protected HttpClient client;@b@  protected HttpState state;@b@  protected URI uri;@b@  static final String JSON_CONTENT_TYPE = "application/json";@b@@b@  public HTTPSession(URI uri)@b@  {@b@    this.uri = uri;@b@  }@b@@b@  public void setState(HttpState state)@b@  {@b@    this.state = state;@b@  }@b@@b@  public JSONObject sendAndReceive(JSONObject message)@b@  {@b@    try@b@    {@b@      if (log.isDebugEnabled())@b@      {@b@        log.debug("Sending: " + message.toString(2));@b@      }@b@      PostMethod postMethod = new PostMethod(this.uri.toString());@b@      postMethod.setRequestHeader("Content-Type", "text/plain");@b@@b@      RequestEntity requestEntity = new StringRequestEntity(message.toString(), "application/json", null);@b@@b@      postMethod.setRequestEntity(requestEntity);@b@@b@      http().executeMethod(null, postMethod, this.state);@b@      int statusCode = postMethod.getStatusCode();@b@      if (statusCode != 200)@b@        throw new ClientError("HTTP Status - " + HttpStatus.getStatusText(statusCode) + " (" + statusCode + ")");@b@@b@      JSONTokener tokener = new JSONTokener(postMethod.getResponseBodyAsString());@b@@b@      Object rawResponseMessage = tokener.nextValue();@b@      JSONObject responseMessage = (JSONObject)rawResponseMessage;@b@      if (responseMessage == null)@b@        throw new ClientError("Invalid response type - " + rawResponseMessage.getClass());@b@@b@      return responseMessage;@b@    }@b@    catch (HttpException e)@b@    {@b@      throw new ClientError(e);@b@    }@b@    catch (IOException e)@b@    {@b@      throw new ClientError(e);@b@    }@b@    catch (JSONException e)@b@    {@b@      throw new ClientError(e);@b@    }@b@  }@b@@b@  public HostConfiguration getHostConfiguration()@b@  {@b@    return http().getHostConfiguration();@b@  }@b@@b@  HttpClient http()@b@  {@b@    if (this.client == null)@b@    {@b@      this.client = new HttpClient();@b@      if (this.state == null)@b@      {@b@        this.state = new HttpState();@b@      }@b@      this.client.setState(this.state);@b@    }@b@    return this.client;@b@  }@b@@b@  public void close()@b@  {@b@    this.state.clear();@b@    this.state = null;@b@  }@b@@b@  public static void register(TransportRegistry registry)@b@  {@b@    registry.registerTransport("http", new Factory());@b@  }@b@@b@  static class Factory@b@    implements TransportRegistry.SessionFactory@b@  {@b@    public Session newSession(URI uri)@b@    {@b@      return new HTTPSession(uri);@b@    }@b@  }@b@}
package org.jabsorb.client;@b@@b@import java.io.IOException;@b@import java.io.InputStreamReader;@b@import java.io.OutputStreamWriter;@b@import java.io.Reader;@b@import java.io.Writer;@b@import java.net.URL;@b@import java.net.URLConnection;@b@import org.json.JSONException;@b@import org.json.JSONObject;@b@import org.json.JSONTokener;@b@@b@public class URLConnectionSession@b@  implements Session@b@{@b@  URL url;@b@@b@  URLConnectionSession(URL url)@b@  {@b@    this.url = url;@b@  }@b@@b@  public void close()@b@  {@b@  }@b@@b@  public JSONObject sendAndReceive(JSONObject message)@b@  {@b@    URLConnection connection;@b@    try {@b@      connection = this.url.openConnection();@b@      connection.setDoOutput(true);@b@@b@      Writer request = new OutputStreamWriter(connection.getOutputStream());@b@      request.write(message.toString());@b@      request.close();@b@@b@      StringBuffer builder = new StringBuffer(1024);@b@      char[] buffer = new char[1024];@b@      Reader reader = new InputStreamReader(connection.getInputStream());@b@      while (true)@b@      {@b@        int bytesRead = reader.read(buffer);@b@        if (bytesRead < 0)@b@          break;@b@        builder.append(buffer, 0, bytesRead);@b@      }@b@      reader.close();@b@      JSONTokener tokener = new JSONTokener(builder.toString());@b@      Object rawResponseMessage = tokener.nextValue();@b@      JSONObject responseMessage = (JSONObject)rawResponseMessage;@b@      if (responseMessage == null)@b@        throw new ClientError("Invalid response type - " + rawResponseMessage.getClass());@b@@b@      return responseMessage;@b@    }@b@    catch (IOException ex)@b@    {@b@      throw new ClientError(ex);@b@    }@b@    catch (JSONException ex)@b@    {@b@      throw new ClientError(ex);@b@    }@b@  }@b@}

3.其它依赖类JSONTokener、TransportRegistry

package org.json;@b@@b@public class JSONTokener@b@{@b@  private int myIndex;@b@  private String mySource;@b@@b@  public JSONTokener(String s)@b@  {@b@    this.myIndex = 0;@b@    this.mySource = s;@b@  }@b@@b@  public void back()@b@  {@b@    if (this.myIndex > 0)@b@      this.myIndex -= 1;@b@  }@b@@b@  public static int dehexchar(char c)@b@  {@b@    if ((c >= '0') && (c <= '9'))@b@      return (c - '0');@b@@b@    if ((c >= 'A') && (c <= 'F'))@b@      return (c - '7');@b@@b@    if ((c >= 'a') && (c <= 'f'))@b@      return (c - 'W');@b@@b@    return -1;@b@  }@b@@b@  public boolean more()@b@  {@b@    return (this.myIndex < this.mySource.length());@b@  }@b@@b@  public char next()@b@  {@b@    if (more()) {@b@      char c = this.mySource.charAt(this.myIndex);@b@      this.myIndex += 1;@b@      return c;@b@    }@b@    return ';@b@  }@b@@b@  public char next(char c)@b@    throws JSONException@b@  {@b@    char n = next();@b@    if (n != c) {@b@      throw syntaxError("Expected '" + c + "' and instead saw '" + n + "'");@b@    }@b@@b@    return n;@b@  }@b@@b@  public String next(int n)@b@    throws JSONException@b@  {@b@    int i = this.myIndex;@b@    int j = i + n;@b@    if (j >= this.mySource.length())@b@      throw syntaxError("Substring bounds error");@b@@b@    this.myIndex += n;@b@    return this.mySource.substring(i, j);@b@  }@b@@b@  public char nextClean()@b@    throws JSONException@b@  {@b@    while (true)@b@    {@b@      char c = next();@b@      if (c == '/');@b@      switch (next()) {@b@      case '/':@b@        do {@b@          c = next();@b@          if ((c == '\n') || (c == '\r')) break label154;  } while (c != 0);@b@        break;@b@      case '*':@b@        while (true) {@b@          do {@b@            c = next();@b@            if (c == 0)@b@              throw syntaxError("Unclosed comment");@b@          }@b@          while (c != '*');@b@          if (next() == '/')@b@            break label154:@b@@b@          back();@b@        }@b@@b@      default:@b@        back();@b@        return '/';@b@@b@        label154: if (c == '#')@b@          do {@b@            c = next();@b@            if ((c == '\n') || (c == '\r')) break label154;  }@b@          while (c != 0);@b@        else if ((c == 0) || (c > ' '))@b@          return c;@b@      }@b@    }@b@  }@b@@b@  public String nextString(char quote)@b@    throws JSONException@b@  {@b@    StringBuffer sb = new StringBuffer();@b@    while (true) { char c;@b@      while (true) { while (true) { while (true) { while (true) { while (true) { while (true) { while (true) { while (true) { c = next();@b@                      switch (c)@b@                      {@b@                      case '\0':@b@                      case '\n':@b@                      case '\r':@b@                        throw syntaxError("Unterminated string");@b@                      case '\\':@b@                        c = next();@b@                        switch (c) { case 'b':@b@                          sb.append('\b');@b@                        case 't':@b@                        case 'n':@b@                        case 'f':@b@                        case 'r':@b@                        case 'u':@b@                        case 'x':@b@                        case 'c':@b@                        case 'd':@b@                        case 'e':@b@                        case 'g':@b@                        case 'h':@b@                        case 'i':@b@                        case 'j':@b@                        case 'k':@b@                        case 'l':@b@                        case 'm':@b@                        case 'o':@b@                        case 'p':@b@                        case 'q':@b@                        case 's':@b@                        case 'v':@b@                        case 'w': }  }  } sb.append('\t');@b@                  }@b@@b@                  sb.append('\n');@b@                }@b@@b@                sb.append('\f');@b@              }@b@@b@              sb.append('\r');@b@            }@b@@b@            sb.append((char)Integer.parseInt(next(4), 16));@b@          }@b@@b@          sb.append((char)Integer.parseInt(next(2), 16));@b@        }@b@@b@        sb.append(c);@b@      }@b@@b@      if (c == quote)@b@        return sb.toString();@b@@b@      sb.append(c);@b@    }@b@  }@b@@b@  public String nextTo(char d)@b@  {@b@    StringBuffer sb = new StringBuffer();@b@    while (true) {@b@      char c = next();@b@      if ((c == d) || (c == 0) || (c == '\n') || (c == '\r')) {@b@        if (c != 0)@b@          back();@b@@b@        return sb.toString().trim();@b@      }@b@      sb.append(c);@b@    }@b@  }@b@@b@  public String nextTo(String delimiters)@b@  {@b@    StringBuffer sb = new StringBuffer();@b@    while (true) {@b@      char c = next();@b@      if ((delimiters.indexOf(c) >= 0) || (c == 0) || (c == '\n') || (c == '\r'))@b@      {@b@        if (c != 0)@b@          back();@b@@b@        return sb.toString().trim();@b@      }@b@      sb.append(c);@b@    }@b@  }@b@@b@  public Object nextValue()@b@    throws JSONException@b@  {@b@    char c = nextClean();@b@@b@    switch (c)@b@    {@b@    case '"':@b@    case '\'':@b@      return nextString(c);@b@    case '{':@b@      back();@b@      return new JSONObject(this);@b@    case '(':@b@    case '[':@b@      back();@b@      return new JSONArray(this);@b@    }@b@@b@    StringBuffer sb = new StringBuffer();@b@    char b = c;@b@    while ((c >= ' ') && (",:]}/\\\"[{;=#".indexOf(c) < 0)) {@b@      sb.append(c);@b@      c = next();@b@    }@b@    back();@b@@b@    String s = sb.toString().trim();@b@    if (s.equals(""))@b@      throw syntaxError("Missing value");@b@@b@    if (s.equalsIgnoreCase("true"))@b@      return Boolean.TRUE;@b@@b@    if (s.equalsIgnoreCase("false"))@b@      return Boolean.FALSE;@b@@b@    if (s.equalsIgnoreCase("null")) {@b@      return JSONObject.NULL;@b@    }@b@@b@    if (((b >= '0') && (b <= '9')) || (b == '.') || (b == '-') || (b == '+')) {@b@      if ((b != '0') || (@b@        (s.length() > 2) && (((s.charAt(1) == 'x') || (s.charAt(1) == 'X')))));@b@      try@b@      {@b@        return new Integer(Integer.parseInt(s.substring(2), 16));@b@      }@b@      catch (Exception e)@b@      {@b@        try@b@        {@b@          return new Integer(Integer.parseInt(s, 8));@b@        }@b@        catch (Exception e)@b@        {@b@          try@b@          {@b@            return new Integer(s);@b@          } catch (Exception e) {@b@            try {@b@              return new Long(s);@b@            } catch (Exception f) {@b@              try {@b@                return new Double(s);@b@              } catch (Exception g) {@b@                return s; } }@b@          }@b@        }@b@      }@b@    }@b@    return s;@b@  }@b@@b@  public char skipTo(char to)@b@  {@b@    char c;@b@    int index = this.myIndex;@b@    do {@b@      c = next();@b@      if (c == 0) {@b@        this.myIndex = index;@b@        return c; }@b@    }@b@    while (c != to);@b@    back();@b@    return c;@b@  }@b@@b@  public boolean skipPast(String to)@b@  {@b@    this.myIndex = this.mySource.indexOf(to, this.myIndex);@b@    if (this.myIndex < 0) {@b@      this.myIndex = this.mySource.length();@b@      return false;@b@    }@b@    this.myIndex += to.length();@b@    return true;@b@  }@b@@b@  public JSONException syntaxError(String message)@b@  {@b@    return new JSONException(message + toString());@b@  }@b@@b@  public String toString()@b@  {@b@    return " at character " + this.myIndex + " of " + this.mySource;@b@  }@b@}
package org.jabsorb.client;@b@@b@import java.net.URI;@b@import java.util.HashMap;@b@@b@public class TransportRegistry@b@{@b@  private static TransportRegistry singleton;@b@  private RegistryMap registry = new RegistryMap();@b@@b@  public static synchronized TransportRegistry i()@b@  {@b@    if (singleton == null)@b@    {@b@      singleton = new TransportRegistry();@b@    }@b@    return singleton;@b@  }@b@@b@  public void registerTransport(String scheme, SessionFactory factory)@b@  {@b@    this.registry.put(scheme, factory);@b@  }@b@@b@  public Session createSession(String uriString)@b@  {@b@    URI uri;@b@    try@b@    {@b@      uri = new URI(uriString);@b@      SessionFactory found = this.registry.get(uri.getScheme());@b@      if (found != null)@b@      {@b@        return found.newSession(uri);@b@      }@b@@b@      return new URLConnectionSession(uri.toURL());@b@    }@b@    catch (Exception e)@b@    {@b@      throw new ClientError(e);@b@    }@b@  }@b@@b@  public static abstract interface SessionFactory@b@  {@b@    public abstract Session newSession(URI paramURI);@b@  }@b@@b@  static class RegistryMap@b@  {@b@    HashMap rep;@b@@b@    public RegistryMap()@b@    {@b@      this.rep = new HashMap();@b@    }@b@@b@    public TransportRegistry.SessionFactory get(String key)@b@    {@b@      return ((TransportRegistry.SessionFactory)this.rep.get(key));@b@    }@b@@b@    public TransportRegistry.SessionFactory put(String key, TransportRegistry.SessionFactory value)@b@    {@b@      return ((TransportRegistry.SessionFactory)this.rep.put(key, value));@b@    }@b@  }@b@}