View Javadoc

1   /*
2    * $Id: TestSecurityFilter.java 20320 2010-11-24 15:03:31Z dfeist $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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.tck.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.security.AbstractEndpointSecurityFilter;
22  
23  public class TestSecurityFilter extends AbstractEndpointSecurityFilter
24  {
25      private boolean accept;
26      private boolean called;
27  
28      public static final String SECURITY_EXCEPTION_MESSAGE = "unauthorized!!";
29  
30      public TestSecurityFilter(boolean accept)
31      {
32          this.accept = accept;
33      }
34  
35      @Override
36      protected void authenticateInbound(MuleEvent event)
37          throws SecurityException, CryptoFailureException, SecurityProviderNotFoundException,
38          EncryptionStrategyNotFoundException, UnknownAuthenticationTypeException
39      {
40          called = true;
41          if (!accept)
42          {
43              throw new StaticMessageUnauthorisedException();
44          }
45      }
46  
47      @Override
48      protected void authenticateOutbound(MuleEvent event)
49          throws SecurityException, SecurityProviderNotFoundException, CryptoFailureException
50      {
51          if (!accept)
52          {
53              throw new StaticMessageUnauthorisedException();
54          }
55      }
56  
57      @Override
58      protected void doInitialise() throws InitialisationException
59      {
60      }
61  
62      public boolean wasCalled()
63      {
64          return called;
65      }
66  
67      public static class StaticMessageUnauthorisedException extends UnauthorisedException
68      {
69          public StaticMessageUnauthorisedException()
70          {
71              super(null);
72          }
73  
74          @Override
75          public String getLocalizedMessage()
76          {
77              return SECURITY_EXCEPTION_MESSAGE;
78          }
79      }
80  
81  }