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  
52      /**
53       * Creates a new instance of WhoAmIResponseDecorator.
54       *
55       * @param codec The LDAP service instance
56       * @param decoratedMessage The decorated message
57       */
58      public WhoAmIResponseDecorator( LdapApiService codec, WhoAmIResponse decoratedMessage )
59      {
60          super( codec, decoratedMessage );
61          whoAmIResponse = decoratedMessage;
62      }
63  
64  
65      /**
66       * {@inheritDoc}
67       */
68      @Override
69      public void setResponseValue( byte[] responseValue )
70      {
71          WhoAmIResponseDecoder decoder = new WhoAmIResponseDecoder();
72  
73          try
74          {
75              if ( responseValue != null )
76              {
77                  whoAmIResponse = decoder.decode( responseValue );
78  
79                  this.responseValue = new byte[responseValue.length];
80                  System.arraycopy( responseValue, 0, this.responseValue, 0, responseValue.length );
81              }
82          }
83          catch ( DecoderException e )
84          {
85              LOG.error( I18n.err( I18n.ERR_04165 ), e );
86              throw new RuntimeException( e );
87          }
88      }
89  
90  
91      /**
92       * {@inheritDoc}
93       */
94      @Override
95      public byte[] getResponseValue()
96      {
97          if ( responseValue == null )
98          {
99              try
100             {
101                 responseValue = encodeInternal().array();
102             }
103             catch ( EncoderException e )
104             {
105                 LOG.error( I18n.err( I18n.ERR_04167 ), e );
106                 throw new RuntimeException( e );
107             }
108         }
109 
110         return responseValue;
111     }
112 
113 
114     /**
115      * {@inheritDoc}
116      */
117     @Override
118     public byte[] getAuthzId()
119     {
120         return getDecorated().getAuthzId();
121     }
122 
123 
124     /**
125      * {@inheritDoc}
126      */
127     @Override
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     @Override
156     public boolean isDnAuthzId()
157     {
158         return whoAmIResponse.isDnAuthzId();
159     }
160 
161 
162     /**
163      * {@inheritDoc}
164      */
165     @Override
166     public boolean isUserAuthzId()
167     {
168         return whoAmIResponse.isUserAuthzId();
169     }
170 
171 
172     /**
173      * {@inheritDoc}
174      */
175     @Override
176     public String getAuthzIdString()
177     {
178         return whoAmIResponse.getAuthzIdString();
179     }
180 
181 
182     /**
183      * {@inheritDoc}
184      */
185     @Override
186     public String getUserId()
187     {
188         return whoAmIResponse.getUserId();
189     }
190 
191 
192     /**
193      * {@inheritDoc}
194      */
195     @Override
196     public Dn getDn()
197     {
198         return whoAmIResponse.getDn();
199     }
200 
201 
202     /**
203      * Overload the parent's getResponseName method, as the WhoAmI response should not
204      * contain the responseName.
205      */
206     @Override
207     public String getResponseName()
208     {
209         return null;
210     }
211 
212 
213     /**
214      * Compute the WhoAmIResponse extended operation length
215      * <pre>
216      * 0x04 L1 authzId
217      * </pre>
218      */
219     /* no qualifier */int computeLengthInternal()
220     {
221         if ( whoAmIResponse.getAuthzId() != null )
222         {
223             return 1 + TLV.getNbBytes( whoAmIResponse.getAuthzId().length )
224                 + whoAmIResponse.getAuthzId().length;
225         }
226         else
227         {
228             return 1 + 1;
229         }
230     }
231 
232 
233     /**
234      * Encodes the WhoAmIResponse extended operation.
235      * 
236      * @return A ByteBuffer that contains the encoded PDU
237      * @throws org.apache.directory.api.asn1.EncoderException If anything goes wrong.
238      */
239     /* no qualifier */ByteBuffer encodeInternal() throws EncoderException
240     {
241         ByteBuffer bb = ByteBuffer.allocate( computeLengthInternal() );
242 
243         BerValue.encode( bb, whoAmIResponse.getAuthzId() );
244 
245         return bb;
246     }
247 }