Search in sources :

Example 1 with AttachmentOutputInterceptor

use of org.apache.cxf.jaxrs.interceptor.AttachmentOutputInterceptor in project cxf by apache.

the class MessageContextImpl method convertToAttachments.

private void convertToAttachments(Object value) {
    List<?> handlers = (List<?>) value;
    List<org.apache.cxf.message.Attachment> atts = new ArrayList<org.apache.cxf.message.Attachment>();
    for (int i = 1; i < handlers.size(); i++) {
        Attachment handler = (Attachment) handlers.get(i);
        AttachmentImpl att = new AttachmentImpl(handler.getContentId(), handler.getDataHandler());
        for (String key : handler.getHeaders().keySet()) {
            att.setHeader(key, handler.getHeader(key));
        }
        att.setXOP(false);
        atts.add(att);
    }
    Message outMessage = getOutMessage();
    outMessage.setAttachments(atts);
    outMessage.put(AttachmentOutInterceptor.WRITE_ATTACHMENTS, "true");
    Attachment root = (Attachment) handlers.get(0);
    String rootContentType = root.getContentType().toString();
    MultivaluedMap<String, String> rootHeaders = new MetadataMap<String, String>(root.getHeaders());
    if (!AttachmentUtil.isMtomEnabled(outMessage)) {
        rootHeaders.putSingle(Message.CONTENT_TYPE, rootContentType);
    }
    String messageContentType = outMessage.get(Message.CONTENT_TYPE).toString();
    int index = messageContentType.indexOf(";type");
    if (index > 0) {
        messageContentType = messageContentType.substring(0, index).trim();
    }
    AttachmentOutputInterceptor attInterceptor = new AttachmentOutputInterceptor(messageContentType, rootHeaders);
    outMessage.put(Message.CONTENT_TYPE, rootContentType);
    Map<String, List<String>> allHeaders = CastUtils.cast((Map<?, ?>) outMessage.get(Message.PROTOCOL_HEADERS));
    if (allHeaders != null) {
        allHeaders.remove(Message.CONTENT_TYPE);
    }
    attInterceptor.handleMessage(outMessage);
}
Also used : Message(org.apache.cxf.message.Message) ArrayList(java.util.ArrayList) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) Endpoint(org.apache.cxf.endpoint.Endpoint) MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) AttachmentOutputInterceptor(org.apache.cxf.jaxrs.interceptor.AttachmentOutputInterceptor) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) AttachmentImpl(org.apache.cxf.attachment.AttachmentImpl)

Aggregations

ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 AttachmentImpl (org.apache.cxf.attachment.AttachmentImpl)1 Endpoint (org.apache.cxf.endpoint.Endpoint)1 Attachment (org.apache.cxf.jaxrs.ext.multipart.Attachment)1 MetadataMap (org.apache.cxf.jaxrs.impl.MetadataMap)1 AttachmentOutputInterceptor (org.apache.cxf.jaxrs.interceptor.AttachmentOutputInterceptor)1 Message (org.apache.cxf.message.Message)1