Search in sources :

Example 1 with ServiceTunnelResponse

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

the class MockServiceTunnel method mockServiceCall.

/**
 * @return the service response You may call callTargetService() to simply call a service for test purpose (without a
 *         transaction!)
 */
protected ServiceTunnelResponse mockServiceCall(ServiceTunnelRequest req) throws Exception {
    try {
        ServiceUtility serviceUtility = BEANS.get(ServiceUtility.class);
        Class<?> serviceInterface = Class.forName(req.getServiceInterfaceClassName());
        Method serviceOperation = serviceUtility.getServiceOperation(serviceInterface, req.getOperation(), req.getParameterTypes());
        Object service = null;
        for (Object t : BEANS.all(serviceInterface)) {
            if (Proxy.isProxyClass(t.getClass())) {
                continue;
            }
            service = t;
            break;
        }
        Object result = serviceUtility.invoke(service, serviceOperation, req.getArgs());
        return new ServiceTunnelResponse(result, null, null);
    } catch (Throwable t) {
        return new ServiceTunnelResponse(null, null, t);
    }
}
Also used : Method(java.lang.reflect.Method) ServiceUtility(org.eclipse.scout.rt.shared.servicetunnel.ServiceUtility) ServiceTunnelResponse(org.eclipse.scout.rt.shared.servicetunnel.ServiceTunnelResponse)

Example 2 with ServiceTunnelResponse

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

the class HttpServiceTunnelTest method getTestContentHandler.

private IServiceTunnelContentHandler getTestContentHandler() {
    return new IServiceTunnelContentHandler() {

        @Override
        public void writeResponse(OutputStream out, ServiceTunnelResponse msg) throws IOException {
        }

        @Override
        public void writeRequest(OutputStream out, ServiceTunnelRequest msg) throws IOException {
        }

        @Override
        public ServiceTunnelResponse readResponse(InputStream in) throws IOException, ClassNotFoundException {
            ByteArrayInputStream bi = (ByteArrayInputStream) in;
            ObjectInputStream in2 = new ObjectInputStream(bi);
            Object o = in2.readObject();
            return (ServiceTunnelResponse) o;
        }

        @Override
        public ServiceTunnelRequest readRequest(InputStream in) throws IOException, ClassNotFoundException {
            return null;
        }

        @Override
        public void initialize() {
        }

        @Override
        public String getContentType() {
            return null;
        }
    };
}
Also used : IServiceTunnelContentHandler(org.eclipse.scout.rt.shared.servicetunnel.IServiceTunnelContentHandler) ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectInputStream(java.io.ObjectInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) OutputStream(java.io.OutputStream) ServiceTunnelRequest(org.eclipse.scout.rt.shared.servicetunnel.ServiceTunnelRequest) ServiceTunnelResponse(org.eclipse.scout.rt.shared.servicetunnel.ServiceTunnelResponse) ObjectInputStream(java.io.ObjectInputStream)

Example 3 with ServiceTunnelResponse

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

the class HttpServiceTunnelTest method testTunnel.

@Test
public void testTunnel() throws IOException {
    when(mockUrl.getValue()).thenReturn("http://localhost");
    final MockLowLevelHttpResponse expectedResponse = new MockLowLevelHttpResponse().setContent(getInputStream(new ServiceTunnelResponse("testData", new Object[] {})));
    HttpServiceTunnel tunnel = new HttpServiceTunnel() {

        @Override
        protected IHttpTransportManager getHttpTransportManager() {
            return new IHttpTransportManager() {

                private MockHttpTransport m_transport = new MockHttpTransport.Builder().setLowLevelHttpResponse(expectedResponse).build();

                @Override
                public HttpTransport getHttpTransport() {
                    return m_transport;
                }

                @Override
                public HttpRequestFactory getHttpRequestFactory() {
                    return m_transport.createRequestFactory();
                }

                @Override
                public void interceptNewHttpTransport(IHttpTransportBuilder builder) {
                // nop
                }
            };
        }
    };
    tunnel.setContentHandler(getTestContentHandler());
    ServiceTunnelRequest request = new ServiceTunnelRequest("IPingService", "ping", null, null);
    ServiceTunnelResponse response = tunnel.tunnel(request);
    assertNotNull(response);
}
Also used : MockLowLevelHttpResponse(com.google.api.client.testing.http.MockLowLevelHttpResponse) MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) IHttpTransportManager(org.eclipse.scout.rt.shared.http.IHttpTransportManager) ServiceTunnelRequest(org.eclipse.scout.rt.shared.servicetunnel.ServiceTunnelRequest) ServiceTunnelResponse(org.eclipse.scout.rt.shared.servicetunnel.ServiceTunnelResponse) IHttpTransportBuilder(org.eclipse.scout.rt.shared.http.IHttpTransportBuilder) Test(org.junit.Test)

Example 4 with ServiceTunnelResponse

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

the class ServiceTunnelServletCall method run.

@Override
public void run() {
    try {
        URL url = new URL(m_serverUrl + "/process");
        final IServiceTunnelContentHandler contentHandler = BEANS.get(IServiceTunnelContentHandler.class);
        contentHandler.initialize();
        // 
        HttpRequestFactory requestFactory = BEANS.get(HttpServiceTunnelTransportManager.class).getHttpRequestFactory();
        HttpRequest req = requestFactory.buildPostRequest(new GenericUrl(url), new HttpContent() {

            @Override
            public void writeTo(OutputStream out) throws IOException {
                contentHandler.writeRequest(out, m_req);
            }

            @Override
            public boolean retrySupported() {
                return false;
            }

            @Override
            public String getType() {
                return null;
            }

            @Override
            public long getLength() throws IOException {
                return 0;
            }
        });
        req.getHeaders().setAuthorization("Basic " + Base64Utility.encode("admin:manager".getBytes()));
        HttpResponse resp = req.execute();
        m_res = contentHandler.readResponse(resp.getContent());
    } catch (Throwable e) {
        m_res = new ServiceTunnelResponse(e);
    }
}
Also used : IServiceTunnelContentHandler(org.eclipse.scout.rt.shared.servicetunnel.IServiceTunnelContentHandler) HttpRequest(com.google.api.client.http.HttpRequest) HttpRequestFactory(com.google.api.client.http.HttpRequestFactory) OutputStream(java.io.OutputStream) HttpServiceTunnelTransportManager(org.eclipse.scout.rt.shared.servicetunnel.http.HttpServiceTunnelTransportManager) HttpResponse(com.google.api.client.http.HttpResponse) GenericUrl(com.google.api.client.http.GenericUrl) IOException(java.io.IOException) URL(java.net.URL) HttpContent(com.google.api.client.http.HttpContent) ServiceTunnelResponse(org.eclipse.scout.rt.shared.servicetunnel.ServiceTunnelResponse)

Example 5 with ServiceTunnelResponse

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

the class PiggyBackClientNotificationTest method testPiggyBack.

@Test
public void testPiggyBack() throws ServletException, IOException {
    ServiceTunnelServlet s = new ServiceTunnelServlet();
    Class[] parameterTypes = new Class[] { String.class };
    Object[] args = new Object[] { "test" };
    ServiceTunnelRequest req = new ServiceTunnelRequest(IPingService.class.getName(), "ping", parameterTypes, args);
    req.setClientNodeId("testNodeId");
    ServiceTunnelResponse res = s.doPost(req);
    assertEquals("pong", res.getData());
    assertNull(res.getException());
    assertEquals(1, res.getNotifications().size());
    assertEquals("testNotification", res.getNotifications().get(0).getNotification());
}
Also used : IPingService(org.eclipse.scout.rt.shared.services.common.ping.IPingService) ServiceTunnelRequest(org.eclipse.scout.rt.shared.servicetunnel.ServiceTunnelRequest) ServiceTunnelResponse(org.eclipse.scout.rt.shared.servicetunnel.ServiceTunnelResponse) Test(org.junit.Test)

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