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      @Override
56      public String getAuthzId()
57      {
58          return authzId;
59      }
60  
61  
62      /**
63       * The authzId syntax is given by the RFC 2829 :
64       * 
65       * <pre>
66       * authzId    = dnAuthzId / uAuthzId / &lt;empty&gt;
67       * dnAuthzId  = "dn:" dn
68       * dn         = utf8string
69       * uAuthzId   = "u:" userid
70       * userid     = utf8string
71       * </pre>
72       * @param authzId the authzId to set
73       */
74      @Override
75      public void setAuthzId( String authzId )
76      {
77          // We should have a valid authzId
78          if ( authzId == null )
79          {
80              throw new RuntimeException( "Invalid proxied authz value : cannot be null" );
81          }
82  
83          if ( !Strings.isEmpty( authzId ) )
84          {
85              String lowercaseAuthzId = Strings.toLowerCaseAscii( authzId );
86  
87              if ( lowercaseAuthzId.startsWith( "dn:" ) )
88              {
89                  String dn = authzId.substring( 3 );
90  
91                  if ( !Dn.isValid( dn ) )
92                  {
93                      throw new RuntimeException( "Invalid proxied authz value : the DN is not valid" );
94                  }
95              }
96              else if ( !lowercaseAuthzId.startsWith( "u:" ) )
97              {
98                  throw new RuntimeException( "Invalid proxied authz value : should start with 'dn:' or 'u:'" );
99              }
100         }
101 
102         this.authzId = authzId;
103     }
104 
105 
106     /**
107      * @see Object#hashCode()
108      */
109     @Override
110     public int hashCode()
111     {
112         int h = super.hashCode();
113 
114         if ( authzId != null )
115         {
116             h = h * 37 + authzId.hashCode();
117         }
118 
119         return h;
120     }
121 
122 
123     /**
124      * @see Object#equals(Object)
125      */
126     @Override
127     public boolean equals( Object o )
128     {
129         if ( !super.equals( o ) )
130         {
131             return false;
132         }
133 
134         ProxiedAuthz otherControl = ( ProxiedAuthz ) o;
135 
136         return ( authzId == otherControl.getAuthzId() )
137             || ( ( authzId != null ) && authzId.equals( otherControl.getAuthzId() ) );
138     }
139 
140 
141     /**
142      * Return a String representing this PagedSearchControl.
143      */
144     @Override
145     public String toString()
146     {
147         StringBuilder sb = new StringBuilder();
148 
149         sb.append( "    Proxied Authz Control\n" );
150         sb.append( "        oid : " ).append( getOid() ).append( '\n' );
151         sb.append( "        critical : " ).append( isCritical() ).append( '\n' );
152         sb.append( "        authzid   : '" ).append( authzId ).append( "'\n" );
153 
154         return sb.toString();
155     }
156 }