Search in sources :

Example 16 with ServiceTunnelResponse

use of org.eclipse.scout.rt.shared.servicetunnel.ServiceTunnelResponse in project scout.rt by eclipse.

the class RemoteServiceInvocationCallable method call.

/**
 * Invokes the remote service operation.
 *
 * @return {@link IServiceTunnelResponse}; is never <code>null</code>.
 */
@Override
public ServiceTunnelResponse call() throws Exception {
    long nBytes = 0;
    final long tStart = LOG.isDebugEnabled() ? System.nanoTime() : 0L;
    try {
        // Create the request.
        final ByteArrayOutputStream requestMessage = new ByteArrayOutputStream();
        m_tunnel.getContentHandler().writeRequest(requestMessage, m_serviceRequest);
        requestMessage.close();
        final byte[] requestData = requestMessage.toByteArray();
        nBytes = requestData.length;
        // Send the request to the server.
        HttpResponse resp = m_tunnel.executeRequest(m_serviceRequest, requestData);
        // Receive the response.
        m_tunnel.interceptHttpResponse(resp, m_serviceRequest);
        if (resp.getStatusCode() != 0 && (resp.getStatusCode() < 200 || resp.getStatusCode() > 299)) {
            // request failed
            return new ServiceTunnelResponse(new HttpException(resp.getStatusCode()));
        }
        try (InputStream in = resp.getContent()) {
            return m_tunnel.getContentHandler().readResponse(in);
        }
    } catch (IOException e) {
        if (Thread.currentThread().isInterrupted()) {
            LOG.debug("Ignoring IOException for interrupted thread.", e);
            return new ServiceTunnelResponse(new ThreadInterruptedError("Thread is interrupted.", e));
        } else if (RunMonitor.CURRENT.get().isCancelled()) {
            LOG.debug("Ignoring IOException for cancelled thread.", e);
            return new ServiceTunnelResponse(new FutureCancelledError("RunMonitor is cancelled.", e));
        }
        throw e;
    } finally {
        if (LOG.isDebugEnabled()) {
            final long elapsedMillis = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - tStart);
            LOG.debug("TIME {}.{} {}ms {} bytes", m_serviceRequest.getServiceInterfaceClassName(), m_serviceRequest.getOperation(), elapsedMillis, nBytes);
        }
    }
}
Also used : InputStream(java.io.InputStream) FutureCancelledError(org.eclipse.scout.rt.platform.util.concurrent.FutureCancelledError) HttpResponse(com.google.api.client.http.HttpResponse) ThreadInterruptedError(org.eclipse.scout.rt.platform.util.concurrent.ThreadInterruptedError) HttpException(org.eclipse.scout.rt.shared.servicetunnel.HttpException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ServiceTunnelResponse(org.eclipse.scout.rt.shared.servicetunnel.ServiceTunnelResponse)

Aggregations

ServiceTunnelResponse (org.eclipse.scout.rt.shared.servicetunnel.ServiceTunnelResponse)16 Test (org.junit.Test)8 StringContains.containsString (org.hamcrest.core.StringContains.containsString)5 ServiceTunnelRequest (org.eclipse.scout.rt.shared.servicetunnel.ServiceTunnelRequest)4 ProcessingException (org.eclipse.scout.rt.platform.exception.ProcessingException)3 HttpResponse (com.google.api.client.http.HttpResponse)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 Method (java.lang.reflect.Method)2 FutureCancelledError (org.eclipse.scout.rt.platform.util.concurrent.FutureCancelledError)2 ThreadInterruptedError (org.eclipse.scout.rt.platform.util.concurrent.ThreadInterruptedError)2 IPingService (org.eclipse.scout.rt.shared.services.common.ping.IPingService)2 IServiceTunnelContentHandler (org.eclipse.scout.rt.shared.servicetunnel.IServiceTunnelContentHandler)2 ServiceUtility (org.eclipse.scout.rt.shared.servicetunnel.ServiceUtility)2 GenericUrl (com.google.api.client.http.GenericUrl)1 HttpContent (com.google.api.client.http.HttpContent)1 HttpRequest (com.google.api.client.http.HttpRequest)1 HttpRequestFactory (com.google.api.client.http.HttpRequestFactory)1