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   */
20  package org.apache.directory.api.ldap.extras.extended.ads_impl.whoAmI;
21  
22  
23  import java.nio.ByteBuffer;
24  
25  import org.apache.directory.api.asn1.DecoderException;
26  import org.apache.directory.api.asn1.EncoderException;
27  import org.apache.directory.api.asn1.ber.tlv.BerValue;
28  import org.apache.directory.api.asn1.ber.tlv.TLV;
29  import org.apache.directory.api.i18n.I18n;
30  import org.apache.directory.api.ldap.codec.api.ExtendedResponseDecorator;
31  import org.apache.directory.api.ldap.codec.api.LdapApiService;
32  import org.apache.directory.api.ldap.extras.extended.whoAmI.WhoAmIResponse;
33  import org.apache.directory.api.ldap.extras.extended.whoAmI.WhoAmIResponseImpl;
34  import org.apache.directory.api.ldap.model.name.Dn;
35  import org.slf4j.Logger;
36  import org.slf4j.LoggerFactory;
37  
38  
39  /**
40   * A Decorator for WhoAmIResponse extended request.
41   *
42   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
43   */
44  public class WhoAmIResponseDecorator extends ExtendedResponseDecorator<WhoAmIResponse>
45      implements WhoAmIResponse
46  {
47      private static final Logger LOG = LoggerFactory.getLogger( WhoAmIResponseDecorator.class );
48  
49      private WhoAmIResponse whoAmIResponse;
50  
51      public WhoAmIResponseDecorator( LdapApiService codec, WhoAmIResponse decoratedMessage )
52      {
53          super( codec, decoratedMessage );
54          whoAmIResponse = decoratedMessage;
55      }
56  
57  
58      /**
59       * {@inheritDoc}
60       */
61      @Override
62      public void setResponseValue( byte[] responseValue )
63      {
64          WhoAmIResponseDecoder decoder = new WhoAmIResponseDecoder();
65  
66          try
67          {
68              whoAmIResponse = decoder.decode( responseValue );
69  
70              if ( responseValue != null )
71              {
72                  this.responseValue = new byte[responseValue.length];
73                  System.arraycopy( responseValue, 0, this.responseValue, 0, responseValue.length );
74              }
75              else
76              {
77                  this.responseValue = null;
78              }
79          }
80          catch ( DecoderException e )
81          {
82              LOG.error( I18n.err( I18n.ERR_04165 ), e );
83              throw new RuntimeException( e );
84          }
85      }
86  
87  
88      /**
89       * {@inheritDoc}
90       */
91      @Override
92      public byte[] getResponseValue()
93      {
94          if ( responseValue == null )
95          {
96              try
97              {
98                  responseValue = encodeInternal().array();
99  
100                 if ( responseValue == null )
101                 {
102                     return null;
103                 }
104             }
105             catch ( EncoderException e )
106             {
107                 LOG.error( I18n.err( I18n.ERR_04167 ), e );
108                 throw new RuntimeException( e );
109             }
110         }
111 
112         return responseValue;
113     }
114 
115 
116     /**
117      * {@inheritDoc}
118      */
119     public byte[] getAuthzId()
120     {
121         return getDecorated().getAuthzId();
122     }
123 
124 
125     /**
126      * {@inheritDoc}
127      */
128     public void setAuthzId( byte[] authzId )
129     {
130         ( ( WhoAmIResponseImpl ) getDecorated() ).setAuthzId( authzId );
131     }
132 
133 
134     /**
135      * Set the userId
136      */
137     /* no qualifier*/ void setUserId( String userId )
138     {
139         ((WhoAmIResponseImpl)whoAmIResponse).setUserId( userId );
140     }
141 
142 
143     /**
144      * Set the DnId
145      */
146     /* no qualifier*/ void setDn( Dn dn )
147     {
148         ((WhoAmIResponseImpl)whoAmIResponse).setDn( dn );
149     }
150 
151 
152     /**
153      * {@inheritDoc}
154      */
155     public boolean isDnAuthzId()
156     {
157         return whoAmIResponse.isDnAuthzId();
158     }
159 
160 
161     /**
162      * {@inheritDoc}
163      */
164     public boolean isUserAuthzId()
165     {
166         return whoAmIResponse.isUserAuthzId();
167     }
168 
169 
170     /**
171      * {@inheritDoc}
172      */
173     public String getAuthzIdString()
174     {
175         return whoAmIResponse.getAuthzIdString();
176     }
177 
178 
179     /**
180      * {@inheritDoc}
181      */
182     public String getUserId()
183     {
184         return whoAmIResponse.getUserId();
185     }
186 
187 
188     /**
189      * {@inheritDoc}
190      */
191     public Dn getDn()
192     {
193         return whoAmIResponse.getDn();
194     }
195 
196 
197     /**
198      * Overload the parent's getResponseName method, as the WhoAmI response should not
199      * contain the responseName.
200      */
201     public String getResponseName()
202     {
203         return null;
204     }
205 
206 
207     /**
208      * Compute the WhoAmIResponse extended operation length
209      * <pre>
210      * 0x04 L1 authzId
211      * </pre>
212      */
213     /* no qualifier */ int computeLengthInternal()
214     {
215         if ( whoAmIResponse.getAuthzId() != null )
216         {
217             return 1 + TLV.getNbBytes( whoAmIResponse.getAuthzId().length ) + 
218                 whoAmIResponse.getAuthzId().length;
219         }
220         else
221         {
222             return 1 + 1;
223         }
224     }
225 
226 
227     /**
228      * Encodes the WhoAmIResponse extended operation.
229      * 
230      * @return A ByteBuffer that contains the encoded PDU
231      * @throws org.apache.directory.api.asn1.EncoderException If anything goes wrong.
232      */
233     /* no qualifier */ ByteBuffer encodeInternal() throws EncoderException
234     {
235         ByteBuffer bb = ByteBuffer.allocate( computeLengthInternal() );
236 
237         BerValue.encode( bb, whoAmIResponse.getAuthzId() );
238     
239         return bb;
240     }
241 }