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      public StartTlsFactory( LdapApiService codec )
47      {
48          this.codec = codec;
49      }
50  
51  
52      /**
53       * {@inheritDoc}
54       */
55      public String getOid()
56      {
57          return StartTlsRequest.EXTENSION_OID;
58      }
59  
60  
61      /**
62       * {@inheritDoc}
63       */
64      public StartTlsResponse newResponse( byte[] encodedValue ) throws DecoderException
65      {
66          StartTlsResponseDecorator response = new StartTlsResponseDecorator( codec,
67              new StartTlsResponseImpl() );
68          response.setResponseValue( encodedValue );
69          return response;
70      }
71  
72  
73      /**
74       * {@inheritDoc}
75       */
76      public StartTlsRequest newRequest( byte[] value )
77      {
78          StartTlsRequestDecorator req = new StartTlsRequestDecorator( codec, new StartTlsRequestImpl() );
79  
80          if ( value != null )
81          {
82              req.setRequestValue( value );
83          }
84  
85          return req;
86      }
87  
88  
89      /**
90       * {@inheritDoc}
91       */
92      public StartTlsRequestDecorator decorate( ExtendedRequest modelRequest )
93      {
94          if ( modelRequest instanceof StartTlsRequestDecorator )
95          {
96              return ( StartTlsRequestDecorator ) modelRequest;
97          }
98  
99          return new StartTlsRequestDecorator( codec, ( StartTlsRequest ) modelRequest );
100     }
101 
102 
103     /**
104      * {@inheritDoc}
105      */
106     public StartTlsResponseDecorator decorate( ExtendedResponse decoratedResponse )
107     {
108         if ( decoratedResponse instanceof StartTlsResponseDecorator )
109         {
110             return ( StartTlsResponseDecorator ) decoratedResponse;
111         }
112 
113         if ( decoratedResponse instanceof StartTlsResponse )
114         {
115             return new StartTlsResponseDecorator( codec, ( StartTlsResponse ) decoratedResponse );
116         }
117 
118         // It's an opaque extended operation
119         @SuppressWarnings("unchecked")
120         ExtendedResponseDecorator<ExtendedResponse> response = ( ExtendedResponseDecorator<ExtendedResponse> ) decoratedResponse;
121 
122         // Decode the response, as it's an opaque operation
123         StartTlsResponse startTlsResponse = new StartTlsResponseImpl( response.getMessageId() );
124         
125         startTlsResponse.getLdapResult().setResultCode( response.getLdapResult().getResultCode() );
126         startTlsResponse.getLdapResult().setDiagnosticMessage( response.getLdapResult().getDiagnosticMessage() );
127         StartTlsResponseDecorator decorated = new StartTlsResponseDecorator( codec, new StartTlsResponseImpl() );
128 
129         return decorated;
130     }
131 }