View Javadoc

1   /*
2    * $Id: CustomSecurityFilter.java 22865 2011-09-05 17:23:45Z dfeist $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.test.integration.security;
12  
13  import org.mule.api.MuleEvent;
14  import org.mule.api.lifecycle.InitialisationException;
15  import org.mule.api.security.CryptoFailureException;
16  import org.mule.api.security.EncryptionStrategyNotFoundException;
17  import org.mule.api.security.SecurityException;
18  import org.mule.api.security.SecurityProviderNotFoundException;
19  import org.mule.api.security.UnauthorisedException;
20  import org.mule.api.security.UnknownAuthenticationTypeException;
21  import org.mule.config.i18n.CoreMessages;
22  import org.mule.security.AbstractEndpointSecurityFilter;
23  import org.mule.tck.FunctionalTestCase;
24  
25  public class CustomSecurityFilter extends AbstractEndpointSecurityFilter
26  {
27  
28      public CustomSecurityFilter()
29      {
30  
31      }
32  
33      @Override
34      protected void authenticateInbound(MuleEvent event)
35          throws SecurityException, CryptoFailureException, SecurityProviderNotFoundException,
36          EncryptionStrategyNotFoundException, UnknownAuthenticationTypeException
37      {
38          if (!isValid(event))
39          {
40              throw new UnauthorisedException(CoreMessages.authFailedForUser("a"));
41          }
42      }
43  
44      @Override
45      protected void authenticateOutbound(MuleEvent event)
46          throws SecurityException, SecurityProviderNotFoundException, CryptoFailureException
47      {
48          if (!isValid(event))
49          {
50              throw new UnauthorisedException(CoreMessages.authFailedForUser("a"));
51          }
52      }
53  
54      private boolean isValid(MuleEvent event)
55      {
56          try
57          {
58              return event.getMessage().getPayloadAsString().equals(FunctionalTestCase.TEST_MESSAGE);
59          }
60          catch (Exception e)
61          {
62              return false;
63          }
64      }
65  
66      @Override
67      protected void doInitialise() throws InitialisationException
68      {
69      }
70  
71  }