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   * https://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.i18n.I18n;
23  import org.apache.directory.api.ldap.model.name.Dn;
24  import org.apache.directory.api.util.Strings;
25  
26  
27  /**
28   * Simple ProxiedAuthz implementation class.
29   *
30   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
31   * @version $Rev$, $Date$
32   */
33  public class ProxiedAuthzImpl extends AbstractControl implements ProxiedAuthz
34  {
35      /**
36       * The authzId used to authorize the user.
37       */
38      private String authzId;
39  
40  
41      /**
42       * Default constructor.
43       */
44      public ProxiedAuthzImpl()
45      {
46          super( OID );
47  
48          // The criticality must be true
49          setCritical( true );
50      }
51  
52  
53      /**
54       * @return the authzId
55       */
56      @Override
57      public String getAuthzId()
58      {
59          return authzId;
60      }
61  
62  
63      /**
64       * The authzId syntax is given by the RFC 2829 :
65       * 
66       * <pre>
67       * authzId    = dnAuthzId / uAuthzId / &lt;empty&gt;
68       * dnAuthzId  = "dn:" dn
69       * dn         = utf8string
70       * uAuthzId   = "u:" userid
71       * userid     = utf8string
72       * </pre>
73       * @param authzId the authzId to set
74       */
75      @Override
76      public void setAuthzId( String authzId )
77      {
78          // We should have a valid authzId
79          if ( authzId == null )
80          {
81              throw new RuntimeException( I18n.err( I18n.ERR_13511_INVALID_PROXIED_AUTHZ_NULL ) );
82          }
83  
84          if ( !Strings.isEmpty( authzId ) )
85          {
86              String lowercaseAuthzId = Strings.toLowerCaseAscii( authzId );
87  
88              if ( lowercaseAuthzId.startsWith( "dn:" ) )
89              {
90                  String dn = authzId.substring( 3 );
91  
92                  if ( !Dn.isValid( dn ) )
93                  {
94                      throw new RuntimeException( I18n.err( I18n.ERR_13512_INVALID_PROXIED_AUTHZ_BAD_DN ) );
95                  }
96              }
97              else if ( !lowercaseAuthzId.startsWith( "u:" ) )
98              {
99                  throw new RuntimeException( I18n.err( I18n.ERR_13513_INVALID_PROXIED_AUTHZ_NO_DN_OR_U ) );
100             }
101         }
102 
103         this.authzId = authzId;
104     }
105 
106 
107     /**
108      * @see Object#hashCode()
109      */
110     @Override
111     public int hashCode()
112     {
113         int h = super.hashCode();
114 
115         if ( authzId != null )
116         {
117             h = h * 37 + authzId.hashCode();
118         }
119 
120         return h;
121     }
122 
123 
124     /**
125      * @see Object#equals(Object)
126      */
127     @Override
128     public boolean equals( Object o )
129     {
130         if ( this == o )
131         {
132             return true;
133         }
134 
135         if ( !( o instanceof ProxiedAuthz ) )
136         {
137             return false;
138         }
139         
140         ProxiedAuthz otherControl = ( ProxiedAuthz ) o;
141 
142         return super.equals( o )
143             && ( ( authzId == otherControl.getAuthzId() ) || ( ( authzId != null ) && authzId.equals( otherControl.getAuthzId() ) ) );
144     }
145 
146 
147     /**
148      * Return a String representing this PagedSearchControl.
149      */
150     @Override
151     public String toString()
152     {
153         StringBuilder sb = new StringBuilder();
154 
155         sb.append( "    Proxied Authz Control\n" );
156         sb.append( "        oid : " ).append( getOid() ).append( '\n' );
157         sb.append( "        critical : " ).append( isCritical() ).append( '\n' );
158         sb.append( "        authzid   : '" ).append( authzId ).append( "'\n" );
159 
160         return sb.toString();
161     }
162 }