DecompressionHandler extends AbstractUserMessageHandler { @Override protected InvocationResponse doProcessing(final IUserMessageEntity um, final IMessageProcessingContext procCtx, final Logger log) throws PersistenceException { if (Utils.isNullOrEmpty(um.getPayloads())) return InvocationResponse.CONTINUE; for (final IPayload p : um.getPayloads()) { if (p.getContainment() == IPayload.Containment.ATTACHMENT && usesCompression(p)) { log.debug("Payload uses compression feature, change DataHandler"); String mimeType = null; for(final Iterator<IProperty> pps = p.getProperties().iterator() ; pps.hasNext() && mimeType == null;) { final IProperty pp = pps.next(); mimeType = CompressionFeature.MIME_TYPE_PROPERTY_NAME.equals(pp.getName()) ? pp.getValue() : null; } if (Utils.isNullOrEmpty(mimeType)) { log.info("No source MIME Type specified for compressed payload!"); final DeCompressionFailure decompressFailure = new DeCompressionFailure(); decompressFailure.setErrorDetail("Missing required MimeType part property for compressed payload [" + p.getPayloadURI() + "]!"); decompressFailure.setRefToMessageInError(um.getMessageId()); procCtx.addGeneratedError(decompressFailure); HolodeckB2BCore.getStorageManager().setProcessingState(um, ProcessingState.FAILURE); } else { try { final String cid = p.getPayloadURI(); final MessageContext mc = procCtx.getParentContext(); mc.addAttachment(cid, new CompressionDataHandler(mc.getAttachment(cid), mimeType)); log.trace("Replaced DataHandler to enable decompression"); removeProperty(p); } catch (final NullPointerException npe) { } } } } 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(IPayload.Containment.ATTACHMENT); ArrayList<IProperty> props = new ArrayList<>(); Property p = new Property(); p.setName(CompressionFeature.FEATURE_PROPERTY_NAME); p.setValue(CompressionFeature.COMPRESSED_CONTENT_TYPE); props.add(p); p = new Property(); p.setName(CompressionFeature.MIME_TYPE_PROPERTY_NAME); p.setValue("image/jpeg"); props.add(p); payload.setProperties(props); String payloadPath = "file: payload.setPayloadURI(payloadPath); userMessage.addPayload(payload); Attachments attachments = new Attachments(); attachments.addDataHandler(payloadPath, new DataHandler(new URL(payload.getPayloadURI()))); mc.setAttachmentMap(attachments); IUserMessageEntity userMessageEntity = HolodeckB2BCore.getStorageManager().storeIncomingMessageUnit(userMessage); IMessageProcessingContext procCtx = MessageProcessingContext.getFromMessageContext(mc); procCtx.setUserMessage(userMessageEntity); try { Handler.InvocationResponse invokeResp = new DecompressionHandler().invoke(mc); assertEquals(Handler.InvocationResponse.CONTINUE, invokeResp); } catch (Exception e) { fail(e.getMessage()); } final IPayload deCompPayload = userMessageEntity.getPayloads().iterator().next(); assertEquals(IPayload.Containment.ATTACHMENT, deCompPayload.getContainment()); Collection<IProperty> properties = deCompPayload.getProperties(); assertFalse(Utils.isNullOrEmpty(properties)); assertFalse(properties.parallelStream().anyMatch(pp -> pp.getName().equals(CompressionFeature.FEATURE_PROPERTY_NAME))); assertTrue(mc.getAttachment(deCompPayload.getPayloadURI()) instanceof CompressionDataHandler); assertEquals("image/jpeg", mc.getAttachment(deCompPayload.getPayloadURI()).getContentType()); }