use of org.apache.cxf.message.Attachment in project cxf by apache.
the class AbstractSTSClient method issue.
/**
* Make an "Issue" invocation and return the response as a STSResponse Object
*/
protected STSResponse issue(String appliesTo, String action, String requestType, String binaryExchange) throws Exception {
createClient();
BindingOperationInfo boi = findOperation("/RST/Issue");
client.getRequestContext().putAll(ctx);
if (action != null) {
client.getRequestContext().put(SoapBindingConstants.SOAP_ACTION, action);
} else if (isSecureConv) {
client.getRequestContext().put(SoapBindingConstants.SOAP_ACTION, namespace + "/RST/SCT");
} else {
client.getRequestContext().put(SoapBindingConstants.SOAP_ACTION, namespace + "/RST/Issue");
}
W3CDOMStreamWriter writer = new W3CDOMStreamWriter();
writer.writeStartElement("wst", "RequestSecurityToken", namespace);
writer.writeNamespace("wst", namespace);
if (context != null) {
writer.writeAttribute(null, "Context", context);
}
boolean wroteKeySize = false;
String keyTypeTemplate = null;
String sptt = null;
if (template != null && DOMUtils.getFirstElement(template) != null) {
if (this.useSecondaryParameters()) {
writer.writeStartElement("wst", "SecondaryParameters", namespace);
}
Element tl = DOMUtils.getFirstElement(template);
while (tl != null) {
StaxUtils.copy(tl, writer);
if ("KeyType".equals(tl.getLocalName())) {
keyTypeTemplate = DOMUtils.getContent(tl);
} else if ("KeySize".equals(tl.getLocalName())) {
wroteKeySize = true;
keySize = Integer.parseInt(DOMUtils.getContent(tl));
} else if ("TokenType".equals(tl.getLocalName())) {
sptt = DOMUtils.getContent(tl);
}
tl = DOMUtils.getNextElement(tl);
}
if (this.useSecondaryParameters()) {
writer.writeEndElement();
}
}
if (isSpnego) {
tokenType = STSUtils.getTokenTypeSCT(namespace);
sendKeyType = false;
}
if (sptt == null) {
addTokenType(writer);
}
addRequestType(requestType, writer);
if (enableAppliesTo) {
addAppliesTo(writer, appliesTo);
}
addClaims(writer);
if (isSecureConv || enableLifetime) {
addLifetime(writer);
}
// Write out renewal semantics
writeRenewalSemantics(writer);
Element onBehalfOfToken = getOnBehalfOfToken();
if (onBehalfOfToken != null) {
writer.writeStartElement("wst", "OnBehalfOf", namespace);
StaxUtils.copy(onBehalfOfToken, writer);
writer.writeEndElement();
}
if (keyTypeTemplate == null) {
keyTypeTemplate = writeKeyType(writer, keyType);
}
byte[] requestorEntropy = null;
X509Certificate cert = null;
Crypto crypto = null;
if (keySize <= 0) {
keySize = 256;
}
if (keyTypeTemplate != null && keyTypeTemplate.endsWith("SymmetricKey")) {
requestorEntropy = writeElementsForRSTSymmetricKey(writer, wroteKeySize);
} else if (keyTypeTemplate != null && keyTypeTemplate.endsWith("PublicKey")) {
// Use the given cert, or else get it from a Crypto instance
if (useKeyCertificate != null) {
cert = useKeyCertificate;
} else {
crypto = createCrypto(false);
cert = getCert(crypto);
}
writeElementsForRSTPublicKey(writer, cert);
} else if (isSpnego || isSecureConv) {
addKeySize(keySize, writer);
}
if (binaryExchange != null) {
addBinaryExchange(binaryExchange, writer);
}
Element actAsSecurityToken = getActAsToken();
if (actAsSecurityToken != null) {
writer.writeStartElement(STSUtils.WST_NS_08_02, "ActAs");
StaxUtils.copy(actAsSecurityToken, writer);
writer.writeEndElement();
}
Element customElement = getCustomContent();
if (customElement != null) {
StaxUtils.copy(customElement, writer);
}
writer.writeEndElement();
Object[] obj = client.invoke(boi, new DOMSource(writer.getDocument().getDocumentElement()));
@SuppressWarnings("unchecked") Collection<Attachment> attachments = (Collection<Attachment>) client.getResponseContext().get(Message.ATTACHMENTS);
return new STSResponse((DOMSource) obj[0], requestorEntropy, cert, crypto, attachments);
}
use of org.apache.cxf.message.Attachment in project cxf by apache.
the class MtomServerTest method testURLBasedAttachment.
@Test
public void testURLBasedAttachment() throws Exception {
JaxWsServerFactoryBean sf = new JaxWsServerFactoryBean();
sf.setServiceBean(new EchoService());
sf.setBus(getStaticBus());
String address = "http://localhost:" + PORT2 + "/EchoService";
sf.setAddress(address);
Map<String, Object> props = new HashMap<>();
props.put(Message.MTOM_ENABLED, "true");
sf.setProperties(props);
Server server = sf.create();
server.getEndpoint().getService().getDataBinding().setMtomThreshold(0);
servStatic(getClass().getResource("mtom-policy.xml"), "http://localhost:" + PORT2 + "/policy.xsd");
EndpointInfo ei = new EndpointInfo(null, HTTP_ID);
ei.setAddress(address);
ConduitInitiatorManager conduitMgr = getStaticBus().getExtension(ConduitInitiatorManager.class);
ConduitInitiator conduitInit = conduitMgr.getConduitInitiator("http://schemas.xmlsoap.org/soap/http");
Conduit conduit = conduitInit.getConduit(ei, getStaticBus());
TestUtilities.TestMessageObserver obs = new TestUtilities.TestMessageObserver();
conduit.setMessageObserver(obs);
Message m = new MessageImpl();
String ct = "multipart/related; type=\"application/xop+xml\"; " + "start=\"<soap.xml@xfire.codehaus.org>\"; " + "start-info=\"text/xml; charset=utf-8\"; " + "boundary=\"----=_Part_4_701508.1145579811786\"";
m.put(Message.CONTENT_TYPE, ct);
conduit.prepare(m);
OutputStream os = m.getContent(OutputStream.class);
InputStream is = testUtilities.getResourceAsStream("request-url-attachment");
if (is == null) {
throw new RuntimeException("Could not find resource " + "request");
}
try (ByteArrayOutputStream bout = new ByteArrayOutputStream()) {
IOUtils.copy(is, bout);
String s = bout.toString(StandardCharsets.UTF_8.name());
s = s.replaceAll(":9036/", ":" + PORT2 + "/");
os.write(s.getBytes(StandardCharsets.UTF_8));
}
os.flush();
is.close();
os.close();
byte[] res = obs.getResponseStream().toByteArray();
MessageImpl resMsg = new MessageImpl();
resMsg.setContent(InputStream.class, new ByteArrayInputStream(res));
resMsg.put(Message.CONTENT_TYPE, obs.getResponseContentType());
resMsg.setExchange(new ExchangeImpl());
AttachmentDeserializer deserializer = new AttachmentDeserializer(resMsg);
deserializer.initializeAttachments();
Collection<Attachment> attachments = resMsg.getAttachments();
assertNotNull(attachments);
assertEquals(1, attachments.size());
Attachment inAtt = attachments.iterator().next();
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
IOUtils.copy(inAtt.getDataHandler().getInputStream(), out);
assertTrue("Wrong size: " + out.size() + "\n" + out.toString(), out.size() > 970 && out.size() < 1020);
}
unregisterServStatic("http://localhost:" + PORT2 + "/policy.xsd");
}
use of org.apache.cxf.message.Attachment in project cxf by apache.
the class PersistenceUtilsTest method addAttachment.
private static void addAttachment(Message msg) throws IOException {
Collection<Attachment> attachments = new ArrayList<>();
DataHandler dh = new DataHandler(new ByteArrayDataSource("hello world!", "text/plain"));
Attachment a = new AttachmentImpl("test.xml", dh);
attachments.add(a);
msg.setAttachments(attachments);
}
use of org.apache.cxf.message.Attachment in project cxf by apache.
the class AttachmentCallbackHandlerTest method parseAttachment.
private void parseAttachment(String attachmentId) throws Exception {
Attachment attachment = new AttachmentImpl(attachmentId);
// Mock up a DataHandler for the Attachment
DataHandler dataHandler = EasyMock.mock(DataHandler.class);
dataHandler.setCommandMap(anyObject(CommandMap.class));
EasyMock.expectLastCall();
EasyMock.expect(dataHandler.getInputStream()).andReturn(null);
EasyMock.expect(dataHandler.getContentType()).andReturn(null);
EasyMock.replay(dataHandler);
((AttachmentImpl) attachment).setDataHandler(dataHandler);
AttachmentCallbackHandler callbackHandler = new AttachmentCallbackHandler(Collections.singletonList(attachment));
AttachmentRequestCallback attachmentRequestCallback = new AttachmentRequestCallback();
attachmentRequestCallback.setAttachmentId(AttachmentUtils.getAttachmentId("cid:" + attachmentId));
attachmentRequestCallback.setRemoveAttachments(false);
callbackHandler.handle(new Callback[] { attachmentRequestCallback });
List<org.apache.wss4j.common.ext.Attachment> attachments = attachmentRequestCallback.getAttachments();
assertNotNull(attachments);
assertEquals(1, attachments.size());
EasyMock.verify(dataHandler);
}
use of org.apache.cxf.message.Attachment in project cxf by apache.
the class MtomPolicyTest method sendMtomMessage.
private void sendMtomMessage(String a) throws Exception {
EndpointInfo ei = new EndpointInfo(null, "http://schemas.xmlsoap.org/wsdl/http");
ei.setAddress(a);
ConduitInitiatorManager conduitMgr = getStaticBus().getExtension(ConduitInitiatorManager.class);
ConduitInitiator conduitInit = conduitMgr.getConduitInitiator("http://schemas.xmlsoap.org/soap/http");
Conduit conduit = conduitInit.getConduit(ei, getStaticBus());
TestUtilities.TestMessageObserver obs = new TestUtilities.TestMessageObserver();
conduit.setMessageObserver(obs);
Message m = new MessageImpl();
String ct = "multipart/related; type=\"application/xop+xml\"; " + "start=\"<soap.xml@xfire.codehaus.org>\"; " + "start-info=\"text/xml; charset=utf-8\"; " + "boundary=\"----=_Part_4_701508.1145579811786\"";
m.put(Message.CONTENT_TYPE, ct);
conduit.prepare(m);
OutputStream os = m.getContent(OutputStream.class);
InputStream is = testUtilities.getResourceAsStream("request");
if (is == null) {
throw new RuntimeException("Could not find resource " + "request");
}
IOUtils.copy(is, os);
os.flush();
is.close();
os.close();
byte[] res = obs.getResponseStream().toByteArray();
MessageImpl resMsg = new MessageImpl();
resMsg.setContent(InputStream.class, new ByteArrayInputStream(res));
resMsg.put(Message.CONTENT_TYPE, obs.getResponseContentType());
resMsg.setExchange(new ExchangeImpl());
AttachmentDeserializer deserializer = new AttachmentDeserializer(resMsg);
deserializer.initializeAttachments();
Collection<Attachment> attachments = resMsg.getAttachments();
assertNotNull(attachments);
assertEquals(1, attachments.size());
Attachment inAtt = attachments.iterator().next();
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
IOUtils.copy(inAtt.getDataHandler().getInputStream(), out);
assertEquals(27364, out.size());
}
}
Aggregations