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.startTls;
21  
22  
23  import org.apache.directory.api.asn1.DecoderException;
24  import org.apache.directory.api.ldap.codec.api.ExtendedOperationFactory;
25  import org.apache.directory.api.ldap.codec.api.ExtendedResponseDecorator;
26  import org.apache.directory.api.ldap.codec.api.LdapApiService;
27  import org.apache.directory.api.ldap.extras.extended.startTls.StartTlsRequest;
28  import org.apache.directory.api.ldap.extras.extended.startTls.StartTlsRequestImpl;
29  import org.apache.directory.api.ldap.extras.extended.startTls.StartTlsResponse;
30  import org.apache.directory.api.ldap.extras.extended.startTls.StartTlsResponseImpl;
31  import org.apache.directory.api.ldap.model.message.ExtendedRequest;
32  import org.apache.directory.api.ldap.model.message.ExtendedResponse;
33  
34  
35  /**
36   * An {@link ExtendedOperationFactory} for creating SartTls extended reques/response 
37   * pairs.
38   *
39   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
40   */
41  public class StartTlsFactory implements ExtendedOperationFactory
42  {
43      private LdapApiService codec;
44  
45  
46      /**
47       * Creates a new instance of StartTlsFactory.
48       *
49       * @param codec The codec for this factory.
50       */
51      public StartTlsFactory( LdapApiService codec )
52      {
53          this.codec = codec;
54      }
55  
56  
57      /**
58       * {@inheritDoc}
59       */
60      @Override
61      public String getOid()
62      {
63          return StartTlsRequest.EXTENSION_OID;
64      }
65  
66  
67      /**
68       * {@inheritDoc}
69       */
70      @Override
71      public StartTlsResponse newResponse( byte[] encodedValue ) throws DecoderException
72      {
73          StartTlsResponseDecorator response = new StartTlsResponseDecorator( codec,
74              new StartTlsResponseImpl() );
75          response.setResponseValue( encodedValue );
76          return response;
77      }
78  
79  
80      /**
81       * {@inheritDoc}
82       */
83      @Override
84      public StartTlsRequest newRequest( byte[] value )
85      {
86          StartTlsRequestDecorator req = new StartTlsRequestDecorator( codec, new StartTlsRequestImpl() );
87  
88          if ( value != null )
89          {
90              req.setRequestValue( value );
91          }
92  
93          return req;
94      }
95  
96  
97      /**
98       * {@inheritDoc}
99       */
100     @Override
101     public StartTlsRequestDecorator decorate( ExtendedRequest modelRequest )
102     {
103         if ( modelRequest instanceof StartTlsRequestDecorator )
104         {
105             return ( StartTlsRequestDecorator ) modelRequest;
106         }
107 
108         return new StartTlsRequestDecorator( codec, ( StartTlsRequest ) modelRequest );
109     }
110 
111 
112     /**
113      * {@inheritDoc}
114      */
115     @Override
116     public StartTlsResponseDecorator decorate( ExtendedResponse decoratedResponse )
117     {
118         if ( decoratedResponse instanceof StartTlsResponseDecorator )
119         {
120             return ( StartTlsResponseDecorator ) decoratedResponse;
121         }
122 
123         if ( decoratedResponse instanceof StartTlsResponse )
124         {
125             return new StartTlsResponseDecorator( codec, ( StartTlsResponse ) decoratedResponse );
126         }
127 
128         // It's an opaque extended operation
129         @SuppressWarnings("unchecked")
130         ExtendedResponseDecorator<ExtendedResponse> response = ( ExtendedResponseDecorator<ExtendedResponse> ) decoratedResponse;
131 
132         // Decode the response, as it's an opaque operation
133         StartTlsResponse startTlsResponse = new StartTlsResponseImpl( response.getMessageId() );
134         
135         startTlsResponse.getLdapResult().setResultCode( response.getLdapResult().getResultCode() );
136         startTlsResponse.getLdapResult().setDiagnosticMessage( response.getLdapResult().getDiagnosticMessage() );
137         return new StartTlsResponseDecorator( codec, new StartTlsResponseImpl() );
138     }
139 }