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 *    https://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.passwordExpired_impl;
021
022import org.apache.directory.api.asn1.DecoderException;
023import org.apache.directory.api.asn1.util.Asn1Buffer;
024import org.apache.directory.api.i18n.I18n;
025import org.apache.directory.api.ldap.codec.api.AbstractControlFactory;
026import org.apache.directory.api.ldap.codec.api.ControlFactory;
027import org.apache.directory.api.ldap.codec.api.LdapApiService;
028import org.apache.directory.api.ldap.extras.controls.passwordExpired.PasswordExpiredResponse;
029import org.apache.directory.api.ldap.extras.controls.passwordExpired.PasswordExpiredResponseImpl;
030import org.apache.directory.api.ldap.model.message.Control;
031import org.apache.directory.api.util.Strings;
032
033/**
034 * A {@link ControlFactory} which creates {@link PasswordExpiredResponse} controls.
035 *
036 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
037 */
038public class PasswordExpiredResponseFactory extends AbstractControlFactory<PasswordExpiredResponse>
039{
040    /**
041     * Creates a new instance of PasswordExpiredResponseFactory.
042     *
043     * @param codec The LDAP codec.
044     */
045    public PasswordExpiredResponseFactory( LdapApiService codec )
046    {
047        super( codec, PasswordExpiredResponse.OID );
048    }
049    
050
051    /**
052     * {@inheritDoc}
053     */
054    @Override
055    public PasswordExpiredResponse newControl() 
056    {
057        return new PasswordExpiredResponseImpl();
058    }
059    
060
061    /**
062     * {@inheritDoc}
063     */
064    @Override
065    public void decodeValue( Control control, byte[] controlBytes ) throws DecoderException 
066    {
067        try 
068        {
069            if ( !Strings.utf8ToString( controlBytes ).equals( "0" ) )
070            {
071                throw new DecoderException( I18n.err( I18n.ERR_08110_BAD_PASSWORD_EXPIRED_VALUE, Strings.dumpBytes( controlBytes ) ) );
072            }
073        }
074        catch ( RuntimeException re ) 
075        {
076            throw new DecoderException( re.getMessage() );
077        }
078    }
079
080
081    /**
082     * {@inheritDoc}
083     */
084    @Override
085    public void encodeValue( Asn1Buffer buffer, Control control )
086    {
087        // Always '0'
088        buffer.put( ( byte ) 0x30 );
089    }
090}