Example usage for javax.xml.soap AttachmentPart getRawContentBytes

List of usage examples for javax.xml.soap AttachmentPart getRawContentBytes

Introduction

In this page you can find the example usage for javax.xml.soap AttachmentPart getRawContentBytes.

Prototype

public abstract byte[] getRawContentBytes() throws SOAPException;

Source Link

Document

Gets the content of this AttachmentPart object as a byte[] array as if a call had been made to getContent and no DataContentHandler had been registered for the content-type of this AttachmentPart .

Usage

From source file:com.nortal.jroad.endpoint.AbstractXTeeBaseEndpoint.java

@SuppressWarnings("unchecked")
protected void getResponse(Document query, SOAPMessage responseMessage, SOAPMessage requestMessage)
        throws Exception {
    XTeeHeader header = metaService ? null : parseXteeHeader(requestMessage);

    // Build request message
    List<XTeeAttachment> attachments = new ArrayList<XTeeAttachment>();
    for (Iterator<AttachmentPart> i = requestMessage.getAttachments(); i.hasNext();) {
        AttachmentPart a = i.next();
        attachments.add(new XTeeAttachment(a.getContentId(), a.getContentType(), a.getRawContentBytes()));
    }//from w  w  w  .jav  a 2  s . c  o m
    XTeeMessage<Document> request = new BeanXTeeMessage<Document>(header, query, attachments);

    SOAPElement teenusElement = createXteeMessageStructure(requestMessage, responseMessage);
    if (XRoadProtocolVersion.V2_0 == version) {
        if (!metaService) {
            copyParing(query, teenusElement);
        }
        teenusElement = teenusElement.addChildElement("keha");
    }

    // Build response message
    XTeeMessage<Element> response = new BeanXTeeMessage<Element>(header, teenusElement,
            new ArrayList<XTeeAttachment>());

    // Run logic
    invokeInternalEx(request, response, requestMessage, responseMessage);

    // Add any attachments
    for (XTeeAttachment a : response.getAttachments()) {
        AttachmentPart attachment = responseMessage.createAttachmentPart(a.getDataHandler());
        attachment.setContentId("<" + a.getCid() + ">");
        responseMessage.addAttachmentPart(attachment);
    }
}

From source file:eu.domibus.ebms3.receiver.MSHWebservice.java

/**
 * This method persists incoming messages into the database (and handles decompression before)
 *
 * @param request          the message to persist
 * @param legConfiguration processing information for the message
 * @throws SOAPException// w  w w .  ja  va2 s.  c  o  m
 * @throws JAXBException
 * @throws TransformerException
 * @throws IOException
 * @throws EbMS3Exception
 */
//TODO: improve error handling
private String persistReceivedMessage(SOAPMessage request, LegConfiguration legConfiguration, String pmodeKey,
        Messaging messaging) throws SOAPException, JAXBException, TransformerException, EbMS3Exception {

    boolean bodyloadFound = false;
    for (PartInfo partInfo : messaging.getUserMessage().getPayloadInfo().getPartInfo()) {
        String cid = partInfo.getHref();
        MSHWebservice.LOG.debug("looking for attachment with cid: " + cid);
        boolean payloadFound = false;
        if (cid == null || cid.isEmpty()) {
            if (bodyloadFound) {
                throw new EbMS3Exception(EbMS3Exception.EbMS3ErrorCode.EBMS_0003,
                        "More than one Partinfo without CID found",
                        messaging.getUserMessage().getMessageInfo().getMessageId(), null, MSHRole.RECEIVING);
            }
            bodyloadFound = true;
            payloadFound = true;
            partInfo.setInBody(true);
            Node bodyContent = (((Node) request.getSOAPBody().getChildElements().next()));
            Source source = new DOMSource(bodyContent);
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            Result result = new StreamResult(out);
            Transformer transformer = this.transformerFactory.newTransformer();
            transformer.transform(source, result);
            partInfo.setBinaryData(out.toByteArray());
        }
        @SuppressWarnings("unchecked")
        Iterator<AttachmentPart> attachmentIterator = request.getAttachments();
        AttachmentPart attachmentPart;
        while (attachmentIterator.hasNext() && !payloadFound) {

            attachmentPart = attachmentIterator.next();
            //remove square brackets from cid for further processing
            attachmentPart.setContentId(AttachmentUtil.cleanContentId(attachmentPart.getContentId()));
            MSHWebservice.LOG.debug("comparing with: " + attachmentPart.getContentId());
            if (attachmentPart.getContentId().equals(AttachmentUtil.cleanContentId(cid))) {
                partInfo.setBinaryData(attachmentPart.getRawContentBytes());
                partInfo.setInBody(false);
                payloadFound = true;
            }
        }
        if (!payloadFound) {
            throw new EbMS3Exception(EbMS3Exception.EbMS3ErrorCode.EBMS_0011,
                    "No Attachment found for cid: " + cid + " of message: "
                            + messaging.getUserMessage().getMessageInfo().getMessageId(),
                    messaging.getUserMessage().getMessageInfo().getMessageId(), null, MSHRole.RECEIVING);
        }
    }

    boolean compressed = this.compressionService.handleDecompression(messaging.getUserMessage(),
            legConfiguration);
    this.payloadProfileValidator.validate(messaging, pmodeKey);
    this.propertyProfileValidator.validate(messaging, pmodeKey);

    MSHWebservice.LOG.debug("Compression for message with id: "
            + messaging.getUserMessage().getMessageInfo().getMessageId() + " applied: " + compressed);
    MessageLogEntry messageLogEntry = new MessageLogEntry();
    messageLogEntry.setMessageId(messaging.getUserMessage().getMessageInfo().getMessageId());
    messageLogEntry.setMessageType(MessageType.USER_MESSAGE);
    messageLogEntry.setMshRole(MSHRole.RECEIVING);
    messageLogEntry.setReceived(new Date());
    String mpc = messaging.getUserMessage().getMpc();
    messageLogEntry.setMpc((mpc == null || mpc.isEmpty()) ? Mpc.DEFAULT_MPC : mpc);
    messageLogEntry.setMessageStatus(MessageStatus.RECEIVED);

    this.messageLogDao.create(messageLogEntry);
    this.messagingDao.create(messaging);
    return messageLogEntry.getMessageId();

}

From source file:org.apache.axis2.saaj.AttachmentTest.java

@Validated
@Test/*from  ww w  .  j a  va  2  s  . c  o m*/
public void testGetRawContents() {
    try {
        MessageFactory factory = MessageFactory.newInstance();
        SOAPMessage msg = factory.createMessage();
        AttachmentPart ap = msg.createAttachmentPart();
        ap = msg.createAttachmentPart();
        byte data1[] = null;
        data1 = ap.getRawContentBytes();

    } catch (SOAPException e) {
        //Caught expected SOAPException
    } catch (NullPointerException e) {
        //Caught expected NullPointerException
    } catch (Exception e) {
        fail();
    }
}