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.shared.ldap.codec.actions.extendedRequest;
021
022
023import org.apache.directory.shared.asn1.DecoderException;
024import org.apache.directory.shared.asn1.ber.grammar.GrammarAction;
025import org.apache.directory.shared.asn1.ber.tlv.TLV;
026import org.apache.directory.shared.asn1.util.Oid;
027import org.apache.directory.shared.i18n.I18n;
028import org.apache.directory.shared.ldap.codec.api.ExtendedRequestDecorator;
029import org.apache.directory.shared.ldap.codec.api.LdapApiServiceFactory;
030import org.apache.directory.shared.ldap.codec.api.LdapMessageContainer;
031import org.apache.directory.shared.ldap.model.message.ExtendedRequest;
032import org.apache.directory.shared.util.Strings;
033import org.slf4j.Logger;
034import org.slf4j.LoggerFactory;
035
036
037/**
038 * The action used to store the Extended Request name
039 * <pre>
040 * ExtendedRequest ::= [APPLICATION 23] SEQUENCE {
041 *     requestName [0] LDAPOID,
042 *     ...
043 * </pre>
044 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
045 */
046public class StoreExtendedRequestName extends GrammarAction<LdapMessageContainer<ExtendedRequestDecorator<?,?>>>
047{
048    /** The logger */
049    private static final Logger LOG = LoggerFactory.getLogger( StoreExtendedRequestName.class );
050
051    /** Speedup for logs */
052    private static final boolean IS_DEBUG = LOG.isDebugEnabled();
053
054    /**
055     * Instantiates a new action.
056     */
057    public StoreExtendedRequestName()
058    {
059        super( "Store ExtendedRequest Name" );
060    }
061
062
063    /**
064     * {@inheritDoc}
065     */
066    public void action( LdapMessageContainer<ExtendedRequestDecorator<?,?>> container ) throws DecoderException
067    {
068        ExtendedRequest<?> req;
069        
070        // Get the Value and store it in the ExtendedRequest
071        TLV tlv = container.getCurrentTLV();
072
073        // We have to handle the special case of a 0 length matched
074        // OID
075        if ( tlv.getLength() == 0 )
076        {
077            String msg = I18n.err( I18n.ERR_04095 );
078            LOG.error( msg );
079            // This will generate a PROTOCOL_ERROR
080            throw new DecoderException( msg );
081        }
082        else
083        {
084            byte[] requestNameBytes = tlv.getValue().getData();
085
086            try
087            {
088                String requestName = Strings.utf8ToString(requestNameBytes);
089
090                if ( !Oid.isOid( requestName ) )
091                {
092
093                    String msg = "The Request name is not a valid OID : "
094                        + Strings.utf8ToString(requestNameBytes) + " ("
095                        + Strings.dumpBytes(requestNameBytes) + ") is invalid";
096                    LOG.error( msg );
097
098                    // throw an exception, we will get a PROTOCOL_ERROR
099                    throw new DecoderException( msg );
100                }
101
102                req = LdapApiServiceFactory.getSingleton().newExtendedRequest( requestName, null );
103                req.setMessageId( container.getMessageId() );
104                container.setMessage( LdapApiServiceFactory.getSingleton().decorate( req ) );
105            }
106            catch ( DecoderException de )
107            {
108                String msg = "The Request name is not a valid OID : "
109                    + Strings.utf8ToString(requestNameBytes) + " ("
110                    + Strings.dumpBytes(requestNameBytes) + ") is invalid";
111                LOG.error( "{} : {}", msg, de.getMessage() );
112
113                // Rethrow the exception, we will get a PROTOCOL_ERROR
114                throw de;
115            }
116        }
117
118        // We can have an END transition
119        container.setGrammarEndAllowed( true );
120
121        if ( IS_DEBUG )
122        {
123            LOG.debug( "OID read : {}", req.getRequestName() );
124        }
125    }
126}