Search in sources :

Example 6 with RemoteInvocationResult

use of org.springframework.remoting.support.RemoteInvocationResult in project opennms by OpenNMS.

the class ServiceRegistryHttpInvokerServiceExporter method handleRequest.

public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    try {
        RemoteInvocation invocation = readRemoteInvocation(request);
        Serializable interfaceNameObject = invocation.getAttribute(ServiceRegistryHttpInvokerProxyFactoryBean.ATTRIBUTE_INTERFACE_NAME);
        if (interfaceNameObject == null) {
            throw new NestedServletException("Interface name attribute not found. This class can only service requests to a " + ServiceRegistryHttpInvokerProxyFactoryBean.class.getSimpleName() + " client.");
        } else {
            String interfaceName = (String) interfaceNameObject;
            try {
                //RemoteInvocationResult result = invokeAndCreateResult(invocation, getProxy());
                // TODO: Use a method similar to {@link RemoteExporter#getProxyForService()} to create an
                // interface proxy that masks any other methods on the remotely invoked object.
                RemoteInvocationResult result = invokeAndCreateResult(invocation, serviceRegistry.findProvider(Class.forName(interfaceName)));
                writeRemoteInvocationResult(request, response, result);
            } catch (IllegalArgumentException e) {
                throw new NestedServletException("No provider registered for interface " + interfaceName, e);
            }
        }
    } catch (ClassNotFoundException e) {
        throw new NestedServletException("Class not found during deserialization", e);
    }
}
Also used : RemoteInvocation(org.springframework.remoting.support.RemoteInvocation) Serializable(java.io.Serializable) NestedServletException(org.springframework.web.util.NestedServletException) RemoteInvocationResult(org.springframework.remoting.support.RemoteInvocationResult)

Example 7 with RemoteInvocationResult

use of org.springframework.remoting.support.RemoteInvocationResult in project spring-framework by spring-projects.

the class HttpInvokerTests method httpInvokerProxyFactoryBeanAndServiceExporterWithInvocationAttributes.

@Test
public void httpInvokerProxyFactoryBeanAndServiceExporterWithInvocationAttributes() throws Exception {
    TestBean target = new TestBean("myname", 99);
    final HttpInvokerServiceExporter exporter = new HttpInvokerServiceExporter();
    exporter.setServiceInterface(ITestBean.class);
    exporter.setService(target);
    exporter.setRemoteInvocationExecutor(new DefaultRemoteInvocationExecutor() {

        @Override
        public Object invoke(RemoteInvocation invocation, Object targetObject) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
            assertNotNull(invocation.getAttributes());
            assertEquals(1, invocation.getAttributes().size());
            assertEquals("myValue", invocation.getAttributes().get("myKey"));
            assertEquals("myValue", invocation.getAttribute("myKey"));
            return super.invoke(invocation, targetObject);
        }
    });
    exporter.afterPropertiesSet();
    HttpInvokerProxyFactoryBean pfb = new HttpInvokerProxyFactoryBean();
    pfb.setServiceInterface(ITestBean.class);
    pfb.setServiceUrl("http://myurl");
    pfb.setRemoteInvocationFactory(new RemoteInvocationFactory() {

        @Override
        public RemoteInvocation createRemoteInvocation(MethodInvocation methodInvocation) {
            RemoteInvocation invocation = new RemoteInvocation(methodInvocation);
            invocation.addAttribute("myKey", "myValue");
            try {
                invocation.addAttribute("myKey", "myValue");
                fail("Should have thrown IllegalStateException");
            } catch (IllegalStateException ex) {
            // expected: already defined
            }
            assertNotNull(invocation.getAttributes());
            assertEquals(1, invocation.getAttributes().size());
            assertEquals("myValue", invocation.getAttributes().get("myKey"));
            assertEquals("myValue", invocation.getAttribute("myKey"));
            return invocation;
        }
    });
    pfb.setHttpInvokerRequestExecutor(new AbstractHttpInvokerRequestExecutor() {

        @Override
        protected RemoteInvocationResult doExecuteRequest(HttpInvokerClientConfiguration config, ByteArrayOutputStream baos) throws Exception {
            assertEquals("http://myurl", config.getServiceUrl());
            MockHttpServletRequest request = new MockHttpServletRequest();
            MockHttpServletResponse response = new MockHttpServletResponse();
            request.setContent(baos.toByteArray());
            exporter.handleRequest(request, response);
            return readRemoteInvocationResult(new ByteArrayInputStream(response.getContentAsByteArray()), config.getCodebaseUrl());
        }
    });
    pfb.afterPropertiesSet();
    ITestBean proxy = (ITestBean) pfb.getObject();
    assertEquals("myname", proxy.getName());
    assertEquals(99, proxy.getAge());
}
Also used : RemoteInvocation(org.springframework.remoting.support.RemoteInvocation) DefaultRemoteInvocationExecutor(org.springframework.remoting.support.DefaultRemoteInvocationExecutor) RemoteInvocationResult(org.springframework.remoting.support.RemoteInvocationResult) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MethodInvocation(org.aopalliance.intercept.MethodInvocation) ByteArrayOutputStream(java.io.ByteArrayOutputStream) InvocationTargetException(java.lang.reflect.InvocationTargetException) ServletException(javax.servlet.ServletException) RemoteAccessException(org.springframework.remoting.RemoteAccessException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ITestBean(org.springframework.tests.sample.beans.ITestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) ByteArrayInputStream(java.io.ByteArrayInputStream) RemoteInvocationFactory(org.springframework.remoting.support.RemoteInvocationFactory) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 8 with RemoteInvocationResult

use of org.springframework.remoting.support.RemoteInvocationResult in project spring-framework by spring-projects.

the class HttpInvokerTests method doTestHttpInvokerProxyFactoryBeanAndServiceExporter.

private void doTestHttpInvokerProxyFactoryBeanAndServiceExporter(boolean explicitClassLoader) throws Throwable {
    TestBean target = new TestBean("myname", 99);
    final HttpInvokerServiceExporter exporter = new HttpInvokerServiceExporter();
    exporter.setServiceInterface(ITestBean.class);
    exporter.setService(target);
    exporter.afterPropertiesSet();
    HttpInvokerProxyFactoryBean pfb = new HttpInvokerProxyFactoryBean();
    pfb.setServiceInterface(ITestBean.class);
    pfb.setServiceUrl("http://myurl");
    pfb.setHttpInvokerRequestExecutor(new AbstractHttpInvokerRequestExecutor() {

        @Override
        protected RemoteInvocationResult doExecuteRequest(HttpInvokerClientConfiguration config, ByteArrayOutputStream baos) throws Exception {
            assertEquals("http://myurl", config.getServiceUrl());
            MockHttpServletRequest request = new MockHttpServletRequest();
            MockHttpServletResponse response = new MockHttpServletResponse();
            request.setContent(baos.toByteArray());
            exporter.handleRequest(request, response);
            return readRemoteInvocationResult(new ByteArrayInputStream(response.getContentAsByteArray()), config.getCodebaseUrl());
        }
    });
    if (explicitClassLoader) {
        ((BeanClassLoaderAware) pfb.getHttpInvokerRequestExecutor()).setBeanClassLoader(getClass().getClassLoader());
    }
    pfb.afterPropertiesSet();
    ITestBean proxy = (ITestBean) pfb.getObject();
    assertEquals("myname", proxy.getName());
    assertEquals(99, proxy.getAge());
    proxy.setAge(50);
    assertEquals(50, proxy.getAge());
    proxy.setStringArray(new String[] { "str1", "str2" });
    assertTrue(Arrays.equals(new String[] { "str1", "str2" }, proxy.getStringArray()));
    proxy.setSomeIntegerArray(new Integer[] { 1, 2, 3 });
    assertTrue(Arrays.equals(new Integer[] { 1, 2, 3 }, proxy.getSomeIntegerArray()));
    proxy.setNestedIntegerArray(new Integer[][] { { 1, 2, 3 }, { 4, 5, 6 } });
    Integer[][] integerArray = proxy.getNestedIntegerArray();
    assertTrue(Arrays.equals(new Integer[] { 1, 2, 3 }, integerArray[0]));
    assertTrue(Arrays.equals(new Integer[] { 4, 5, 6 }, integerArray[1]));
    proxy.setSomeIntArray(new int[] { 1, 2, 3 });
    assertTrue(Arrays.equals(new int[] { 1, 2, 3 }, proxy.getSomeIntArray()));
    proxy.setNestedIntArray(new int[][] { { 1, 2, 3 }, { 4, 5, 6 } });
    int[][] intArray = proxy.getNestedIntArray();
    assertTrue(Arrays.equals(new int[] { 1, 2, 3 }, intArray[0]));
    assertTrue(Arrays.equals(new int[] { 4, 5, 6 }, intArray[1]));
    try {
        proxy.exceptional(new IllegalStateException());
        fail("Should have thrown IllegalStateException");
    } catch (IllegalStateException ex) {
    // expected
    }
    try {
        proxy.exceptional(new IllegalAccessException());
        fail("Should have thrown IllegalAccessException");
    } catch (IllegalAccessException ex) {
    // expected
    }
}
Also used : RemoteInvocationResult(org.springframework.remoting.support.RemoteInvocationResult) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ServletException(javax.servlet.ServletException) RemoteAccessException(org.springframework.remoting.RemoteAccessException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ITestBean(org.springframework.tests.sample.beans.ITestBean) BeanClassLoaderAware(org.springframework.beans.factory.BeanClassLoaderAware) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) ByteArrayInputStream(java.io.ByteArrayInputStream) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse)

Example 9 with RemoteInvocationResult

use of org.springframework.remoting.support.RemoteInvocationResult in project spring-framework by spring-projects.

the class HttpInvokerTests method httpInvokerProxyFactoryBeanAndServiceExporterWithGzipCompression.

@Test
public void httpInvokerProxyFactoryBeanAndServiceExporterWithGzipCompression() throws Throwable {
    TestBean target = new TestBean("myname", 99);
    final HttpInvokerServiceExporter exporter = new HttpInvokerServiceExporter() {

        @Override
        protected InputStream decorateInputStream(HttpServletRequest request, InputStream is) throws IOException {
            if ("gzip".equals(request.getHeader("Compression"))) {
                return new GZIPInputStream(is);
            } else {
                return is;
            }
        }

        @Override
        protected OutputStream decorateOutputStream(HttpServletRequest request, HttpServletResponse response, OutputStream os) throws IOException {
            if ("gzip".equals(request.getHeader("Compression"))) {
                return new GZIPOutputStream(os);
            } else {
                return os;
            }
        }
    };
    exporter.setServiceInterface(ITestBean.class);
    exporter.setService(target);
    exporter.afterPropertiesSet();
    HttpInvokerProxyFactoryBean pfb = new HttpInvokerProxyFactoryBean();
    pfb.setServiceInterface(ITestBean.class);
    pfb.setServiceUrl("http://myurl");
    pfb.setHttpInvokerRequestExecutor(new AbstractHttpInvokerRequestExecutor() {

        @Override
        protected RemoteInvocationResult doExecuteRequest(HttpInvokerClientConfiguration config, ByteArrayOutputStream baos) throws IOException, ClassNotFoundException {
            assertEquals("http://myurl", config.getServiceUrl());
            MockHttpServletRequest request = new MockHttpServletRequest();
            request.addHeader("Compression", "gzip");
            MockHttpServletResponse response = new MockHttpServletResponse();
            request.setContent(baos.toByteArray());
            try {
                exporter.handleRequest(request, response);
            } catch (ServletException ex) {
                throw new IOException(ex.toString());
            }
            return readRemoteInvocationResult(new ByteArrayInputStream(response.getContentAsByteArray()), config.getCodebaseUrl());
        }

        @Override
        protected OutputStream decorateOutputStream(OutputStream os) throws IOException {
            return new GZIPOutputStream(os);
        }

        @Override
        protected InputStream decorateInputStream(InputStream is) throws IOException {
            return new GZIPInputStream(is);
        }
    });
    pfb.afterPropertiesSet();
    ITestBean proxy = (ITestBean) pfb.getObject();
    assertEquals("myname", proxy.getName());
    assertEquals(99, proxy.getAge());
    proxy.setAge(50);
    assertEquals(50, proxy.getAge());
    try {
        proxy.exceptional(new IllegalStateException());
        fail("Should have thrown IllegalStateException");
    } catch (IllegalStateException ex) {
    // expected
    }
    try {
        proxy.exceptional(new IllegalAccessException());
        fail("Should have thrown IllegalAccessException");
    } catch (IllegalAccessException ex) {
    // expected
    }
}
Also used : RemoteInvocationResult(org.springframework.remoting.support.RemoteInvocationResult) GZIPInputStream(java.util.zip.GZIPInputStream) ObjectInputStream(java.io.ObjectInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) OutputStream(java.io.OutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) GZIPInputStream(java.util.zip.GZIPInputStream) ServletException(javax.servlet.ServletException) ITestBean(org.springframework.tests.sample.beans.ITestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 10 with RemoteInvocationResult

use of org.springframework.remoting.support.RemoteInvocationResult in project spring-framework by spring-projects.

the class HttpInvokerTests method httpInvokerProxyFactoryBeanAndServiceExporterWithIOException.

@Test
public void httpInvokerProxyFactoryBeanAndServiceExporterWithIOException() throws Exception {
    TestBean target = new TestBean("myname", 99);
    final HttpInvokerServiceExporter exporter = new HttpInvokerServiceExporter();
    exporter.setServiceInterface(ITestBean.class);
    exporter.setService(target);
    exporter.afterPropertiesSet();
    HttpInvokerProxyFactoryBean pfb = new HttpInvokerProxyFactoryBean();
    pfb.setServiceInterface(ITestBean.class);
    pfb.setServiceUrl("http://myurl");
    pfb.setHttpInvokerRequestExecutor(new HttpInvokerRequestExecutor() {

        @Override
        public RemoteInvocationResult executeRequest(HttpInvokerClientConfiguration config, RemoteInvocation invocation) throws IOException {
            throw new IOException("argh");
        }
    });
    pfb.afterPropertiesSet();
    ITestBean proxy = (ITestBean) pfb.getObject();
    try {
        proxy.setAge(50);
        fail("Should have thrown RemoteAccessException");
    } catch (RemoteAccessException ex) {
        // expected
        assertTrue(ex.getCause() instanceof IOException);
    }
}
Also used : RemoteInvocation(org.springframework.remoting.support.RemoteInvocation) ITestBean(org.springframework.tests.sample.beans.ITestBean) RemoteInvocationResult(org.springframework.remoting.support.RemoteInvocationResult) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) RemoteAccessException(org.springframework.remoting.RemoteAccessException) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

RemoteInvocationResult (org.springframework.remoting.support.RemoteInvocationResult)13 RemoteInvocation (org.springframework.remoting.support.RemoteInvocation)11 IOException (java.io.IOException)7 RemoteAccessException (org.springframework.remoting.RemoteAccessException)7 ITestBean (org.springframework.tests.sample.beans.ITestBean)7 Test (org.junit.Test)6 TestBean (org.springframework.tests.sample.beans.TestBean)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 ServletException (javax.servlet.ServletException)5 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)5 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)5 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 ObjectInputStream (java.io.ObjectInputStream)2 ObjectOutputStream (java.io.ObjectOutputStream)2 MethodInvocation (org.aopalliance.intercept.MethodInvocation)2 RemoteInvocationFailureException (org.springframework.remoting.RemoteInvocationFailureException)2 DefaultRemoteInvocationExecutor (org.springframework.remoting.support.DefaultRemoteInvocationExecutor)2 RemoteInvocationFactory (org.springframework.remoting.support.RemoteInvocationFactory)2 NestedServletException (org.springframework.web.util.NestedServletException)2