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