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.codec.controls.proxiedauthz;
21  
22  
23  import java.nio.ByteBuffer;
24  
25  import org.apache.directory.api.asn1.Asn1Object;
26  import org.apache.directory.api.asn1.DecoderException;
27  import org.apache.directory.api.asn1.EncoderException;
28  import org.apache.directory.api.asn1.ber.Asn1Decoder;
29  import org.apache.directory.api.asn1.ber.tlv.BerValue;
30  import org.apache.directory.api.i18n.I18n;
31  import org.apache.directory.api.ldap.codec.api.ControlDecorator;
32  import org.apache.directory.api.ldap.codec.api.LdapApiService;
33  import org.apache.directory.api.ldap.model.message.controls.ProxiedAuthz;
34  import org.apache.directory.api.ldap.model.message.controls.ProxiedAuthzImpl;
35  import org.apache.directory.api.util.Strings;
36  
37  
38  /**
39   * An ProxiedAuthz implementation, that wraps and decorates the Control with codec
40   * specific functionality.
41   *
42   *
43   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
44   */
45  public class ProxiedAuthzDecorator extends ControlDecorator<ProxiedAuthz> implements ProxiedAuthz
46  {
47      /** A temporary storage for the authzId */
48      private byte[] authzIdBytes = null;
49  
50      /** An instance of this decoder */
51      private static final Asn1Decoder decoder = new Asn1Decoder();
52  
53  
54      /**
55       * Creates a new instance of ProxiedAuthzDecoder wrapping a newly created
56       * ProxiedAuthz Control object.
57       */
58      public ProxiedAuthzDecorator( LdapApiService codec )
59      {
60          super( codec, new ProxiedAuthzImpl() );
61      }
62  
63  
64      /**
65       * Creates a new instance of ProxiedAuthzDecorator wrapping the supplied
66       * ProxiedAuthz Control.
67       *
68       * @param control The ProxiedAuthz Control to be decorated.
69       */
70      public ProxiedAuthzDecorator( LdapApiService codec, ProxiedAuthz control )
71      {
72          super( codec, control );
73      }
74  
75  
76      /**
77       * Internally used to not have to cast the decorated Control.
78       *
79       * @return the decorated Control.
80       */
81      private ProxiedAuthz getProxiedAuthz()
82      {
83          return ( ProxiedAuthz ) getDecorated();
84      }
85  
86  
87      /**
88       * Compute the ProxiedAuthzControl length 
89       * 
90       *  0x04 L1 authzId] 
91       */
92      public int computeLength()
93      {
94          int valueLength = 0;
95  
96          if ( getAuthzId() != null )
97          {
98              authzIdBytes = Strings.getBytesUtf8( getAuthzId() );
99              valueLength = authzIdBytes.length;
100         }
101 
102         return valueLength;
103     }
104 
105 
106     /**
107      * Encodes the ProxiedAuthz control.
108      * 
109      * @param buffer The encoded sink
110      * @return A ByteBuffer that contains the encoded PDU
111      * @throws EncoderException If anything goes wrong.
112      */
113     public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
114     {
115         if ( buffer == null )
116         {
117             throw new EncoderException( I18n.err( I18n.ERR_04023 ) );
118         }
119 
120         if ( getAuthzId() != null )
121         {
122             buffer.put( authzIdBytes );
123         }
124 
125         return buffer;
126     }
127 
128 
129     /**
130      * {@inheritDoc}
131      */
132     public byte[] getValue()
133     {
134         if ( value == null )
135         {
136             try
137             {
138                 computeLength();
139                 ByteBuffer buffer = ByteBuffer.allocate( valueLength );
140 
141                 if ( authzIdBytes != null )
142                 {
143                     BerValue.encode( buffer, authzIdBytes );
144                 }
145                 else
146                 {
147                     BerValue.encode( buffer, Strings.EMPTY_BYTES );
148                 }
149 
150                 value = buffer.array();
151             }
152             catch ( Exception e )
153             {
154                 return null;
155             }
156         }
157 
158         return value;
159     }
160 
161 
162     /**
163      * {@inheritDoc}
164      */
165     public String getAuthzId()
166     {
167         return getProxiedAuthz().getAuthzId();
168     }
169 
170 
171     /**
172      * {@inheritDoc}
173      */
174     public void setAuthzId( String authzId )
175     {
176         getProxiedAuthz().setAuthzId( authzId );
177     }
178 
179 
180     /**
181      * {@inheritDoc}
182      */
183     public Asn1Object decode( byte[] controlBytes ) throws DecoderException
184     {
185         getProxiedAuthz().setAuthzId( Strings.utf8ToString( controlBytes ) );
186 
187         return this;
188     }
189 }