@Override
  protected final void authenticateInbound(MuleEvent event)
      throws SecurityException, CryptoFailureException, EncryptionStrategyNotFoundException,
          UnknownAuthenticationTypeException {
    String userHeader = (String) getCredentialsAccessor().getCredentials(event);
    if (userHeader == null) {
      throw new CredentialsNotSetException(event, event.getSession().getSecurityContext(), this);
    }

    Credentials user = new MuleCredentials(userHeader, getSecurityManager());

    Authentication authentication;
    try {
      authentication =
          getSecurityManager().authenticate(new DefaultMuleAuthentication(user, event));
    } catch (Exception e) {
      // Authentication failed
      if (logger.isDebugEnabled()) {
        logger.debug(
            "Authentication request for user: "******" failed: " + e.toString());
      }
      throw new UnauthorisedException(CoreMessages.authFailedForUser(user.getUsername()), event, e);
    }

    // Authentication success
    if (logger.isDebugEnabled()) {
      logger.debug("Authentication success: " + authentication.toString());
    }

    SecurityContext context = getSecurityManager().createSecurityContext(authentication);
    context.setAuthentication(authentication);
    event.getSession().setSecurityContext(context);
  }
Example #2
0
 // This method is used when the service invoked synchronously. It should really
 // be used independantly of if the service is invoked synchronously when we are
 // using an out-only outbound message exchange pattern
 protected MuleMessage sendToOutboundRouter(MuleEvent event, MuleMessage result)
     throws MessagingException {
   if (event.isStopFurtherProcessing()) {
     logger.debug(
         "MuleEvent stop further processing has been set, no outbound routing will be performed.");
   }
   if (result != null && !event.isStopFurtherProcessing()) {
     if (getOutboundRouter().hasEndpoints()) {
       // Here we need to use a copy of the message instance because there
       // is an inbound response so that transformers executed as part of
       // the outbound phase do not affect the inbound response. MULE-3307
       MuleMessage outboundReturnMessage =
           getOutboundRouter()
               .route(new DefaultMuleMessage(result), event.getSession(), event.isSynchronous());
       if (outboundReturnMessage != null) {
         result = outboundReturnMessage;
       }
     } else {
       logger.debug(
           "Outbound router on service '"
               + getName()
               + "' doesn't have any endpoints configured.");
     }
   }
   return result;
 }
Example #3
0
 /**
  * Dispatches an event asynchronously to a endpointUri via a mule server. the Url determines where
  * to dispathc the event to, this can be in the form of
  *
  * @param url the Mule url used to determine the destination and transport of the message
  * @param payload the object that is the payload of the event
  * @param messageProperties any properties to be associated with the payload. In the case of Jms
  *     you could set the JMSReplyTo property in these properties.
  * @throws org.mule.api.MuleException
  */
 public void dispatch(String url, Object payload, Map messageProperties) throws MuleException {
   MuleMessage message = new DefaultMuleMessage(payload, messageProperties);
   MuleEvent event = getEvent(message, url, false);
   try {
     event.getSession().dispatchEvent(event);
   } catch (MuleException e) {
     throw e;
   } catch (Exception e) {
     throw new DispatchException(
         ClientMessages.failedToDispatchClientEvent(), event.getMessage(), event.getEndpoint(), e);
   }
 }
Example #4
0
 // This method is used when the service invoked asynchronously. It should really
 // be used independantly of if the service is invoked asynchronously when we are
 // using an out-in or out-optional-in outbound message exchange pattern
 protected void dispatchToOutboundRouter(MuleEvent event, MuleMessage result)
     throws MessagingException {
   if (event.isStopFurtherProcessing()) {
     logger.debug(
         "MuleEvent stop further processing has been set, no outbound routing will be performed.");
   }
   if (result != null && !event.isStopFurtherProcessing()) {
     if (getOutboundRouter().hasEndpoints()) {
       // Here we can use the same message instance because there is no inbound response.
       getOutboundRouter().route(result, event.getSession(), event.isSynchronous());
     }
   }
 }
Example #5
0
  @Test
  public void testSessionScope() throws Exception {
    MuleEvent e = getTestEvent("testing");
    e.getSession().setProperty("SESSION_PROP", "Value1");
    RequestContext.setEvent(e);

    MessagePropertiesContext mpc = new MessagePropertiesContext();

    assertEquals("Value1", mpc.getProperty("SESSION_PROP", PropertyScope.SESSION));
    // test case insensitivity
    assertEquals("Value1", mpc.getProperty("SESSION_prop", PropertyScope.SESSION));
    assertNull(mpc.getProperty("SESSION_X", PropertyScope.SESSION));
  }
Example #6
0
  /**
   * Sends an object (payload) synchronous to the given url and returns a MuleMessage response back.
   *
   * @param url the Mule url used to determine the destination and transport of the message
   * @param payload the object that is the payload of the event
   * @param messageProperties any properties to be associated with the payload. In the case of Jms
   *     you could set the JMSReplyTo property in these properties.
   * @return a umomessage response.
   * @throws org.mule.api.MuleException
   */
  public MuleMessage send(String url, Object payload, Map messageProperties) throws MuleException {
    MuleMessage message = new DefaultMuleMessage(payload, messageProperties);
    MuleEvent event = getEvent(message, url, true);

    MuleMessage response;
    try {
      response = event.getSession().sendEvent(event);
    } catch (MuleException e) {
      throw e;
    } catch (Exception e) {
      throw new DispatchException(
          ClientMessages.failedToDispatchClientEvent(), event.getMessage(), event.getEndpoint(), e);
    }
    return response;
  }
Example #7
0
 public void run() {
   try {
     event = OptimizedRequestContext.criticalSetEvent(event);
     Object replyTo = event.getMessage().getReplyTo();
     ReplyToHandler replyToHandler =
         getReplyToHandler(event.getMessage(), (InboundEndpoint) event.getEndpoint());
     MuleMessage result = invokeComponent(event);
     dispatchToOutboundRouter(event, result);
     processReplyTo(event, result, replyToHandler, replyTo);
   } catch (Exception e) {
     event.getSession().setValid(false);
     if (e instanceof MessagingException) {
       handleException(e);
     } else {
       handleException(
           new MessagingException(
               CoreMessages.eventProcessingFailedFor(getName()), event.getMessage(), e));
     }
   }
 }
Example #8
0
  @Test
  public void testPropertyScopeOrder() throws Exception {
    MuleEvent e = getTestEvent("testing");
    e.getSession().setProperty("Prop", "session");
    RequestContext.setEvent(e);

    MessagePropertiesContext mpc = new MessagePropertiesContext();
    // Note that we cannot write to the Inbound scope, its read only
    mpc.setProperty("Prop", "invocation", PropertyScope.INVOCATION);
    mpc.setProperty("Prop", "outbound", PropertyScope.OUTBOUND);

    assertEquals("outbound", mpc.getProperty("Prop"));
    mpc.removeProperty("Prop", PropertyScope.OUTBOUND);

    assertEquals("invocation", mpc.getProperty("Prop"));
    mpc.removeProperty("Prop", PropertyScope.INVOCATION);

    assertEquals("session", mpc.getProperty("Prop"));
    assertNull(mpc.getProperty("Prop", PropertyScope.INBOUND));
    assertNull(mpc.getProperty("Prop", PropertyScope.INVOCATION));
    assertNull(mpc.getProperty("Prop", PropertyScope.OUTBOUND));
  }
Example #9
0
 protected MuleMessage doSend(MuleEvent event) throws MuleException {
   MuleMessage result = null;
   try {
     if (logger.isDebugEnabled()) {
       logger.debug(
           MessageFormat.format("{0} : got proxy for {1} = {2}", this, event.getId(), component));
     }
     Object replyTo = event.getMessage().getReplyTo();
     ReplyToHandler replyToHandler =
         getReplyToHandler(event.getMessage(), (InboundEndpoint) event.getEndpoint());
     result = invokeComponent(event);
     result = sendToOutboundRouter(event, result);
     result = processAsyncReplyRouter(result);
     processReplyTo(event, result, replyToHandler, replyTo);
   } catch (Exception e) {
     event.getSession().setValid(false);
     if (e instanceof MessagingException) {
       handleException(e);
     } else {
       handleException(
           new MessagingException(
               CoreMessages.eventProcessingFailedFor(getName()), event.getMessage(), e));
     }
     if (result == null) {
       // important that we pull event from request context here as it may
       // have been modified
       // (necessary to avoid scribbling between threads)
       result =
           new DefaultMuleMessage(
               NullPayload.getInstance(), RequestContext.getEvent().getMessage());
     }
     ExceptionPayload exceptionPayload = result.getExceptionPayload();
     if (exceptionPayload == null) {
       exceptionPayload = new DefaultExceptionPayload(e);
     }
     result.setExceptionPayload(exceptionPayload);
   }
   return result;
 }
Example #10
0
 // This method is used when the service invoked synchronously. It should really
 // be used independantly of if the service is invoked synchronously when we are
 // using an out-only outbound message exchange pattern
 protected MuleMessage sendToOutboundRouter(MuleEvent event, MuleMessage result)
     throws MessagingException {
   if (event.isStopFurtherProcessing()) {
     logger.debug(
         "MuleEvent stop further processing has been set, no outbound routing will be performed.");
   }
   if (result != null
       && !event.isStopFurtherProcessing()
       && !(result.getPayload() instanceof NullPayload)) {
     if (getOutboundRouter().hasEndpoints()) {
       // Here we need to use a copy of the message instance because there
       // is an inbound response so that transformers executed as part of
       // the outbound phase do not affect the inbound response. MULE-3307
       if (stats.isEnabled()) {
         stats.incSentEventSync();
       }
       MuleMessage outboundReturnMessage =
           getOutboundRouter()
               .route(
                   new DefaultMuleMessage(result.getPayload(), result, muleContext),
                   event.getSession());
       if (outboundReturnMessage != null) {
         result = outboundReturnMessage;
       } else if (getComponent() instanceof PassThroughComponent) {
         // If there was no component, then we really want to return the response from
         // the outbound router as the actual payload - even if it's null.
         return new DefaultMuleMessage(NullPayload.getInstance(), result, muleContext);
       }
     } else {
       logger.debug(
           "Outbound router on service '"
               + getName()
               + "' doesn't have any endpoints configured.");
     }
   }
   return result;
 }
  @Override
  protected void authenticateOutbound(MuleEvent event)
      throws SecurityException, SecurityProviderNotFoundException, CryptoFailureException {
    SecurityContext securityContext = event.getSession().getSecurityContext();
    if (securityContext == null) {
      if (isAuthenticate()) {
        throw new UnauthorisedException(event, securityContext, this);
      } else {
        return;
      }
    }

    Authentication auth = securityContext.getAuthentication();
    if (isAuthenticate()) {
      auth = getSecurityManager().authenticate(auth);
      if (logger.isDebugEnabled()) {
        logger.debug("Authentication success: " + auth.toString());
      }
    }

    String token = auth.getCredentials().toString();
    String header = new String(strategy.encrypt(token.getBytes(), null));
    getCredentialsAccessor().setCredentials(event, header);
  }