Example usage for org.springframework.remoting.support RemoteInvocationResult RemoteInvocationResult

List of usage examples for org.springframework.remoting.support RemoteInvocationResult RemoteInvocationResult

Introduction

In this page you can find the example usage for org.springframework.remoting.support RemoteInvocationResult RemoteInvocationResult.

Prototype

public RemoteInvocationResult(@Nullable Throwable exception) 

Source Link

Document

Create a new RemoteInvocationResult for the given exception.

Usage

From source file:com.seajas.search.utilities.remoting.JsonHttpSupport.java

@Test
public void resultValue() throws Exception {
    HashMap<String, Object> map = new HashMap<String, Object>();
    map.put("a", null);
    map.put("b", "text");
    // stack overflow: map.put("c", map);
    RemoteInvocationResult result = new RemoteInvocationResult(map);

    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    exporter.writeRemoteInvocationResult(null, result, buffer);
    ByteArrayInputStream stream = new ByteArrayInputStream(buffer.toByteArray());
    RemoteInvocationResult deserialized = executor.readRemoteInvocationResult(stream, null);
    assertEquals(result.getValue(), deserialized.getValue());
    assertNull(deserialized.getException());
}

From source file:com.seajas.search.utilities.remoting.JsonHttpSupport.java

@Test
public void resultError() throws Exception {
    Throwable error = new RuntimeException("test");
    RemoteInvocationResult result = new RemoteInvocationResult(error);

    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    exporter.writeRemoteInvocationResult(null, result, buffer);
    ByteArrayInputStream stream = new ByteArrayInputStream(buffer.toByteArray());
    RemoteInvocationResult deserialized = executor.readRemoteInvocationResult(stream, null);
    assertNull(deserialized.getValue());
    assertNotNull(deserialized.getException());
    assertEquals(error.getClass(), deserialized.getException().getClass());
    assertEquals(error.getMessage(), deserialized.getException().getMessage());
}

From source file:com.seajas.search.utilities.remoting.JsonInvokerRequestExecutor.java

@Override
protected RemoteInvocationResult readRemoteInvocationResult(InputStream is, String codebaseUrl)
        throws IOException {
    JsonParser parser = factory.createJsonParser(is);
    parser.setCodec(codec);/*w w w. j  a v  a 2  s.co  m*/

    System.err.println(parser.nextToken());
    System.err.println(parser.nextToken());
    if (!"type".equals(parser.getCurrentName()))
        throw new StreamCorruptedException("Expected type field instead of " + parser.getCurrentName());
    System.err.println(parser.nextToken());
    Class<?> type = null;
    try {
        type = Class.forName(parser.getText());
    } catch (ClassNotFoundException e) {
        e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
    }

    System.err.println(parser.nextToken());
    if (!"content".equals(parser.getCurrentName()))
        throw new StreamCorruptedException("Expected content field instead of " + parser.getCurrentName());
    System.err.println(parser.nextToken());
    Object content = parser.readValueAs(type);
    if (content instanceof Throwable)
        return new RemoteInvocationResult((Throwable) content);
    return new RemoteInvocationResult(content);
}

From source file:org.apache.servicemix.http.endpoints.SerializedMarshalerTest.java

public void testUsingSpringHttpRemoting() throws Exception {
    final Person person = new PersonImpl("Hunter", "Thompson", 67);

    // Create a consumer endpoint
    HttpConsumerEndpoint ep = new HttpConsumerEndpoint();
    ep.setService(new QName("urn:HttpConsumer", "HttpConsumer"));
    ep.setEndpoint("HttpConsumer");
    ep.setLocationURI("http://localhost:8192/service/");
    ep.setTargetService(new QName("urn:HttpInvoker", "Endpoint"));

    // Configure the SerializedMarshaler and specifiy it on the endpoint
    SerializedMarshaler marshaler = new SerializedMarshaler();
    marshaler.setDefaultMep(MessageExchangeSupport.IN_OUT);
    ep.setMarshaler(marshaler);/*from ww  w.j  a  v  a  2  s  .c  om*/

    // Add the endpoint to the component and activate it
    HttpComponent component = new HttpComponent();
    component.setEndpoints(new HttpEndpointType[] { ep });
    container.activateComponent(component, "HttpConsumer");

    // Dummy up a component as a receiver and route it to urn:HttpInvoker/Endpoint
    TransformComponentSupport rmiComponent = new TransformComponentSupport() {
        protected boolean transform(MessageExchange exchange, NormalizedMessage in, NormalizedMessage out)
                throws MessagingException {
            try {
                // Deserialize rmi invocation
                XStream xstream = new XStream(new DomDriver());
                SourceTransformer st = new SourceTransformer();
                Object rmi = xstream.fromXML(st.toString(in.getContent()));

                DefaultRemoteInvocationExecutor executor = new DefaultRemoteInvocationExecutor();
                Object result = executor.invoke((RemoteInvocation) rmi, person);

                // Convert result to an rmi invocation
                RemoteInvocationResult rmiResult = new RemoteInvocationResult(result);
                out.setContent(new StringSource(xstream.toXML(rmiResult)));
            } catch (Exception e) {
                throw new MessagingException(e);
            }

            return true;
        }
    };
    ActivationSpec asReceiver = new ActivationSpec("rmiComponent", rmiComponent);
    asReceiver.setService(new QName("urn:HttpInvoker", "Endpoint"));
    container.activateComponent(asReceiver);

    // Start the JBI container
    container.start();

    // Set up the Spring bean to call into the URL specified for the consumer endpoint
    HttpInvokerProxyFactoryBean pfb = new HttpInvokerProxyFactoryBean();
    pfb.setServiceInterface(Person.class);
    pfb.setServiceUrl("http://localhost:8192/service/");
    pfb.setHttpInvokerRequestExecutor(new SimpleHttpInvokerRequestExecutor());
    pfb.afterPropertiesSet();

    // Grab the object via the proxy factory bean
    Person test = (Person) pfb.getObject();

    // Test getters
    assertEquals("Hunter", test.getGivenName());
    assertEquals("Thompson", test.getSurName());
    assertEquals(67, test.getAge());

    // Test setters
    test.setGivenName("John");
    test.setSurName("Doe");
    test.setAge(34);

    assertEquals(person.getGivenName(), "John");
    assertEquals(person.getSurName(), "Doe");
    assertEquals(person.getAge(), 34);
}

From source file:org.mule.module.spring.remoting.ObjectToRemoteInvocationResultTransformer.java

@Override
protected Object doTransform(Object src, String outputEncoding) throws TransformerException {
    try {/*w  w w .  java  2  s.c om*/
        if (logger.isDebugEnabled()) {
            logger.debug("ObjectToRemoteInvocationResult.doTransform(" + src + ")");
        }

        RemoteInvocationResult rval;

        if (src instanceof RemoteInvocationResult) {
            rval = (RemoteInvocationResult) src;
        } else {
            rval = new RemoteInvocationResult(src);
        }

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(rval);
        oos.close();
        return baos.toByteArray();
    } catch (Exception e) {
        throw new TransformerException(this, e);
    }
}

From source file:org.tizzit.util.spring.httpinvoker.StreamSupportingHttpInvokerServiceExporter.java

protected RemoteInvocationResult invokeAndCreateResult(final RemoteInvocation invocation,
        final Object targetObject) {
    try {/*from   www .  ja  v a 2s  . c  o  m*/
        final Object value = invoke(invocation, targetObject);
        if (invocation instanceof StreamSupportingRemoteInvocation) {
            final Boolean closedInputStreamParam = getParameterInputStreamClosedFlag(invocation);
            if (value instanceof InputStream) {
                return new StreamSupportingRemoteInvocationResult((InputStream) value, closedInputStreamParam);
            } else {
                return new StreamSupportingRemoteInvocationResult(value, closedInputStreamParam);
            }
        } else {
            return new RemoteInvocationResult(value);
        }
    } catch (Throwable ex) {
        if (invocation instanceof StreamSupportingRemoteInvocation) {
            return new StreamSupportingRemoteInvocationResult(ex,
                    getParameterInputStreamClosedFlag(invocation));
        } else {
            if (log.isWarnEnabled()) {
                log.warn(ex.getCause().getMessage());
            }
            if (log.isDebugEnabled()) {
                log.debug(ex);
            }
            if (ex.getCause() != null) {
                return new RemoteInvocationResult(ex.getCause());
            } else {
                return new RemoteInvocationResult(new Exception(ex.getMessage()));
            }

        }
    } finally {
        final ParameterInputStream pi = getParameterInputStreamFrom(invocation);
        if (pi != null) {
            try {
                pi.doRealClose(getEmptyInputStreamParameterBeforeReturn());
            } catch (IOException e) {
                log.warn("Error while attempting to close InputStream parameter for RemoteInvocation '"
                        + invocation + "'", e);
            }
        }
    }
}