Search in sources :

Example 96 with CachedOutputStream

use of org.apache.cxf.io.CachedOutputStream in project tesb-rt-se by Talend.

the class MessageToEventMapperTest method getTestMessage.

private Message getTestMessage() throws IOException, EndpointException {
    Message message = new MessageImpl();
    ExchangeImpl exchange = new ExchangeImpl();
    ServiceInfo serviceInfo = new ServiceInfo();
    InterfaceInfo interfaceInfo = new InterfaceInfo(serviceInfo, new QName("interfaceNs", "interfaceName"));
    serviceInfo.setInterface(interfaceInfo);
    SoapBindingInfo bInfo = new SoapBindingInfo(serviceInfo, WSDLConstants.NS_SOAP12);
    bInfo.setTransportURI(TransportType);
    OperationInfo opInfo = new OperationInfo();
    opInfo.setName(new QName("namespace", "opName"));
    BindingOperationInfo bindingOpInfo = new BindingOperationInfo(bInfo, opInfo);
    exchange.put(BindingOperationInfo.class, bindingOpInfo);
    SoapBinding binding = new SoapBinding(bInfo);
    exchange.put(Binding.class, binding);
    String ns = "ns";
    EndpointInfo ei = new EndpointInfo(serviceInfo, ns);
    ei.setAddress(Address);
    Service service = new ServiceImpl();
    Bus bus = BusFactory.getThreadDefaultBus();
    Endpoint endpoint = new EndpointImpl(bus, service, ei);
    exchange.put(Endpoint.class, endpoint);
    message.setExchange(exchange);
    FlowIdHelper.setFlowId(message, FlowID);
    Principal principal = new X500Principal(PrincipalString);
    SecurityContext sc = new DefaultSecurityContext(principal, new Subject());
    message.put(SecurityContext.class, sc);
    CachedOutputStream cos = new CachedOutputStream();
    InputStream is = new ByteArrayInputStream(TESTCONTENT.getBytes("UTF-8"));
    IOUtils.copy(is, cos);
    message.setContent(CachedOutputStream.class, cos);
    CustomInfo customInfo = CustomInfo.getOrCreateCustomInfo(message);
    customInfo.put("key1", "value1");
    return message;
}
Also used : BindingOperationInfo(org.apache.cxf.service.model.BindingOperationInfo) OperationInfo(org.apache.cxf.service.model.OperationInfo) DefaultSecurityContext(org.apache.cxf.interceptor.security.DefaultSecurityContext) BindingOperationInfo(org.apache.cxf.service.model.BindingOperationInfo) Message(org.apache.cxf.message.Message) CachedOutputStream(org.apache.cxf.io.CachedOutputStream) ServiceInfo(org.apache.cxf.service.model.ServiceInfo) EndpointInfo(org.apache.cxf.service.model.EndpointInfo) Endpoint(org.apache.cxf.endpoint.Endpoint) CustomInfo(org.talend.esb.sam.agent.message.CustomInfo) Bus(org.apache.cxf.Bus) QName(javax.xml.namespace.QName) ServiceImpl(org.apache.cxf.service.ServiceImpl) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) EndpointImpl(org.apache.cxf.endpoint.EndpointImpl) Service(org.apache.cxf.service.Service) SoapBinding(org.apache.cxf.binding.soap.SoapBinding) Subject(javax.security.auth.Subject) ByteArrayInputStream(java.io.ByteArrayInputStream) SoapBindingInfo(org.apache.cxf.binding.soap.model.SoapBindingInfo) DefaultSecurityContext(org.apache.cxf.interceptor.security.DefaultSecurityContext) SecurityContext(org.apache.cxf.security.SecurityContext) X500Principal(javax.security.auth.x500.X500Principal) InterfaceInfo(org.apache.cxf.service.model.InterfaceInfo) MessageImpl(org.apache.cxf.message.MessageImpl) ExchangeImpl(org.apache.cxf.message.ExchangeImpl) X500Principal(javax.security.auth.x500.X500Principal) Principal(java.security.Principal)

Example 97 with CachedOutputStream

use of org.apache.cxf.io.CachedOutputStream in project tesb-rt-se by Talend.

the class CompressionInInterceptor method decompressMessage.

public void decompressMessage(Message message) throws Fault {
    if (isGET(message)) {
        return;
    }
    final CachedOutputStream cache = new CachedOutputStream();
    final CachedOutputStream decompressedSoapMessage = new CachedOutputStream();
    try {
        LOG.fine("Uncompressing response");
        // Original stream with compressed body
        InputStream is = message.getContent(InputStream.class);
        if (is == null) {
            return;
        }
        // Loading content of original InputStream to cache
        IOUtils.copyAndCloseInput(is, cache);
        // Loading SOAP body content to separate stream
        CachedOutputStream soapBodyContent = new CachedOutputStream();
        Scanner scanner = new Scanner(cache.getInputStream());
        MatchResult bodyPosition = null;
        try {
            bodyPosition = CompressionHelper.loadSoapBodyContent(soapBodyContent, scanner, CompressionConstants.COMPRESSED_SOAP_BODY_PATTERN);
        } catch (XMLStreamException e) {
            throw new Fault("Can not read compressed SOAP Body", LOG, e, e.getMessage());
        }
        if (bodyPosition == null) {
            // compressed SOAP body content is not found
            // skipping decompression
            message.setContent(InputStream.class, cache.getInputStream());
        } else {
            // compressed SOAP body content is found
            // apply Base64 decoding for encoded soap body content
            final byte[] base64DecodedSoapBody = (new Base64()).decode(soapBodyContent.getBytes());
            // uncompress soap body
            GZIPInputStream decompressedBody = new GZIPInputStream(new ByteArrayInputStream(base64DecodedSoapBody));
            // replace original soap body by compressed one
            CompressionHelper.replaceBodyInSOAP(cache.getBytes(), bodyPosition, decompressedBody, decompressedSoapMessage, null, null, true);
            message.setContent(InputStream.class, decompressedSoapMessage.getInputStream());
        }
        if (message.getInterceptorChain() != null) {
            message.getInterceptorChain().add(new AbstractPhaseInterceptor<Message>(Phase.POST_INVOKE) {

                @Override
                public void handleMessage(Message message) throws Fault {
                    closeCacheStreams(cache, decompressedSoapMessage);
                }
            });
        }
    } catch (Exception ex) {
        closeCacheStreams(cache, decompressedSoapMessage);
        throw new Fault("SOAP Body decompression failed", LOG, ex);
    }
}
Also used : Scanner(java.util.Scanner) Base64(org.apache.commons.codec.binary.Base64) Message(org.apache.cxf.message.Message) GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Fault(org.apache.cxf.interceptor.Fault) MatchResult(java.util.regex.MatchResult) IOException(java.io.IOException) XMLStreamException(javax.xml.stream.XMLStreamException) CachedOutputStream(org.apache.cxf.io.CachedOutputStream) GZIPInputStream(java.util.zip.GZIPInputStream) XMLStreamException(javax.xml.stream.XMLStreamException) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 98 with CachedOutputStream

use of org.apache.cxf.io.CachedOutputStream in project tesb-rt-se by Talend.

the class CompressionHelperTest method testSoapBodyIsNotFound.

@Test
public void testSoapBodyIsNotFound() throws Exception {
    // Loading SOAP body content to separate stream
    CachedOutputStream soapBodyContent = new CachedOutputStream();
    CachedOutputStream cache = new CachedOutputStream();
    cache.write("1".getBytes());
    Scanner scanner = new Scanner(cache.getInputStream());
    try {
        CompressionHelper.loadSoapBodyContent(soapBodyContent, scanner, "(\\d)(a)*");
    } catch (XMLStreamException e) {
        // TODO Auto-generated catch block
        fail("XMLStreamException is not expected");
    } catch (RuntimeException ex) {
        return;
    } finally {
        cache.close();
    }
    fail("No exception is not expected");
}
Also used : Scanner(java.util.Scanner) XMLStreamException(javax.xml.stream.XMLStreamException) CachedOutputStream(org.apache.cxf.io.CachedOutputStream) Test(org.junit.Test)

Example 99 with CachedOutputStream

use of org.apache.cxf.io.CachedOutputStream in project tesb-rt-se by Talend.

the class CompressionHelperTest method testNullScaner.

@Test
public void testNullScaner() throws Exception {
    // Loading SOAP body content to separate stream
    CachedOutputStream soapBodyContent = new CachedOutputStream();
    CachedOutputStream cache = new CachedOutputStream();
    cache.write("1".getBytes());
    try {
        CompressionHelper.loadSoapBodyContent(soapBodyContent, null, "(\\d)(a)*");
    } catch (XMLStreamException e) {
        // TODO Auto-generated catch block
        fail("XMLStreamException is not expected");
    } catch (RuntimeException ex) {
        return;
    } finally {
        cache.close();
    }
    fail("No exception is not expected");
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) CachedOutputStream(org.apache.cxf.io.CachedOutputStream) Test(org.junit.Test)

Example 100 with CachedOutputStream

use of org.apache.cxf.io.CachedOutputStream in project tesb-rt-se by Talend.

the class CompressionOutInterceptorTest method handleRuntimeException2.

@Test
public void handleRuntimeException2() throws Exception {
    Map<String, Object> map = new HashMap<String, Object>();
    map.put(Message.HTTP_REQUEST_METHOD, "GET");
    Message message = CompressionCommonTest.getMessageStub(null, map);
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    CompressionCachedOutputStreamCallback callback = new CompressionCachedOutputStreamCallback(os, 1024, message);
    CachedOutputStream wrapper = new CachedOutputStream();
    try {
        callback.onClose(wrapper);
    } catch (RuntimeException ex) {
        return;
    }
    fail("No exception is not expected");
}
Also used : CompressionCachedOutputStreamCallback(org.talend.esb.policy.compression.impl.CompressionOutInterceptor.CompressionCachedOutputStreamCallback) Message(org.apache.cxf.message.Message) HashMap(java.util.HashMap) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CachedOutputStream(org.apache.cxf.io.CachedOutputStream) Test(org.junit.Test)

Aggregations

CachedOutputStream (org.apache.cxf.io.CachedOutputStream)105 InputStream (java.io.InputStream)38 IOException (java.io.IOException)35 Test (org.junit.Test)24 Message (org.apache.cxf.message.Message)22 OutputStream (java.io.OutputStream)18 Fault (org.apache.cxf.interceptor.Fault)18 MessageImpl (org.apache.cxf.message.MessageImpl)11 ByteArrayInputStream (java.io.ByteArrayInputStream)10 XMLStreamException (javax.xml.stream.XMLStreamException)10 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)10 PrintWriter (java.io.PrintWriter)8 XMLStreamReader (javax.xml.stream.XMLStreamReader)6 StreamSource (javax.xml.transform.stream.StreamSource)6 Endpoint (org.apache.cxf.endpoint.Endpoint)6 ExchangeImpl (org.apache.cxf.message.ExchangeImpl)6 RMMessage (org.apache.cxf.ws.rm.persistence.RMMessage)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 Source (javax.xml.transform.Source)5 ArrayList (java.util.ArrayList)4