001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *  
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *  
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License. 
018 *  
019 */
020package org.apache.directory.api.ldap.codec.controls.proxiedauthz;
021
022
023import java.nio.ByteBuffer;
024
025import org.apache.directory.api.asn1.Asn1Object;
026import org.apache.directory.api.asn1.DecoderException;
027import org.apache.directory.api.asn1.EncoderException;
028import org.apache.directory.api.asn1.ber.Asn1Decoder;
029import org.apache.directory.api.asn1.ber.tlv.BerValue;
030import org.apache.directory.api.i18n.I18n;
031import org.apache.directory.api.ldap.codec.api.ControlDecorator;
032import org.apache.directory.api.ldap.codec.api.LdapApiService;
033import org.apache.directory.api.ldap.model.message.controls.ProxiedAuthz;
034import org.apache.directory.api.ldap.model.message.controls.ProxiedAuthzImpl;
035import org.apache.directory.api.util.Strings;
036
037
038/**
039 * An ProxiedAuthz implementation, that wraps and decorates the Control with codec
040 * specific functionality.
041 *
042 *
043 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
044 */
045public class ProxiedAuthzDecorator extends ControlDecorator<ProxiedAuthz> implements ProxiedAuthz
046{
047    /** A temporary storage for the authzId */
048    private byte[] authzIdBytes = null;
049
050    /** An instance of this decoder */
051    private static final Asn1Decoder decoder = new Asn1Decoder();
052
053
054    /**
055     * Creates a new instance of ProxiedAuthzDecoder wrapping a newly created
056     * ProxiedAuthz Control object.
057     */
058    public ProxiedAuthzDecorator( LdapApiService codec )
059    {
060        super( codec, new ProxiedAuthzImpl() );
061    }
062
063
064    /**
065     * Creates a new instance of ProxiedAuthzDecorator wrapping the supplied
066     * ProxiedAuthz Control.
067     *
068     * @param control The ProxiedAuthz Control to be decorated.
069     */
070    public ProxiedAuthzDecorator( LdapApiService codec, ProxiedAuthz control )
071    {
072        super( codec, control );
073    }
074
075
076    /**
077     * Internally used to not have to cast the decorated Control.
078     *
079     * @return the decorated Control.
080     */
081    private ProxiedAuthz getProxiedAuthz()
082    {
083        return ( ProxiedAuthz ) getDecorated();
084    }
085
086
087    /**
088     * Compute the ProxiedAuthzControl length 
089     * 
090     *  0x04 L1 authzId] 
091     */
092    public int computeLength()
093    {
094        int valueLength = 0;
095
096        if ( getAuthzId() != null )
097        {
098            authzIdBytes = Strings.getBytesUtf8( getAuthzId() );
099            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}