Search in sources :

Example 41 with Attachment

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);
}
Also used : Message(org.apache.cxf.message.Message) Attachment(org.apache.cxf.message.Attachment) DataHandler(javax.activation.DataHandler) LinkedList(java.util.LinkedList) ByteDataSource(org.apache.cxf.attachment.ByteDataSource) DataSource(javax.activation.DataSource) Exchange(org.apache.cxf.message.Exchange) ByteDataSource(org.apache.cxf.attachment.ByteDataSource) WrappedMessageContext(org.apache.cxf.jaxws.context.WrappedMessageContext) AttachmentImpl(org.apache.cxf.attachment.AttachmentImpl) WrappedMessageContext(org.apache.cxf.jaxws.context.WrappedMessageContext) MessageContext(javax.xml.ws.handler.MessageContext) MessageImpl(org.apache.cxf.message.MessageImpl) HashMap(java.util.HashMap) Map(java.util.Map) ExchangeImpl(org.apache.cxf.message.ExchangeImpl) Test(org.junit.Test)

Example 42 with Attachment

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);
    }
}
Also used : DatabindingException(org.apache.cxf.aegis.DatabindingException) Attachment(org.apache.cxf.message.Attachment) IOException(java.io.IOException)

Example 43 with Attachment

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());
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Attachment(org.apache.cxf.message.Attachment) DataHandler(javax.activation.DataHandler) AttachmentImpl(org.apache.cxf.attachment.AttachmentImpl) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) Test(org.junit.Test)

Example 44 with Attachment

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);
}
Also used : W3CDOMStreamWriter(org.apache.cxf.staxutils.W3CDOMStreamWriter) BindingOperationInfo(org.apache.cxf.service.model.BindingOperationInfo) DOMSource(javax.xml.transform.dom.DOMSource) ExtensibilityElement(javax.wsdl.extensions.ExtensibilityElement) Element(org.w3c.dom.Element) Collection(java.util.Collection) ClaimCollection(org.apache.cxf.rt.security.claims.ClaimCollection) Attachment(org.apache.cxf.message.Attachment)

Example 45 with Attachment

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");
        }
    }
}
Also used : AttachmentRequestCallback(org.apache.wss4j.common.ext.AttachmentRequestCallback) AttachmentDataSource(org.apache.cxf.attachment.AttachmentDataSource) ArrayList(java.util.ArrayList) Attachment(org.apache.cxf.message.Attachment) DataHandler(javax.activation.DataHandler) AttachmentRemovalCallback(org.apache.wss4j.common.ext.AttachmentRemovalCallback) Attachment(org.apache.cxf.message.Attachment) AttachmentResultCallback(org.apache.wss4j.common.ext.AttachmentResultCallback) AttachmentRemovalCallback(org.apache.wss4j.common.ext.AttachmentRemovalCallback) Callback(javax.security.auth.callback.Callback) AttachmentRequestCallback(org.apache.wss4j.common.ext.AttachmentRequestCallback) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) AttachmentResultCallback(org.apache.wss4j.common.ext.AttachmentResultCallback) Map(java.util.Map)

Aggregations

Attachment (org.apache.cxf.message.Attachment)51 DataHandler (javax.activation.DataHandler)18 Test (org.junit.Test)18 InputStream (java.io.InputStream)14 Message (org.apache.cxf.message.Message)14 ByteArrayInputStream (java.io.ByteArrayInputStream)13 AttachmentImpl (org.apache.cxf.attachment.AttachmentImpl)12 ByteArrayOutputStream (java.io.ByteArrayOutputStream)11 IOException (java.io.IOException)11 ArrayList (java.util.ArrayList)11 MessageImpl (org.apache.cxf.message.MessageImpl)11 HashMap (java.util.HashMap)10 List (java.util.List)9 PushbackInputStream (java.io.PushbackInputStream)7 ByteArrayDataSource (javax.mail.util.ByteArrayDataSource)6 Fault (org.apache.cxf.interceptor.Fault)6 BindingOperationInfo (org.apache.cxf.service.model.BindingOperationInfo)6 Map (java.util.Map)5 OutputStream (java.io.OutputStream)4 TreeMap (java.util.TreeMap)4