Example usage for com.google.gwt.user.client.rpc InvocationException InvocationException

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

Introduction

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

Prototype

public InvocationException(String s, Throwable cause) 

Source Link

Document

Constructs an exception with the given description and cause.

Usage

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

License:Apache License

public RemoteServiceSyncProxy(String moduleBaseURL, String remoteServiceRelativePath,
        String serializationPolicyName, SessionManager connectionManager) {
    this.moduleBaseURL = moduleBaseURL;
    if (remoteServiceRelativePath.startsWith("/")) {
        int idx = moduleBaseURL.indexOf("//") + 2;
        idx = moduleBaseURL.indexOf("/", idx);
        this.remoteServiceURL = moduleBaseURL.substring(0, idx) + remoteServiceRelativePath;
    } else {//from   w  w  w  .  j  a v a2 s .  com
        this.remoteServiceURL = moduleBaseURL + remoteServiceRelativePath;
    }
    this.serializationPolicyName = serializationPolicyName;
    this.connectionManager = connectionManager;
    if (serializationPolicyName == null) {
        serializationPolicy = new DummySerializationPolicy();
    } else {
        // TODO
        if (true) {
            // serializationPolicy = new DummySerializationPolicy();
            // return;
        }
        String policyFileName = SerializationPolicyLoader
                .getSerializationPolicyFileName(serializationPolicyName);
        // if pre-loaded, use the pre-loaded version.
        if (connectionManager instanceof DefaultSessionManager) {
            // may be unnecessary check and instead modify SessionManager
            // interface
            serializationPolicy = ((DefaultSessionManager) connectionManager)
                    .getSerializationPolicy(serializationPolicyName);
        }
        if (serializationPolicy == null) {
            InputStream is = getClass().getResourceAsStream("/" + policyFileName);
            try {
                serializationPolicy = SerializationPolicyLoader.loadFromStream(is, null);
            } catch (Exception e) {
                throw new InvocationException(
                        "Error while loading serialization policy " + serializationPolicyName, e);
            } finally {
                if (is != null) {
                    try {
                        is.close();
                    } catch (IOException e) {
                        // Ignore this error
                    }
                }
            }
        } // en
    }
    unionizeWhitelists();
}

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

License:Apache License

public Object doInvoke(RequestCallbackAdapter.ResponseReader responseReader, String requestData)
        throws Throwable {
    Map<String, String> headersCopy = new LinkedHashMap<String, String>();
    synchronized (RemoteServiceSyncProxy.class) {
        headersCopy.putAll(headers);/*from   w ww.ja  v a2  s.  com*/
    }
    HttpURLConnection connection = null;
    InputStream is = null;
    int statusCode;
    // Send request
    try {
        URL url = new URL(remoteServiceURL);
        connection = connectionManager.openConnection(url);
        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setRequestMethod("POST");
        connection.setRequestProperty(RpcRequestBuilder.STRONG_NAME_HEADER, serializationPolicyName);
        connection.setRequestProperty("Content-Type", "text/x-gwt-rpc; charset=utf-8");
        connection.setRequestProperty("Content-Length", "" + requestData.getBytes("UTF-8").length);
        // Explicitly set these to override any system properties.
        connection.setReadTimeout(socketTimeout * 1000);
        connection.setConnectTimeout(socketTimeout * 1000);
        for (Entry<String, String> header : headersCopy.entrySet()) {
            connection.setRequestProperty(header.getKey(), header.getValue());
        }
        OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
        writer.write(requestData);
        writer.flush();
        writer.close();
    } catch (IOException e) {
        throw new InvocationException("IOException while sending RPC request", e);
    }
    // Receive and process response
    try {
        connectionManager.handleResponseHeaders(connection);
        statusCode = connection.getResponseCode();
        is = connection.getInputStream();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len;
        while ((len = is.read(buffer)) > 0) {
            baos.write(buffer, 0, len);
        }
        String encodedResponse = baos.toString("UTF8");
        // System.out.println("Response payload (len = " +
        // encodedResponse.length() + "): " + encodedResponse);
        if (statusCode != HttpURLConnection.HTTP_OK) {
            throw new StatusCodeException(statusCode, encodedResponse);
        } else if (encodedResponse == null) {
            // This can happen if the XHR is interrupted by the server dying
            throw new InvocationException("No response payload");
        } else if (isReturnValue(encodedResponse)) {
            encodedResponse = encodedResponse.substring(4);
            return responseReader.read(createStreamReader(encodedResponse));
        } else if (isThrownException(encodedResponse)) {
            encodedResponse = encodedResponse.substring(4);
            Throwable throwable = (Throwable) createStreamReader(encodedResponse).readObject();
            throw throwable;
        } else {
            throw new InvocationException("Unknown response " + encodedResponse);
        }
    } catch (IOException e) {
        throw new InvocationException("IOException while receiving RPC response", e);
    } catch (SerializationException e) {
        throw new InvocationException("Error while deserialization response", e);
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException ignore) {
            }
        }
        if (connection != null) {
            // connection.disconnect();
        }
    }
}

From source file:com.seanchenxi.resteasy.autobean.client.RESTServiceProxy.java

License:Apache License

protected <T> Request invoke(RESTRequest<T> builder, Class<T> clazz, AsyncCallback<T> callback) {
    assert builder != null : "create RESTRequestBuilder at first";
    if (callback == null)
        throw new IllegalArgumentException("The AsyncCallback in " + builder.getResourceName() + " is null !");
    try {/*from  w ww . j av a2 s .  c om*/
        return builder.execute(clazz, callback);
    } catch (RequestException ex) {
        InvocationException iex = new InvocationException(
                "Unable to initiate the asynchronous service invocation (" + builder.getResourceName()
                        + ") -- check the network connection",
                ex);
        callback.onFailure(iex);
    }
    return null;
}

From source file:com.seanchenxi.resteasy.autobean.test.client.GreetingServiceAsyncM.java

License:Apache License

void greetServerObject(String name, boolean ok, AsyncCallback<Greeting> callback) {
    RequestBuilder builder = new RequestBuilder(RequestBuilder.GET,
            "/rest/greetM/serverInfoObject/" + name + "-" + ok);
    builder.setHeader("Content-Type", "application/json; charset=utf-8");
    builder.setHeader("Accept", "application/json");
    builder.setCallback(new ResponseHandler(Greeting.class, callback));
    try {/*from ww  w. j av  a 2s .c o m*/
        builder.send();
    } catch (RequestException ex) {
        InvocationException iex = new InvocationException(
                "Unable to initiate the asynchronous service invocation ( greetServerObject ) -- check the network connection",
                ex);
        callback.onFailure(iex);
    }
}

From source file:org.rapla.rest.gwtjsonrpc.client.JsonUtil.java

License:Apache License

public static <T> void invoke(final ResultDeserializer<T> resultDeserializer, final AsyncCallback<T> callback,
        final JavaScriptObject rpcResult) {
    final T result;
    try {//from ww w. j a  v  a  2  s . c  om
        result = resultDeserializer.fromResult(rpcResult);
    } catch (RuntimeException e) {
        callback.onFailure(new InvocationException("Invalid JSON Response", e));
        return;
    }
    callback.onSuccess(result);
}

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

License:Apache License

public RemoteServiceSyncProxy(String moduleBaseURL, String remoteServiceRelativePath,
        String serializationPolicyName, CookieManager cookieManager, Context context) throws IOException {
    this.moduleBaseURL = moduleBaseURL;
    this.remoteServiceURL = moduleBaseURL + remoteServiceRelativePath;
    this.serializationPolicyName = serializationPolicyName;
    this.cookieManager = cookieManager;

    if (serializationPolicyName == null) {
        serializationPolicy = new DummySerializationPolicy();
    } else {/* w w  w  .j  a v a 2  s.  c om*/
        String policyFileName = SerializationPolicyLoader
                .getSerializationPolicyFileName(serializationPolicyName);
        InputStream is = context.getAssets().open(new StringBuilder().append(policyFileName).toString());
        try {
            if (is == null) {
                // Try to get from cache
                String text = RpcPolicyFinder.getCachedPolicyFile(moduleBaseURL + policyFileName);
                if (text != null) {
                    is = new ByteArrayInputStream(text.getBytes("UTF8"));
                }
            }
            serializationPolicy = SerializationPolicyLoader.loadFromStream(is, null);
        } catch (Exception e) {
            throw new InvocationException("Error while loading serialization policy " + serializationPolicyName,
                    e);
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    // Ignore this error
                }
            }
        }
    }
}

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

License:Apache License

public Object doInvoke(RequestCallbackAdapter.ResponseReader responseReader, String requestData)
        throws Throwable {
    HttpURLConnection connection = null;
    InputStream is = null;/*w  w  w.ja  v a 2 s.co m*/
    int statusCode;

    if (DUMP_PAYLOAD) {
        System.out.println("Send request to " + remoteServiceURL);
        System.out.println("Request payload: " + requestData);
    }

    // Send request
    CookieHandler oldCookieHandler = CookieHandler.getDefault();
    try {
        CookieHandler.setDefault(cookieManager);

        URL url = new URL(remoteServiceURL);

        connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setRequestMethod("POST");
        connection.setRequestProperty(RpcRequestBuilder.STRONG_NAME_HEADER, serializationPolicyName);
        connection.setRequestProperty(RpcRequestBuilder.MODULE_BASE_HEADER, moduleBaseURL);
        connection.setRequestProperty("Content-Type", "text/x-gwt-rpc; charset=utf-8");
        connection.setRequestProperty("Content-Length", "" + requestData.getBytes("UTF-8").length);

        OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
        writer.write(requestData);
        writer.flush();
        writer.close();
    } catch (IOException e) {
        throw new InvocationException("IOException while sending RPC request", e);
    } finally {
        CookieHandler.setDefault(oldCookieHandler);
    }

    // Receive and process response
    try {
        statusCode = connection.getResponseCode();
        is = connection.getInputStream();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len;
        while ((len = is.read(buffer)) > 0) {
            baos.write(buffer, 0, len);
        }
        String encodedResponse = baos.toString("UTF8");
        if (DUMP_PAYLOAD) {
            System.out.println("Response code: " + statusCode);
            System.out.println("Response payload: " + encodedResponse);
        }

        // System.out.println("Response payload (len = " +
        // encodedResponse.length() + "): " + encodedResponse);
        if (statusCode != HttpURLConnection.HTTP_OK) {
            throw new StatusCodeException(statusCode, encodedResponse);
        } else if (encodedResponse == null) {
            // This can happen if the XHR is interrupted by the server dying
            throw new InvocationException("No response payload");
        } else if (isReturnValue(encodedResponse)) {
            encodedResponse = encodedResponse.substring(4);
            return responseReader.read(createStreamReader(encodedResponse));
        } else if (isThrownException(encodedResponse)) {
            encodedResponse = encodedResponse.substring(4);
            Throwable throwable = (Throwable) createStreamReader(encodedResponse).readObject();
            throw throwable;
        } else {
            throw new InvocationException("Unknown response " + encodedResponse);
        }
    } catch (IOException e) {
        throw new InvocationException("IOException while receiving RPC response", e);
    } catch (SerializationException e) {
        throw new InvocationException("Error while deserialization response", e);
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException ignore) {
            }
        }
        if (connection != null) {
            // connection.disconnect();
        }
    }
}