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.pwdModify;
021
022
023import java.nio.ByteBuffer;
024
025import org.apache.directory.api.asn1.DecoderException;
026import org.apache.directory.api.asn1.ber.Asn1Decoder;
027import org.apache.directory.api.asn1.ber.tlv.BerValue;
028import org.apache.directory.api.asn1.util.Asn1Buffer;
029import org.apache.directory.api.ldap.codec.api.AbstractExtendedOperationFactory;
030import org.apache.directory.api.ldap.codec.api.ExtendedOperationFactory;
031import org.apache.directory.api.ldap.codec.api.LdapApiService;
032import org.apache.directory.api.ldap.extras.extended.pwdModify.PasswordModifyRequest;
033import org.apache.directory.api.ldap.extras.extended.pwdModify.PasswordModifyRequestImpl;
034import org.apache.directory.api.ldap.extras.extended.pwdModify.PasswordModifyResponse;
035import org.apache.directory.api.ldap.extras.extended.pwdModify.PasswordModifyResponseImpl;
036import org.apache.directory.api.ldap.model.message.ExtendedRequest;
037import org.apache.directory.api.ldap.model.message.ExtendedResponse;
038
039
040/**
041 * An {@link ExtendedOperationFactory} for creating PwdModify extended request response 
042 * pairs.
043 *
044 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
045 */
046public class PasswordModifyFactory extends AbstractExtendedOperationFactory
047{
048    /**
049     * Creates a new instance of PasswordModifyFactory.
050     *
051     * @param codec The codec for this factory.
052     */
053    public PasswordModifyFactory( LdapApiService codec )
054    {
055        super( codec, PasswordModifyRequest.EXTENSION_OID );
056    }
057
058
059    /**
060     * {@inheritDoc}
061     */
062    @Override
063    public PasswordModifyRequest newRequest()
064    {
065        PasswordModifyRequest passwordModifyRequest = new PasswordModifyRequestImpl();
066
067        return passwordModifyRequest;
068    }
069
070
071    /**
072     * {@inheritDoc}
073     */
074    @Override
075    public PasswordModifyRequest newRequest( byte[] encodedValue ) throws DecoderException
076    {
077        PasswordModifyRequest passwordModifyRequest = new PasswordModifyRequestImpl();
078        decodeValue( passwordModifyRequest, encodedValue );
079
080        return passwordModifyRequest;
081    }
082
083
084    /**
085     * {@inheritDoc}
086     */
087    @Override
088    public PasswordModifyResponse newResponse()
089    {
090        PasswordModifyResponse passwordModifyResponse = new PasswordModifyResponseImpl();
091        
092        return passwordModifyResponse;
093    }
094
095
096    /**
097     * {@inheritDoc}
098     */
099    @Override
100    public PasswordModifyResponse newResponse( byte[] encodedValue ) throws DecoderException
101    {
102        PasswordModifyResponse passwordModifyResponse = new PasswordModifyResponseImpl();
103        decodeValue( passwordModifyResponse, encodedValue );
104
105        return passwordModifyResponse;
106    }
107
108
109    /**
110     * {@inheritDoc}
111     */
112    @Override
113    public void decodeValue( ExtendedRequest extendedRequest, byte[] requestValue ) throws DecoderException
114    {
115        ByteBuffer bb = ByteBuffer.wrap( requestValue );
116        PasswordModifyRequestContainer container = new PasswordModifyRequestContainer();
117        container.setPasswordModifyRequest( ( PasswordModifyRequest ) extendedRequest ); 
118        Asn1Decoder.decode( bb, container );
119    }
120
121
122    /**
123     * {@inheritDoc}
124     */
125    @Override
126    public void encodeValue( Asn1Buffer buffer, ExtendedRequest extendedRequest )
127    {
128        int start  = buffer.getPos();
129        PasswordModifyRequest passwordModifyRequest = ( PasswordModifyRequest ) extendedRequest;
130        
131        // The new password if any
132        if ( passwordModifyRequest.getNewPassword() != null )
133        {
134            BerValue.encodeOctetString( buffer, 
135                ( byte ) PasswordModifyRequestConstants.NEW_PASSWORD_TAG,
136                passwordModifyRequest.getNewPassword() );
137        }
138
139        // The old password if any
140        if ( passwordModifyRequest.getOldPassword() != null )
141        {
142            BerValue.encodeOctetString( buffer, 
143                ( byte ) PasswordModifyRequestConstants.OLD_PASSWORD_TAG,
144                passwordModifyRequest.getOldPassword() );
145        }
146        
147        // The user identity if any
148        if ( passwordModifyRequest.getUserIdentity() != null )
149        {
150            BerValue.encodeOctetString( buffer, 
151                ( byte ) PasswordModifyRequestConstants.USER_IDENTITY_TAG,
152                passwordModifyRequest.getUserIdentity() );
153        }
154        
155        // The sequence
156        BerValue.encodeSequence( buffer, start );
157    }
158
159
160    /**
161     * {@inheritDoc}
162     */
163    @Override
164    public void decodeValue( ExtendedResponse extendedResponse, byte[] responseValue ) throws DecoderException
165    {
166        ByteBuffer bb = ByteBuffer.wrap( responseValue );
167        PasswordModifyResponseContainer container = new PasswordModifyResponseContainer();
168        container.setPasswordModifyResponse( ( PasswordModifyResponse ) extendedResponse ); 
169        Asn1Decoder.decode( bb, container );
170    }
171
172
173    /**
174     * {@inheritDoc}
175     */
176    @Override
177    public void encodeValue( Asn1Buffer buffer, ExtendedResponse extendedResponse )
178    {
179        // This is a hack !!! We remove the response name from the response
180        // because it has only be added to find the factory, but we don't want it
181        // top be injected in the encoded PDU...
182        extendedResponse.setResponseName( null );
183
184        int start  = buffer.getPos();
185        
186        // The gen password
187        if ( ( ( PasswordModifyResponse ) extendedResponse ).getGenPassword() != null )
188        {
189            BerValue.encodeOctetString( buffer, 
190                ( byte ) PasswordModifyResponseConstants.GEN_PASSWORD_TAG,
191                ( ( PasswordModifyResponse ) extendedResponse ).getGenPassword() );
192        }
193        
194        // The sequence
195        BerValue.encodeSequence( buffer, start );
196    }
197}