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.extras.extended.ads_impl.whoAmI;
021
022
023import java.nio.ByteBuffer;
024
025import org.apache.directory.api.asn1.DecoderException;
026import org.apache.directory.api.asn1.EncoderException;
027import org.apache.directory.api.asn1.ber.tlv.BerValue;
028import org.apache.directory.api.asn1.ber.tlv.TLV;
029import org.apache.directory.api.i18n.I18n;
030import org.apache.directory.api.ldap.codec.api.ExtendedResponseDecorator;
031import org.apache.directory.api.ldap.codec.api.LdapApiService;
032import org.apache.directory.api.ldap.extras.extended.whoAmI.WhoAmIResponse;
033import org.apache.directory.api.ldap.extras.extended.whoAmI.WhoAmIResponseImpl;
034import org.apache.directory.api.ldap.model.name.Dn;
035import org.slf4j.Logger;
036import org.slf4j.LoggerFactory;
037
038
039/**
040 * A Decorator for WhoAmIResponse extended request.
041 *
042 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
043 */
044public class WhoAmIResponseDecorator extends ExtendedResponseDecorator<WhoAmIResponse>
045    implements WhoAmIResponse
046{
047    private static final Logger LOG = LoggerFactory.getLogger( WhoAmIResponseDecorator.class );
048
049    private WhoAmIResponse whoAmIResponse;
050
051    public WhoAmIResponseDecorator( LdapApiService codec, WhoAmIResponse decoratedMessage )
052    {
053        super( codec, decoratedMessage );
054        whoAmIResponse = decoratedMessage;
055    }
056
057
058    /**
059     * {@inheritDoc}
060     */
061    @Override
062    public void setResponseValue( byte[] responseValue )
063    {
064        WhoAmIResponseDecoder decoder = new WhoAmIResponseDecoder();
065
066        try
067        {
068            whoAmIResponse = decoder.decode( responseValue );
069
070            if ( responseValue != null )
071            {
072                this.responseValue = new byte[responseValue.length];
073                System.arraycopy( responseValue, 0, this.responseValue, 0, responseValue.length );
074            }
075            else
076            {
077                this.responseValue = null;
078            }
079        }
080        catch ( DecoderException e )
081        {
082            LOG.error( I18n.err( I18n.ERR_04165 ), e );
083            throw new RuntimeException( e );
084        }
085    }
086
087
088    /**
089     * {@inheritDoc}
090     */
091    @Override
092    public byte[] getResponseValue()
093    {
094        if ( responseValue == null )
095        {
096            try
097            {
098                responseValue = encodeInternal().array();
099
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}