Search in sources :

Example 1 with TransformOutInterceptor

use of org.apache.cxf.interceptor.transform.TransformOutInterceptor in project tesb-rt-se by Talend.

the class SOAPClient method addTransformInterceptors.

/**
 * Prepares transformation interceptors for a client.
 *
 * @param clientConfig the client configuration
 * @param newClient indicates if it is a new/updated client
 */
private void addTransformInterceptors(List<Interceptor<?>> inInterceptors, List<Interceptor<?>> outInterceptors, boolean newClient) {
    // The old service expects the Customer data be qualified with
    // the 'http://customer/v1' namespace.
    // The new service expects the Customer data be qualified with
    // the 'http://customer/v2' namespace.
    // If it is an old client talking to the new service then:
    // - the out transformation interceptor is configured for
    // 'http://customer/v1' qualified data be transformed into
    // 'http://customer/v2' qualified data.
    // - the in transformation interceptor is configured for
    // 'http://customer/v2' qualified response data be transformed into
    // 'http://customer/v1' qualified data.
    // If it is a new client talking to the old service then:
    // - the out transformation interceptor is configured for
    // 'http://customer/v2' qualified data be transformed into
    // 'http://customer/v1' qualified data.
    // - the in transformation interceptor is configured for
    // 'http://customer/v1' qualified response data be transformed into
    // 'http://customer/v2' qualified data.
    // - new Customer type also introduces a briefDescription property
    // which needs to be dropped for the old service validation to succeed
    // this configuration can be provided externally
    Map<String, String> newToOldTransformMap = new HashMap<String, String>();
    newToOldTransformMap.put("{http://customer/v2}*", "{http://customer/v1}*");
    Map<String, String> oldToNewTransformMap = Collections.singletonMap("{http://customer/v1}*", "{http://customer/v2}*");
    TransformOutInterceptor outTransform = new TransformOutInterceptor();
    outTransform.setOutTransformElements(newClient ? newToOldTransformMap : oldToNewTransformMap);
    if (newClient) {
        newToOldTransformMap.put("{http://customer/v2}briefDescription", "");
    // outTransform.setOutDropElements(
    // Collections.singletonList("{http://customer/v2}briefDescription"));
    }
    TransformInInterceptor inTransform = new TransformInInterceptor();
    inTransform.setInTransformElements(newClient ? oldToNewTransformMap : newToOldTransformMap);
    inInterceptors.add(inTransform);
    outInterceptors.add(outTransform);
}
Also used : HashMap(java.util.HashMap) TransformOutInterceptor(org.apache.cxf.interceptor.transform.TransformOutInterceptor) TransformInInterceptor(org.apache.cxf.interceptor.transform.TransformInInterceptor)

Example 2 with TransformOutInterceptor

use of org.apache.cxf.interceptor.transform.TransformOutInterceptor 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 3 with TransformOutInterceptor

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

the class ProtocolVariationsTest method testInvalidWSRMOnReceive.

@Test
public void testInvalidWSRMOnReceive() throws Exception {
    init("org/apache/cxf/systest/ws/rm/rminterceptors.xml", false);
    // WS-RM 1.0 using the WS-A 1.0 namespace
    Client client = ClientProxy.getClient(greeter);
    client.getRequestContext().put(RMManager.WSRM_VERSION_PROPERTY, RM10Constants.NAMESPACE_URI);
    client.getRequestContext().put(RMManager.WSRM_WSA_VERSION_PROPERTY, Names200408.WSA_NAMESPACE_NAME);
    // remove the outgoing message's WS-RM header
    TransformOutInterceptor trans = new TransformOutInterceptor();
    Map<String, String> outElements = new HashMap<>();
    outElements.put("{" + RM10Constants.NAMESPACE_URI + "}Sequence", "");
    trans.setOutTransformElements(outElements);
    client.getOutInterceptors().add(trans);
    try {
        greeter.greetMe("one");
        fail("invalid wsrm header");
    } catch (Exception e) {
        assertTrue(e.getCause() instanceof SoapFault);
        // verify a partial error text match to exclude an unexpected exception
        // (see WSRM_REQUIRED_EXC in Messages.properties)
        final String text = "WS-ReliableMessaging is required";
        assertTrue(e.getCause().getMessage() != null && e.getCause().getMessage().indexOf(text) >= 0);
    }
}
Also used : SoapFault(org.apache.cxf.binding.soap.SoapFault) HashMap(java.util.HashMap) Client(org.apache.cxf.endpoint.Client) TransformOutInterceptor(org.apache.cxf.interceptor.transform.TransformOutInterceptor) RMException(org.apache.cxf.ws.rm.RMException) Test(org.junit.Test)

Example 4 with TransformOutInterceptor

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

the class ProtocolVariationsTest method testInvalidWSRMMustUnderstandOnReceive.

@Test
public void testInvalidWSRMMustUnderstandOnReceive() throws Exception {
    init("org/apache/cxf/systest/ws/rm/rminterceptors.xml", false);
    // WS-RM 1.0 using the WS-A 1.0 namespace
    Client client = ClientProxy.getClient(greeter);
    client.getRequestContext().put(RMManager.WSRM_VERSION_PROPERTY, RM10Constants.NAMESPACE_URI);
    client.getRequestContext().put(RMManager.WSRM_WSA_VERSION_PROPERTY, Names200408.WSA_NAMESPACE_NAME);
    // rewrite the outgoing message's WS-RM namespace to an invalid one
    TransformOutInterceptor trans = new TransformOutInterceptor();
    Map<String, String> outElements = new HashMap<>();
    outElements.put("{" + RM10Constants.NAMESPACE_URI + "}*", "{http://cxf.apache.org/invalid}*");
    trans.setOutTransformElements(outElements);
    client.getOutInterceptors().add(trans);
    try {
        greeter.greetMe("one");
        fail("invalid wsrm header");
    } catch (Exception e) {
        assertTrue(e.getCause() instanceof SoapFault);
        final String text = "WS-ReliableMessaging is required";
        assertTrue(e.getCause().getMessage(), e.getCause().getMessage() != null && e.getCause().getMessage().indexOf(text) >= 0);
    }
}
Also used : SoapFault(org.apache.cxf.binding.soap.SoapFault) HashMap(java.util.HashMap) Client(org.apache.cxf.endpoint.Client) TransformOutInterceptor(org.apache.cxf.interceptor.transform.TransformOutInterceptor) RMException(org.apache.cxf.ws.rm.RMException) Test(org.junit.Test)

Example 5 with TransformOutInterceptor

use of org.apache.cxf.interceptor.transform.TransformOutInterceptor 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)

Aggregations

TransformOutInterceptor (org.apache.cxf.interceptor.transform.TransformOutInterceptor)10 HashMap (java.util.HashMap)8 Test (org.junit.Test)7 Client (org.apache.cxf.endpoint.Client)5 TransformInInterceptor (org.apache.cxf.interceptor.transform.TransformInInterceptor)5 SoapFault (org.apache.cxf.binding.soap.SoapFault)4 RMException (org.apache.cxf.ws.rm.RMException)4 WebClient (org.apache.cxf.jaxrs.client.WebClient)2 URL (java.net.URL)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 QName (javax.xml.namespace.QName)1 Bus (org.apache.cxf.Bus)1 SpringBusFactory (org.apache.cxf.bus.spring.SpringBusFactory)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