Search in sources :

Example 1 with RemoteInvocationResult

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

the class HttpInvokerServiceExporter method handleRequest.

/**
	 * Reads a remote invocation from the request, executes it,
	 * and writes the remote invocation result to the response.
	 * @see #readRemoteInvocation(HttpServletRequest)
	 * @see #invokeAndCreateResult(org.springframework.remoting.support.RemoteInvocation, Object)
	 * @see #writeRemoteInvocationResult(HttpServletRequest, HttpServletResponse, RemoteInvocationResult)
	 */
@Override
public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    try {
        RemoteInvocation invocation = readRemoteInvocation(request);
        RemoteInvocationResult result = invokeAndCreateResult(invocation, getProxy());
        writeRemoteInvocationResult(request, response, result);
    } catch (ClassNotFoundException ex) {
        throw new NestedServletException("Class not found during deserialization", ex);
    }
}
Also used : RemoteInvocation(org.springframework.remoting.support.RemoteInvocation) NestedServletException(org.springframework.web.util.NestedServletException) RemoteInvocationResult(org.springframework.remoting.support.RemoteInvocationResult)

Example 2 with RemoteInvocationResult

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

the class HttpInvokerTests method httpInvokerProxyFactoryBeanAndServiceExporterWithWrappedInvocations.

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

        @Override
        protected RemoteInvocation doReadRemoteInvocation(ObjectInputStream ois) throws IOException, ClassNotFoundException {
            Object obj = ois.readObject();
            if (!(obj instanceof TestRemoteInvocationWrapper)) {
                throw new IOException("Deserialized object needs to be assignable to type [" + TestRemoteInvocationWrapper.class.getName() + "]: " + obj);
            }
            return ((TestRemoteInvocationWrapper) obj).remoteInvocation;
        }

        @Override
        protected void doWriteRemoteInvocationResult(RemoteInvocationResult result, ObjectOutputStream oos) throws IOException {
            oos.writeObject(new TestRemoteInvocationResultWrapper(result));
        }
    };
    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());
        }

        @Override
        protected void doWriteRemoteInvocation(RemoteInvocation invocation, ObjectOutputStream oos) throws IOException {
            oos.writeObject(new TestRemoteInvocationWrapper(invocation));
        }

        @Override
        protected RemoteInvocationResult doReadRemoteInvocationResult(ObjectInputStream ois) throws IOException, ClassNotFoundException {
            Object obj = ois.readObject();
            if (!(obj instanceof TestRemoteInvocationResultWrapper)) {
                throw new IOException("Deserialized object needs to be assignable to type [" + TestRemoteInvocationResultWrapper.class.getName() + "]: " + obj);
            }
            return ((TestRemoteInvocationResultWrapper) obj).remoteInvocationResult;
        }
    });
    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 : RemoteInvocation(org.springframework.remoting.support.RemoteInvocation) RemoteInvocationResult(org.springframework.remoting.support.RemoteInvocationResult) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) 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) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Example 3 with RemoteInvocationResult

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

the class HttpInvokerTests method httpInvokerProxyFactoryBeanAndServiceExporterWithCustomInvocationObject.

@Test
public void httpInvokerProxyFactoryBeanAndServiceExporterWithCustomInvocationObject() 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 {
            assertTrue(invocation instanceof TestRemoteInvocation);
            assertNull(invocation.getAttributes());
            assertNull(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 TestRemoteInvocation(methodInvocation);
            assertNull(invocation.getAttributes());
            assertNull(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 4 with RemoteInvocationResult

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

the class HttpInvokerTests method httpInvokerWithSpecialLocalMethods.

@Test
public void httpInvokerWithSpecialLocalMethods() throws Exception {
    String serviceUrl = "http://myurl";
    HttpInvokerProxyFactoryBean pfb = new HttpInvokerProxyFactoryBean();
    pfb.setServiceInterface(ITestBean.class);
    pfb.setServiceUrl(serviceUrl);
    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();
    // shouldn't go through to remote service
    assertTrue(proxy.toString().indexOf("HTTP invoker") != -1);
    assertTrue(proxy.toString().indexOf(serviceUrl) != -1);
    assertEquals(proxy.hashCode(), proxy.hashCode());
    assertTrue(proxy.equals(proxy));
    // should go through
    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) RemoteAccessException(org.springframework.remoting.RemoteAccessException) IOException(java.io.IOException) Test(org.junit.Test)

Example 5 with RemoteInvocationResult

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

the class JmsInvokerClientInterceptor method invoke.

@Override
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
    if (AopUtils.isToStringMethod(methodInvocation.getMethod())) {
        return "JMS invoker proxy for queue [" + this.queue + "]";
    }
    RemoteInvocation invocation = createRemoteInvocation(methodInvocation);
    RemoteInvocationResult result;
    try {
        result = executeRequest(invocation);
    } catch (JMSException ex) {
        throw convertJmsInvokerAccessException(ex);
    }
    try {
        return recreateRemoteInvocationResult(result);
    } catch (Throwable ex) {
        if (result.hasInvocationTargetException()) {
            throw ex;
        } else {
            throw new RemoteInvocationFailureException("Invocation of method [" + methodInvocation.getMethod() + "] failed in JMS invoker remote service at queue [" + this.queue + "]", ex);
        }
    }
}
Also used : RemoteInvocation(org.springframework.remoting.support.RemoteInvocation) RemoteInvocationResult(org.springframework.remoting.support.RemoteInvocationResult) JMSException(javax.jms.JMSException) RemoteInvocationFailureException(org.springframework.remoting.RemoteInvocationFailureException)

Aggregations

RemoteInvocationResult (org.springframework.remoting.support.RemoteInvocationResult)15 RemoteInvocation (org.springframework.remoting.support.RemoteInvocation)12 IOException (java.io.IOException)8 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)3 NestedServletException (org.springframework.web.util.NestedServletException)3 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