Search in sources :

Example 1 with MarshallException

use of org.jabsorb.serializer.MarshallException in project servoy-client by Servoy.

the class FoundSetManager method replaceValuesWithSerializedString.

private static void replaceValuesWithSerializedString(IDataSet dataSet, boolean[] rowsToStringserialize) {
    // run stringserializer over the rows
    JSONSerializerWrapper stringserializer = new JSONSerializerWrapper(false);
    for (int r = 0; r < dataSet.getRowCount(); r++) {
        Object[] row = dataSet.getRow(r).clone();
        for (int i = 0; i < rowsToStringserialize.length; i++) {
            if (rowsToStringserialize[i] && row[i] != null && !(row[i] instanceof String)) {
                // the data was not a string (so not pre-serialized from js), run it through the stringserializer.
                try {
                    row[i] = stringserializer.toJSON(row[i]).toString();
                } catch (MarshallException e) {
                    Debug.warn(e);
                }
            }
        }
        dataSet.setRow(r, row);
    }
}
Also used : JSONSerializerWrapper(com.servoy.j2db.util.serialize.JSONSerializerWrapper) MarshallException(org.jabsorb.serializer.MarshallException) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject)

Example 2 with MarshallException

use of org.jabsorb.serializer.MarshallException 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 3 with MarshallException

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

the class ERXBeanSerializer method marshall.

public Object marshall(SerializerState state, Object p, Object o) throws MarshallException {
    BeanData bd;
    try {
        bd = getBeanData(o.getClass());
    } catch (IntrospectionException e) {
        throw new MarshallException(o.getClass().getName() + " is not a bean", e);
    }
    JSONObject val = new JSONObject();
    if (ser.getMarshallClassHints()) {
        try {
            val.put("javaClass", o.getClass().getName());
        } catch (JSONException e) {
            throw new MarshallException("JSONException: " + e.getMessage(), e);
        }
    }
    Object[] args = new Object[0];
    Object result;
    for (Map.Entry<String, Method> ent : bd.readableProps.entrySet()) {
        String prop = ent.getKey();
        Method getMethod = ent.getValue();
        if (log.isDebugEnabled()) {
            log.debug("invoking " + getMethod.getName() + "()");
        }
        try {
            result = getMethod.invoke(o, args);
        } catch (Throwable e) {
            if (e instanceof InvocationTargetException) {
                e = ((InvocationTargetException) e).getTargetException();
            }
            throw new MarshallException("bean " + o.getClass().getName() + " can't invoke " + getMethod.getName() + ": " + e.getMessage(), e);
        }
        try {
            if (result != null || ser.getMarshallNullAttributes()) {
                try {
                    Object json = ser.marshall(state, o, result, prop);
                    val.put(prop, json);
                } catch (JSONException e) {
                    throw new MarshallException("JSONException: " + e.getMessage(), e);
                }
            }
        } catch (MarshallException e) {
            throw new MarshallException("bean " + o.getClass().getName() + " " + e.getMessage(), e);
        }
    }
    return val;
}
Also used : IntrospectionException(java.beans.IntrospectionException) JSONException(org.json.JSONException) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) JSONObject(org.json.JSONObject) MarshallException(org.jabsorb.serializer.MarshallException) JSONObject(org.json.JSONObject) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with MarshallException

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

the class NSArraySerializer method marshall.

public Object marshall(SerializerState state, Object p, Object o) throws MarshallException {
    NSArray nsarray = (NSArray) o;
    JSONObject obj = new JSONObject();
    JSONArray arr = new JSONArray();
    // Have a single function to do it.
    if (ser.getMarshallClassHints()) {
        try {
            obj.put("javaClass", o.getClass().getName());
        } catch (JSONException e) {
            throw new MarshallException("javaClass not found!");
        }
    }
    try {
        obj.put("nsarray", arr);
        state.push(o, arr, "nsarray");
    } catch (JSONException e) {
        throw new MarshallException("Error setting nsarray: " + e);
    }
    int index = 0;
    try {
        Enumeration e = nsarray.objectEnumerator();
        while (e.hasMoreElements()) {
            Object json = ser.marshall(state, arr, e.nextElement(), Integer.valueOf(index));
            if (JSONSerializer.CIRC_REF_OR_DUPLICATE != json) {
                arr.put(json);
            } else {
                // put a slot where the object would go, so it can be fixed up properly in the fix up phase
                arr.put(JSONObject.NULL);
            }
            index++;
        }
    } catch (MarshallException e) {
        throw (MarshallException) new MarshallException("element " + index).initCause(e);
    } finally {
        state.pop();
    }
    return obj;
}
Also used : Enumeration(java.util.Enumeration) JSONObject(org.json.JSONObject) NSArray(com.webobjects.foundation.NSArray) MarshallException(org.jabsorb.serializer.MarshallException) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject)

Example 5 with MarshallException

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

the class NSSetSerializer method marshall.

public Object marshall(SerializerState state, Object p, Object o) throws MarshallException {
    try {
        NSSet set = (NSSet) o;
        JSONObject obj = new JSONObject();
        JSONObject setdata = new JSONObject();
        obj.put("javaClass", o.getClass().getName());
        obj.put("set", setdata);
        String key = null;
        try {
            int index = 0;
            Enumeration i = set.objectEnumerator();
            while (i.hasMoreElements()) {
                Object value = i.nextElement();
                setdata.put(key, ser.marshall(state, o, value, Integer.valueOf(index)));
                index++;
            }
        } catch (MarshallException e) {
            throw new MarshallException("set key " + key + e.getMessage());
        }
        return obj;
    } catch (JSONException e) {
        throw new MarshallException("Failed to marshall NSSet.", e);
    }
}
Also used : Enumeration(java.util.Enumeration) JSONObject(org.json.JSONObject) MarshallException(org.jabsorb.serializer.MarshallException) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) NSSet(com.webobjects.foundation.NSSet)

Aggregations

MarshallException (org.jabsorb.serializer.MarshallException)14 JSONException (org.json.JSONException)12 JSONObject (org.json.JSONObject)12 JSONArray (org.json.JSONArray)4 Enumeration (java.util.Enumeration)3 ServoyJSONObject (com.servoy.j2db.util.ServoyJSONObject)2 JSONSerializerWrapper (com.servoy.j2db.util.serialize.JSONSerializerWrapper)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Method (java.lang.reflect.Method)2 HashMap (java.util.HashMap)2 SerializerState (org.jabsorb.serializer.SerializerState)2 UnmarshallException (org.jabsorb.serializer.UnmarshallException)2 QuerySelect (com.servoy.j2db.query.QuerySelect)1 QBSelect (com.servoy.j2db.querybuilder.impl.QBSelect)1 ServoyNativeArray (com.servoy.j2db.scripting.ServoyNativeArray)1 HierarchicalStreamWriter (com.thoughtworks.xstream.io.HierarchicalStreamWriter)1 CompactWriter (com.thoughtworks.xstream.io.xml.CompactWriter)1 NSArray (com.webobjects.foundation.NSArray)1 NSData (com.webobjects.foundation.NSData)1 NSDictionary (com.webobjects.foundation.NSDictionary)1