Search in sources :

Example 11 with ServiceTunnelResponse

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

the class ServiceOperationInvokerTest method testInvokeWithSession.

@Test
public void testInvokeWithSession() {
    when(m_pingSvc.ping(any(String.class))).thenReturn(m_testData);
    ServiceTunnelResponse res = invokePingService(createRunContextWithSession());
    assertValidResponse(res, m_testData);
}
Also used : StringContains.containsString(org.hamcrest.core.StringContains.containsString) ServiceTunnelResponse(org.eclipse.scout.rt.shared.servicetunnel.ServiceTunnelResponse) Test(org.junit.Test)

Example 12 with ServiceTunnelResponse

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

the class ServiceOperationInvoker method invoke.

/**
 * Invoke the service associated with the {@link ServiceTunnelRequest}. <br>
 * Must be called within a transaction.
 */
@SuppressWarnings("squid:S1193")
public ServiceTunnelResponse invoke(final RunContext runContext, final ServiceTunnelRequest serviceReq) {
    final long t0 = System.nanoTime();
    ServiceTunnelResponse response;
    try {
        response = runContext.call(new Callable<ServiceTunnelResponse>() {

            @Override
            public ServiceTunnelResponse call() throws Exception {
                return invokeInternal(serviceReq);
            }
        }, DefaultExceptionTranslator.class);
    } catch (Exception e) {
        // Associate the exception with context information about the service call.
        if (e instanceof PlatformException) {
            ((PlatformException) e).withContextInfo("service.name", serviceReq.getServiceInterfaceClassName()).withContextInfo("service.operation", serviceReq.getOperation());
        }
        // Handle the exception.
        handleException(e);
        // Prepare ServiceTunnelResponse.
        response = new ServiceTunnelResponse(interceptException(e));
    }
    response.setProcessingDuration(TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - t0));
    LOG.debug("TIME {}.{} {}ms", serviceReq.getServiceInterfaceClassName(), serviceReq.getOperation(), response.getProcessingDuration());
    return response;
}
Also used : DefaultExceptionTranslator(org.eclipse.scout.rt.platform.exception.DefaultExceptionTranslator) PlatformException(org.eclipse.scout.rt.platform.exception.PlatformException) ServiceTunnelResponse(org.eclipse.scout.rt.shared.servicetunnel.ServiceTunnelResponse) Callable(java.util.concurrent.Callable) ProcessingException(org.eclipse.scout.rt.platform.exception.ProcessingException) PlatformException(org.eclipse.scout.rt.platform.exception.PlatformException) VetoException(org.eclipse.scout.rt.platform.exception.VetoException)

Example 13 with ServiceTunnelResponse

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

the class ServiceOperationInvoker method invokeInternal.

protected ServiceTunnelResponse invokeInternal(ServiceTunnelRequest serviceReq) throws ClassNotFoundException {
    IServerSession serverSession = ServerSessionProvider.currentSession();
    if (LOG.isDebugEnabled()) {
        String userId = serverSession != null ? serverSession.getUserId() : "";
        LOG.debug("started {}.{} by {} at {}", serviceReq.getServiceInterfaceClassName(), serviceReq.getOperation(), userId, new Date());
    }
    CallInspector callInspector = getCallInspector(serviceReq, serverSession);
    ServiceTunnelResponse serviceRes = null;
    try {
        ServiceUtility serviceUtility = BEANS.get(ServiceUtility.class);
        Class<?> serviceInterfaceClass = SerializationUtility.getClassLoader().loadClass(serviceReq.getServiceInterfaceClassName());
        Method serviceOp = serviceUtility.getServiceOperation(serviceInterfaceClass, serviceReq.getOperation(), serviceReq.getParameterTypes());
        Object[] args = serviceReq.getArgs();
        Object service = getValidatedServiceAccess(serviceInterfaceClass, serviceOp, args);
        Object data = serviceUtility.invoke(service, serviceOp, args);
        Object[] outParameters = serviceUtility.extractHolderArguments(args);
        serviceRes = new ServiceTunnelResponse(data, outParameters);
        return serviceRes;
    } finally {
        updateInspector(callInspector, serviceRes);
    }
}
Also used : CallInspector(org.eclipse.scout.rt.server.admin.inspector.CallInspector) Method(java.lang.reflect.Method) ServiceUtility(org.eclipse.scout.rt.shared.servicetunnel.ServiceUtility) Date(java.util.Date) ServiceTunnelResponse(org.eclipse.scout.rt.shared.servicetunnel.ServiceTunnelResponse)

Example 14 with ServiceTunnelResponse

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

the class ServiceTunnelServlet method doPost.

protected ServiceTunnelResponse doPost(ServiceTunnelRequest serviceRequest) throws ServletException {
    ClientNotificationCollector collector = new ClientNotificationCollector();
    ServerRunContext serverRunContext = ServerRunContexts.copyCurrent().withLocale(serviceRequest.getLocale()).withUserAgent(UserAgents.createByIdentifier(serviceRequest.getUserAgent())).withClientNotificationCollector(collector).withClientNodeId(serviceRequest.getClientNodeId());
    if (serviceRequest.getSessionId() != null) {
        serverRunContext.withSession(lookupServerSessionOnHttpSession(serviceRequest.getSessionId(), serverRunContext));
    }
    final IRegistrationHandle registrationHandle = registerForCancellation(serverRunContext, serviceRequest);
    try {
        ServiceTunnelResponse serviceResponse = invokeService(serverRunContext, serviceRequest);
        // include client notifications in response (piggyback)
        serviceResponse.setNotifications(collector.consume());
        return serviceResponse;
    } finally {
        registrationHandle.unregister();
    }
}
Also used : ServerRunContext(org.eclipse.scout.rt.server.context.ServerRunContext) ClientNotificationCollector(org.eclipse.scout.rt.server.clientnotification.ClientNotificationCollector) IRegistrationHandle(org.eclipse.scout.rt.server.context.RunMonitorCancelRegistry.IRegistrationHandle) ServiceTunnelResponse(org.eclipse.scout.rt.shared.servicetunnel.ServiceTunnelResponse)

Example 15 with ServiceTunnelResponse

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

the class HttpServiceTunnel method tunnel.

@Override
protected ServiceTunnelResponse tunnel(final ServiceTunnelRequest serviceRequest) {
    final long requestSequence = serviceRequest.getRequestSequence();
    // Create the Callable to be given to the job manager for execution.
    final RemoteServiceInvocationCallable remoteInvocationCallable = createRemoteServiceInvocationCallable(serviceRequest);
    // Register the execution monitor as child monitor of the current monitor so that the service request is cancelled once the current monitor gets cancelled.
    // Invoke the service operation asynchronously (to enable cancellation) and wait until completed or cancelled.
    final IFuture<ServiceTunnelResponse> future = Jobs.schedule(remoteInvocationCallable, Jobs.newInput().withRunContext(RunContext.CURRENT.get().copy()).withName(createServiceRequestName(requestSequence)).withExceptionHandling(null, // do not handle uncaught exceptions because typically invoked from within a model job (might cause a deadlock, because ClientExceptionHandler schedules and waits for a model job to visualize the exception).
    false)).whenDone(new IDoneHandler<ServiceTunnelResponse>() {

        @Override
        public void onDone(DoneEvent<ServiceTunnelResponse> event) {
            if (event.isCancelled()) {
                remoteInvocationCallable.cancel();
            }
        }
    }, RunContext.CURRENT.get().copy().withRunMonitor(// separate monitor to not cancel this cancellation action.
    BEANS.get(RunMonitor.class)));
    try {
        return future.awaitDoneAndGet();
    } catch (ThreadInterruptedError e) {
        // NOSONAR
        // Ensure the monitor to be cancelled once this thread is interrupted to cancel the remote call.
        future.cancel(true);
        // Interruption has precedence over computation result or computation error.
        return new ServiceTunnelResponse(new ThreadInterruptedError(TEXTS.get("UserInterrupted")));
    } catch (FutureCancelledError e) {
        // Cancellation has precedence over computation result or computation error.
        return new ServiceTunnelResponse(new FutureCancelledError(TEXTS.get("UserInterrupted")));
    }
}
Also used : FutureCancelledError(org.eclipse.scout.rt.platform.util.concurrent.FutureCancelledError) ThreadInterruptedError(org.eclipse.scout.rt.platform.util.concurrent.ThreadInterruptedError) 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