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.tck.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.security.AbstractAuthenticationFilter;
18  
19  public class TestSecurityFilter extends AbstractAuthenticationFilter
20  {
21      private boolean accept;
22      private boolean called;
23  
24      public static final String SECURITY_EXCEPTION_MESSAGE = "unauthorized!!";
25  
26      public TestSecurityFilter(boolean accept)
27      {
28          this.accept = accept;
29      }
30  
31      @Override
32      protected void authenticateInbound(MuleEvent event)
33          throws SecurityException, CryptoFailureException, SecurityProviderNotFoundException,
34          EncryptionStrategyNotFoundException, UnknownAuthenticationTypeException
35      {
36          called = true;
37          if (!accept)
38          {
39              throw new StaticMessageUnauthorisedException();
40          }
41      }
42  
43      @Override
44      protected void authenticateOutbound(MuleEvent event)
45          throws SecurityException, SecurityProviderNotFoundException, CryptoFailureException
46      {
47          if (!accept)
48          {
49              throw new StaticMessageUnauthorisedException();
50          }
51      }
52  
53      @Override
54      protected void doInitialise() throws InitialisationException
55      {
56      }
57  
58      public boolean wasCalled()
59      {
60          return called;
61      }
62  
63      public static class StaticMessageUnauthorisedException extends UnauthorisedException
64      {
65          public StaticMessageUnauthorisedException()
66          {
67              super(null);
68          }
69  
70          @Override
71          public String getLocalizedMessage()
72          {
73              return SECURITY_EXCEPTION_MESSAGE;
74          }
75      }
76  
77  }