Example usage for com.google.gwt.user.client.rpc SerializationStreamWriter toString

List of usage examples for com.google.gwt.user.client.rpc SerializationStreamWriter toString

Introduction

In this page you can find the example usage for com.google.gwt.user.client.rpc SerializationStreamWriter toString.

Prototype

String toString();

Source Link

Document

Serializes the contents of this stream into a string.

Usage

From source file:com.gdevelop.gwt.syncrpc.RemoteServiceInvocationHandler.java

License:Apache License

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    RemoteServiceSyncProxy syncProxy = createSyncProxy();
    Class remoteServiceInft = method.getDeclaringClass();
    for (Class intf : proxy.getClass().getInterfaces()) {
        if (RemoteService.class.isAssignableFrom(intf)) {
            remoteServiceInft = intf;//w  w  w.ja  v a  2s .  co m
        }
    }
    SerializationStreamWriter streamWriter = syncProxy.createStreamWriter();
    AsyncCallback callback = null;
    Class[] paramTypes = method.getParameterTypes();
    try {
        // Determine whether sync or async
        boolean isAsync = false;
        // String serviceIntfName =
        // method.getDeclaringClass().getCanonicalName();
        String serviceIntfName = remoteServiceInft.getCanonicalName();
        int paramCount = paramTypes.length;
        Class returnType = method.getReturnType();
        if (method.getDeclaringClass().getCanonicalName().endsWith("Async")) {
            isAsync = true;
            serviceIntfName = serviceIntfName.substring(0, serviceIntfName.length() - 5);
            paramCount--;
            callback = (AsyncCallback) args[paramCount];
            if (asyncCallbackWrapperSupplier != null) {
                callback = asyncCallbackWrapperSupplier.get().wrap(callback);
            }
            // Determine the return type
            Class[] syncParamTypes = new Class[paramCount];
            System.arraycopy(paramTypes, 0, syncParamTypes, 0, paramCount);
            Class clazz;
            try {
                clazz = Class.forName(serviceIntfName);
            } catch (ClassNotFoundException e) {
                throw new InvocationException("There are not sync version of " + serviceIntfName + "Async");
            }
            Method syncMethod = clazz.getMethod(method.getName(), syncParamTypes);
            if (syncMethod != null) {
                returnType = syncMethod.getReturnType();
            } else {
                throw new InvocationException("Sync & Async method does not match.");
            }
        }
        // Interface name
        streamWriter.writeString(serviceIntfName);
        // Method name
        streamWriter.writeString(method.getName());
        // Params count
        streamWriter.writeInt(paramCount);
        // Params type
        for (int i = 0; i < paramCount; i++) {
            streamWriter.writeString(computeBinaryClassName(paramTypes[i]));
        }
        // Params
        for (int i = 0; i < paramCount; i++) {
            writeParam(streamWriter, paramTypes[i], args[i]);
        }
        String payload = streamWriter.toString();
        if (isAsync) {
            final RemoteServiceSyncProxy syncProxy_2 = syncProxy;
            final Class returnType_2 = returnType;
            final String payload_2 = payload;
            final AsyncCallback callback_2 = callback;
            new Thread() {
                public void run() {
                    Object result;
                    try {
                        result = syncProxy_2.doInvoke(getReaderFor(returnType_2), payload_2);
                        if (callback_2 != null) {
                            callback_2.onSuccess(result);
                        }
                    } catch (Throwable e) {
                        if (callback_2 != null) {
                            callback_2.onFailure(e);
                        }
                    }
                }
            }.start();
            return null;
        } else {
            return syncProxy.doInvoke(getReaderFor(returnType), payload);
        }
        /*
         * Object result = syncProxy.doInvoke(getReaderFor(returnType),
         * payload); if (callback != null){ callback.onSuccess(result); }
         * return result;
         */
    } catch (Throwable ex) {
        if (callback != null) {
            callback.onFailure(ex);
            return null;
        }
        Class[] expClasses = method.getExceptionTypes();
        for (Class clazz : expClasses) {
            if (clazz.isAssignableFrom(ex.getClass())) {
                throw ex;
            }
        }
        throw new InvocationException("Exception while invoking the remote service "
                + method.getDeclaringClass().getName() + "." + method.getName(), ex);
    }
}

From source file:fr.putnami.pwt.core.service.client.DefaultCommandController.java

License:Open Source License

private int sendRequest(List<Request> requests, AsyncCallback<List<CommandResponse>> callback) {
    if (requests == null || requests.isEmpty()) {
        return 0;
    }//from www .  j av a2  s .  c  o m
    try {
        Collection<Serializer> serializers = Sets.newHashSet();
        List<CommandRequest> commands = Lists.newArrayList();

        for (Request request : requests) {
            serializers.add(request.param.getSerializer());
            commands.add(request.command);
            if (!request.param.isQuiet()) {
                this.fireEvent(new CommandRequestEvent(request.requestId, request.command));
            }
        }

        ServiceCallback serviceCallback = new ServiceCallback(requests, callback);
        CommandServiceCompositeSerializer compositeSerializer = new CommandServiceCompositeSerializer(
                serializers);

        SerializationStreamFactory streamFactory = new CommandSerializationStreamFactory(compositeSerializer,
                this.moduleBaseURL);
        SerializationStreamWriter streamWriter = streamFactory.createStreamWriter();

        streamWriter.writeString(DefaultCommandController.REMOTE_SERVICE_INTERFACE_NAME);
        streamWriter.writeString(DefaultCommandController.METHOD_NAME);
        streamWriter.writeInt(1);
        streamWriter.writeString(List.class.getName());
        streamWriter.writeObject(commands);

        String payload = streamWriter.toString();

        RpcStatsContext statsContext = new RpcStatsContext();

        RequestCallback responseHandler = new RequestCallbackAdapter<List<CommandResponse>>(streamFactory,
                DefaultCommandController.METHOD_NAME, statsContext, serviceCallback, null,
                ResponseReader.OBJECT);

        this.rpcRequestBuilder.create(this.remoteServiceURL);
        this.rpcRequestBuilder.setCallback(responseHandler);
        this.rpcRequestBuilder.setContentType(DefaultCommandController.RPC_CONTENT_TYPE);
        this.rpcRequestBuilder.setRequestData(payload);
        this.rpcRequestBuilder.setRequestId(statsContext.getRequestId());

        RequestBuilder rb = this.rpcRequestBuilder.finish();
        CsrfController.get().securize(rb);
        rb.send();

        return requests.size();
    } catch (SerializationException e) {
        throw new CommandException(e.getMessage());
    } catch (RequestException e) {
        throw new CommandException(e.getMessage());
    }
}

From source file:nl.fontys.fhict.jea.gwt.jee7.client.bus.impl.RPC_MIME_TYPE.java

public static String encode(ArrayList<ClientIncoming> incomings) throws EncodeException {
    try {/*ww  w. j ava  2 s .co  m*/
        final SerializationStreamWriter writer = PROXY.createStreamWriter();
        writer.writeObject(incomings);
        return writer.toString();
    } catch (SerializationException ex) {
        throw new EncodeException("cannot decode " + incomings, ex);
    }
}

From source file:org.restlet.client.representation.ObjectRepresentation.java

License:LGPL

@Override
public String getText() {
    if ((this.object != null) && (super.getText() == null)) {
        try {/* w  ww  .  j a  v  a 2s.  c o  m*/
            // Create a stream writer
            SerializationStreamWriter objectWriter = getSerializationStreamFactory().createStreamWriter();

            // Serialize the object
            objectWriter.writeObject(this.object);
            setText(objectWriter.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    return super.getText();
}

From source file:se.aaslin.developer.roboproxy.remote.RemoteServiceInvocationHandler.java

License:Apache License

@SuppressWarnings("unchecked")
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    RemoteServiceSyncProxy syncProxy = new RemoteServiceSyncProxy(moduleBaseURL, remoteServiceRelativePath,
            serializationPolicyName, cookieManager, context);
    Class<?> remoteServiceInft = method.getDeclaringClass();
    for (Class<?> intf : proxy.getClass().getInterfaces()) {
        if (RemoteService.class.isAssignableFrom(intf)) {
            remoteServiceInft = intf;// w w w.j av  a2s . c  om
        }
    }

    SerializationStreamWriter streamWriter = syncProxy.createStreamWriter();

    AsyncCallback<Object> callback = null;
    Class<?>[] paramTypes = method.getParameterTypes();
    try {
        // Determine whether sync or async
        boolean isAsync = false;
        // String serviceIntfName =
        // method.getDeclaringClass().getCanonicalName();
        String serviceIntfName = remoteServiceInft.getCanonicalName();
        int paramCount = paramTypes.length;
        Class<?> returnType = method.getReturnType();
        if (method.getDeclaringClass().getCanonicalName().endsWith("Async")) {
            isAsync = true;
            serviceIntfName = serviceIntfName.substring(0, serviceIntfName.length() - 5);
            paramCount--;
            callback = (AsyncCallback<Object>) args[paramCount];

            // Determine the return type
            Class<?>[] syncParamTypes = new Class[paramCount];
            System.arraycopy(paramTypes, 0, syncParamTypes, 0, paramCount);
            Class<?> clazz;
            try {
                clazz = Class.forName(serviceIntfName);
            } catch (ClassNotFoundException e) {
                throw new InvocationException("There are not sync version of " + serviceIntfName + "Async");
            }
            Method syncMethod = clazz.getMethod(method.getName(), syncParamTypes);
            if (syncMethod != null) {
                returnType = syncMethod.getReturnType();
            } else {
                throw new InvocationException("Sync & Async method does not match.");
            }
        }

        // Interface name
        streamWriter.writeString(serviceIntfName);
        // Method name
        streamWriter.writeString(method.getName());

        // Params count
        streamWriter.writeInt(paramCount);

        // Params type
        for (int i = 0; i < paramCount; i++) {
            // streamWriter.writeString(computeBinaryClassName(paramTypes[i]));
            streamWriter.writeString(SerializabilityUtil.getSerializedTypeName(paramTypes[i]));
        }

        // Params
        for (int i = 0; i < paramCount; i++) {
            writeParam(streamWriter, paramTypes[i], args[i]);
        }

        String payload = streamWriter.toString();
        if (isAsync) {
            final RemoteServiceSyncProxy syncProxy_2 = syncProxy;
            final Class<?> returnType_2 = returnType;
            final String payload_2 = payload;
            final AsyncCallback<Object> callback_2 = callback;
            AsyncTask<Void, Void, Object> asyncTask = new AsyncTask<Void, Void, Object>() {

                @Override
                protected Object doInBackground(Void... params) {
                    try {
                        return syncProxy_2.doInvoke(getReaderFor(returnType_2), payload_2);
                    } catch (Throwable e) {
                        return e;
                    }
                }

                @Override
                protected void onPostExecute(Object result) {
                    if (result instanceof Throwable) {
                        Throwable caught = (Throwable) result;
                        if (callback_2 != null) {
                            callback_2.onFailure(caught);
                        }
                    } else {
                        if (callback_2 != null) {
                            callback_2.onSuccess(result);
                        }
                    }
                }

            };
            asyncTask.execute();

            return null;
        } else {
            return syncProxy.doInvoke(getReaderFor(returnType), payload);
        }
        /*
         * Object result = syncProxy.doInvoke(getReaderFor(returnType),
         * payload); if (callback != null){ callback.onSuccess(result); }
         * return result;
         */
    } catch (Throwable ex) {
        if (callback != null) {
            callback.onFailure(ex);
            return null;
        }
        Class<?>[] expClasses = method.getExceptionTypes();
        for (Class<?> clazz : expClasses) {
            if (clazz.isAssignableFrom(ex.getClass())) {
                throw ex;
            }
        }

        throw ex;
    }
}