Java Code Examples for org.apache.cxf.message.Message#getAttachments()

The following examples show how to use org.apache.cxf.message.Message#getAttachments() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JAXBAttachmentSchemaValidationHack.java    From cxf with Apache License 2.0 6 votes vote down vote up
public void handleMessage(Message message) throws Fault {
    // This assumes that this interceptor is only use in IN / IN Fault chains.
    if (ServiceUtils.isSchemaValidationEnabled(SchemaValidationType.IN, message)
        && message.getAttachments() != null) {
        Collection<AttachmentDataSource> dss = new ArrayList<>();
        for (Attachment at : message.getAttachments()) {
            if (at.getDataHandler().getDataSource() instanceof AttachmentDataSource) {
                AttachmentDataSource ds = (AttachmentDataSource)at.getDataHandler().getDataSource();
                try {
                    ds.hold(message);
                } catch (IOException e) {
                    throw new Fault(e);
                }
                dss.add(ds);
            }
        }
        if (!dss.isEmpty()) {
            message.put(SAVED_DATASOURCES, dss);
            message.getInterceptorChain().add(EndingInterceptor.INSTANCE);
        }
    }
}
 
Example 2
Source File: PersistenceUtils.java    From cxf with Apache License 2.0 6 votes vote down vote up
public static void encodeRMContent(RMMessage rmmsg, Message msg, InputStream msgContent)
    throws IOException {
    CachedOutputStream cos = new CachedOutputStream();
    if (msg.getAttachments() == null) {
        rmmsg.setContentType((String)msg.get(Message.CONTENT_TYPE));
        IOUtils.copyAndCloseInput(msgContent, cos);
        cos.flush();
        rmmsg.setContent(cos);
    } else {
        MessageImpl msgImpl1 = new MessageImpl();
        msgImpl1.setContent(OutputStream.class, cos);
        msgImpl1.setAttachments(msg.getAttachments());
        msgImpl1.put(Message.CONTENT_TYPE, msg.get(Message.CONTENT_TYPE));
        msgImpl1.setContent(InputStream.class, msgContent);
        AttachmentSerializer serializer = new AttachmentSerializer(msgImpl1);
        serializer.setXop(false);
        serializer.writeProlog();
        // write soap root message into cached output stream
        IOUtils.copyAndCloseInput(msgContent, cos);
        cos.flush();
        serializer.writeAttachments();
        rmmsg.setContentType((String) msgImpl1.get(Message.CONTENT_TYPE));
        rmmsg.setContent(cos);
    }
}
 
Example 3
Source File: AbstractOutDatabindingInterceptor.java    From cxf with Apache License 2.0 6 votes vote down vote up
protected <T> DataWriter<T> getDataWriter(Message message, Service service, Class<T> output) {
    DataWriter<T> writer = service.getDataBinding().createWriter(output);

    Collection<Attachment> atts = message.getAttachments();
    if (MessageUtils.getContextualBoolean(message, Message.MTOM_ENABLED, false)
          && atts == null) {
        atts = new ArrayList<>();
        message.setAttachments(atts);
    }

    writer.setAttachments(atts);
    writer.setProperty(DataWriter.ENDPOINT, message.getExchange().getEndpoint());
    writer.setProperty(Message.class.getName(), message);

    setDataWriterValidation(service, message, writer);
    return writer;
}
 
Example 4
Source File: MessageModeInInterceptor.java    From cxf with Apache License 2.0 5 votes vote down vote up
private void doDataSource(final Message message) {
    MessageContentsList list = (MessageContentsList)message.getContent(List.class);
    //reconstitute all the parts into a Mime data source
    if (message.getAttachments() != null && !message.getAttachments().isEmpty()
        && list != null
        && !list.isEmpty() && list.get(0) instanceof DataSource) {
        list.set(0, new MultiPartDataSource(message, (DataSource)list.get(0)));
    }
}
 
Example 5
Source File: AbstractHTTPDestination.java    From cxf with Apache License 2.0 5 votes vote down vote up
/**
 * On first write, we need to make sure any attachments and such that are still on the incoming stream
 * are read in.  Otherwise we can get into a deadlock where the client is still trying to send the
 * request, but the server is trying to send the response.   Neither side is reading and both blocked
 * on full buffers.  Not a good situation.
 * @param outMessage
 */
private void cacheInput(Message outMessage) {
    if (outMessage.getExchange() == null) {
        return;
    }
    Message inMessage = outMessage.getExchange().getInMessage();
    if (inMessage == null) {
        return;
    }
    Object o = inMessage.get("cxf.io.cacheinput");
    DelegatingInputStream in = inMessage.getContent(DelegatingInputStream.class);
    if (PropertyUtils.isTrue(o)) {
        Collection<Attachment> atts = inMessage.getAttachments();
        if (atts != null) {
            for (Attachment a : atts) {
                if (a.getDataHandler().getDataSource() instanceof AttachmentDataSource) {
                    try {
                        ((AttachmentDataSource)a.getDataHandler().getDataSource()).cache(inMessage);
                    } catch (IOException e) {
                        throw new Fault(e);
                    }
                }
            }
        }
        if (in != null) {
            in.cacheInput();
        }
    } else if (in != null) {
        //We don't need to cache it, but we may need to consume it in order for the client
        // to be able to receive a response. (could be blocked sending)
        //However, also don't want to consume indefinitely.   We'll limit to 16M.
        try {
            IOUtils.consume(in, 16 * 1024 * 1024);
        } catch (Exception ioe) {
            //ignore
        }
    }
}
 
Example 6
Source File: JMSConduit.java    From cxf with Apache License 2.0 5 votes vote down vote up
private void assertIsNotTextMessageAndMtom(final Message outMessage) {
    boolean isTextPayload = JMSConstants.TEXT_MESSAGE_TYPE.equals(jmsConfig.getMessageType());
    if (isTextPayload && MessageUtils.getContextualBoolean(outMessage, org.apache.cxf.message.Message.MTOM_ENABLED)
        && outMessage.getAttachments() != null && outMessage.getAttachments().size() > 0) {
        org.apache.cxf.common.i18n.Message msg =
            new org.apache.cxf.common.i18n.Message("INVALID_MESSAGE_TYPE", LOG);
        throw new ConfigurationException(msg);
    }
}
 
Example 7
Source File: AttachmentCallbackHandler.java    From cxf with Apache License 2.0 4 votes vote down vote up
public AttachmentCallbackHandler(Message message) {
    if (message.getAttachments() == null) {
        message.setAttachments(new ArrayList<Attachment>());
    }
    attachments = message.getAttachments();
}
 
Example 8
Source File: DestinationSequence.java    From cxf with Apache License 2.0 4 votes vote down vote up
public void acknowledge(Message message) throws SequenceFault {
    RMProperties rmps = RMContextUtils.retrieveRMProperties(message, false);
    SequenceType st = rmps.getSequence();
    long messageNumber = st.getMessageNumber();
    LOG.fine("Acknowledging message: " + messageNumber);
    if (0L != lastMessageNumber && messageNumber > lastMessageNumber) {
        RMConstants consts = getProtocol().getConstants();
        SequenceFaultFactory sff = new SequenceFaultFactory(consts);
        throw sff.createSequenceTerminatedFault(st.getIdentifier(), false);
    }

    monitor.acknowledgeMessage();
    boolean updated = false;

    synchronized (this) {
        boolean done = false;
        int i = 0;
        for (; i < acknowledgement.getAcknowledgementRange().size(); i++) {
            AcknowledgementRange r = acknowledgement.getAcknowledgementRange().get(i);
            if (r.getLower().compareTo(messageNumber) <= 0
                && r.getUpper().compareTo(messageNumber) >= 0) {
                done = true;
                break;
            }
            long diff = r.getLower() - messageNumber;
            if (diff == 1L) {
                r.setLower(messageNumber);
                updated = true;
                done = true;
            } else if (diff > 0L) {
                break;
            } else if (messageNumber - r.getUpper() == 1L) {
                r.setUpper(messageNumber);
                updated = true;
                done = true;
                break;
            }
        }

        if (!done) {

            // need new acknowledgement range
            AcknowledgementRange range = new AcknowledgementRange();
            range.setLower(messageNumber);
            range.setUpper(messageNumber);
            updated = true;
            acknowledgement.getAcknowledgementRange().add(i, range);
            if (acknowledgement.getAcknowledgementRange().size() > 1) {

                // acknowledge out-of-order at first opportunity
                scheduleImmediateAcknowledgement();

            }
        }
        mergeRanges();
    }

    if (updated) {
        RMStore store = destination.getManager().getStore();
        if (null != store && !MessageUtils.getContextualBoolean(message, Message.ROBUST_ONEWAY)) {
            try {
                RMMessage msg = new RMMessage();
                CachedOutputStream cos = (CachedOutputStream)message
                    .get(RMMessageConstants.SAVED_CONTENT);
                msg.setMessageNumber(st.getMessageNumber());
                msg.setCreatedTime(rmps.getCreatedTime());
                // in case no attachments are available, cos can be saved directly
                if (message.getAttachments() == null) {
                    msg.setContent(cos);
                    msg.setContentType((String)message.get(Message.CONTENT_TYPE));
                } else {
                    InputStream is = cos.getInputStream();
                    PersistenceUtils.encodeRMContent(msg, message, is);
                }
                store.persistIncoming(this, msg);
            } catch (IOException e) {
                throw new Fault(e);
            }
        }
    }
    deliveringMessageNumbers.add(messageNumber);

    RMEndpoint reliableEndpoint = destination.getReliableEndpoint();
    RMConfiguration cfg = reliableEndpoint.getConfiguration();

    if (null == rmps.getCloseSequence()) {
        scheduleAcknowledgement(cfg.getAcknowledgementIntervalTime());
    }
    long inactivityTimeout = cfg.getInactivityTimeoutTime();
    scheduleSequenceTermination(inactivityTimeout);
}
 
Example 9
Source File: AttachmentOutInterceptor.java    From cxf with Apache License 2.0 4 votes vote down vote up
public void handleMessage(Message message) {
    //avoid AttachmentOutInterceptor invoked twice on the 
    //same message
    if (message.get(ATTACHMENT_OUT_CHECKED) != null
        && (boolean)message.get(ATTACHMENT_OUT_CHECKED)) {
        return;
    } else {
        message.put(ATTACHMENT_OUT_CHECKED, Boolean.TRUE);
    }
    // Make it possible to step into this process in spite of Eclipse
    // by declaring the Object.
    boolean mtomEnabled = AttachmentUtil.isMtomEnabled(message);
    boolean writeAtts = MessageUtils.getContextualBoolean(message, WRITE_ATTACHMENTS, false)
        || (message.getAttachments() != null && !message.getAttachments().isEmpty());

    if (!mtomEnabled && !writeAtts) {
        return;
    }
    if (message.getContent(OutputStream.class) == null) {
        return;
    }

    AttachmentSerializer serializer =
        new AttachmentSerializer(message,
                                 getMultipartType(),
                                 writeOptionalTypeParameters(),
                                 getRootHeaders());
    serializer.setXop(mtomEnabled);
    String contentTransferEncoding = (String)message.getContextualProperty(
                                        org.apache.cxf.message.Message.CONTENT_TRANSFER_ENCODING);
    if (contentTransferEncoding != null) {
        serializer.setContentTransferEncoding(contentTransferEncoding);
    }

    try {
        serializer.writeProlog();
    } catch (IOException e) {
        throw new Fault(new org.apache.cxf.common.i18n.Message("WRITE_ATTACHMENTS", BUNDLE), e);
    }
    message.setContent(AttachmentSerializer.class, serializer);

    // Add a final interceptor to write attachements
    message.getInterceptorChain().add(ending);
}
 
Example 10
Source File: AttachmentDeserializerTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
@Test
public void testDoesntReturnZero() throws Exception {
    String contentType = "multipart/mixed;boundary=----=_Part_1";
    byte[] messageBytes = (
              "------=_Part_1\n\n"
            + "JJJJ\n"
            + "------=_Part_1"
            + "\n\nContent-Transfer-Encoding: binary\n\n"
            + "ABCD1\r\n"
            + "------=_Part_1"
            + "\n\nContent-Transfer-Encoding: binary\n\n"
            + "ABCD2\r\n"
            + "------=_Part_1"
            + "\n\nContent-Transfer-Encoding: binary\n\n"
            + "ABCD3\r\n"
            + "------=_Part_1--").getBytes(StandardCharsets.UTF_8);
    ByteArrayInputStream in = new ByteArrayInputStream(messageBytes) {
        public int read(byte[] b, int off, int len) {
            return super.read(b, off, len >= 2 ? 2 : len);
        }
    };

    Message message = new MessageImpl();
    message.put(Message.CONTENT_TYPE, contentType);
    message.setContent(InputStream.class, in);
    message.put(AttachmentDeserializer.ATTACHMENT_DIRECTORY, System
            .getProperty("java.io.tmpdir"));
    message.put(AttachmentDeserializer.ATTACHMENT_MEMORY_THRESHOLD, String
            .valueOf(AttachmentDeserializer.THRESHOLD));


    AttachmentDeserializer ad
        = new AttachmentDeserializer(message,
                                     Collections.singletonList("multipart/mixed"));

    ad.initializeAttachments();

    String s = IOUtils.toString(message.getContent(InputStream.class));
    assertEquals("JJJJ", s.trim());
    int count = 1;
    for (Attachment a : message.getAttachments()) {
        s = IOUtils.toString(a.getDataHandler().getInputStream());
        assertEquals("ABCD" + count++, s);
    }

    in.close();
}