Exemplo n.º 1
0
public class CxfConsumerResponseTest extends CamelTestSupport {

  private static final String ECHO_OPERATION = "echo";
  private static final String ECHO_BOOLEAN_OPERATION = "echoBoolean";
  private static final String PING_OPERATION = "ping";
  private static final String TEST_MESSAGE = "Hello World!";
  private static int pingCounter;

  protected final String simpleEndpointAddress =
      "http://localhost:" + CXFTestSupport.getPort1() + "/" + getClass().getSimpleName() + "/test";

  protected final String simpleEndpointURI =
      "cxf://"
          + simpleEndpointAddress
          + "?serviceClass=org.apache.camel.component.cxf.HelloService"
          + "&publishedEndpointUrl=http://www.simple.com/services/test";

  @Override
  public boolean isCreateCamelContextPerClass() {
    return true;
  }

  // START SNIPPET: example
  protected RouteBuilder createRouteBuilder() {
    return new RouteBuilder() {
      public void configure() {
        from(simpleEndpointURI)
            .inOnly("log:test")
            .choice()
            .when(header(CxfConstants.OPERATION_NAME).isEqualTo(ECHO_OPERATION))
            .process(
                new Processor() {
                  public void process(final Exchange exchange) {
                    assertEquals(
                        DataFormat.POJO,
                        exchange.getProperty(CxfConstants.DATA_FORMAT_PROPERTY, DataFormat.class));
                    Message in = exchange.getIn();
                    // check the remote IP from the cxfMessage
                    org.apache.cxf.message.Message cxfMessage =
                        in.getHeader(
                            CxfConstants.CAMEL_CXF_MESSAGE, org.apache.cxf.message.Message.class);
                    assertNotNull(
                        "Should get the cxfMessage instance from message header", cxfMessage);
                    ServletRequest request = (ServletRequest) cxfMessage.get("HTTP.REQUEST");
                    assertNotNull("Should get the ServletRequest", request);
                    assertNotNull("Should get the RemoteAddress" + request.getRemoteAddr());
                    // Get the parameter list
                    List<?> parameter = in.getBody(List.class);
                    // Get the operation name
                    String operation = (String) in.getHeader(CxfConstants.OPERATION_NAME);
                    Object result = operation + " " + (String) parameter.get(0);
                    // Put the result back
                    exchange.getIn().setBody(result);
                    // set up the response context which force start document
                    Map<String, Object> map = new HashMap<String, Object>();
                    map.put("org.apache.cxf.stax.force-start-document", Boolean.TRUE);
                    exchange.getIn().setHeader(Client.RESPONSE_CONTEXT, map);
                  }
                })
            .when(header(CxfConstants.OPERATION_NAME).isEqualTo(ECHO_BOOLEAN_OPERATION))
            .process(
                new Processor() {
                  public void process(final Exchange exchange) {
                    Message in = exchange.getIn();
                    // Get the parameter list
                    List<?> parameter = in.getBody(List.class);
                    // Put the result back
                    exchange.getOut().setBody(parameter.get(0));
                  }
                })
            .when(header(CxfConstants.OPERATION_NAME).isEqualTo(PING_OPERATION))
            .process(
                new Processor() {
                  public void process(final Exchange exchange) {
                    pingCounter++;
                  }
                });
      }
    };
  }
  // END SNIPPET: example

  @Test
  public void testInvokingServiceFromCXFClient() throws Exception {
    ClientProxyFactoryBean proxyFactory = new ClientProxyFactoryBean();
    ClientFactoryBean clientBean = proxyFactory.getClientFactoryBean();
    clientBean.setAddress(simpleEndpointAddress);
    clientBean.setServiceClass(HelloService.class);
    clientBean.setBus(BusFactory.getDefaultBus());

    HelloService client = (HelloService) proxyFactory.create();
    assertNotNull(client);

    String result = client.echo(TEST_MESSAGE);
    assertEquals(
        "We should get the echo string result from router", result, "echo " + TEST_MESSAGE);

    Boolean bool = client.echoBoolean(Boolean.TRUE);
    assertNotNull("The result should not be null", bool);
    assertEquals("We should get the echo boolean result from router ", bool.toString(), "true");

    int beforeCallingPing = pingCounter;
    client.ping();
    int afterCallingPing = pingCounter;
    assertTrue("The ping operation doesn't be called", afterCallingPing - beforeCallingPing == 1);
  }
}
Exemplo n.º 2
0
public class CxfProducerSessionTest extends CamelTestSupport {
  private static final int PORT = CXFTestSupport.getPort1();
  private static final String SIMPLE_SERVER_ADDRESS =
      "http://127.0.0.1:" + PORT + "/CxfProducerSessionTest/test";
  private static final String REQUEST_MESSAGE_EXPRESSION =
      "<ns1:echo xmlns:ns1=\"http://cxf.component.camel.apache.org/\"><arg0>${in.body}</arg0></ns1:echo>";
  private static final Map<String, String> NAMESPACES =
      Collections.singletonMap("ns1", "http://cxf.component.camel.apache.org/");
  private static final String PARAMETER_XPATH = "/ns1:echoResponse/return/text()";

  private String url =
      "cxf://"
          + SIMPLE_SERVER_ADDRESS
          + "?serviceClass=org.apache.camel.component.cxf.EchoService&dataFormat=PAYLOAD&synchronous=true";

  @Override
  public boolean isCreateCamelContextPerClass() {
    return true;
  }

  @BeforeClass
  public static void startServer() throws Exception {
    // start a simple front service
    JaxWsServiceFactoryBean svrFBean = new JaxWsServiceFactoryBean();
    svrFBean.setServiceClass(EchoService.class);
    JaxWsServerFactoryBean svrBean = new JaxWsServerFactoryBean(svrFBean);
    svrBean.setAddress(SIMPLE_SERVER_ADDRESS);
    svrBean.setServiceClass(EchoService.class);
    svrBean.setServiceBean(new EchoServiceSessionImpl());
    // make the Jetty server support sessions
    Bus bus = BusFactory.newInstance().createBus();
    JettyHTTPServerEngineFactory jettyFactory =
        bus.getExtension(JettyHTTPServerEngineFactory.class);
    jettyFactory.createJettyHTTPServerEngine(PORT, "http").setSessionSupport(true);
    svrBean.setBus(bus);
    svrBean.create();
  }

  @AfterClass
  public static void destroyServer() {
    // If we don't destroy this the session support will spill over to other
    // tests and they will fail
    JettyHTTPServerEngineFactory.destroyForPort(PORT);
  }

  @Test
  public void testNoSession() throws Exception {
    getMockEndpoint("mock:result").expectedMessageCount(2);
    String response = template.requestBody("direct:start", "World", String.class);
    assertEquals("New New World", response);
    response = template.requestBody("direct:start", "World", String.class);
    assertEquals("New New World", response);
    assertMockEndpointsSatisfied();
  }

  @Test
  public void testExchangeSession() throws Exception {
    getMockEndpoint("mock:result").expectedMessageCount(2);
    String response = template.requestBody("direct:exchange", "World", String.class);
    assertEquals("Old New World", response);
    response = template.requestBody("direct:exchange", "World", String.class);
    assertEquals("Old New World", response);
    assertMockEndpointsSatisfied();
  }

  @Test
  public void testInstanceSession() throws Exception {
    getMockEndpoint("mock:result").expectedMessageCount(2);
    String response = template.requestBody("direct:instance", "World", String.class);
    assertEquals("Old New World", response);
    response = template.requestBody("direct:instance", "World", String.class);
    assertEquals("Old Old World", response);
    assertMockEndpointsSatisfied();
  }

  protected RouteBuilder createRouteBuilder() {
    return new RouteBuilder() {
      public void configure() {
        from("direct:start")
            .setBody()
            .simple(REQUEST_MESSAGE_EXPRESSION)
            .to(url)
            .setBody()
            .xpath(PARAMETER_XPATH, String.class, NAMESPACES)
            .setBody()
            .simple(REQUEST_MESSAGE_EXPRESSION)
            .to(url)
            .setBody()
            .xpath(PARAMETER_XPATH, String.class, NAMESPACES)
            .to("mock:result");
        from("direct:instance")
            .setBody()
            .simple(REQUEST_MESSAGE_EXPRESSION)
            .to(url + "&cookieHandler=#instanceCookieHandler")
            .setBody()
            .xpath(PARAMETER_XPATH, String.class, NAMESPACES)
            .setBody()
            .simple(REQUEST_MESSAGE_EXPRESSION)
            .to(url + "&cookieHandler=#instanceCookieHandler")
            .setBody()
            .xpath(PARAMETER_XPATH, String.class, NAMESPACES)
            .to("mock:result");
        from("direct:exchange")
            .setBody()
            .simple(REQUEST_MESSAGE_EXPRESSION)
            .to(url + "&cookieHandler=#exchangeCookieHandler")
            .setBody()
            .xpath(PARAMETER_XPATH, String.class, NAMESPACES)
            .setBody()
            .simple(REQUEST_MESSAGE_EXPRESSION)
            .to(url + "&cookieHandler=#exchangeCookieHandler")
            .setBody()
            .xpath(PARAMETER_XPATH, String.class, NAMESPACES)
            .to("mock:result");
      }
    };
  }

  @Override
  protected JndiRegistry createRegistry() throws Exception {
    JndiRegistry jndiRegistry = super.createRegistry();
    jndiRegistry.bind("instanceCookieHandler", new InstanceCookieHandler());
    jndiRegistry.bind("exchangeCookieHandler", new ExchangeCookieHandler());
    return jndiRegistry;
  }
}