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     */
020    package org.apache.directory.shared.ldap.codec.actions;
021    
022    
023    import java.util.HashMap;
024    import java.util.Map;
025    
026    import org.apache.directory.shared.asn1.ber.IAsn1Container;
027    import org.apache.directory.shared.asn1.ber.grammar.GrammarAction;
028    import org.apache.directory.shared.asn1.ber.tlv.TLV;
029    import org.apache.directory.shared.asn1.ber.tlv.Value;
030    import org.apache.directory.shared.asn1.codec.DecoderException;
031    import org.apache.directory.shared.ldap.codec.ControlCodec;
032    import org.apache.directory.shared.ldap.codec.ControlDecoder;
033    import org.apache.directory.shared.ldap.codec.LdapMessageCodec;
034    import org.apache.directory.shared.ldap.codec.LdapMessageContainer;
035    import org.apache.directory.shared.ldap.codec.controls.ManageDsaITControlDecoder;
036    import org.apache.directory.shared.ldap.codec.controls.replication.syncDoneValue.SyncDoneValueControlDecoder;
037    import org.apache.directory.shared.ldap.codec.controls.replication.syncInfoValue.SyncInfoValueControlDecoder;
038    import org.apache.directory.shared.ldap.codec.controls.replication.syncRequestValue.SyncRequestValueControlDecoder;
039    import org.apache.directory.shared.ldap.codec.controls.replication.syncStateValue.SyncStateValueControlDecoder;
040    import org.apache.directory.shared.ldap.codec.search.controls.pSearch.PSearchControlDecoder;
041    import org.apache.directory.shared.ldap.codec.search.controls.pagedSearch.PagedSearchControlDecoder;
042    import org.apache.directory.shared.ldap.codec.search.controls.subEntry.SubEntryControlDecoder;
043    import org.apache.directory.shared.ldap.util.StringTools;
044    
045    import org.slf4j.Logger;
046    import org.slf4j.LoggerFactory;
047    
048    
049    /**
050     * The action used to set the value of a control. This is an extension point
051     * where different controls can be plugged in (at least eventually). For now we
052     * hard code controls.
053     * 
054     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
055     * @version $Rev$, $Date$, 
056     */
057    public class ControlValueAction extends GrammarAction
058    {
059        /** The logger */
060        private static final Logger log = LoggerFactory.getLogger( ControlValueAction.class );
061    
062        /** Speedup for logs */
063        private static final boolean IS_DEBUG = log.isDebugEnabled();
064    
065        private static Map<String, ControlDecoder> controlDecoders = new HashMap<String, ControlDecoder>();
066    
067    
068        public ControlValueAction()
069        {
070            super( "Sets the control value" );
071    
072            ControlDecoder decoder;
073            decoder = new PSearchControlDecoder();
074            controlDecoders.put( decoder.getControlType(), decoder );
075    
076            decoder = new ManageDsaITControlDecoder();
077            controlDecoders.put( decoder.getControlType(), decoder );
078    
079            decoder = new SubEntryControlDecoder();
080            controlDecoders.put( decoder.getControlType(), decoder );
081    
082            decoder = new PagedSearchControlDecoder();
083            controlDecoders.put( decoder.getControlType(), decoder );
084            
085            decoder = new SyncDoneValueControlDecoder();
086            controlDecoders.put( decoder.getControlType(), decoder );
087            
088            decoder = new SyncInfoValueControlDecoder();
089            controlDecoders.put( decoder.getControlType(), decoder );
090            
091            decoder = new SyncRequestValueControlDecoder();
092            controlDecoders.put( decoder.getControlType(), decoder );
093            
094            decoder = new SyncStateValueControlDecoder();
095            controlDecoders.put( decoder.getControlType(), decoder );
096        }
097    
098    
099        public void action( IAsn1Container container ) throws DecoderException
100        {
101            LdapMessageContainer ldapMessageContainer = ( LdapMessageContainer ) container;
102            TLV tlv = ldapMessageContainer.getCurrentTLV();
103            LdapMessageCodec message = ldapMessageContainer.getLdapMessage();
104    
105            // Get the current control
106            ControlCodec control = message.getCurrentControl();
107            Value value = tlv.getValue();
108    
109            ControlDecoder decoder = controlDecoders.get( control.getControlType() );
110    
111            // Store the value - have to handle the special case of a 0 length value
112            if ( tlv.getLength() == 0 )
113            {
114                control.setControlValue( new byte[]
115                    {} );
116            }
117            else
118            {
119                Object decoded;
120    
121                if ( decoder != null )
122                {
123                    decoded = decoder.decode( value.getData() );
124                }
125                else
126                {
127                    decoded = value.getData();
128                }
129    
130                control.setEncodedValue( value.getData() );
131                control.setControlValue( decoded );
132            }
133    
134            // We can have an END transition
135            ldapMessageContainer.grammarEndAllowed( true );
136    
137            if ( IS_DEBUG )
138            {
139                if ( control.getControlValue() instanceof byte[] )
140                {
141                    log.debug( "Control value : " + StringTools.dumpBytes( ( byte[] ) control.getControlValue() ) );
142                }
143                else if ( control.getControlValue() instanceof String )
144                {
145                    log.debug( "Control value : " + ( String ) control.getControlValue() );
146                }
147                else
148                {
149                    log.debug( "Control value : " + control.getControlValue() );
150                }
151            }
152        }
153    }