View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.directory.api.ldap.model.message.controls;
20  
21  
22  import org.apache.directory.api.ldap.model.name.Dn;
23  import org.apache.directory.api.util.Strings;
24  
25  
26  /**
27   * Simple ProxiedAuthz implementation class.
28   *
29   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
30   * @version $Rev$, $Date$
31   */
32  public class ProxiedAuthzImpl extends AbstractControl implements ProxiedAuthz
33  {
34      /**
35       * The authzId used to authorize the user.
36       */
37      private String authzId;
38  
39  
40      /**
41       * Default constructor.
42       */
43      public ProxiedAuthzImpl()
44      {
45          super( OID );
46  
47          // The criticality must be true
48          setCritical( true );
49      }
50  
51  
52      /**
53       * @return the authzId
54       */
55      public String getAuthzId()
56      {
57          return authzId;
58      }
59  
60  
61      /**
62       * The authzId syntax is given by the RFC 2829 :
63       * 
64       * <pre>
65       * authzId    = dnAuthzId / uAuthzId / <empty>
66       * dnAuthzId  = "dn:" dn
67       * dn         = utf8string
68       * uAuthzId   = "u:" userid
69       * userid     = utf8string
70       * </pre>
71       * @param authzId the authzId to set
72       */
73      public void setAuthzId( String authzId )
74      {
75          // We should have a valid authzId
76          if ( authzId == null )
77          {
78              throw new RuntimeException( "Invalid proxied authz value : cannot be null" );
79          }
80  
81          if ( !Strings.isEmpty( authzId ) )
82          {
83              String lowercaseAuthzId = Strings.toLowerCase( authzId );
84  
85              if ( lowercaseAuthzId.startsWith( "dn:" ) )
86              {
87                  String dn = authzId.substring( 3 );
88  
89                  if ( !Dn.isValid( dn ) )
90                  {
91                      throw new RuntimeException( "Invalid proxied authz value : the DN is not valid" );
92                  }
93              }
94              else if ( !lowercaseAuthzId.startsWith( "u:" ) )
95              {
96                  throw new RuntimeException( "Invalid proxied authz value : should start with 'dn:' or 'u:'" );
97              }
98          }
99  
100         this.authzId = authzId;
101     }
102 
103 
104     /**
105      * @see Object#hashCode()
106      */
107     @Override
108     public int hashCode()
109     {
110         int h = super.hashCode();
111 
112         if ( authzId != null )
113         {
114             h = h * 37 + authzId.hashCode();
115         }
116 
117         return h;
118     }
119 
120 
121     /**
122      * @see Object#equals(Object)
123      */
124     @Override
125     public boolean equals( Object o )
126     {
127         if ( !super.equals( o ) )
128         {
129             return false;
130         }
131 
132         ProxiedAuthz otherControl = ( ProxiedAuthz ) o;
133 
134         return ( authzId == otherControl.getAuthzId() )
135             || ( ( authzId != null ) && authzId.equals( otherControl.getAuthzId() ) );
136     }
137 
138 
139     /**
140      * Return a String representing this PagedSearchControl.
141      */
142     public String toString()
143     {
144         StringBuffer sb = new StringBuffer();
145 
146         sb.append( "    Proxied Authz Control\n" );
147         sb.append( "        oid : " ).append( getOid() ).append( '\n' );
148         sb.append( "        critical : " ).append( isCritical() ).append( '\n' );
149         sb.append( "        authzid   : '" ).append( authzId ).append( "'\n" );
150 
151         return sb.toString();
152     }
153 }