Search in sources :

Example 1 with HttpException

use of org.eclipse.scout.rt.shared.servicetunnel.HttpException 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

HttpResponse (com.google.api.client.http.HttpResponse)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 FutureCancelledError (org.eclipse.scout.rt.platform.util.concurrent.FutureCancelledError)1 ThreadInterruptedError (org.eclipse.scout.rt.platform.util.concurrent.ThreadInterruptedError)1 HttpException (org.eclipse.scout.rt.shared.servicetunnel.HttpException)1 ServiceTunnelResponse (org.eclipse.scout.rt.shared.servicetunnel.ServiceTunnelResponse)1