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.extendedRequest;
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.ExtendedRequestDecorator;
029import org.apache.directory.api.ldap.codec.api.LdapApiServiceFactory;
030import org.apache.directory.api.ldap.codec.api.LdapMessageContainer;
031import org.apache.directory.api.ldap.model.message.ExtendedRequest;
032import org.apache.directory.api.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    /**
056     * Instantiates a new action.
057     */
058    public StoreExtendedRequestName()
059    {
060        super( "Store ExtendedRequest Name" );
061    }
062
063
064    /**
065     * {@inheritDoc}
066     */
067    public void action( LdapMessageContainer<ExtendedRequestDecorator<?>> container ) throws DecoderException
068    {
069        ExtendedRequest req;
070
071        // Get the Value and store it in the ExtendedRequest
072        TLV tlv = container.getCurrentTLV();
073
074        // We have to handle the special case of a 0 length matched
075        // OID
076        if ( tlv.getLength() == 0 )
077        {
078            String msg = I18n.err( I18n.ERR_04095 );
079            LOG.error( msg );
080            // This will generate a PROTOCOL_ERROR
081            throw new DecoderException( msg );
082        }
083        else
084        {
085            byte[] requestNameBytes = tlv.getValue().getData();
086
087            try
088            {
089                String requestName = Strings.utf8ToString( requestNameBytes );
090
091                if ( !Oid.isOid( requestName ) )
092                {
093
094                    String msg = "The Request name is not a valid OID : "
095                        + Strings.utf8ToString( requestNameBytes ) + " ("
096                        + Strings.dumpBytes( requestNameBytes ) + ") is invalid";
097                    LOG.error( msg );
098
099                    // throw an exception, we will get a PROTOCOL_ERROR
100                    throw new DecoderException( msg );
101                }
102
103                req = LdapApiServiceFactory.getSingleton().newExtendedRequest( requestName, null );
104                req.setMessageId( container.getMessageId() );
105                container.setMessage( LdapApiServiceFactory.getSingleton().decorate( req ) );
106            }
107            catch ( DecoderException de )
108            {
109                String msg = "The Request name is not a valid OID : "
110                    + Strings.utf8ToString( requestNameBytes ) + " ("
111                    + Strings.dumpBytes( requestNameBytes ) + ") is invalid";
112                LOG.error( "{} : {}", msg, de.getMessage() );
113
114                // Rethrow the exception, we will get a PROTOCOL_ERROR
115                throw de;
116            }
117        }
118
119        // We can have an END transition
120        container.setGrammarEndAllowed( true );
121
122        if ( IS_DEBUG )
123        {
124            LOG.debug( "OID read : {}", req.getRequestName() );
125        }
126    }
127}