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.startTls;
021
022
023import org.apache.directory.api.asn1.DecoderException;
024import org.apache.directory.api.ldap.codec.api.ExtendedOperationFactory;
025import org.apache.directory.api.ldap.codec.api.ExtendedResponseDecorator;
026import org.apache.directory.api.ldap.codec.api.LdapApiService;
027import org.apache.directory.api.ldap.extras.extended.startTls.StartTlsRequest;
028import org.apache.directory.api.ldap.extras.extended.startTls.StartTlsRequestImpl;
029import org.apache.directory.api.ldap.extras.extended.startTls.StartTlsResponse;
030import org.apache.directory.api.ldap.extras.extended.startTls.StartTlsResponseImpl;
031import org.apache.directory.api.ldap.model.message.ExtendedRequest;
032import org.apache.directory.api.ldap.model.message.ExtendedResponse;
033
034
035/**
036 * An {@link ExtendedOperationFactory} for creating SartTls extended reques/response 
037 * pairs.
038 *
039 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
040 */
041public class StartTlsFactory implements ExtendedOperationFactory
042{
043    private LdapApiService codec;
044
045
046    public StartTlsFactory( LdapApiService codec )
047    {
048        this.codec = codec;
049    }
050
051
052    /**
053     * {@inheritDoc}
054     */
055    public String getOid()
056    {
057        return StartTlsRequest.EXTENSION_OID;
058    }
059
060
061    /**
062     * {@inheritDoc}
063     */
064    public StartTlsResponse newResponse( byte[] encodedValue ) throws DecoderException
065    {
066        StartTlsResponseDecorator response = new StartTlsResponseDecorator( codec,
067            new StartTlsResponseImpl() );
068        response.setResponseValue( encodedValue );
069        return response;
070    }
071
072
073    /**
074     * {@inheritDoc}
075     */
076    public StartTlsRequest newRequest( byte[] value )
077    {
078        StartTlsRequestDecorator req = new StartTlsRequestDecorator( codec, new StartTlsRequestImpl() );
079
080        if ( value != null )
081        {
082            req.setRequestValue( value );
083        }
084
085        return req;
086    }
087
088
089    /**
090     * {@inheritDoc}
091     */
092    public StartTlsRequestDecorator decorate( ExtendedRequest modelRequest )
093    {
094        if ( modelRequest instanceof StartTlsRequestDecorator )
095        {
096            return ( StartTlsRequestDecorator ) modelRequest;
097        }
098
099        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}