View Javadoc
1   /*
2    *   Licensed to the Apache Software Foundation (ASF) under one
3    *   or more contributor license agreements.  See the NOTICE file
4    *   distributed with this work for additional information
5    *   regarding copyright ownership.  The ASF licenses this file
6    *   to you under the Apache License, Version 2.0 (the
7    *   "License"); you may not use this file except in compliance
8    *   with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *   Unless required by applicable law or agreed to in writing,
13   *   software distributed under the License is distributed on an
14   *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *   KIND, either express or implied.  See the License for the
16   *   specific language governing permissions and limitations
17   *   under the License.
18   *
19   */
20  package org.apache.directory.api.ldap.extras.extended.ads_impl.pwdModify;
21  
22  
23  import java.io.PrintWriter;
24  import java.io.StringWriter;
25  import java.nio.ByteBuffer;
26  
27  import org.apache.directory.api.asn1.DecoderException;
28  import org.apache.directory.api.asn1.ber.Asn1Decoder;
29  import org.apache.directory.api.ldap.codec.api.ExtendedOperationFactory;
30  import org.apache.directory.api.ldap.codec.api.ExtendedResponseDecorator;
31  import org.apache.directory.api.ldap.codec.api.LdapApiService;
32  import org.apache.directory.api.ldap.extras.controls.ppolicy.PasswordPolicy;
33  import org.apache.directory.api.ldap.extras.extended.pwdModify.PasswordModifyRequest;
34  import org.apache.directory.api.ldap.extras.extended.pwdModify.PasswordModifyRequestImpl;
35  import org.apache.directory.api.ldap.extras.extended.pwdModify.PasswordModifyResponse;
36  import org.apache.directory.api.ldap.extras.extended.pwdModify.PasswordModifyResponseImpl;
37  import org.apache.directory.api.ldap.model.message.Control;
38  import org.apache.directory.api.ldap.model.message.ExtendedRequest;
39  import org.apache.directory.api.ldap.model.message.ExtendedResponse;
40  import org.apache.directory.api.ldap.model.message.ResultCodeEnum;
41  
42  
43  /**
44   * An {@link ExtendedOperationFactory} for creating PwdModify extended request response 
45   * pairs.
46   *
47   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
48   */
49  public class PasswordModifyFactory implements ExtendedOperationFactory
50  {
51      private LdapApiService codec;
52  
53  
54      /**
55       * Creates a new instance of PasswordModifyFactory.
56       *
57       * @param codec The codec for this factory.
58       */
59      public PasswordModifyFactory( LdapApiService codec )
60      {
61          this.codec = codec;
62      }
63  
64  
65      /**
66       * {@inheritDoc}
67       */
68      @Override
69      public String getOid()
70      {
71          return PasswordModifyRequest.EXTENSION_OID;
72      }
73  
74  
75      /**
76       * {@inheritDoc}
77       */
78      @Override
79      public PasswordModifyResponse newResponse( byte[] encodedValue ) throws DecoderException
80      {
81          PasswordModifyResponseDecorator response = new PasswordModifyResponseDecorator( codec,
82              new PasswordModifyResponseImpl() );
83          response.setResponseValue( encodedValue );
84          return response;
85      }
86  
87  
88      /**
89       * {@inheritDoc}
90       */
91      @Override
92      public PasswordModifyRequest newRequest( byte[] value )
93      {
94          PasswordModifyRequestDecorator req = new PasswordModifyRequestDecorator( codec, new PasswordModifyRequestImpl() );
95  
96          if ( value != null )
97          {
98              req.setRequestValue( value );
99          }
100 
101         return req;
102     }
103 
104 
105     /**
106      * {@inheritDoc}
107      */
108     @Override
109     public PasswordModifyRequestDecorator decorate( ExtendedRequest modelRequest )
110     {
111         if ( modelRequest instanceof PasswordModifyRequestDecorator )
112         {
113             return ( PasswordModifyRequestDecorator ) modelRequest;
114         }
115 
116         return new PasswordModifyRequestDecorator( codec, ( PasswordModifyRequest ) modelRequest );
117     }
118 
119 
120     /**
121      * {@inheritDoc}
122      */
123     @Override
124 public PasswordModifyResponseDecorator decorate( ExtendedResponse decoratedResponse )
125     {
126         if ( decoratedResponse instanceof PasswordModifyResponseDecorator )
127         {
128             return ( PasswordModifyResponseDecorator ) decoratedResponse;
129         }
130 
131         if ( decoratedResponse instanceof PasswordModifyResponse )
132         {
133             return new PasswordModifyResponseDecorator( codec, ( PasswordModifyResponse ) decoratedResponse );
134         }
135 
136         // It's an opaque extended operation
137         @SuppressWarnings("unchecked")
138         ExtendedResponseDecorator<ExtendedResponse> response = ( ExtendedResponseDecorator<ExtendedResponse> ) decoratedResponse;
139 
140         // Decode the response, as it's an opaque operation
141         Asn1Decoder decoder = new Asn1Decoder();
142 
143         byte[] value = response.getResponseValue();
144         PasswordModifyResponseContainer container = new PasswordModifyResponseContainer();
145         
146         PasswordModifyResponse pwdModifyResponse;
147         
148         if ( value != null )
149         {
150             ByteBuffer buffer = ByteBuffer.wrap( value );
151     
152             try
153             {
154                 decoder.decode( buffer, container );
155                 pwdModifyResponse = container.getPwdModifyResponse();
156 
157                 // Now, update the created response with what we got from the extendedResponse
158                 pwdModifyResponse.getLdapResult().setResultCode( response.getLdapResult().getResultCode() );
159                 pwdModifyResponse.getLdapResult().setDiagnosticMessage( response.getLdapResult().getDiagnosticMessage() );
160                 pwdModifyResponse.getLdapResult().setMatchedDn( response.getLdapResult().getMatchedDn() );
161                 pwdModifyResponse.getLdapResult().setReferral( response.getLdapResult().getReferral() );
162             }
163             catch ( DecoderException de )
164             {
165                 StringWriter sw = new StringWriter();
166                 de.printStackTrace( new PrintWriter( sw ) );
167                 String stackTrace = sw.toString();
168     
169                 // Error while decoding the value. 
170                 pwdModifyResponse = new PasswordModifyResponseImpl(
171                     decoratedResponse.getMessageId(),
172                     ResultCodeEnum.OPERATIONS_ERROR,
173                     stackTrace );
174             }
175         }
176         else
177         {
178             pwdModifyResponse = new PasswordModifyResponseImpl();
179     
180             // Now, update the created response with what we got from the extendedResponse
181             pwdModifyResponse.getLdapResult().setResultCode( response.getLdapResult().getResultCode() );
182             pwdModifyResponse.getLdapResult().setDiagnosticMessage( response.getLdapResult().getDiagnosticMessage() );
183             pwdModifyResponse.getLdapResult().setMatchedDn( response.getLdapResult().getMatchedDn() );
184             pwdModifyResponse.getLdapResult().setReferral( response.getLdapResult().getReferral() );
185         }
186 
187         PasswordModifyResponseDecorator decorated = new PasswordModifyResponseDecorator( codec, pwdModifyResponse );
188 
189         Control ppolicyControl = response.getControl( PasswordPolicy.OID );
190 
191         if ( ppolicyControl != null )
192         {
193             decorated.addControl( ppolicyControl );
194         }
195 
196         return decorated;
197     }
198 }