Example usage for com.google.gwt.user.server.rpc.impl ServerSerializationStreamReader prepareToRead

List of usage examples for com.google.gwt.user.server.rpc.impl ServerSerializationStreamReader prepareToRead

Introduction

In this page you can find the example usage for com.google.gwt.user.server.rpc.impl ServerSerializationStreamReader prepareToRead.

Prototype

@Override
    public void prepareToRead(String encodedTokens) throws SerializationException 

Source Link

Usage

From source file:com.apress.progwt.server.gwt.RPC1524.java

License:Apache License

/**
 * Returns an {@link RPCRequest} that is built by decoding the
 * contents of an encoded RPC request and optionally validating that
 * type can handle the request. If the type parameter is not
 * <code>null</code>, the implementation checks that the type is
 * assignable to the {@link RemoteService} interface requested in the
 * encoded request string.//  ww  w. ja v  a  2s . c o m
 * 
 * <p>
 * If the serializationPolicyProvider parameter is not
 * <code>null</code>, it is asked for a {@link SerializationPolicy}
 * to use to restrict the set of types that can be decoded from the
 * request. If this parameter is <code>null</code>, then only
 * subtypes of
 * {@link com.google.gwt.user.client.rpc.IsSerializable IsSerializable}
 * or types which have custom field serializers can be decoded.
 * </p>
 * 
 * <p>
 * Invoking this method with <code>null</code> for the type
 * parameter, <code>decodeRequest(encodedRequest, null)</code>, is
 * equivalent to calling <code>decodeRequest(encodedRequest)</code>.
 * </p>
 * 
 * @param encodedRequest
 *            a string that encodes the {@link RemoteService}
 *            interface, the service method, and the arguments to pass
 *            to the service method
 * @param type
 *            if not <code>null</code>, the implementation checks
 *            that the type is assignable to the {@link RemoteService}
 *            interface encoded in the encoded request string.
 * @param serializationPolicyProvider
 *            if not <code>null</code>, the implementation asks
 *            this provider for a {@link SerializationPolicy} which
 *            will be used to restrict the set of types that can be
 *            decoded from this request
 * @return an {@link RPCRequest} instance
 * 
 * @throws NullPointerException
 *             if the encodedRequest is <code>null</code>
 * @throws IllegalArgumentException
 *             if the encodedRequest is an empty string
 * @throws IncompatibleRemoteServiceException
 *             if any of the following conditions apply:
 *             <ul>
 *             <li>if the types in the encoded request cannot be
 *             deserialized</li>
 *             <li>if the {@link ClassLoader} acquired from
 *             <code>Thread.currentThread().getContextClassLoader()</code>
 *             cannot load the service interface or any of the types
 *             specified in the encodedRequest</li>
 *             <li>the requested interface is not assignable to
 *             {@link RemoteService}</li>
 *             <li>the service method requested in the encodedRequest
 *             is not a member of the requested service interface</li>
 *             <li>the type parameter is not <code>null</code> and
 *             is not assignable to the requested
 *             {@link RemoteService} interface
 *             </ul>
 */
public static RPCRequest decodeRequest(String encodedRequest, Class<?> type,
        SerializationPolicyProvider serializationPolicyProvider) {
    if (encodedRequest == null) {
        throw new NullPointerException("encodedRequest cannot be null");
    }

    if (encodedRequest.length() == 0) {
        throw new IllegalArgumentException("encodedRequest cannot be empty");
    }

    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();

    try {
        ServerSerializationStreamReader streamReader = new ServerSerializationStreamReader(classLoader,
                serializationPolicyProvider);
        streamReader.prepareToRead(encodedRequest);

        // Read the name of the RemoteService interface
        String serviceIntfName = streamReader.readString();

        if (type != null) {
            if (!implementsInterface(type, serviceIntfName)) {
                // The service does not implement the requested
                // interface
                throw new IncompatibleRemoteServiceException("Blocked attempt to access interface '"
                        + serviceIntfName + "', which is not implemented by '" + printTypeName(type)
                        + "'; this is either misconfiguration or a hack attempt");
            }
        }

        SerializationPolicy serializationPolicy = streamReader.getSerializationPolicy();
        Class<?> serviceIntf;
        try {
            serviceIntf = getClassFromSerializedName(serviceIntfName, classLoader);
            if (!RemoteService.class.isAssignableFrom(serviceIntf)) {
                // The requested interface is not a RemoteService
                // interface
                throw new IncompatibleRemoteServiceException("Blocked attempt to access interface '"
                        + printTypeName(serviceIntf)
                        + "', which doesn't extend RemoteService; this is either misconfiguration or a hack attempt");
            }
        } catch (ClassNotFoundException e) {
            throw new IncompatibleRemoteServiceException(
                    "Could not locate requested interface '" + serviceIntfName + "' in default classloader", e);
        }

        String serviceMethodName = streamReader.readString();

        int paramCount = streamReader.readInt();
        Class<?>[] parameterTypes = new Class[paramCount];

        for (int i = 0; i < parameterTypes.length; i++) {
            String paramClassName = streamReader.readString();
            try {
                parameterTypes[i] = getClassFromSerializedName(paramClassName, classLoader);
            } catch (ClassNotFoundException e) {
                throw new IncompatibleRemoteServiceException(
                        "Parameter " + i + " of is of an unknown type '" + paramClassName + "'", e);
            }
        }

        try {
            Method method = serviceIntf.getMethod(serviceMethodName, parameterTypes);

            Object[] parameterValues = new Object[parameterTypes.length];
            for (int i = 0; i < parameterValues.length; i++) {
                parameterValues[i] = streamReader.deserializeValue(parameterTypes[i]);
            }

            return new RPCRequest(method, parameterValues, serializationPolicy);

        } catch (NoSuchMethodException e) {
            throw new IncompatibleRemoteServiceException(
                    formatMethodNotFoundErrorMessage(serviceIntf, serviceMethodName, parameterTypes));
        }
    } catch (SerializationException ex) {
        throw new IncompatibleRemoteServiceException(ex.getMessage(), ex);
    }
}

From source file:com.brightedu.server.util.MyRPC.java

License:Apache License

/**
 * Returns an {@link RPCRequest} that is built by decoding the contents of
 * an encoded RPC request and optionally validating that type can handle the
 * request. If the type parameter is not <code>null</code>, the
 * implementation checks that the type is assignable to the
 * {@link RemoteService} interface requested in the encoded request string.
 * /*w  w  w  .j  a  v a2 s  . c om*/
 * <p>
 * If the serializationPolicyProvider parameter is not <code>null</code>, it
 * is asked for a {@link SerializationPolicy} to use to restrict the set of
 * types that can be decoded from the request. If this parameter is
 * <code>null</code>, then only subtypes of
 * {@link com.google.gwt.user.client.rpc.IsSerializable IsSerializable} or
 * types which have custom field serializers can be decoded.
 * </p>
 * 
 * <p>
 * Invoking this method with <code>null</code> for the type parameter,
 * <code>decodeRequest(encodedRequest, null)</code>, is equivalent to
 * calling <code>decodeRequest(encodedRequest)</code>.
 * </p>
 * 
 * @param encodedRequest
 *            a string that encodes the {@link RemoteService} interface, the
 *            service method, and the arguments to pass to the service
 *            method
 * @param type
 *            if not <code>null</code>, the implementation checks that the
 *            type is assignable to the {@link RemoteService} interface
 *            encoded in the encoded request string.
 * @param serializationPolicyProvider
 *            if not <code>null</code>, the implementation asks this
 *            provider for a {@link SerializationPolicy} which will be used
 *            to restrict the set of types that can be decoded from this
 *            request
 * @return an {@link RPCRequest} instance
 * 
 * @throws NullPointerException
 *             if the encodedRequest is <code>null</code>
 * @throws IllegalArgumentException
 *             if the encodedRequest is an empty string
 * @throws IncompatibleRemoteServiceException
 *             if any of the following conditions apply:
 *             <ul>
 *             <li>if the types in the encoded request cannot be
 *             deserialized</li>
 *             <li>if the {@link ClassLoader} acquired from
 *             <code>Thread.currentThread().getContextClassLoader()</code>
 *             cannot load the service interface or any of the types
 *             specified in the encodedRequest</li>
 *             <li>the requested interface is not assignable to
 *             {@link RemoteService}</li>
 *             <li>the service method requested in the encodedRequest is not
 *             a member of the requested service interface</li>
 *             <li>the type parameter is not <code>null</code> and is not
 *             assignable to the requested {@link RemoteService} interface
 *             </ul>
 */
public static RPCRequest decodeRequest(String encodedRequest, Class<?> type,
        SerializationPolicyProvider serializationPolicyProvider) {
    if (encodedRequest == null) {
        throw new NullPointerException("encodedRequest cannot be null");
    }

    if (encodedRequest.length() == 0) {
        throw new IllegalArgumentException("encodedRequest cannot be empty");
    }

    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();

    try {
        ServerSerializationStreamReader streamReader = new ServerSerializationStreamReader(classLoader,
                serializationPolicyProvider);
        streamReader.prepareToRead(encodedRequest);

        RpcToken rpcToken = null;
        if (streamReader.hasFlags(AbstractSerializationStream.FLAG_RPC_TOKEN_INCLUDED)) {
            // Read the RPC token
            rpcToken = (RpcToken) streamReader.deserializeValue(RpcToken.class);
        }

        // Read the name of the RemoteService interface
        String serviceIntfName = maybeDeobfuscate(streamReader, streamReader.readString());

        if (type != null) {
            if (!implementsInterface(type, serviceIntfName)) {
                // The service does not implement the requested interface
                throw new IncompatibleRemoteServiceException("Blocked attempt to access interface '"
                        + serviceIntfName + "', which is not implemented by '" + printTypeName(type)
                        + "'; this is either misconfiguration or a hack attempt");
            }
        }

        SerializationPolicy serializationPolicy = streamReader.getSerializationPolicy();
        Class<?> serviceIntf;
        try {
            serviceIntf = getClassFromSerializedName(serviceIntfName, classLoader);
            if (!RemoteService.class.isAssignableFrom(serviceIntf)) {
                // The requested interface is not a RemoteService interface
                throw new IncompatibleRemoteServiceException("Blocked attempt to access interface '"
                        + printTypeName(serviceIntf)
                        + "', which doesn't extend RemoteService; this is either misconfiguration or a hack attempt");
            }
        } catch (ClassNotFoundException e) {
            throw new IncompatibleRemoteServiceException(
                    "Could not locate requested interface '" + serviceIntfName + "' in default classloader", e);
        }

        String serviceMethodName = streamReader.readString();

        int paramCount = streamReader.readInt();
        if (paramCount > streamReader.getNumberOfTokens()) {
            throw new IncompatibleRemoteServiceException("Invalid number of parameters");
        }
        Class<?>[] parameterTypes = new Class[paramCount];

        for (int i = 0; i < parameterTypes.length; i++) {
            String paramClassName = maybeDeobfuscate(streamReader, streamReader.readString());

            try {
                parameterTypes[i] = getClassFromSerializedName(paramClassName, classLoader);
            } catch (ClassNotFoundException e) {
                throw new IncompatibleRemoteServiceException(
                        "Parameter " + i + " of is of an unknown type '" + paramClassName + "'", e);
            }
        }

        try {
            Method method = serviceIntf.getMethod(serviceMethodName, parameterTypes);

            Object[] parameterValues = new Object[parameterTypes.length];
            for (int i = 0; i < parameterValues.length; i++) {
                parameterValues[i] = streamReader.deserializeValue(parameterTypes[i]);
            }

            return new RPCRequest(method, parameterValues, rpcToken, serializationPolicy,
                    streamReader.getFlags());

        } catch (NoSuchMethodException e) {
            throw new IncompatibleRemoteServiceException(
                    formatMethodNotFoundErrorMessage(serviceIntf, serviceMethodName, parameterTypes));
        }
    } catch (SerializationException ex) {
        throw new IncompatibleRemoteServiceException(ex.getMessage(), ex);
    }
}

From source file:com.google.gwt.sample.dynatable.utils.RPC_2_0_1.java

/**
 * Returns an {@link RPCRequest} that is built by decoding the contents of an
 * encoded RPC request and optionally validating that type can handle the
 * request. If the type parameter is not <code>null</code>, the implementation
 * checks that the type is assignable to the {@link com.google.gwt.user.client.rpc.RemoteService} interface
 * requested in the encoded request string.
 * <p/>//from w  w w.j a va 2 s  .c  o  m
 * <p>
 * If the serializationPolicyProvider parameter is not <code>null</code>, it
 * is asked for a {@link com.google.gwt.user.server.rpc.SerializationPolicy} to use to restrict the set of
 * types that can be decoded from the request. If this parameter is
 * <code>null</code>, then only subtypes of
 * {@link com.google.gwt.user.client.rpc.IsSerializable IsSerializable} or
 * types which have custom field serializers can be decoded.
 * </p>
 * <p/>
 * <p>
 * Invoking this method with <code>null</code> for the type parameter,
 * <code>decodeRequest(encodedRequest, null)</code>, is equivalent to calling
 * <code>decodeRequest(encodedRequest)</code>.
 * </p>
 *
 * @param encodedRequest              a string that encodes the {@link com.google.gwt.user.client.rpc.RemoteService}
 *                                    interface, the service method, and the arguments to pass to the
 *                                    service method
 * @param type                        if not <code>null</code>, the implementation checks that the
 *                                    type is assignable to the {@link com.google.gwt.user.client.rpc.RemoteService} interface encoded
 *                                    in the encoded request string.
 * @param serializationPolicyProvider if not <code>null</code>, the
 *                                    implementation asks this provider for a
 *                                    {@link com.google.gwt.user.server.rpc.SerializationPolicy} which will be used to restrict the set
 *                                    of types that can be decoded from this request
 * @return an {@link RPCRequest} instance
 * @throws NullPointerException     if the encodedRequest is <code>null</code>
 * @throws IllegalArgumentException if the encodedRequest is an empty string
 * @throws com.google.gwt.user.client.rpc.IncompatibleRemoteServiceException
 *                                  if any of the following
 *                                  conditions apply:
 *                                  <ul>
 *                                  <li>if the types in the encoded request cannot be deserialized</li>
 *                                  <li>if the {@link ClassLoader} acquired from
 *                                  <code>Thread.currentThread().getContextClassLoader()</code>
 *                                  cannot load the service interface or any of the types specified
 *                                  in the encodedRequest</li>
 *                                  <li>the requested interface is not assignable to
 *                                  {@link com.google.gwt.user.client.rpc.RemoteService}</li>
 *                                  <li>the service method requested in the encodedRequest is not a
 *                                  member of the requested service interface</li>
 *                                  <li>the type parameter is not <code>null</code> and is not
 *                                  assignable to the requested {@link com.google.gwt.user.client.rpc.RemoteService} interface
 *                                  </ul>
 */
public static RPCRequest decodeRequest(String encodedRequest, Class<?> type,
        SerializationPolicyProvider serializationPolicyProvider) {
    if (encodedRequest == null) {
        throw new NullPointerException("encodedRequest cannot be null");
    }

    if (encodedRequest.length() == 0) {
        throw new IllegalArgumentException("encodedRequest cannot be empty");
    }

    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();

    try {
        ServerSerializationStreamReader streamReader = new ServerSerializationStreamReader(classLoader,
                serializationPolicyProvider);
        streamReader.prepareToRead(encodedRequest);

        // Read the name of the RemoteService interface
        String serviceIntfName = maybeDeobfuscate(streamReader, streamReader.readString());

        if (type != null) {
            if (!implementsInterface(type, serviceIntfName)) {
                // The service does not implement the requested interface
                throw new IncompatibleRemoteServiceException("Blocked attempt to access interface '"
                        + serviceIntfName + "', which is not implemented by '" + printTypeName(type)
                        + "'; this is either misconfiguration or a hack attempt");
            }
        }

        SerializationPolicy serializationPolicy = streamReader.getSerializationPolicy();
        Class<?> serviceIntf;
        try {
            serviceIntf = getClassFromSerializedName(serviceIntfName, classLoader);
            if (!RemoteService.class.isAssignableFrom(serviceIntf)) {
                // The requested interface is not a RemoteService interface
                throw new IncompatibleRemoteServiceException("Blocked attempt to access interface '"
                        + printTypeName(serviceIntf)
                        + "', which doesn't extend RemoteService; this is either misconfiguration or a hack attempt");
            }
        } catch (ClassNotFoundException e) {
            throw new IncompatibleRemoteServiceException(
                    "Could not locate requested interface '" + serviceIntfName + "' in default classloader", e);
        }

        String serviceMethodName = streamReader.readString();

        int paramCount = streamReader.readInt();
        if (paramCount > streamReader.getNumberOfTokens()) {
            throw new IncompatibleRemoteServiceException("Invalid number of parameters");
        }
        Class<?>[] parameterTypes = new Class[paramCount];

        for (int i = 0; i < parameterTypes.length; i++) {
            String paramClassName = maybeDeobfuscate(streamReader, streamReader.readString());

            try {
                parameterTypes[i] = getClassFromSerializedName(paramClassName, classLoader);
            } catch (ClassNotFoundException e) {
                throw new IncompatibleRemoteServiceException(
                        "Parameter " + i + " of is of an unknown type '" + paramClassName + "'", e);
            }
        }

        try {
            Method method = serviceIntf.getMethod(serviceMethodName, parameterTypes);

            Object[] parameterValues = new Object[parameterTypes.length];
            for (int i = 0; i < parameterValues.length; i++) {
                parameterValues[i] = streamReader.deserializeValue(parameterTypes[i]);
            }

            return new RPCRequest(method, parameterValues, serializationPolicy, streamReader.getFlags());

        } catch (NoSuchMethodException e) {
            throw new IncompatibleRemoteServiceException(
                    formatMethodNotFoundErrorMessage(serviceIntf, serviceMethodName, parameterTypes));
        }
    } catch (SerializationException ex) {
        throw new IncompatibleRemoteServiceException(ex.getMessage(), ex);
    }
}

From source file:com.guit.server.guice.GuiceGwtServlet.java

License:Apache License

public String decodeRequest(String encodedRequest, Class<?> type,
        SerializationPolicyProvider serializationPolicyProvider) {
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();

    Method method = execute;//w ww . j  a v a 2  s.co  m
    try {
        ServerSerializationStreamReader streamReader = new ServerSerializationStreamReader(classLoader,
                serializationPolicyProvider);
        streamReader.prepareToRead(encodedRequest);

        SerializationPolicy serializationPolicy = streamReader.getSerializationPolicy();

        // Predecible values
        streamReader.readString();

        // Method name
        String methodName = streamReader.readString();

        // Ignore, we know the values
        streamReader.readInt();
        streamReader.readString();

        Object[] parameterValues = new Object[1];
        if ("execute".equals(methodName)) {
            method = execute;
            parameterValues[0] = streamReader.deserializeValue(Action.class);
        } else {
            method = executeBatch;
            parameterValues[0] = streamReader.deserializeValue(ArrayList.class);
        }

        int flags = streamReader.getFlags();
        try {
            return encodeResponse(method.getReturnType(), method.invoke(service, parameterValues), false, flags,
                    serializationPolicy);
        } catch (InvocationTargetException e) {
            Throwable cause = e.getCause();
            if (!(cause instanceof CommandException)) {
                throw new UnexpectedException(
                        "Service method threw an unexpected exception: " + cause.toString(), cause);
            }

            return encodeResponse(cause.getClass(), cause, true, flags, serializationPolicy);
        }
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}

From source file:de.csenk.gwt.ws.server.filter.serialization.ServerGWTSerializer.java

License:Open Source License

@Override
public Object deserialize(String serializedContent) throws SerializationException {
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    ServerSerializationStreamReader streamReader = new ServerSerializationStreamReader(classLoader,
            serializationPolicyProvider);
    streamReader.prepareToRead(serializedContent);

    if (serializationPolicy == null)
        serializationPolicy = streamReader.getSerializationPolicy();

    String objClassName = maybeDeobfuscate(streamReader, streamReader.readString());
    try {/*from  w w w  . ja  va  2  s  .  c  om*/
        Class<?> objClass = Class.forName(objClassName, false, classLoader);
        return streamReader.deserializeValue(objClass);
    } catch (ClassNotFoundException e) {
        throw new SerializationException(e);
    }
}

From source file:org.atmosphere.gwt.server.AtmosphereGwtHandler.java

License:Apache License

protected Serializable deserialize(String data) {
    try {//from w w  w.j  ava2  s. com
        ServerSerializationStreamReader reader = new ServerSerializationStreamReader(
                getClass().getClassLoader(), cometSerializationPolicyProvider);
        reader.prepareToRead(data);
        return (Serializable) reader.readObject();
    } catch (SerializationException ex) {
        logger.error("Failed to deserialize message", ex);
        return null;
    }
}

From source file:org.gwtspringhibernate.reference.rlogman.spring.GwtServiceExporter.java

License:Apache License

/**
 * This is public so that it can be unit tested easily without HTTP.
 *///  w ww  . java 2 s  .c  o  m
public String processCall(String payload) throws SerializationException {

    // Let subclasses see the serialized request.
    //
    onBeforeRequestDeserialized(payload);

    // Create a stream to deserialize the request.
    //
    ServerSerializationStreamReader streamReader = new ServerSerializationStreamReader(serializableTypeOracle);
    streamReader.prepareToRead(payload);

    // Read the service interface
    //
    String serviceIntfName = streamReader.readString();

    // TODO(mmendez): need to check the signature
    // Verify that this very servlet implements the specified interface
    // name.
    //
    if (!isImplementedRemoteServiceInterface(serviceIntfName)) {
        // Bad payload, possible hack attempt.
        //
        throw new SecurityException("Blocked attempt to access interface '" + serviceIntfName
                + "', which is either not implemented by this servlet or which doesn't extend RemoteService; this is either misconfiguration or a hack attempt");
    }

    // Actually get the service interface, so that we can query its methods.
    //
    Class serviceIntf;
    try {
        serviceIntf = getClassFromName(serviceIntfName);
    } catch (ClassNotFoundException e) {
        throw new SerializationException("Unknown service interface class '" + serviceIntfName + "'", e);
    }

    // Read the method name.
    //
    String methodName = streamReader.readString();

    // Read the number and names of the parameter classes from the stream.
    // We have to do this so that we can find the correct overload of the
    // method.
    //
    int paramCount = streamReader.readInt();
    Class[] paramTypes = new Class[paramCount];
    for (int i = 0; i < paramTypes.length; i++) {
        String paramClassName = streamReader.readString();
        try {
            paramTypes[i] = getClassFromName(paramClassName);
        } catch (ClassNotFoundException e) {
            throw new SerializationException("Unknown parameter " + i + " type '" + paramClassName + "'", e);
        }
    }

    // For security, make sure the method is found in the service interface
    // and not just one that happens to be defined on this class.
    //
    Method serviceIntfMethod = findInterfaceMethod(serviceIntf, methodName, paramTypes, true);

    // If it wasn't found, don't continue.
    //
    if (serviceIntfMethod == null) {
        // Bad payload, possible hack attempt.
        //
        throw new SecurityException("Method '" + methodName + "' (or a particular overload) on interface '"
                + serviceIntfName + "' was not found, this is either misconfiguration or a hack attempt");
    }

    // Deserialize the parameters.
    //
    Object[] args = new Object[paramCount];
    for (int i = 0; i < args.length; i++) {
        args[i] = streamReader.deserializeValue(paramTypes[i]);
    }

    // Make the call via reflection.
    //
    String responsePayload = GENERIC_FAILURE_MSG;
    ServerSerializationStreamWriter streamWriter = new ServerSerializationStreamWriter(serializableTypeOracle);
    Throwable caught = null;
    try {
        Class returnType = serviceIntfMethod.getReturnType();
        /**
         * The method is not invoked from <code>this</code> but from <code>this.proxy</code>;
         * <code>this</code> is the exporter, <code>this.proxy</code> is the actual service
         * implementation
         * @author rlogman@gmail.com
         */
        Object returnVal = serviceIntfMethod.invoke(this.proxy, args);
        responsePayload = createResponse(streamWriter, returnType, returnVal, false);
    } catch (IllegalArgumentException e) {
        caught = e;
    } catch (IllegalAccessException e) {
        caught = e;
    } catch (InvocationTargetException e) {
        // Try to serialize the caught exception if the client is expecting
        // it,
        // otherwise log the exception server-side.
        caught = e;
        Throwable cause = e.getCause();
        if (cause != null) {
            // Update the caught exception to the underlying cause
            caught = cause;
            // Serialize the exception back to the client if it's a declared
            // exception
            if (isExpectedException(serviceIntfMethod, cause)) {
                Class thrownClass = cause.getClass();
                responsePayload = createResponse(streamWriter, thrownClass, cause, true);
                // Don't log the exception on the server
                caught = null;
            }
        }
    }

    if (caught != null) {
        responsePayload = GENERIC_FAILURE_MSG;
        // servletContext may be null (for example, when unit testing)
        /**
         * Our logger will not be servlet context's log (we don't have
         * direct access to it at this point)
         * @author rlogman@gmail.com
         */
        if (logger != null) {
            // Log the exception server side
            logger.error("Exception while dispatching incoming RPC call", caught);
        }
    }

    // Let subclasses see the serialized response.
    //
    onAfterResponseSerialized(responsePayload);

    return responsePayload;
}

From source file:org.jboss.seam.remoting.gwt.GWTRemoteServiceServlet.java

License:Open Source License

/**
 * This is public so that it can be unit tested easily without HTTP.
 *///from  w w w.jav a  2s. c  o  m
public String processCall(String payload) throws SerializationException {

    // Let subclasses see the serialized request.
    //
    onBeforeRequestDeserialized(payload);

    // Create a stream to deserialize the request.
    //
    ServerSerializationStreamReader streamReader = new ServerSerializationStreamReader(
            Thread.currentThread().getContextClassLoader(), null);

    streamReader.prepareToRead(payload);

    // Read the service interface
    //
    String serviceIntfName = streamReader.readString();

    //    // TODO(mmendez): need a way to check the type signature of the service intf
    //    // Verify that this very servlet implements the specified interface name.
    //    //
    //    if (!isImplementedRemoteServiceInterface(serviceIntfName)) {
    //      // Bad payload, possible hack attempt.
    //      //
    //      throw new SecurityException(
    //          "Blocked attempt to access interface '"
    //              + serviceIntfName
    //              + "', which is either not implemented by this servlet or which doesn't extend RemoteService; this is either misconfiguration or a hack attempt");
    //    }

    // Actually get the service interface, so that we can query its methods.
    //
    //    Class serviceIntf;
    //    try {
    //      serviceIntf = getClassFromName(serviceIntfName);
    //    } catch (ClassNotFoundException e) {
    //      throw new SerializationException("Unknown service interface class '"
    //          + serviceIntfName + "'", e);
    //    }

    // Read the method name.
    //
    String methodName = streamReader.readString();

    // Read the number and names of the parameter classes from the stream.
    // We have to do this so that we can find the correct overload of the
    // method.
    //
    int paramCount = streamReader.readInt();
    Class[] paramTypes = new Class[paramCount];
    for (int i = 0; i < paramTypes.length; i++) {
        String paramClassName = streamReader.readString();
        try {
            paramTypes[i] = getClassOrPrimitiveFromName(paramClassName);
        } catch (ClassNotFoundException e) {
            throw new SerializationException("Unknown parameter " + i + " type '" + paramClassName + "'", e);
        }
    }

    //    // For security, make sure the method is found in the service interface
    //    // and not just one that happens to be defined on this class.
    //    //
    //    Method serviceIntfMethod = findInterfaceMethod(serviceIntf, methodName,
    //        paramTypes, true);

    //    // If it wasn't found, don't continue.
    //    //
    //    if (serviceIntfMethod == null) {
    //      // Bad payload, possible hack attempt.
    //      //
    //      throw new SecurityException(
    //          "Method '"
    //              + methodName
    //              + "' (or a particular overload) on interface '"
    //              + serviceIntfName
    //              + "' was not found, this is either misconfiguration or a hack attempt");
    //    }

    // Deserialize the parameters.
    //
    Object[] args = new Object[paramCount];
    for (int i = 0; i < args.length; i++) {
        args[i] = streamReader.deserializeValue(paramTypes[i]);
    }

    GWTToSeamAdapter adapter = new GWTToSeamAdapter();

    // Make the call via reflection.
    //
    String responsePayload = GENERIC_FAILURE_MSG;
    ServerSerializationStreamWriter streamWriter = new ServerSerializationStreamWriter(
            LegacySerializationPolicy.getInstance());
    Throwable caught = null;
    try {

        //call the component
        ReturnedObject returnedObject = adapter.callWebRemoteMethod(serviceIntfName, methodName, paramTypes,
                args);
        Class returnType = returnedObject.returnType;
        Object returnVal = returnedObject.returnedObject;
        //      Class returnType = serviceIntfMethod.getReturnType();
        //      Object returnVal = serviceIntfMethod.invoke(this, args);
        responsePayload = createResponse(streamWriter, returnType, returnVal, false);
    } catch (IllegalArgumentException e) {
        caught = e;
    } catch (IllegalAccessException e) {
        caught = e;
    } catch (InvocationTargetException e) {
        // Try to serialize the caught exception if the client is expecting it,
        // otherwise log the exception server-side.
        caught = e;
        Throwable cause = e.getCause();
        if (cause != null) {
            // Update the caught exception to the underlying cause
            caught = cause;
            // Serialize the exception back to the client if it's a declared
            // exception
            if (cause instanceof SerializableException) {
                Class thrownClass = cause.getClass();
                responsePayload = createResponse(streamWriter, thrownClass, cause, true);
                // Don't log the exception on the server
                caught = null;
            }
        }
    }

    if (caught != null) {

        responsePayload = GENERIC_FAILURE_MSG;
        ServletContext servletContext = getServletContext();
        // servletContext may be null (for example, when unit testing)
        if (servletContext != null) {
            // Log the exception server side
            servletContext.log("Exception while dispatching incoming RPC call", caught);
        }
    }

    // Let subclasses see the serialized response.
    //
    onAfterResponseSerialized(responsePayload);

    return responsePayload;
}

From source file:org.opencms.ui.components.extensions.CmsPropertyDialogExtension.java

License:Open Source License

/**
 * @see org.opencms.ui.shared.rpc.I_CmsPropertyServerRpc#savePropertiesForNewResource(java.lang.String)
 *//*from   w ww .  j  a v  a2 s .c o  m*/
public void savePropertiesForNewResource(String data) {

    try {
        getRpcProxy(I_CmsPropertyClientRpc.class).confirmSaveForNew();
        ServerSerializationStreamReader streamReader = new ServerSerializationStreamReader(
                Thread.currentThread().getContextClassLoader(), null);
        // Filling stream reader with data
        streamReader.prepareToRead(data);
        // Reading deserialized object from the stream
        CmsPropertyChangeSet changes = (CmsPropertyChangeSet) (streamReader.readObject());
        m_newResourceBuilder.setPropertyChanges(changes);
        m_newResourceBuilder.safeCreateResource();
        remove();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.restlet.ext.gwt.ObjectRepresentation.java

License:LGPL

/**
 * The wrapped object. Triggers the deserialization if necessary.
 * /*w  ww. j  av a 2  s. c o  m*/
 * @return The wrapped object.
 * @throws IOException
 */
@SuppressWarnings("unchecked")
public T getObject() throws IOException {
    if ((this.object == null) && (getText() != null)) {
        try {
            ServerSerializationStreamReader objectReader = new ServerSerializationStreamReader(
                    Engine.getInstance().getClassLoader(), new SimpleSerializationPolicyProvider());
            String encodedString = getText();

            if (encodedString.indexOf('|') == -1) {
                encodedString = AbstractSerializationStream.SERIALIZATION_STREAM_VERSION + "|1|0|0|0|"
                        + getText() + '|';
            }

            objectReader.prepareToRead(encodedString);
            this.object = (T) objectReader.deserializeValue(this.targetClass);
        } catch (Exception e) {
            this.object = null;
            IOException ioe = new IOException("Couldn't read the GWT object representation: " + e.getMessage());
            ioe.initCause(e);
            throw ioe;
        }

    }

    return object;
}