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);
}
}
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());
}
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);
}
}
}
}
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);
}
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);
}
Aggregations