Search in sources :

Example 1 with UnmarshallException

use of org.jabsorb.serializer.UnmarshallException in project wonder-slim by undur.

the class NSTimestampSerializer method tryUnmarshall.

public ObjectMatch tryUnmarshall(SerializerState state, Class clazz, Object o) throws UnmarshallException {
    try {
        JSONObject jso = (JSONObject) o;
        String java_class = jso.getString("javaClass");
        if (java_class == null) {
            throw new UnmarshallException("no type hint");
        }
        if (!(java_class.equals("com.webobjects.foundation.NSTimestamp"))) {
            throw new UnmarshallException("not a NSTimestamp");
        }
        long time = jso.getLong("time");
        String tz = jso.getString("tz");
        return ObjectMatch.OKAY;
    } catch (JSONException e) {
        throw new UnmarshallException("Failed to unmarshall NSTimestamp.", e);
    }
}
Also used : JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) UnmarshallException(org.jabsorb.serializer.UnmarshallException)

Example 2 with UnmarshallException

use of org.jabsorb.serializer.UnmarshallException in project wonder-slim by undur.

the class JSONRPCBridge method tryUnmarshallArgs.

/**
 * Tries to unmarshall the arguments to a method
 *
 * @param method The method to unmarshall the arguments for.
 * @param arguments The arguments to unmarshall
 * @return The MethodCandidate that should suit the arguements and method.
 * @throws UnmarshallException If one of the arguments cannot be unmarshalled
 */
private MethodCandidate tryUnmarshallArgs(Method method, JSONArray arguments) throws UnmarshallException {
    MethodCandidate candidate = new MethodCandidate(method);
    Class[] param = method.getParameterTypes();
    int i = 0, j = 0;
    try {
        for (; i < param.length; i++) {
            SerializerState serialiserState = new SerializerState();
            if (LocalArgController.isLocalArg(param[i])) {
                candidate.match[i] = ObjectMatch.OKAY;
            } else {
                candidate.match[i] = ser.tryUnmarshall(serialiserState, param[i], arguments.get(j++));
            }
        }
    } catch (JSONException e) {
        throw (NoSuchElementException) new NoSuchElementException(e.getMessage()).initCause(e);
    } catch (UnmarshallException e) {
        throw new UnmarshallException("arg " + (i + 1) + " " + e.getMessage(), e);
    }
    return candidate;
}
Also used : JSONException(org.json.JSONException) SerializerState(org.jabsorb.serializer.SerializerState) UnmarshallException(org.jabsorb.serializer.UnmarshallException) NoSuchElementException(java.util.NoSuchElementException)

Example 3 with UnmarshallException

use of org.jabsorb.serializer.UnmarshallException in project wonder-slim by undur.

the class JSONRPCBridge method call.

/**
 * Call a method using a JSON-RPC request object.
 *
 * @param context The transport context (the HttpServletRequest object in the
 *          case of the HTTP transport).
 * @param jsonReq The JSON-RPC request structured as a JSON object tree.
 * @return a JSONRPCResult object with the result of the invocation or an
 *         error.
 */
public JSONRPCResult call(Object[] context, JSONObject jsonReq) {
    String encodedMethod;
    Object requestId;
    JSONArray arguments;
    JSONArray fixups;
    try {
        // Get method name, arguments and request id
        encodedMethod = jsonReq.getString("method");
        arguments = jsonReq.getJSONArray("params");
        requestId = jsonReq.opt("id");
        fixups = jsonReq.optJSONArray("fixups");
    } catch (JSONException e) {
        log.error("no method or parameters in request");
        return new JSONRPCResult(JSONRPCResult.CODE_ERR_NOMETHOD, null, JSONRPCResult.MSG_ERR_NOMETHOD);
    }
    if (log.isDebugEnabled()) {
        if (fixups != null) {
            log.debug("call " + encodedMethod + "(" + arguments + ")" + ", requestId=" + requestId);
        } else {
            log.debug("call " + encodedMethod + "(" + arguments + ")" + ", fixups=" + fixups + ", requestId=" + requestId);
        }
    }
    if (fixups != null) {
        try {
            for (int i = 0; i < fixups.length(); i++) {
                JSONArray assignment = fixups.getJSONArray(i);
                JSONArray fixup = assignment.getJSONArray(0);
                JSONArray original = assignment.getJSONArray(1);
                applyFixup(arguments, fixup, original);
            }
        } catch (JSONException e) {
            log.error("error applying fixups", e);
            return new JSONRPCResult(JSONRPCResult.CODE_ERR_FIXUP, requestId, JSONRPCResult.MSG_ERR_FIXUP + ": " + e.getMessage());
        }
    }
    String className = null;
    String methodName = null;
    int objectID = 0;
    // Parse the class and methodName
    StringTokenizer t = new StringTokenizer(encodedMethod, ".");
    if (t.hasMoreElements()) {
        className = t.nextToken();
    }
    if (t.hasMoreElements()) {
        methodName = t.nextToken();
    }
    // See if we have an object method in the format ".obj#<objectID>"
    if (encodedMethod.startsWith(".obj#")) {
        t = new StringTokenizer(className, "#");
        t.nextToken();
        objectID = Integer.parseInt(t.nextToken());
    }
    // one of oi or cd will resolve (first oi is attempted, and if that fails,
    // then cd is attempted)
    // object instance of object being invoked
    ObjectInstance oi = null;
    // ClassData for resolved object instance, or if object instance cannot
    // resolve, class data for
    // class instance (static method) we are resolving to
    ClassData cd = null;
    HashMap methodMap = null;
    Method method = null;
    Object itsThis = null;
    if (objectID == 0) {
        // when a new JSONRpcClient object is initialized.
        if (encodedMethod.equals("system.listMethods")) {
            HashSet m = new HashSet();
            globalBridge.allInstanceMethods(m);
            if (globalBridge != this) {
                globalBridge.allStaticMethods(m);
                globalBridge.allInstanceMethods(m);
            }
            allStaticMethods(m);
            allInstanceMethods(m);
            JSONArray methods = new JSONArray();
            Iterator i = m.iterator();
            while (i.hasNext()) {
                methods.put(i.next());
            }
            return new JSONRPCResult(JSONRPCResult.CODE_SUCCESS, requestId, methods);
        }
        // Look up the class, object instance and method objects
        if (className == null || methodName == null || ((oi = resolveObject(className)) == null && (cd = resolveClass(className)) == null)) {
            return new JSONRPCResult(JSONRPCResult.CODE_ERR_NOMETHOD, requestId, JSONRPCResult.MSG_ERR_NOMETHOD);
        }
        if (oi != null) {
            itsThis = oi.o;
            cd = ClassAnalyzer.getClassData(oi.clazz);
            methodMap = cd.getMethodMap();
        } else {
            if (cd != null) {
                methodMap = cd.getStaticMethodMap();
            }
        }
    } else {
        if ((oi = resolveObject(Integer.valueOf(objectID))) == null) {
            return new JSONRPCResult(JSONRPCResult.CODE_ERR_NOMETHOD, requestId, JSONRPCResult.MSG_ERR_NOMETHOD);
        }
        itsThis = oi.o;
        cd = ClassAnalyzer.getClassData(oi.clazz);
        methodMap = cd.getMethodMap();
        if (methodName != null && methodName.equals("listMethods")) {
            HashSet m = new HashSet();
            uniqueMethods(m, "", cd.getStaticMethodMap());
            uniqueMethods(m, "", cd.getMethodMap());
            JSONArray methods = new JSONArray();
            Iterator i = m.iterator();
            while (i.hasNext()) {
                methods.put(i.next());
            }
            return new JSONRPCResult(JSONRPCResult.CODE_SUCCESS, requestId, methods);
        }
    }
    // Find the specific method
    if ((method = resolveMethod(methodMap, methodName, arguments)) == null) {
        return new JSONRPCResult(JSONRPCResult.CODE_ERR_NOMETHOD, requestId, JSONRPCResult.MSG_ERR_NOMETHOD);
    }
    JSONRPCResult result;
    // Call the method
    try {
        if (log.isDebugEnabled()) {
            log.debug("invoking " + method.getReturnType().getName() + " " + method.getName() + "(" + argSignature(method) + ")");
        }
        // Unmarshall arguments
        Object[] javaArgs = unmarshallArgs(context, method, arguments);
        // Call pre invoke callbacks
        if (cbc != null) {
            for (int i = 0; i < context.length; i++) {
                cbc.preInvokeCallback(context[i], itsThis, method, javaArgs);
            }
        }
        // Invoke the method
        Object returnObj = method.invoke(itsThis, javaArgs);
        // Call post invoke callbacks
        if (cbc != null) {
            for (int i = 0; i < context.length; i++) {
                cbc.postInvokeCallback(context[i], itsThis, method, returnObj);
            }
        }
        // Marshall the result
        SerializerState serializerState = new SerializerState();
        Object json = ser.marshall(serializerState, null, returnObj, "r");
        result = new JSONRPCResult(JSONRPCResult.CODE_SUCCESS, requestId, json, serializerState.getFixUps());
    // Handle exceptions creating exception results and
    // calling error callbacks
    } catch (UnmarshallException e) {
        if (cbc != null) {
            for (int i = 0; i < context.length; i++) {
                cbc.errorCallback(context[i], itsThis, method, e);
            }
        }
        log.error("exception occured", e);
        result = new JSONRPCResult(JSONRPCResult.CODE_ERR_UNMARSHALL, requestId, e.getMessage());
    } catch (MarshallException e) {
        if (cbc != null) {
            for (int i = 0; i < context.length; i++) {
                cbc.errorCallback(context[i], itsThis, method, e);
            }
        }
        log.error("exception occured", e);
        result = new JSONRPCResult(JSONRPCResult.CODE_ERR_MARSHALL, requestId, e.getMessage());
    } catch (Throwable e) {
        if (e instanceof InvocationTargetException) {
            e = ((InvocationTargetException) e).getTargetException();
        }
        if (cbc != null) {
            for (int i = 0; i < context.length; i++) {
                cbc.errorCallback(context[i], itsThis, method, e);
            }
        }
        log.error("exception occured", e);
        result = new JSONRPCResult(JSONRPCResult.CODE_REMOTE_EXCEPTION, requestId, exceptionTransformer.transform(e));
    }
    // Return the results
    return result;
}
Also used : HashMap(java.util.HashMap) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) StringTokenizer(java.util.StringTokenizer) ClassData(org.jabsorb.reflect.ClassData) MarshallException(org.jabsorb.serializer.MarshallException) Iterator(java.util.Iterator) JSONObject(org.json.JSONObject) SerializerState(org.jabsorb.serializer.SerializerState) UnmarshallException(org.jabsorb.serializer.UnmarshallException) HashSet(java.util.HashSet)

Example 4 with UnmarshallException

use of org.jabsorb.serializer.UnmarshallException in project wonder-slim by undur.

the class LocalArgController method resolveLocalArg.

/**
 * Using the caller's context, resolve a given method call parameter to a
 * local argument.
 *
 * @param context
 *          callers context. In an http servlet environment, this will contain
 *          the servlet request and response objects.
 * @param param
 *          class type parameter to resolve to a local argument.
 *
 * @return the run time instance that is resolved, to be used when calling the
 *         method.
 *
 * @throws UnmarshallException
 *           if there if a failure during resolution.
 */
public static Object resolveLocalArg(Object[] context, Class param) throws UnmarshallException {
    HashSet resolverSet = (HashSet) localArgResolverMap.get(param);
    Iterator i = resolverSet.iterator();
    while (i.hasNext()) {
        LocalArgResolverData resolverData = (LocalArgResolverData) i.next();
        for (int j = 0; j < context.length; j++) {
            if (resolverData.understands(context[j])) {
                try {
                    return resolverData.getArgResolver().resolveArg(context[j]);
                } catch (LocalArgResolveException e) {
                    throw new UnmarshallException("error resolving local argument: " + e, e);
                }
            }
        }
    }
    throw new UnmarshallException("couldn't find local arg resolver");
}
Also used : Iterator(java.util.Iterator) UnmarshallException(org.jabsorb.serializer.UnmarshallException) HashSet(java.util.HashSet)

Example 5 with UnmarshallException

use of org.jabsorb.serializer.UnmarshallException in project wonder-slim by undur.

the class JSONEnterpriseObjectSerializer method unmarshall.

public Object unmarshall(SerializerState state, Class clazz, Object o) throws UnmarshallException {
    try {
        JSONObject jso = (JSONObject) o;
        String gid = jso.getString("gid");
        jso.put("globalID", gid);
        jso.remove("gid");
        String javaClassName = jso.getString("javaClass");
        Class javaClass = Class.forName(javaClassName);
        BeanSerializer beanSerializer = new BeanSerializer();
        beanSerializer.setOwner(ser);
        Object obj = beanSerializer.unmarshall(state, javaClass, jso);
        state.setSerialized(o, obj);
        return obj;
    } catch (Exception e) {
        throw new UnmarshallException("Failed to unmarshall EO.", e);
    }
}
Also used : BeanSerializer(org.jabsorb.serializer.impl.BeanSerializer) JSONObject(org.json.JSONObject) JSONObject(org.json.JSONObject) IJSONEnterpriseObject(er.ajax.json.client.IJSONEnterpriseObject) UnmarshallException(org.jabsorb.serializer.UnmarshallException) JSONException(org.json.JSONException) MarshallException(org.jabsorb.serializer.MarshallException) UnmarshallException(org.jabsorb.serializer.UnmarshallException)

Aggregations

UnmarshallException (org.jabsorb.serializer.UnmarshallException)23 JSONException (org.json.JSONException)22 JSONObject (org.json.JSONObject)21 Iterator (java.util.Iterator)6 JSONArray (org.json.JSONArray)5 ObjectMatch (org.jabsorb.serializer.ObjectMatch)4 SerializerState (org.jabsorb.serializer.SerializerState)4 NativeObject (org.mozilla.javascript.NativeObject)4 Method (java.lang.reflect.Method)3 NoSuchElementException (java.util.NoSuchElementException)3 MarshallException (org.jabsorb.serializer.MarshallException)3 IdScriptableObject (org.mozilla.javascript.IdScriptableObject)3 NSArray (com.webobjects.foundation.NSArray)2 NSData (com.webobjects.foundation.NSData)2 IntrospectionException (java.beans.IntrospectionException)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 BeanSerializer (org.jabsorb.serializer.impl.BeanSerializer)2 QuerySelect (com.servoy.j2db.query.QuerySelect)1