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.codec.actions.controls; 021 022 023import org.apache.directory.api.asn1.DecoderException; 024import org.apache.directory.api.asn1.ber.grammar.GrammarAction; 025import org.apache.directory.api.asn1.ber.tlv.TLV; 026import org.apache.directory.api.asn1.util.Oid; 027import org.apache.directory.api.i18n.I18n; 028import org.apache.directory.api.ldap.codec.api.ControlFactory; 029import org.apache.directory.api.ldap.codec.api.LdapApiService; 030import org.apache.directory.api.ldap.codec.api.LdapMessageContainer; 031import org.apache.directory.api.ldap.model.message.Control; 032import org.apache.directory.api.ldap.model.message.Message; 033import org.apache.directory.api.ldap.model.message.Request; 034import org.apache.directory.api.ldap.model.message.controls.OpaqueControl; 035import org.apache.directory.api.util.Strings; 036import org.slf4j.Logger; 037import org.slf4j.LoggerFactory; 038 039 040/** 041 * The action used add a new control. We store its OID. 042 * <pre> 043 * Control ::= SEQUENCE { 044 * controlType LDAPOID, 045 * ...LdapMessageContainerDirect<Message> 046 * </pre> 047 * 048 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 049 */ 050public class StoreControlName extends GrammarAction<LdapMessageContainer<Message>> 051{ 052 /** The logger */ 053 private static final Logger LOG = LoggerFactory.getLogger( StoreControlName.class ); 054 055 /** 056 * Instantiates a new AddControl action. 057 */ 058 public StoreControlName() 059 { 060 super( "Add a new control" ); 061 } 062 063 064 /** 065 * {@inheritDoc} 066 */ 067 @Override 068 public void action( LdapMessageContainer<Message> container ) throws DecoderException 069 { 070 TLV tlv = container.getCurrentTLV(); 071 072 // Store the type 073 // We have to handle the special case of a 0 length OID 074 if ( tlv.getLength() == 0 ) 075 { 076 String msg = I18n.err( I18n.ERR_08214_NULL_OID ); 077 LOG.error( msg ); 078 079 // This will generate a PROTOCOL_ERROR 080 throw new DecoderException( msg ); 081 } 082 083 byte[] value = tlv.getValue().getData(); 084 String oidValue = Strings.asciiBytesToString( value ); 085 086 // The OID is encoded as a String, not an Object Id 087 if ( !Oid.isOid( oidValue ) ) 088 { 089 String msg = I18n.err( I18n.ERR_08215_INVALID_CONTROL_OID, oidValue ); 090 LOG.error( msg ); 091 092 // This will generate a PROTOCOL_ERROR 093 throw new DecoderException( msg ); 094 } 095 096 // Search for the control. It can be a request or a response control. 097 // If the control is not known, we create an Opaque control 098 Message message = container.getMessage(); 099 LdapApiService codec = container.getLdapCodecService(); 100 ControlFactory<? extends Control> controlFactory; 101 102 Control control; 103 104 if ( message instanceof Request ) 105 { 106 controlFactory = codec.getRequestControlFactories().get( oidValue ); 107 } 108 else 109 { 110 controlFactory = codec.getResponseControlFactories().get( oidValue ); 111 } 112 113 if ( controlFactory == null ) 114 { 115 control = new OpaqueControl( oidValue ); 116 } 117 else 118 { 119 control = controlFactory.newControl(); 120 } 121 122 container.setControlFactory( controlFactory ); 123 124 // At this point, the control exists, we may have to decode the value and feed it 125 //In any case, add it to the message's controls, and store it in the container for further 126 // processing (aka, value decoding) 127 message.addControl( control ); 128 129 container.setCurrentControl( control ); 130 131 // We can have an END transition 132 container.setGrammarEndAllowed( true ); 133 134 if ( LOG.isDebugEnabled() ) 135 { 136 LOG.debug( I18n.msg( I18n.MSG_08201_CONTROL_OID, oidValue ) ); 137 } 138 } 139}