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      public PasswordModifyFactory( LdapApiService codec )
55      {
56          this.codec = codec;
57      }
58  
59  
60      /**
61       * {@inheritDoc}
62       */
63      public String getOid()
64      {
65          return PasswordModifyRequest.EXTENSION_OID;
66      }
67  
68  
69      /**
70       * {@inheritDoc}
71       */
72      public PasswordModifyResponse newResponse( byte[] encodedValue ) throws DecoderException
73      {
74          PasswordModifyResponseDecorator response = new PasswordModifyResponseDecorator( codec,
75              new PasswordModifyResponseImpl() );
76          response.setResponseValue( encodedValue );
77          return response;
78      }
79  
80  
81      /**
82       * {@inheritDoc}
83       */
84      public PasswordModifyRequest newRequest( byte[] value )
85      {
86          PasswordModifyRequestDecorator req = new PasswordModifyRequestDecorator( codec, new PasswordModifyRequestImpl() );
87  
88          if ( value != null )
89          {
90              req.setRequestValue( value );
91          }
92  
93          return req;
94      }
95  
96  
97      /**
98       * {@inheritDoc}
99       */
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 }