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 /** 047 * Creates a new instance of StartTlsFactory. 048 * 049 * @param codec The codec for this factory. 050 */ 051 public StartTlsFactory( LdapApiService codec ) 052 { 053 this.codec = codec; 054 } 055 056 057 /** 058 * {@inheritDoc} 059 */ 060 @Override 061 public String getOid() 062 { 063 return StartTlsRequest.EXTENSION_OID; 064 } 065 066 067 /** 068 * {@inheritDoc} 069 */ 070 @Override 071 public StartTlsResponse newResponse( byte[] encodedValue ) throws DecoderException 072 { 073 StartTlsResponseDecorator response = new StartTlsResponseDecorator( codec, 074 new StartTlsResponseImpl() ); 075 response.setResponseValue( encodedValue ); 076 return response; 077 } 078 079 080 /** 081 * {@inheritDoc} 082 */ 083 @Override 084 public StartTlsRequest newRequest( byte[] value ) 085 { 086 StartTlsRequestDecorator req = new StartTlsRequestDecorator( codec, new StartTlsRequestImpl() ); 087 088 if ( value != null ) 089 { 090 req.setRequestValue( value ); 091 } 092 093 return req; 094 } 095 096 097 /** 098 * {@inheritDoc} 099 */ 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}