Search in sources :

Example 1 with AttachmentInputInterceptor

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

the class MessageContextImpl method createAttachments.

private MultipartBody createAttachments(String propertyName) {
    Message inMessage = m.getExchange().getInMessage();
    boolean embeddedAttachment = inMessage.get("org.apache.cxf.multipart.embedded") != null;
    Object o = inMessage.get(propertyName);
    if (o != null) {
        return (MultipartBody) o;
    }
    if (embeddedAttachment) {
        inMessage = new MessageImpl();
        inMessage.setExchange(new ExchangeImpl());
        inMessage.put(AttachmentDeserializer.ATTACHMENT_DIRECTORY, m.getExchange().getInMessage().get(AttachmentDeserializer.ATTACHMENT_DIRECTORY));
        inMessage.put(AttachmentDeserializer.ATTACHMENT_MEMORY_THRESHOLD, m.getExchange().getInMessage().get(AttachmentDeserializer.ATTACHMENT_MEMORY_THRESHOLD));
        inMessage.put(AttachmentDeserializer.ATTACHMENT_MAX_SIZE, m.getExchange().getInMessage().get(AttachmentDeserializer.ATTACHMENT_MAX_SIZE));
        inMessage.put(AttachmentDeserializer.ATTACHMENT_MAX_HEADER_SIZE, m.getExchange().getInMessage().get(AttachmentDeserializer.ATTACHMENT_MAX_HEADER_SIZE));
        inMessage.setContent(InputStream.class, m.getExchange().getInMessage().get("org.apache.cxf.multipart.embedded.input"));
        inMessage.put(Message.CONTENT_TYPE, m.getExchange().getInMessage().get("org.apache.cxf.multipart.embedded.ctype").toString());
    }
    new AttachmentInputInterceptor().handleMessage(inMessage);
    List<Attachment> newAttachments = new LinkedList<Attachment>();
    try {
        Map<String, List<String>> headers = CastUtils.cast((Map<?, ?>) inMessage.get(AttachmentDeserializer.ATTACHMENT_PART_HEADERS));
        Attachment first = new Attachment(AttachmentUtil.createAttachment(inMessage.getContent(InputStream.class), headers), new ProvidersImpl(inMessage));
        newAttachments.add(first);
    } catch (IOException ex) {
        throw ExceptionUtils.toInternalServerErrorException(ex, null);
    }
    Collection<org.apache.cxf.message.Attachment> childAttachments = inMessage.getAttachments();
    if (childAttachments == null) {
        childAttachments = Collections.emptyList();
    }
    childAttachments.size();
    for (org.apache.cxf.message.Attachment a : childAttachments) {
        newAttachments.add(new Attachment(a, new ProvidersImpl(inMessage)));
    }
    MediaType mt = embeddedAttachment ? (MediaType) inMessage.get("org.apache.cxf.multipart.embedded.ctype") : getHttpHeaders().getMediaType();
    MultipartBody body = new MultipartBody(newAttachments, mt, false);
    inMessage.put(propertyName, body);
    return body;
}
Also used : Message(org.apache.cxf.message.Message) AttachmentInputInterceptor(org.apache.cxf.jaxrs.interceptor.AttachmentInputInterceptor) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) IOException(java.io.IOException) LinkedList(java.util.LinkedList) ProvidersImpl(org.apache.cxf.jaxrs.impl.ProvidersImpl) MultipartBody(org.apache.cxf.jaxrs.ext.multipart.MultipartBody) MediaType(javax.ws.rs.core.MediaType) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) MessageImpl(org.apache.cxf.message.MessageImpl) ExchangeImpl(org.apache.cxf.message.ExchangeImpl)

Aggregations

IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 MediaType (javax.ws.rs.core.MediaType)1 Attachment (org.apache.cxf.jaxrs.ext.multipart.Attachment)1 MultipartBody (org.apache.cxf.jaxrs.ext.multipart.MultipartBody)1 ProvidersImpl (org.apache.cxf.jaxrs.impl.ProvidersImpl)1 AttachmentInputInterceptor (org.apache.cxf.jaxrs.interceptor.AttachmentInputInterceptor)1 ExchangeImpl (org.apache.cxf.message.ExchangeImpl)1 Message (org.apache.cxf.message.Message)1 MessageImpl (org.apache.cxf.message.MessageImpl)1