// //@Override protected void initialise(MuleMessage message) { if (message.getPayload() instanceof List) { // get a copy of the list List payload = new LinkedList((List) message.getPayload()); payloadContext.set(payload); if (enableCorrelation != ENABLE_CORRELATION_NEVER) { // always set correlation group size, even if correlation id // has already been set (usually you don't have group size yet // by this point. final int groupSize = payload.size(); message.setCorrelationGroupSize(groupSize); if (logger.isDebugEnabled()) { logger.debug( "java.util.List payload detected, setting correlation group size to " + groupSize); } } } else { throw new IllegalArgumentException( "The payload for this router must be of type java.util.List"); } // Cache the properties here because for some message types getting the // properties can be expensive Map props = new HashMap(); for (Iterator iterator = message.getPropertyNames().iterator(); iterator.hasNext(); ) { String propertyKey = (String) iterator.next(); props.put(propertyKey, message.getProperty(propertyKey)); } propertiesContext.set(props); }
public ExceptionMessage( Object message, Throwable exception, String componentName, EndpointURI endpointUri) { super(message); this.exception = exception; timeStamp = new Date(); this.componentName = componentName; this.endpointUri = endpointUri; MuleEventContext ctx = RequestContext.getEventContext(); if (ctx != null) { MuleMessage msg = ctx.getMessage(); for (Iterator iterator = msg.getPropertyNames().iterator(); iterator.hasNext(); ) { String propertyKey = (String) iterator.next(); setProperty(propertyKey, msg.getProperty(propertyKey)); } } }
public void testOverwriteFlagEnabledByDefault() throws Exception { MessagePropertiesTransformer t = new MessagePropertiesTransformer(); Map add = new HashMap(); add.put("addedProperty", "overwrittenValue"); t.setAddProperties(add); t.setMuleContext(muleContext); MuleMessage msg = new DefaultMuleMessage("message", (Map) null); msg.setProperty("addedProperty", "originalValue"); MuleEventContext ctx = getTestEventContext(msg); // context clones message msg = ctx.getMessage(); DefaultMuleMessage transformed = (DefaultMuleMessage) t.transform(msg, null); assertSame(msg, transformed); assertEquals(msg.getUniqueId(), transformed.getUniqueId()); assertEquals(msg.getPayload(), transformed.getPayload()); // property values will be different assertEquals(msg.getPropertyNames(), transformed.getPropertyNames()); assertEquals("overwrittenValue", transformed.getProperty("addedProperty")); }
public void testProperties() throws Exception { // Will be treated as inbound properties Map props = new HashMap(); props.put("inbound-foo", "foo"); DefaultMessageAdapter adapter = new DefaultMessageAdapter(TEST_MESSAGE, props, null); MuleMessage message = new DefaultMuleMessage(adapter, muleContext); try { message.setProperty("inbound-bar", "bar", PropertyScope.INBOUND); fail("Inboiund scope should be read-only"); } catch (Exception e) { // Expected } message.setProperty("invocation-foo", "foo", PropertyScope.INVOCATION); // simulate an inbound session MuleSession session = getTestSession(getTestService(), muleContext); session.setProperty("session-foo", "foo"); MuleEvent event = new DefaultMuleEvent( message, getTestInboundEndpoint("test1", "test://test1?foo=bar&coo=car"), session, true); message = event.getMessage(); try { message.getPropertyNames(new PropertyScope("XXX", 5)); fail("Should throw exception, XXX not a valid scope"); } catch (Exception e) { // Exprected } assertEquals(0, message.getPropertyNames(PropertyScope.OUTBOUND).size()); // Endpoint props + any props added to the message assertEquals(3, message.getPropertyNames(PropertyScope.INVOCATION).size()); assertEquals("foo", message.getProperty("invocation-foo")); // defined on the endpoint assertEquals("bar", message.getProperty("foo")); assertEquals("car", message.getProperty("coo")); assertEquals("foo", message.getProperty("invocation-foo", PropertyScope.INVOCATION)); assertNull(message.getProperty("invocation-foo", PropertyScope.INBOUND)); assertNull(message.getProperty("invocation-foo", PropertyScope.OUTBOUND)); message.setProperty("outbound-foo", "foo", PropertyScope.OUTBOUND); assertEquals("foo", message.getProperty("outbound-foo", PropertyScope.OUTBOUND)); assertNull(message.getProperty("invocation-foo", PropertyScope.INBOUND)); // TODO MULE-3999. Should session properties be copied to the message? // message.setProperty("session-bar", "bar", PropertyScope.SESSION); // assertEquals(2, message.getPropertyNames(PropertyScope.SESSION).size()); // assertEquals("foo", message.getProperty("session-foo", PropertyScope.SESSION)); // assertEquals("bar", message.getProperty("session-bar", PropertyScope.SESSION)); // Session properties are available on the event assertEquals("foo", event.getProperty("session-foo")); }
protected HttpResponse createResponse(Object src, String encoding, MuleMessage msg) throws IOException, TransformerException { HttpResponse response = new HttpResponse(); Object tmp = msg.getProperty(HttpConnector.HTTP_STATUS_PROPERTY, PropertyScope.OUTBOUND); int status = HttpConstants.SC_OK; if (tmp != null) { status = Integer.valueOf(tmp.toString()); } else if (msg.getExceptionPayload() != null) { status = HttpConstants.SC_INTERNAL_SERVER_ERROR; } String version = (String) msg.getProperty(HttpConnector.HTTP_VERSION_PROPERTY, PropertyScope.OUTBOUND); if (version == null) { version = HttpConstants.HTTP11; } String date; synchronized (format) { date = format.format(new Date()); } String contentType = (String) msg.getProperty(HttpConstants.HEADER_CONTENT_TYPE, PropertyScope.OUTBOUND); if (contentType == null) { contentType = (String) msg.getProperty(HttpConstants.HEADER_CONTENT_TYPE, PropertyScope.INVOCATION); } // MULE-4047 Don't explicitly set the content-type to a default value here, // otherwise any settings on the servlet/transport will be happily ignored. // if (contentType == null) // { // contentType = HttpConstants.DEFAULT_CONTENT_TYPE; // // if (encoding != null) // { // contentType += "; charset=" + encoding; // } // logger.warn("Content-Type was not set, defaulting to: " + contentType); // } response.setStatusLine(HttpVersion.parse(version), status); if (contentType != null) { response.setHeader(new Header(HttpConstants.HEADER_CONTENT_TYPE, contentType)); } response.setHeader(new Header(HttpConstants.HEADER_DATE, date)); response.setHeader(new Header(HttpConstants.HEADER_SERVER, server)); if (msg.getProperty(HttpConstants.HEADER_EXPIRES) == null) { response.setHeader(new Header(HttpConstants.HEADER_EXPIRES, date)); } String etag = (String) msg.getProperty(HttpConstants.HEADER_ETAG, PropertyScope.OUTBOUND); if (etag != null) { response.setHeader(new Header(HttpConstants.HEADER_ETAG, etag)); } response.setFallbackCharset(encoding); Collection headerNames = HttpConstants.RESPONSE_HEADER_NAMES.values(); String headerName, value; for (Iterator iterator = headerNames.iterator(); iterator.hasNext(); ) { headerName = (String) iterator.next(); value = msg.getStringProperty(headerName, null); if (value != null) { response.setHeader(new Header(headerName, value)); } } // TODO: This is the legacy way of setting custom headers and can be removed in 3.0 // Custom responseHeaderNames Map customHeaders = (Map) msg.getProperty(HttpConnector.HTTP_CUSTOM_HEADERS_MAP_PROPERTY); if (customHeaders != null) { Map.Entry entry; for (Iterator iterator = customHeaders.entrySet().iterator(); iterator.hasNext(); ) { entry = (Map.Entry) iterator.next(); if (entry.getValue() != null) { response.setHeader(new Header(entry.getKey().toString(), entry.getValue().toString())); } } } // attach the outbound prorperties to the message Object v; for (Iterator iterator = msg.getPropertyNames(PropertyScope.OUTBOUND).iterator(); iterator.hasNext(); ) { headerName = (String) iterator.next(); v = msg.getProperty(headerName, PropertyScope.OUTBOUND); if (v != null) { response.setHeader(new Header(headerName, v.toString())); } } // Mule properties String user = msg.getStringProperty(MuleProperties.MULE_USER_PROPERTY, null); if (user != null) { response.setHeader( new Header(CUSTOM_HEADER_PREFIX + MuleProperties.MULE_USER_PROPERTY, user)); } if (msg.getCorrelationId() != null) { response.setHeader( new Header( CUSTOM_HEADER_PREFIX + MuleProperties.MULE_CORRELATION_ID_PROPERTY, msg.getCorrelationId())); response.setHeader( new Header( CUSTOM_HEADER_PREFIX + MuleProperties.MULE_CORRELATION_GROUP_SIZE_PROPERTY, String.valueOf(msg.getCorrelationGroupSize()))); response.setHeader( new Header( CUSTOM_HEADER_PREFIX + MuleProperties.MULE_CORRELATION_SEQUENCE_PROPERTY, String.valueOf(msg.getCorrelationSequence()))); } if (msg.getReplyTo() != null) { response.setHeader( new Header( CUSTOM_HEADER_PREFIX + MuleProperties.MULE_REPLY_TO_PROPERTY, msg.getReplyTo().toString())); } response.setBody(msg); return response; }