Search in sources :

Example 6 with CachedOutputStream

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

the class MessageToEventMapper method getPayload.

/**
 * Gets the message payload.
 *
 * @param message the message
 * @return the payload
 */
protected String getPayload(Message message) {
    try {
        String encoding = (String) message.get(Message.ENCODING);
        if (encoding == null) {
            encoding = "UTF-8";
        }
        CachedOutputStream cos = message.getContent(CachedOutputStream.class);
        if (cos == null) {
            LOG.warning("Could not find CachedOutputStream in message." + " Continuing without message content");
            return "";
        }
        return new String(cos.getBytes(), encoding);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : IOException(java.io.IOException) CachedOutputStream(org.apache.cxf.io.CachedOutputStream)

Example 7 with CachedOutputStream

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

the class MessageToEventMapperTest method testMapEventRest.

@Test
public void testMapEventRest() throws IOException, EndpointException {
    QName portType = new QName("PORT_TYPE");
    EndpointInfo info = EasyMock.createMock(EndpointInfo.class);
    EasyMock.expect(info.getName()).andReturn(portType).anyTimes();
    EasyMock.expect(info.getAddress()).andReturn(null).anyTimes();
    EasyMock.replay(info);
    Endpoint endpoint = EasyMock.createMock(Endpoint.class);
    EasyMock.expect(endpoint.getEndpointInfo()).andReturn(info).anyTimes();
    Map<String, String> samProperties = new HashMap<String, String>();
    EasyMock.expect(endpoint.get(EventFeature.SAM_PROPERTIES)).andReturn(samProperties).anyTimes();
    EasyMock.replay(endpoint);
    Message outMessage = EasyMock.createMock(Message.class);
    EasyMock.expect(outMessage.containsKey(Message.HTTP_REQUEST_METHOD)).andReturn(true).anyTimes();
    EasyMock.expect(outMessage.get(Message.HTTP_REQUEST_METHOD)).andReturn("POST").anyTimes();
    EasyMock.expect(outMessage.containsKey(Message.REQUEST_URI)).andReturn(true).anyTimes();
    EasyMock.expect(outMessage.get(Message.REQUEST_URI)).andReturn("REQUEST_URI").anyTimes();
    EasyMock.expect(outMessage.containsKey(Message.BASE_PATH)).andReturn(true).anyTimes();
    EasyMock.expect(outMessage.get(Message.BASE_PATH)).andReturn("REQUEST_URI").anyTimes();
    EasyMock.replay(outMessage);
    Exchange e = EasyMock.createMock(Exchange.class);
    EasyMock.expect(e.getOutMessage()).andReturn(outMessage).anyTimes();
    EasyMock.expect(e.getOutFaultMessage()).andReturn(null).anyTimes();
    EasyMock.expect(e.getInFaultMessage()).andReturn(null).anyTimes();
    EasyMock.expect(e.getBinding()).andReturn(null).anyTimes();
    EasyMock.expect(e.getEndpoint()).andReturn(endpoint).anyTimes();
    EasyMock.expect(e.get("org.apache.cxf.resource.operation.name")).andReturn("operationName").anyTimes();
    EasyMock.replay(e);
    AuthorizationPolicy authPolicy = EasyMock.createMock(AuthorizationPolicy.class);
    EasyMock.expect(authPolicy.getUserName()).andReturn("USERNAME").anyTimes();
    EasyMock.replay(authPolicy);
    CachedOutputStream cos = new CachedOutputStream();
    cos.write(1);
    cos.write(2);
    cos.write(3);
    Message message = EasyMock.createNiceMock(Message.class);
    EasyMock.expect(message.entrySet()).andReturn(null).anyTimes();
    EasyMock.expect(message.get(Message.REQUESTOR_ROLE)).andReturn(true).anyTimes();
    EasyMock.expect(message.getExchange()).andReturn(e).anyTimes();
    EasyMock.expect(message.get(Message.ENCODING)).andReturn("UTF-8").anyTimes();
    EasyMock.expect(message.getContent(CachedOutputStream.class)).andReturn(cos).anyTimes();
    EasyMock.expect(message.get("FlowId")).andReturn(FlowID).anyTimes();
    EasyMock.expect(message.get(CorrelationIdHelper.CORRELATION_ID_KEY)).andReturn("CORRELATION_ID_KEY").anyTimes();
    EasyMock.expect(message.get(Message.ACCEPT_CONTENT_TYPE)).andReturn("XML").anyTimes();
    EasyMock.expect(message.get(Message.CONTENT_TYPE)).andReturn("XML").anyTimes();
    EasyMock.expect(message.get(Message.RESPONSE_CODE)).andReturn(0).anyTimes();
    EasyMock.expect(message.get(SecurityContext.class)).andReturn(null).anyTimes();
    EasyMock.expect(message.get(AuthorizationPolicy.class)).andReturn(authPolicy).anyTimes();
    EasyMock.expect(message.get(CustomInfo.class)).andReturn(EasyMock.createMock(CustomInfo.class)).anyTimes();
    EasyMock.replay(message);
    MessageToEventMapper mapper = new MessageToEventMapper();
    mapper.setMaxContentLength(2);
    Event event = mapper.mapToEvent(message);
    Assert.assertEquals(EventTypeEnum.RESP_IN, event.getEventType());
    Assert.assertEquals("PORT_TYPE", event.getMessageInfo().getPortType());
    Assert.assertEquals("POST[/]", event.getMessageInfo().getOperationName());
    Assert.assertEquals("http://cxf.apache.org/transports/http", event.getMessageInfo().getTransportType());
    Assert.assertEquals(FlowID, event.getMessageInfo().getFlowId());
    Assert.assertNull(event.getMessageInfo().getMessageId());
}
Also used : Message(org.apache.cxf.message.Message) HashMap(java.util.HashMap) QName(javax.xml.namespace.QName) MessageToEventMapper(org.talend.esb.sam.agent.eventproducer.MessageToEventMapper) CachedOutputStream(org.apache.cxf.io.CachedOutputStream) Exchange(org.apache.cxf.message.Exchange) EndpointInfo(org.apache.cxf.service.model.EndpointInfo) AuthorizationPolicy(org.apache.cxf.configuration.security.AuthorizationPolicy) Endpoint(org.apache.cxf.endpoint.Endpoint) DefaultSecurityContext(org.apache.cxf.interceptor.security.DefaultSecurityContext) SecurityContext(org.apache.cxf.security.SecurityContext) CustomInfo(org.talend.esb.sam.agent.message.CustomInfo) Event(org.talend.esb.sam.common.event.Event) Test(org.junit.Test)

Example 8 with CachedOutputStream

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

the class WireTapIn method handleMessage.

/* (non-Javadoc)
     * @see org.apache.cxf.interceptor.Interceptor#handleMessage(org.apache.cxf.message.Message)
     */
@Override
public void handleMessage(final Message message) throws Fault {
    final InputStream is = message.getContent(InputStream.class);
    if (logMessageContent) {
        if (null == is) {
            Reader reader = message.getContent(Reader.class);
            if (null != reader) {
                String encoding = (String) message.get(Message.ENCODING);
                if (encoding == null) {
                    encoding = "UTF-8";
                }
                try {
                    final CachedOutputStream cos = new CachedOutputStream();
                    final Writer writer = new OutputStreamWriter(cos, encoding);
                    IOUtils.copy(reader, writer, 1024);
                    reader.reset();
                    writer.flush();
                    message.setContent(InputStream.class, cos.getInputStream());
                    message.setContent(Reader.class, null);
                    message.setContent(CachedOutputStream.class, cos);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                } finally {
                    try {
                        reader.reset();
                    } catch (IOException e) {
                    }
                }
            }
        } else {
            try {
                final CachedOutputStream cos = new CachedOutputStream();
                // TODO: We should try to make this streaming
                // WireTapInputStream wtis = new WireTapInputStream(is, cos);
                // message.setContent(InputStream.class, wtis);
                IOUtils.copyAndCloseInput(is, cos);
                message.setContent(InputStream.class, cos.getInputStream());
                message.setContent(CachedOutputStream.class, cos);
                message.getInterceptorChain().add(new AbstractPhaseInterceptor<Message>(Phase.POST_INVOKE) {

                    @Override
                    public void handleMessage(Message message) throws Fault {
                        if (cos != null) {
                            try {
                                cos.close();
                            } catch (IOException e) {
                            }
                        }
                    }
                });
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
Also used : Message(org.apache.cxf.message.Message) InputStream(java.io.InputStream) Reader(java.io.Reader) OutputStreamWriter(java.io.OutputStreamWriter) Fault(org.apache.cxf.interceptor.Fault) IOException(java.io.IOException) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) CachedOutputStream(org.apache.cxf.io.CachedOutputStream)

Example 9 with CachedOutputStream

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

the class ThirdPartyRegistrationService method register.

@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Path("/")
public ConsumerRegistration register(MultipartBody body) {
    String appName = body.getAttachmentObject("appName", String.class);
    String appURI = body.getAttachmentObject("appURI", String.class);
    String appRedirectURI = body.getAttachmentObject("appRedirectURI", String.class);
    String appDesc = body.getAttachmentObject("appDescription", String.class);
    URI logoURI = null;
    Attachment att = body.getAttachment("appLogo");
    if (att != null) {
        InputStream logoStream = att.getObject(InputStream.class);
        CachedOutputStream cos = new CachedOutputStream();
        try {
            IOUtils.copy(logoStream, cos);
            appLogos.put(appName.toLowerCase(), cos);
            UriBuilder ub = uriInfo.getAbsolutePathBuilder();
            ub.path("logo").path(appName.toLowerCase());
            ContentDisposition cd = att.getContentDisposition();
            if (cd != null && cd.getParameter("filename") != null) {
                ub.path(cd.getParameter("filename"));
            }
            logoURI = ub.build();
        } catch (IOException ex) {
        // ignore
        }
    }
    String clientId = generateClientId(appName, appURI);
    String clientSecret = generateClientSecret();
    Client newClient = new Client(clientId, clientSecret, true, appName, appURI);
    newClient.setApplicationDescription(appDesc);
    newClient.setApplicationLogoUri(logoURI.toString());
    newClient.setRedirectUris(Collections.singletonList(appRedirectURI));
    manager.registerClient(newClient);
    return new ConsumerRegistration(clientId, clientSecret);
}
Also used : ContentDisposition(org.apache.cxf.jaxrs.ext.multipart.ContentDisposition) InputStream(java.io.InputStream) ConsumerRegistration(oauth2.common.ConsumerRegistration) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) IOException(java.io.IOException) UriBuilder(javax.ws.rs.core.UriBuilder) Client(org.apache.cxf.rs.security.oauth2.common.Client) URI(java.net.URI) CachedOutputStream(org.apache.cxf.io.CachedOutputStream) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes)

Example 10 with CachedOutputStream

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

the class ThirdPartyRegistrationService method register.

@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Path("/")
public ConsumerRegistration register(MultipartBody body) {
    String appName = body.getAttachmentObject("appName", String.class);
    String appURI = body.getAttachmentObject("appURI", String.class);
    String appDesc = body.getAttachmentObject("appDescription", String.class);
    URI logoURI = null;
    Attachment att = body.getAttachment("appLogo");
    if (att != null) {
        InputStream logoStream = att.getObject(InputStream.class);
        CachedOutputStream cos = new CachedOutputStream();
        try {
            IOUtils.copy(logoStream, cos);
            appLogos.put(appName.toLowerCase(), cos);
            UriBuilder ub = uriInfo.getAbsolutePathBuilder();
            ub.path("logo").path(appName.toLowerCase());
            ContentDisposition cd = att.getContentDisposition();
            if (cd != null && cd.getParameter("filename") != null) {
                ub.path(cd.getParameter("filename"));
            }
            logoURI = ub.build();
        } catch (IOException ex) {
        // ignore
        }
    }
    String clientId = generateClientId(appName, appURI);
    String clientSecret = generateClientSecret();
    Client newClient = new Client(clientId, clientSecret, appName, appURI);
    newClient.setApplicationDescription(appDesc);
    newClient.setLogoUri(logoURI.toString());
    manager.registerClient(newClient);
    return new ConsumerRegistration(clientId, clientSecret);
}
Also used : ContentDisposition(org.apache.cxf.jaxrs.ext.multipart.ContentDisposition) InputStream(java.io.InputStream) ConsumerRegistration(oauth.common.ConsumerRegistration) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) IOException(java.io.IOException) UriBuilder(javax.ws.rs.core.UriBuilder) Client(org.apache.cxf.rs.security.oauth.data.Client) URI(java.net.URI) CachedOutputStream(org.apache.cxf.io.CachedOutputStream) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes)

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