use of org.apache.cxf.message.Attachment in project cxf by apache.
the class ContextPropertiesMappingTest method testCreateWebServiceContextWithInAttachments.
@Test
public void testCreateWebServiceContextWithInAttachments() {
Exchange exchange = new ExchangeImpl();
Message inMessage = new MessageImpl();
Collection<Attachment> attachments = new LinkedList<>();
DataSource source = new ByteDataSource(new byte[0], "text/xml");
DataHandler handler1 = new DataHandler(source);
attachments.add(new AttachmentImpl("part1", handler1));
DataHandler handler2 = new DataHandler(source);
attachments.add(new AttachmentImpl("part2", handler2));
inMessage.setAttachments(attachments);
inMessage.putAll(message);
exchange.setInMessage(inMessage);
exchange.setOutMessage(new MessageImpl());
MessageContext ctx = new WrappedMessageContext(exchange.getInMessage(), Scope.APPLICATION);
Object inAttachments = ctx.get(MessageContext.INBOUND_MESSAGE_ATTACHMENTS);
assertNotNull("inbound attachments object must be initialized", inAttachments);
assertTrue("inbound attachments must be in a Map", inAttachments instanceof Map);
Map<String, DataHandler> dataHandlers = CastUtils.cast((Map<?, ?>) inAttachments);
assertEquals("two inbound attachments expected", 2, dataHandlers.size());
assertTrue("part1 attachment is missing", dataHandlers.containsKey("part1"));
// should do as it's the same instance
assertTrue("part1 handler is missing", dataHandlers.get("part1") == handler1);
assertTrue("part2 attachment is missing", dataHandlers.containsKey("part2"));
assertTrue("part2 handler is missing", dataHandlers.get("part2") == handler2);
}
use of org.apache.cxf.message.Attachment in project cxf by apache.
the class AbstractXOPType method readInclude.
private Object readInclude(String type, MessageReader reader, Context context) throws DatabindingException {
String href = reader.getAttributeReader(XOP_HREF).getValue().trim();
Attachment att = AttachmentUtil.getAttachment(href, context.getAttachments());
if (att == null) {
throw new DatabindingException("Could not find the attachment " + href);
}
try {
return readAttachment(att, context);
} catch (IOException e) {
throw new DatabindingException("Could not read attachment", e);
}
}
use of org.apache.cxf.message.Attachment in project cxf by apache.
the class WrappedAttachmentsTest method testCreateAndModify.
@Test
public void testCreateAndModify() {
Map<String, DataHandler> content = new HashMap<>();
content.put("att-1", new DataHandler(new ByteArrayDataSource("Hello world!".getBytes(), "text/plain")));
content.put("att-2", new DataHandler(new ByteArrayDataSource("Hola mundo!".getBytes(), "text/plain")));
WrappedAttachments attachments = new WrappedAttachments(content);
Attachment att3 = new AttachmentImpl("att-3", new DataHandler(new ByteArrayDataSource("Bonjour tout le monde!".getBytes(), "text/plain")));
assertEquals(2, attachments.size());
assertFalse(attachments.isEmpty());
assertTrue(attachments.containsAll(attachments));
List<String> testCollection = new ArrayList<>();
testCollection.add("Some value");
assertFalse(attachments.containsAll(testCollection));
attachments.add(att3);
assertEquals(3, attachments.size());
attachments.add(att3);
assertEquals(3, attachments.size());
attachments.remove(att3);
assertEquals(2, attachments.size());
Attachment attx = attachments.iterator().next();
attachments.remove(attx);
assertEquals(1, attachments.size());
// NOPMD - explicitly test this
Attachment[] atts = attachments.toArray(new Attachment[0]);
assertEquals(1, atts.length);
assertEquals("att-1".equals(attx.getId()) ? "att-2" : "att-1", atts[0].getId());
// NOPMD - explicitly test this
atts = attachments.toArray(new Attachment[attachments.size()]);
assertEquals(1, atts.length);
assertEquals("att-1".equals(attx.getId()) ? "att-2" : "att-1", atts[0].getId());
// NOPMD - explicitly test this
Object[] o = attachments.toArray();
assertEquals(1, o.length);
Attachment a = (Attachment) o[0];
assertEquals("att-1".equals(attx.getId()) ? "att-2" : "att-1", a.getId());
attachments.clear();
assertTrue(attachments.isEmpty());
assertTrue(content.isEmpty());
}
use of org.apache.cxf.message.Attachment in project cxf by apache.
the class AbstractSTSClient method renew.
/**
* Make an "Renew" invocation and return the response as a STSResponse Object
*/
public STSResponse renew(SecurityToken tok) throws Exception {
createClient();
BindingOperationInfo boi = findOperation("/RST/Renew");
client.getRequestContext().putAll(ctx);
client.getRequestContext().remove(SecurityConstants.TOKEN_ID);
if (isSecureConv) {
client.getRequestContext().put(SoapBindingConstants.SOAP_ACTION, namespace + "/RST/SCT/Renew");
} else {
client.getRequestContext().put(SoapBindingConstants.SOAP_ACTION, namespace + "/RST/Renew");
}
W3CDOMStreamWriter writer = new W3CDOMStreamWriter();
writer.writeStartElement("wst", "RequestSecurityToken", namespace);
writer.writeNamespace("wst", namespace);
if (context != null) {
writer.writeAttribute(null, "Context", context);
}
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 ("TokenType".equals(tl.getLocalName())) {
sptt = DOMUtils.getContent(tl);
}
tl = DOMUtils.getNextElement(tl);
}
if (this.useSecondaryParameters()) {
writer.writeEndElement();
}
}
if (isSpnego) {
tokenType = STSUtils.getTokenTypeSCT(namespace);
}
if (sptt == null) {
addTokenType(writer);
}
addRequestType("/Renew", writer);
if (enableAppliesTo) {
addAppliesTo(writer, tok.getIssuerAddress());
}
if (isSecureConv || enableLifetime) {
addLifetime(writer);
}
writer.writeStartElement("wst", "RenewTarget", namespace);
StaxUtils.copy(tok.getToken(), writer);
writer.writeEndElement();
// Write out renewal semantics
writeRenewalSemantics(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], null, null, null, attachments);
}
use of org.apache.cxf.message.Attachment in project cxf by apache.
the class AttachmentCallbackHandler method handle.
@Override
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
for (Callback callback : callbacks) {
if (callback instanceof AttachmentRequestCallback) {
AttachmentRequestCallback attachmentRequestCallback = (AttachmentRequestCallback) callback;
List<org.apache.wss4j.common.ext.Attachment> attachmentList = new ArrayList<>();
attachmentRequestCallback.setAttachments(attachmentList);
String attachmentId = attachmentRequestCallback.getAttachmentId();
if ("Attachments".equals(attachmentId)) {
// Load all attachments
attachmentId = null;
}
loadAttachments(attachmentList, attachmentId, attachmentRequestCallback.isRemoveAttachments());
} else if (callback instanceof AttachmentResultCallback) {
AttachmentResultCallback attachmentResultCallback = (AttachmentResultCallback) callback;
org.apache.cxf.attachment.AttachmentImpl securedAttachment = new org.apache.cxf.attachment.AttachmentImpl(attachmentResultCallback.getAttachmentId(), new DataHandler(new AttachmentDataSource(attachmentResultCallback.getAttachment().getMimeType(), attachmentResultCallback.getAttachment().getSourceStream())));
Map<String, String> headers = attachmentResultCallback.getAttachment().getHeaders();
for (Map.Entry<String, String> entry : headers.entrySet()) {
securedAttachment.setHeader(entry.getKey(), entry.getValue());
}
attachments.add(securedAttachment);
} else if (callback instanceof AttachmentRemovalCallback) {
AttachmentRemovalCallback attachmentRemovalCallback = (AttachmentRemovalCallback) callback;
String attachmentId = attachmentRemovalCallback.getAttachmentId();
if (attachmentId != null) {
// Calling LazyAttachmentCollection.size() here to force it to load the attachments
if (attachments != null && attachments.size() > 0) {
// NOPMD
for (Iterator<org.apache.cxf.message.Attachment> iterator = attachments.iterator(); iterator.hasNext(); ) {
org.apache.cxf.message.Attachment attachment = iterator.next();
if (attachmentId.equals(attachment.getId())) {
iterator.remove();
break;
}
}
}
}
} else {
throw new UnsupportedCallbackException(callback, "Unsupported callback");
}
}
}
Aggregations