SaveUserMsgAttachments extends AbstractUserMessageHandler { @Override protected InvocationResponse doProcessing(final IUserMessageEntity um, final IMessageProcessingContext procCtx, final Logger log) throws PersistenceException { StorageManager updateManager = HolodeckB2BCore.getStorageManager(); final Collection<IPayload> payloads = um.getPayloads(); if (Utils.isNullOrEmpty(payloads)) { log.debug("UserMessage contains no payloads."); return InvocationResponse.CONTINUE; } log.debug("UserMessage contains " + payloads.size() + " payloads."); try { final File tmpPayloadDir = getTempDir(); log.trace("Payload content will be stored in " + tmpPayloadDir.getAbsolutePath()); ArrayList<IPayload> newPayloadData = new ArrayList<>(payloads.size()); for(final IPayload ip : payloads) { Payload p = new Payload(ip); final File plFile = File.createTempFile("pl-", null, tmpPayloadDir); final String plRef = p.getPayloadURI(); switch (p.getContainment()) { case BODY: OMElement plElement= null; if (Utils.isNullOrEmpty(plRef)) { log.trace("No reference included in payload meta data => SOAP body is the payload"); plElement = procCtx.getParentContext().getEnvelope().getBody().getFirstElement(); } else { log.trace("Payload is element with id " + plRef + " of SOAP body"); plElement = getPayloadFromBody(procCtx.getParentContext().getEnvelope().getBody(), plRef); } if (plElement == null) { createInconsistentError(procCtx, um, null, log); return InvocationResponse.CONTINUE; } else { log.trace("Found referenced element in SOAP body"); saveXMLPayload(plElement, plFile); log.trace("Payload saved to temporary file, set content location in meta data"); p.setMimeType("application/xml"); p.setContentLocation(plFile.getAbsolutePath()); } break; case ATTACHMENT: log.trace("Payload is contained in attachment with MIME Content-id= " + plRef); log.debug("Get DataHandler for attachment"); final DataHandler dh = procCtx.getParentContext().getAttachment(plRef); if (dh == null) { createInconsistentError(procCtx, um, plRef, log); return InvocationResponse.CONTINUE; } else { try (final OutputStream aOS = new FileOutputStream(plFile)) { dh.writeTo(aOS); } catch (final IOException ioException) { Throwable rootCause = Utils.getRootCause(ioException); EbmsError writeFailure; String errMessage; if (rootCause instanceof ZipException) { errMessage = "decompressed"; writeFailure = new DeCompressionFailure("Payload [" + plRef + "] in message could not be decompressed!", um.getMessageId()); } else if (Utils.getRootCause(ioException) instanceof java.security.GeneralSecurityException) { errMessage = "decrypted"; writeFailure = new FailedDecryption("Payload [" + plRef + "] in message could not be decrypted!", um.getMessageId()); } else { errMessage = "written to temp directory"; writeFailure = new OtherContentError("Unexpected error in payload processing!", um.getMessageId()); } log.info("Payload [" + plRef + "] in message [" + um.getMessageId() + "] could not be " + errMessage + "!\n\tDetails: " + rootCause.getMessage()); procCtx.addGeneratedError( writeFailure); log.trace("Error generated and stored in MC, change processing state of user message"); updateManager.setProcessingState(um, ProcessingState.FAILURE); return InvocationResponse.CONTINUE; } log.debug("Payload saved to temporary file, set content location in meta data"); p.setContentLocation(plFile.getAbsolutePath()); p.setMimeType(dh.getContentType()); } break; default: log.debug("Payload is not contained in message but located at " + plRef); } newPayloadData.add(p); } log.debug("All payloads saved to temp file"); updateManager.setPayloadInformation(um, newPayloadData); } catch (IOException | XMLStreamException ex) { log.error("Payload(s) could not be saved to temporary file! Details:" + ex.getMessage()); procCtx.addGeneratedError(new OtherContentError("Internal error", um.getMessageId())); updateManager.setProcessingState(um, ProcessingState.FAILURE); } return InvocationResponse.CONTINUE; }    }
@Test public void testDoProcessing() throws Exception { MessageContext mc = new MessageContext(); mc.setFLOW(MessageContext.IN_FLOW); UserMessage userMessage = new UserMessage(); Payload payload = new Payload(); payload.setContainment(Containment.ATTACHMENT); payload.setMimeType("image/jpeg"); payload.setPayloadURI("some-att-cid"); userMessage.addPayload(payload); IMessageProcessingContext procCtx = MessageProcessingContext.getFromMessageContext(mc); IUserMessageEntity userMsgEntity = HolodeckB2BCore.getStorageManager().storeIncomingMessageUnit(userMessage); procCtx.setUserMessage(userMsgEntity); Attachments attachments = new Attachments(); DataHandler dh = new DataHandler(new FileDataSource(baseDir + "/dandelion.jpg")); attachments.addDataHandler(payload.getPayloadURI(), dh); mc.setAttachmentMap(attachments); try { assertEquals(Handler.InvocationResponse.CONTINUE, new SaveUserMsgAttachments().invoke(mc)); } catch (Exception e) { fail(e.getMessage()); } String storedPayload = userMsgEntity.getPayloads().iterator().next().getContentLocation(); assertFalse(Utils.isNullOrEmpty(storedPayload)); assertTrue(new File(storedPayload).exists()); }