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