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.controls.ppolicy_impl;
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.asn1.ber.tlv.TLV;
031import org.apache.directory.api.asn1.ber.tlv.UniversalTag;
032import org.apache.directory.api.i18n.I18n;
033import org.apache.directory.api.ldap.codec.api.ControlDecorator;
034import org.apache.directory.api.ldap.codec.api.LdapApiService;
035import org.apache.directory.api.ldap.extras.controls.ppolicy.PasswordPolicy;
036import org.apache.directory.api.ldap.extras.controls.ppolicy.PasswordPolicyImpl;
037import org.apache.directory.api.ldap.extras.controls.ppolicy.PasswordPolicyResponse;
038
039
040/**
041 * PasswordPolicy decorator.
042 *
043 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
044 */
045public class PasswordPolicyDecorator extends ControlDecorator<PasswordPolicy> implements PasswordPolicy
046{
047    /** An instance of this decoder */
048    private static final Asn1Decoder decoder = new Asn1Decoder();
049
050    // Storage for computed lengths
051    private int ppolicySeqLength = 0;
052    private int warningLength = 0;
053    private int timeBeforeExpirationValueLength;
054    private int graceAuthNsRemainingValueLength;
055
056
057    public PasswordPolicyDecorator( LdapApiService codec )
058    {
059        super( codec, new PasswordPolicyImpl() );
060    }
061
062
063    public PasswordPolicyDecorator( LdapApiService codec, boolean hasResponse )
064    {
065        super( codec, new PasswordPolicyImpl( hasResponse ) );
066    }
067
068
069    public PasswordPolicyDecorator( LdapApiService codec, PasswordPolicy policy )
070    {
071        super( codec, policy );
072    }
073
074
075    /**
076     * {@inheritDoc}
077     */
078    @Override
079    public void setValue( byte[] value )
080    {
081        if ( value == null || value.length <= 2 )
082        {
083            setResponse( null );
084        }
085        else if ( value != null && !hasResponse() )
086        {
087            setResponse( true );
088        }
089
090        super.setValue( value );
091    }
092
093
094    @Override
095    public int computeLength()
096    {
097        // reset the length values
098        valueLength = 0;
099        ppolicySeqLength = 0;
100        warningLength = 0;
101        timeBeforeExpirationValueLength = 0;
102        graceAuthNsRemainingValueLength = 0;
103
104        if ( !hasResponse() )
105        {
106            return 0;
107        }
108
109        if ( getResponse().getTimeBeforeExpiration() >= 0 )
110        {
111            timeBeforeExpirationValueLength = BerValue.getNbBytes( getResponse().getTimeBeforeExpiration() );
112            warningLength = 1 + TLV.getNbBytes( timeBeforeExpirationValueLength ) + timeBeforeExpirationValueLength;
113        }
114        else if ( getResponse().getGraceAuthNRemaining() >= 0 )
115        {
116            graceAuthNsRemainingValueLength = BerValue.getNbBytes( getResponse().getGraceAuthNRemaining() );
117            warningLength = 1 + TLV.getNbBytes( graceAuthNsRemainingValueLength ) + graceAuthNsRemainingValueLength;
118        }
119
120        if ( warningLength != 0 )
121        {
122            ppolicySeqLength = 1 + TLV.getNbBytes( warningLength ) + warningLength;
123        }
124
125        if ( getResponse().getPasswordPolicyError() != null )
126        {
127            ppolicySeqLength += 1 + 1 + 1;
128        }
129
130        valueLength = 1 + TLV.getNbBytes( ppolicySeqLength ) + ppolicySeqLength;
131
132        return valueLength;
133    }
134
135
136    @Override
137    public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
138    {
139        if ( !hasResponse() )
140        {
141            return buffer;
142        }
143
144        if ( buffer == null )
145        {
146            throw new EncoderException( I18n.err( I18n.ERR_04023 ) );
147        }
148
149        // Encode the Sequence tag
150        buffer.put( UniversalTag.SEQUENCE.getValue() );
151        buffer.put( TLV.getBytes( ppolicySeqLength ) );
152
153        if ( ( getResponse().getTimeBeforeExpiration() < 0 ) && ( getResponse().getGraceAuthNRemaining() < 0 ) && (
154            getResponse().getPasswordPolicyError() == null ) )
155        {
156            return buffer;
157        }
158        else
159        {
160            if ( warningLength > 0 )
161            {
162                // Encode the Warning tag
163                buffer.put( ( byte ) PasswordPolicyTags.PPOLICY_WARNING_TAG.getValue() );
164                buffer.put( TLV.getBytes( warningLength ) );
165
166                if ( getResponse().getTimeBeforeExpiration() >= 0 )
167                {
168                    BerValue.encode(
169                        buffer,
170                        ( byte ) PasswordPolicyTags.TIME_BEFORE_EXPIRATION_TAG.getValue(),
171                        getResponse().getTimeBeforeExpiration() );
172                }
173                else if ( getResponse().getGraceAuthNRemaining() >= 0 )
174                {
175                    BerValue.encode(
176                        buffer,
177                        ( byte ) PasswordPolicyTags.GRACE_AUTHNS_REMAINING_TAG.getValue(),
178                        getResponse().getGraceAuthNRemaining() );
179                }
180            }
181
182            if ( getResponse().getPasswordPolicyError() != null )
183            {
184                BerValue.encode(
185                    buffer,
186                    ( byte ) PasswordPolicyTags.PPOLICY_ERROR_TAG.getValue(),
187                    getResponse().getPasswordPolicyError().getValue() );
188            }
189        }
190
191        return buffer;
192    }
193
194
195    @Override
196    public String toString()
197    {
198        StringBuilder sb = new StringBuilder();
199
200        sb.append( "  PasswordPolicyResponse control :\n" );
201        sb.append( "   oid          : '" ).append( getOid() ).append( '\n' );
202
203        if ( hasResponse() && getResponse().getTimeBeforeExpiration() >= 0 )
204        {
205            sb.append( "   timeBeforeExpiration          : '" ).append( getResponse().getTimeBeforeExpiration() )
206                .append( '\n' );
207        }
208        else if ( hasResponse() && getResponse().getGraceAuthNRemaining() >= 0 )
209        {
210            sb.append( "   graceAuthNsRemaining          : '" ).append( getResponse().getGraceAuthNRemaining() )
211                .append( '\n' );
212        }
213
214        if ( hasResponse() && getResponse().getPasswordPolicyError() != null )
215        {
216            sb.append( "   ppolicyError          : '" ).append( getResponse().getPasswordPolicyError().toString() )
217                .append( '\n' );
218        }
219
220        return sb.toString();
221    }
222
223
224    /**
225     * {@inheritDoc}
226     */
227    public Asn1Object decode( byte[] controlBytes ) throws DecoderException
228    {
229        if ( !hasResponse() )
230        {
231            return this;
232        }
233
234        ByteBuffer bb = ByteBuffer.wrap( controlBytes );
235        PasswordPolicyContainer container = new PasswordPolicyContainer( getCodecService(), this );
236        decoder.decode( bb, container );
237        return this;
238    }
239
240
241    /**
242     *
243     * {@inheritDoc}
244     */
245    public boolean hasResponse()
246    {
247        return getDecorated().hasResponse();
248    }
249
250
251    /**
252     *
253     * {@inheritDoc}
254     */
255    public void setResponse( PasswordPolicyResponse response )
256    {
257        getDecorated().setResponse( response );
258    }
259
260
261    /**
262     *
263     * {@inheritDoc}
264     */
265    public PasswordPolicyResponse setResponse( boolean hasResponse )
266    {
267        return getDecorated().setResponse( hasResponse );
268    }
269
270
271    /**
272     *
273     * {@inheritDoc}
274     */
275    public PasswordPolicyResponse getResponse()
276    {
277        return getDecorated().getResponse();
278    }
279}