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