@Override
  public Object transformMessage(final MuleMessage message, final String outputEncoding)
      throws TransformerException {

    BaseNodeRq nodeRq = null;

    // create the context.
    TransformContext context = new TransformContext();
    message.setProperty("txContext", context, PropertyScope.SESSION);

    String contentType = message.getInboundProperty("Content-Type");
    if (contentType != null && contentType.contains("multipart/form-data")) {

      // transform the multipart request to node
      Node node = httpMultipartTransformer.transformMessage(message);

      // run the transform.
      nodeRq = this.httpRqToNodeRq.transform(context, node);

    } else if (message.getProperty("http.request", PropertyScope.INBOUND) != null) {

      // adapt the http request
      String muleQueryString = (String) message.getProperty("http.request", PropertyScope.INBOUND);
      HttpRequestAdapter httpRequest = new HttpRequestAdapter(muleQueryString);

      // run the transform.
      nodeRq = this.httpRqToNodeRq.transform(context, httpRequest);
    }

    return nodeRq;
  }
Esempio n. 2
0
  /**
   * Packages a mule event for the current request
   *
   * @param message the event payload
   * @param uri the destination endpointUri
   * @param synchronous whether the event will be synchronously processed
   * @return the MuleEvent
   * @throws MuleException in case of Mule error
   */
  protected MuleEvent getEvent(MuleMessage message, String uri, boolean synchronous)
      throws MuleException {
    ImmutableEndpoint endpoint =
        manager.getRegistry().lookupEndpointFactory().getOutboundEndpoint(uri);
    // Connector connector = endpoint.getConnector();

    //        if (!connector.isStarted() && manager.isStarted())
    //        {
    //            connector.start();
    //        }

    try {
      MuleSession session =
          new DefaultMuleSession(
              message, ((AbstractConnector) endpoint.getConnector()).getSessionHandler());

      if (credentials != null) {
        message.setProperty(MuleProperties.MULE_USER_PROPERTY, "Plain " + credentials.getToken());
      }

      return new DefaultMuleEvent(message, endpoint, session, synchronous);
    } catch (Exception e) {
      throw new DispatchException(
          CoreMessages.failedToCreate("Client event"), message, endpoint, e);
    }
  }
Esempio n. 3
0
 // @Override
 protected MuleMessage handleUnacceptedFilter(MuleMessage message) {
   if (logger.isDebugEnabled()) {
     logger.debug(
         "Message request '"
             + message.getProperty(HttpConnector.HTTP_REQUEST_PROPERTY)
             + "' is being rejected since it does not match the filter on this endpoint: "
             + endpoint);
   }
   message.setProperty(
       HttpConnector.HTTP_STATUS_PROPERTY, String.valueOf(HttpConstants.SC_NOT_ACCEPTABLE));
   return message;
 }
Esempio n. 4
0
  @Test
  public void testPropertiesCaseWithMessageCopy() throws Exception {
    // Creates a MPC implicitly
    MuleMessage msg = new DefaultMuleMessage("test", muleContext);

    msg.setProperty("FOO", "BAR");
    assertEquals("BAR", msg.getProperty("foo"));

    msg.setProperty("DOO", "DAR", PropertyScope.INVOCATION);

    // Look in all scopes
    assertEquals("DAR", msg.getProperty("doo"));

    // Look in specific scope
    assertEquals("DAR", msg.getProperty("doO", PropertyScope.INVOCATION));

    // Not found using other specific scopes
    assertNull(msg.getProperty("doo", PropertyScope.INBOUND));
    assertNull(msg.getProperty("doo", PropertyScope.OUTBOUND));
    assertNull(msg.getProperty("doo", PropertyScope.SESSION));

    // This will invoke the copy method on the MPC, want to make sure the copy function behaves as
    // expected
    MuleMessage copy = new DefaultMuleMessage("test copy", msg, muleContext);

    assertEquals("BAR", copy.getProperty("foo"));

    // Look in all scopes
    assertEquals("DAR", copy.getProperty("doo"));

    // Look in specific scope
    assertEquals("DAR", copy.getProperty("doO", PropertyScope.INVOCATION));

    // Not found using other specific scopes
    assertNull(copy.getProperty("doo", PropertyScope.INBOUND));
    assertNull(copy.getProperty("doo", PropertyScope.OUTBOUND));
    assertNull(copy.getProperty("doo", PropertyScope.SESSION));
  }
 @Override
 public Object transformMessage(MuleMessage message, String outputEncoding)
     throws TransformerException {
   Object keyValue = identifierEvaluator.resolveValue(message);
   String key = (keyValue == null ? null : keyValue.toString());
   if (key == null) {
     logger.error("Setting Null variable keys is not supported, this entry is being ignored");
   } else {
     Object value = valueEvaluator.resolveValue(message);
     if (value == null) {
       logger.info(
           MessageFormat.format(
               "Variable with key \"{0}\", not found on message using \"{1}\". Since the value was marked optional, nothing was set on the message for this variable",
               key, valueEvaluator.getRawValue()));
     } else {
       message.setProperty(key, value, getScope());
     }
   }
   return message;
 }
Esempio n. 6
0
  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"));
  }
Esempio n. 7
0
 public Object invoke(String s) {
   MuleMessage result = binding.process(s, new Integer(MAGIC_NUMBER));
   result.setProperty(PROCESSED, Boolean.TRUE);
   return result;
 }
Esempio n. 8
0
 protected void preRouteMessage(MuleMessage message) {
   message.setProperty(MuleProperties.MULE_REMOTE_CLIENT_ADDRESS, remoteClientAddress);
 }
Esempio n. 9
0
    protected HttpResponse doRequest(HttpRequest request, RequestLine requestLine)
        throws IOException, MuleException {
      Map headers = parseHeaders(request);

      // TODO Mule 2.0 generic way to set stream message adapter
      MessageAdapter adapter = buildStandardAdapter(request, headers);

      MuleMessage message = new DefaultMuleMessage(adapter);

      String path = (String) message.getProperty(HttpConnector.HTTP_REQUEST_PROPERTY);
      int i = path.indexOf('?');
      if (i > -1) {
        path = path.substring(0, i);
      }

      message.setProperty(HttpConnector.HTTP_REQUEST_PATH_PROPERTY, path);

      if (logger.isDebugEnabled()) {
        logger.debug(message.getProperty(HttpConnector.HTTP_REQUEST_PROPERTY));
      }

      message.setProperty(
          HttpConnector.HTTP_CONTEXT_PATH_PROPERTY,
          HttpConnector.normalizeUrl(endpoint.getEndpointURI().getPath()));

      // determine if the request path on this request denotes a different receiver
      MessageReceiver receiver = getTargetReceiver(message, endpoint);

      HttpResponse response;
      // the response only needs to be transformed explicitly if
      // A) the request was not served or B) a null result was returned
      if (receiver != null) {
        preRouteMessage(message);
        MuleMessage returnMessage = receiver.routeMessage(message, endpoint.isSynchronous(), null);

        Object tempResponse;
        if (returnMessage != null) {
          tempResponse = returnMessage.getPayload();
        } else {
          tempResponse = NullPayload.getInstance();
        }
        // This removes the need for users to explicitly adding the response transformer
        // ObjectToHttpResponse in their config
        if (tempResponse instanceof HttpResponse) {
          response = (HttpResponse) tempResponse;
        } else {
          response = transformResponse(returnMessage);
        }

        response.disableKeepAlive(!((HttpConnector) connector).isKeepAlive());

        // Check if endpoint has a keep-alive property configured. Note the translation from
        // keep-alive in the schema to keepAlive here.
        boolean endpointOverride = Boolean.parseBoolean((String) endpoint.getProperty("keepAlive"));

        Header connectionHeader = request.getFirstHeader("Connection");
        if (connectionHeader != null) {
          String value = connectionHeader.getValue();
          if ("keep-alive".equalsIgnoreCase(value) && !endpointOverride) {
            response.setKeepAlive(true);
            Header header =
                new Header(
                    HttpConstants.HEADER_KEEP_ALIVE,
                    "timeout=" + ((HttpConnector) connector).getKeepAliveTimeout());
            response.addHeader(header);
          } else if ("close".equalsIgnoreCase(value)) {
            response.setKeepAlive(false);
          } else if (response.getHttpVersion().greaterEquals(HttpVersion.HTTP_1_1)) {
            response.setKeepAlive(true);
          } else {
            response.setKeepAlive(false);
          }
        }
      } else {
        response = buildFailureResponse(requestLine, message);
      }
      return response;
    }
Esempio n. 10
0
  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"));
  }