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.io.PrintWriter;
024import java.io.StringWriter;
025import java.nio.ByteBuffer;
026
027import org.apache.directory.api.asn1.DecoderException;
028import org.apache.directory.api.asn1.ber.Asn1Decoder;
029import org.apache.directory.api.ldap.codec.api.ExtendedOperationFactory;
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.controls.ppolicy.PasswordPolicy;
033import org.apache.directory.api.ldap.extras.extended.pwdModify.PasswordModifyRequest;
034import org.apache.directory.api.ldap.extras.extended.pwdModify.PasswordModifyRequestImpl;
035import org.apache.directory.api.ldap.extras.extended.pwdModify.PasswordModifyResponse;
036import org.apache.directory.api.ldap.extras.extended.pwdModify.PasswordModifyResponseImpl;
037import org.apache.directory.api.ldap.model.message.Control;
038import org.apache.directory.api.ldap.model.message.ExtendedRequest;
039import org.apache.directory.api.ldap.model.message.ExtendedResponse;
040import org.apache.directory.api.ldap.model.message.ResultCodeEnum;
041
042
043/**
044 * An {@link ExtendedOperationFactory} for creating PwdModify extended request response 
045 * pairs.
046 *
047 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
048 */
049public class PasswordModifyFactory implements ExtendedOperationFactory
050{
051    private LdapApiService codec;
052
053
054    public PasswordModifyFactory( LdapApiService codec )
055    {
056        this.codec = codec;
057    }
058
059
060    /**
061     * {@inheritDoc}
062     */
063    public String getOid()
064    {
065        return PasswordModifyRequest.EXTENSION_OID;
066    }
067
068
069    /**
070     * {@inheritDoc}
071     */
072    public PasswordModifyResponse newResponse( byte[] encodedValue ) throws DecoderException
073    {
074        PasswordModifyResponseDecorator response = new PasswordModifyResponseDecorator( codec,
075            new PasswordModifyResponseImpl() );
076        response.setResponseValue( encodedValue );
077        return response;
078    }
079
080
081    /**
082     * {@inheritDoc}
083     */
084    public PasswordModifyRequest newRequest( byte[] value )
085    {
086        PasswordModifyRequestDecorator req = new PasswordModifyRequestDecorator( codec, new PasswordModifyRequestImpl() );
087
088        if ( value != null )
089        {
090            req.setRequestValue( value );
091        }
092
093        return req;
094    }
095
096
097    /**
098     * {@inheritDoc}
099     */
100    public PasswordModifyRequestDecorator decorate( ExtendedRequest modelRequest )
101    {
102        if ( modelRequest instanceof PasswordModifyRequestDecorator )
103        {
104            return ( PasswordModifyRequestDecorator ) modelRequest;
105        }
106
107        return new PasswordModifyRequestDecorator( codec, ( PasswordModifyRequest ) modelRequest );
108    }
109
110
111    /**
112     * {@inheritDoc}
113     */
114    public PasswordModifyResponseDecorator decorate( ExtendedResponse decoratedResponse )
115    {
116        if ( decoratedResponse instanceof PasswordModifyResponseDecorator )
117        {
118            return ( PasswordModifyResponseDecorator ) decoratedResponse;
119        }
120
121        if ( decoratedResponse instanceof PasswordModifyResponse )
122        {
123            return new PasswordModifyResponseDecorator( codec, ( PasswordModifyResponse ) decoratedResponse );
124        }
125
126        // It's an opaque extended operation
127        @SuppressWarnings("unchecked")
128        ExtendedResponseDecorator<ExtendedResponse> response = ( ExtendedResponseDecorator<ExtendedResponse> ) decoratedResponse;
129
130        // Decode the response, as it's an opaque operation
131        Asn1Decoder decoder = new Asn1Decoder();
132
133        byte[] value = response.getResponseValue();
134        ByteBuffer buffer = ByteBuffer.wrap( value );
135
136        PasswordModifyResponseContainer container = new PasswordModifyResponseContainer();
137        PasswordModifyResponse pwdModifyResponse = null;
138
139        try
140        {
141            decoder.decode( buffer, container );
142
143            pwdModifyResponse = container.getPwdModifyResponse();
144
145            // Now, update the created response with what we got from the extendedResponse
146            pwdModifyResponse.getLdapResult().setResultCode( response.getLdapResult().getResultCode() );
147            pwdModifyResponse.getLdapResult().setDiagnosticMessage( response.getLdapResult().getDiagnosticMessage() );
148            pwdModifyResponse.getLdapResult().setMatchedDn( response.getLdapResult().getMatchedDn() );
149            pwdModifyResponse.getLdapResult().setReferral( response.getLdapResult().getReferral() );
150        }
151        catch ( DecoderException de )
152        {
153            StringWriter sw = new StringWriter();
154            de.printStackTrace( new PrintWriter( sw ) );
155            String stackTrace = sw.toString();
156
157            // Error while decoding the value. 
158            pwdModifyResponse = new PasswordModifyResponseImpl(
159                decoratedResponse.getMessageId(),
160                ResultCodeEnum.OPERATIONS_ERROR,
161                stackTrace );
162        }
163
164        PasswordModifyResponseDecorator decorated = new PasswordModifyResponseDecorator( codec, pwdModifyResponse );
165
166        Control ppolicyControl = response.getControl( PasswordPolicy.OID );
167
168        if ( ppolicyControl != null )
169        {
170            decorated.addControl( ppolicyControl );
171        }
172
173        return decorated;
174    }
175}