Search in sources :

Example 1 with TransformInInterceptor

use of org.apache.cxf.interceptor.transform.TransformInInterceptor in project cxf by apache.

the class JAXRSSoapBookTest method testPostBookTransform.

@Test
public void testPostBookTransform() throws Exception {
    String address = "http://localhost:" + PORT + "/test/v1/rest-transform/bookstore/books";
    TransformOutInterceptor out = new TransformOutInterceptor();
    out.setOutTransformElements(Collections.singletonMap("{http://www.example.org/books}*", "{http://www.example.org/super-books}*"));
    TransformInInterceptor in = new TransformInInterceptor();
    Map<String, String> map = new HashMap<>();
    // If Book2 didn't have {http://www.example.org/books}Book
    // then we'd just do '"*" : "{http://www.example.org/books}*'
    // but given that we have TheBook being returned, we need
    map.put("TheBook", "{http://www.example.org/books}Book");
    map.put("id", "{http://www.example.org/books}id");
    in.setInTransformElements(map);
    WebClient client = WebClient.create(address);
    WebClient.getConfig(client).getInInterceptors().add(in);
    WebClient.getConfig(client).getOutInterceptors().add(out);
    Book2 book = client.type("application/xml").accept("text/xml").post(new Book2(), Book2.class);
    assertEquals(124L, book.getId());
}
Also used : HashMap(java.util.HashMap) TransformOutInterceptor(org.apache.cxf.interceptor.transform.TransformOutInterceptor) WebClient(org.apache.cxf.jaxrs.client.WebClient) TransformInInterceptor(org.apache.cxf.interceptor.transform.TransformInInterceptor) Test(org.junit.Test)

Example 2 with TransformInInterceptor

use of org.apache.cxf.interceptor.transform.TransformInInterceptor in project cxf by apache.

the class StaxTransformFeatureTest method testTransformWithLogging.

@Test
public void testTransformWithLogging() throws Exception {
    SpringBusFactory bf = new SpringBusFactory();
    Bus bus = bf.createBus();
    BusFactory.setDefaultBus(bus);
    TestLoggingEventSender logIn = new TestLoggingEventSender();
    bus.getInInterceptors().add(new LoggingInInterceptor(logIn));
    bus.getInFaultInterceptors().add(new LoggingInInterceptor(logIn));
    TestLoggingEventSender logOut = new TestLoggingEventSender();
    bus.getOutInterceptors().add(new LoggingOutInterceptor(logOut));
    bus.getOutFaultInterceptors().add(new LoggingOutInterceptor(logOut));
    TransformInInterceptor transIn = new TransformInInterceptor();
    Map<String, String> inElements = new HashMap<>();
    inElements.put("{http://cxf.apache.org/greeter_control/types}noFaultDetail", "{http://cxf.apache.org/greeter_control/types}faultDetail");
    bus.getInInterceptors().add(transIn);
    TransformOutInterceptor transOut = new TransformOutInterceptor();
    Map<String, String> outElements = new HashMap<>();
    outElements.put("{http://cxf.apache.org/greeter_control/types}pingMe", "{http://cxf.apache.org/greeter_control/types}dontPingMe");
    transOut.setOutTransformElements(outElements);
    bus.getOutInterceptors().add(transOut);
    bus.getOutFaultInterceptors().add(transOut);
    GreeterService gs = new GreeterService();
    greeter = gs.getGreeterPort();
    updateAddressPort(greeter, PORT);
    LOG.fine("Created greeter client.");
    // ping 1: request-response transformation
    greeter.pingMe();
    verifyPayload(logOut.getMessage(), "dontPingMe");
    verifyPayload(logIn.getMessage(), "pingMeResponse");
    verifyPayload(serverlogIn.getMessage(), "dontPingMe");
    verifyPayload(serverlogOut.getMessage(), "pingMeResponse");
    serverlogOut.cleaerMessage();
    serverlogIn.cleaerMessage();
    logOut.cleaerMessage();
    logIn.cleaerMessage();
    // ping 2: request-fault transformation
    try {
        greeter.pingMe();
        fail("Ping should have failed");
    } catch (Exception e) {
        assertEquals("Pings succeed only every other time.", e.getMessage());
    }
    verifyPayload(logOut.getMessage(), "dontPingMe");
    verifyPayload(logIn.getMessage(), "noFaultDetail");
    verifyPayload(serverlogIn.getMessage(), "dontPingMe");
    verifyPayload(serverlogOut.getMessage(), "noFaultDetail");
    // ping 3: idle
    greeter.pingMe();
    serverlogOut.cleaerMessage();
    serverlogIn.cleaerMessage();
    logOut.cleaerMessage();
    logIn.cleaerMessage();
    // ping 4: request-fault transformation with skipOnFault
    transOut.setSkipOnFault(true);
    servertransOut.setSkipOnFault(true);
    try {
        greeter.pingMe();
        fail("Ping should have failed");
    } catch (Exception e) {
        assertEquals("Pings succeed only every other time.", e.getMessage());
    }
    verifyPayload(logOut.getMessage(), "dontPingMe");
    verifyPayload(logIn.getMessage(), "faultDetail");
    verifyPayload(serverlogIn.getMessage(), "dontPingMe");
    verifyPayload(serverlogOut.getMessage(), "faultDetail");
    bus.shutdown(true);
}
Also used : Bus(org.apache.cxf.Bus) SpringBusFactory(org.apache.cxf.bus.spring.SpringBusFactory) HashMap(java.util.HashMap) LoggingOutInterceptor(org.apache.cxf.ext.logging.LoggingOutInterceptor) LoggingInInterceptor(org.apache.cxf.ext.logging.LoggingInInterceptor) GreeterService(org.apache.cxf.greeter_control.GreeterService) TransformOutInterceptor(org.apache.cxf.interceptor.transform.TransformOutInterceptor) TransformInInterceptor(org.apache.cxf.interceptor.transform.TransformInInterceptor) Test(org.junit.Test)

Example 3 with TransformInInterceptor

use of org.apache.cxf.interceptor.transform.TransformInInterceptor in project cxf by apache.

the class JAXRSSoapBookTest method testGetUnqualifiedBookSoap.

@Test
public void testGetUnqualifiedBookSoap() throws Exception {
    String wsdlAddress = "http://localhost:" + PORT + "/test/services/soap-transform/bookservice?wsdl";
    BookSoapService service = new BookSoapService(new URL(wsdlAddress), new QName("http://books.com", "BookService"));
    BookStoreJaxrsJaxws store = service.getBookPort();
    TransformOutInterceptor out = new TransformOutInterceptor();
    Map<String, String> mapOut = new HashMap<>();
    // Book content (id, name) is unqualified, thus the following works
    // because JAXB will report
    // - {http://jaxws.jaxrs.systest.cxf.apache.org/}Book
    // - id
    // - name
    // and only the qualified top-level Book tag gets matched by the following
    // mapping
    mapOut.put("{http://jaxws.jaxrs.systest.cxf.apache.org/}*", "*");
    out.setOutTransformElements(mapOut);
    TransformInInterceptor in = new TransformInInterceptor();
    Map<String, String> mapIn = new HashMap<>();
    // mapIn.put("*", "{http://jaxws.jaxrs.systest.cxf.apache.org/}*");
    // won't work for a case where a totally unqualified getBookResponse needs to be
    // qualified such that only the top-level getBookResponse is processed because of '*'.
    // Such a mapping would work nicely if we had say a package-info making both
    // Book id & name qualified; otherwise we need to choose what tag we need to qualify
    // mapIn.put("*", "{http://jaxws.jaxrs.systest.cxf.apache.org/}*");
    // works too if the schema validation is disabled
    mapIn.put("getBookResponse", "{http://jaxws.jaxrs.systest.cxf.apache.org/}getBookResponse");
    in.setInTransformElements(mapIn);
    Client cl = ClientProxy.getClient(store);
    ((HTTPConduit) cl.getConduit()).getClient().setReceiveTimeout(10000000);
    cl.getInInterceptors().add(in);
    cl.getOutInterceptors().add(out);
    Book book = store.getBook(new Long(123));
    assertEquals("id is wrong", book.getId(), 123);
}
Also used : BookStoreJaxrsJaxws(org.apache.cxf.systest.jaxrs.jaxws.BookStoreJaxrsJaxws) HashMap(java.util.HashMap) BookSoapService(org.apache.cxf.systest.jaxrs.jaxws.BookSoapService) QName(javax.xml.namespace.QName) TransformOutInterceptor(org.apache.cxf.interceptor.transform.TransformOutInterceptor) Client(org.apache.cxf.endpoint.Client) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) WebClient(org.apache.cxf.jaxrs.client.WebClient) URL(java.net.URL) TransformInInterceptor(org.apache.cxf.interceptor.transform.TransformInInterceptor) Test(org.junit.Test)

Aggregations

HashMap (java.util.HashMap)3 TransformInInterceptor (org.apache.cxf.interceptor.transform.TransformInInterceptor)3 TransformOutInterceptor (org.apache.cxf.interceptor.transform.TransformOutInterceptor)3 Test (org.junit.Test)3 WebClient (org.apache.cxf.jaxrs.client.WebClient)2 URL (java.net.URL)1 QName (javax.xml.namespace.QName)1 Bus (org.apache.cxf.Bus)1 SpringBusFactory (org.apache.cxf.bus.spring.SpringBusFactory)1 Client (org.apache.cxf.endpoint.Client)1 LoggingInInterceptor (org.apache.cxf.ext.logging.LoggingInInterceptor)1 LoggingOutInterceptor (org.apache.cxf.ext.logging.LoggingOutInterceptor)1 GreeterService (org.apache.cxf.greeter_control.GreeterService)1 BookSoapService (org.apache.cxf.systest.jaxrs.jaxws.BookSoapService)1 BookStoreJaxrsJaxws (org.apache.cxf.systest.jaxrs.jaxws.BookStoreJaxrsJaxws)1 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)1