@Test public void compatibilityToString() { Message message = InternalMessage.builder() .payload(TypedValue.of("test")) .attributes(new TypedValue<>("{}", JSON_STRING)) .mediaType(TEXT) .addInboundProperty("bar", "in") .addOutboundProperty(FOO_PROPERTY, "out") .exceptionPayload(new DefaultExceptionPayload(new NullPointerException("error"))) .build(); assertThat(message.toString(), is(equalToIgnoringLineBreaks("\n" + "org.mule.runtime.core.internal.message.DefaultMessageBuilder$MessageImplementation\n" + "{\n" + " payload=test\n" + " mediaType=text/plain\n" + " attributes={}\n" + " attributesMediaType=application/json\n" + " exceptionPayload:\n" + " message=error\n" + " exception=java.lang.NullPointerException: error\n" + " Message properties:\n" + " INBOUND scoped properties:\n" + " bar=in\n" + " OUTBOUND scoped properties:\n" + " foo=out\n" + "}"))); }
public MessageBuilderDecorator withOutboundAttachments(Map<String, DataHandler> attachments) { if (null != attachments && !attachments.isEmpty()) { attachments.forEach((k, v) -> builder.addOutboundAttachment(k, v)); } return this; }
@Test public void outboundAttachmentsRespectOrder() { InternalMessage message = InternalMessage.builder() .payload(TypedValue.of("test")) .mediaType(TEXT) .outboundAttachments(getAttachments()) .exceptionPayload(new DefaultExceptionPayload(new NullPointerException("error"))) .build(); validateAttachments(message.getOutboundAttachmentNames()); }
InternalMessage.builder().value(payload).inboundProperties(inboundProps).outboundProperties(outboundProps).build(); Splitter splitter = new Splitter(); Grabber grabber = new Grabber();
properties.put("apple", apple); InternalMessage message = InternalMessage.builder().value(TEST_MESSAGE).outboundProperties(properties).build(); assertTrue(message.getOutboundProperty("boolean", false)); assertEquals(new Integer(24), message.getOutboundProperty("number", 0));
@Override public CoreEvent process(CoreEvent event) throws MuleException { try { Message msg; Error error = null; String payload = ((InternalEvent) event).getMessageAsString(muleContext); if (payload.indexOf(rejectIfMatches) >= 0) { throw new DefaultMuleException("Saw " + rejectIfMatches); } else if (payload.toLowerCase().indexOf(rejectIfMatches) >= 0) { Exception exception = new Exception(); error = createErrorMock(exception); msg = InternalMessage.builder().nullValue().exceptionPayload(new DefaultExceptionPayload(exception)).build(); } else { msg = of("No " + rejectIfMatches); } CoreEvent muleEvent = eventBuilder(muleContext).message(msg).error(error).build(); return muleEvent; } catch (Exception e) { throw new DefaultMuleException(e); } } }
@Override public void handleException(Exception ex, RollbackSourceCallback rollbackMethod) { fireNotification(ex, getCurrentEvent()); resolveAndLogException(ex); logger.debug("Rolling back transaction"); rollback(ex, rollbackMethod); ExceptionPayload exceptionPayload = new DefaultExceptionPayload(ex); if (getCurrentEvent() != null) { PrivilegedEvent currentEvent = getCurrentEvent(); currentEvent = PrivilegedEvent.builder(currentEvent) .message(InternalMessage.builder(currentEvent.getMessage()).exceptionPayload(exceptionPayload).build()).build(); setCurrentEvent(currentEvent); } if (ex instanceof ConnectException) { ((ConnectException) ex).handleReconnection(retryScheduler); } }
@Override public void run() { try { Exception exception = new Exception(); event = PrivilegedEvent.builder(event) .message(InternalMessage.builder(event.getMessage()).exceptionPayload(new DefaultExceptionPayload(exception)).build()) .error(createErrorMock(exception)).build(); setCurrentEvent(event); success.set(true); } catch (RuntimeException e) { LOGGER.error("error in thread", e); } } }
public MessageBuilderDecorator withOutboundAttachment(String key, DataHandler value) { builder.addOutboundAttachment(key, value); return this; }
public MessageBuilderDecorator withInboundAttachment(String key, DataHandler value) { builder.addInboundAttachment(key, value); return this; }
public MessageBuilderDecorator withAttributes(TypedValue attributes) { builder.attributes(attributes); return this; }
public MessageBuilderDecorator withAttributes(Object attributes) { builder.attributes(TypedValue.of(attributes)); return this; }
protected Function<CoreEvent, Publisher<CoreEvent>> route(Exception exception) { return event -> { if (getMessageProcessors().isEmpty()) { return just(event); } else { event = CoreEvent.builder(event) .message(InternalMessage.builder(event.getMessage()).exceptionPayload(new DefaultExceptionPayload(exception)).build()) .build(); } return from(processWithChildContext(event, configuredMessageProcessors, ofNullable(getLocation()), NullExceptionHandler.getInstance())); }; }
/** * Prepares a flow variable with the given key and value to be set in the product. * * @param attributes the attributes object for the produced {@link org.mule.runtime.api.message.Message} * @param mediaType the media type of the variable to add * @param charset the charset of the property to add * @return this {@link EventBuilder} */ public EventBuilder withAttributes(Object attributes, String mediaType, String charset) { muleMessageBuilder = muleMessageBuilder.attributes(new TypedValue<>(attributes, buildDataType(attributes, mediaType, charset))); return this; }
/** * Prepares the outbound properties with the given map. Should the map be null or empty the new event will contain an empty map. * Otherwise the content will be added to the pre existing map. * * @param outboundProperties the map to be set * @return this {@link EventBuilder} */ public EventBuilder withOutboundProperties(Map<String, Serializable> outboundProperties) { muleMessageBuilder = muleMessageBuilder.outboundProperties(outboundProperties); return this; }
/** * Prepares an attachment with the given key and value to be sent in the product. * * @param key the key of the attachment to add * @param value the {@link DataHandler} for the attachment to add * @return this {@link EventBuilder} */ public EventBuilder addOutboundAttachment(String key, DataHandler value) { muleMessageBuilder = muleMessageBuilder.addOutboundAttachment(key, value); return this; }
/** * Sets the {@link org.mule.runtime.api.message.Message#getAttributes()} value of the produced message * * @param attributes the attributes object for the produced {@link org.mule.runtime.api.message.Message} * @return this {@link EventBuilder} */ public EventBuilder withAttributes(Object attributes) { muleMessageBuilder = muleMessageBuilder.attributesValue(attributes); return this; }
@Test public void outboundPropertyMap() { Map<String, Serializable> outboundProperties = singletonMap(PROPERTY_KEY, PROPERTY_VALUE); InternalMessage message = new DefaultMessageBuilder().value(TEST_PAYLOAD).outboundProperties(outboundProperties).build(); assertThat(message.getOutboundProperty(PROPERTY_KEY), equalTo(PROPERTY_VALUE)); assertThat(message.getOutboundPropertyDataType(PROPERTY_KEY), equalTo(STRING)); assertThat(message.getOutboundPropertyNames(), hasSize(1)); assertThat(message.getOutboundPropertyNames(), hasItem(PROPERTY_KEY)); }
@Test public void outboundPropertyMapCopy() { Map<String, Serializable> outboundProperties = singletonMap(PROPERTY_KEY, PROPERTY_VALUE); InternalMessage copy = new DefaultMessageBuilder(new DefaultMessageBuilder().value(TEST_PAYLOAD) .outboundProperties(outboundProperties).build()).build(); assertThat(copy.getOutboundProperty(PROPERTY_KEY), equalTo(PROPERTY_VALUE)); assertThat(copy.getOutboundPropertyDataType(PROPERTY_KEY), equalTo(STRING)); assertThat(copy.getOutboundPropertyNames(), hasSize(1)); assertThat(copy.getOutboundPropertyNames(), hasItem(PROPERTY_KEY)); }
@Test public void createMessageViaMessageInterfaceFromCopy() { Message messageCopy = InternalMessage.builder(createTestMessage()).value(true).attributesValue(BASE_ATTRIBUTES).build(); assertThat(messageCopy.getPayload().getValue(), is(true)); assertThat(messageCopy.getPayload().getDataType(), is(assignableTo(BOOLEAN))); assertThat(messageCopy.getAttributes().getValue(), is(BASE_ATTRIBUTES)); assertThat(messageCopy.getAttributes().getDataType(), is(BASE_ATTRIBUTES_DATATYPE)); }