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 *    https://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.request.extended;
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.ExtendedOperationFactory;
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.ExtendedRequest;
032import org.apache.directory.api.ldap.model.message.OpaqueExtendedRequest;
033import org.apache.directory.api.util.Strings;
034import org.slf4j.Logger;
035import org.slf4j.LoggerFactory;
036
037
038/**
039 * The action used to store the Extended Request name
040 * <pre>
041 * ExtendedRequest ::= [APPLICATION 23] SEQUENCE {
042 *     requestName [0] LDAPOID,
043 *     ...
044 * </pre>
045 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
046 */
047public class StoreExtendedRequestName extends GrammarAction<LdapMessageContainer<ExtendedRequest>>
048{
049    /** The logger */
050    private static final Logger LOG = LoggerFactory.getLogger( StoreExtendedRequestName.class );
051
052    /**
053     * Instantiates a new action.
054     */
055    public StoreExtendedRequestName()
056    {
057        super( "Store ExtendedRequest Name" );
058    }
059
060
061    /**
062     * {@inheritDoc}
063     */
064    @Override
065    public void action( LdapMessageContainer<ExtendedRequest> container ) throws DecoderException
066    {
067        // Get the Name and store it in the ExtendedRequest. That will
068        // allow us to find the proper extended request instance, if it's 
069        // already declared. Otherwise, we will use a default ExtendedRequest
070        // in which the value will be stored un-decoded.
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_05122_NULL_NAME );
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            String requestName = Strings.utf8ToString( requestNameBytes );
086
087            try
088            {
089                // Check the OID first, if it's invalid, reject the operation
090                if ( !Oid.isOid( requestName ) )
091                {
092                    String msg = I18n.err( I18n.ERR_05121_INVALID_REQUEST_NAME_OID,
093                        requestName, Strings.dumpBytes( requestNameBytes ) );
094                    LOG.error( msg );
095
096                    // throw an exception, we will get a PROTOCOL_ERROR
097                    throw new DecoderException( msg );
098                }
099
100                // Get the extended request factory from the LdapApiService, if it's registered
101                LdapApiService codec = container.getLdapCodecService();
102                ExtendedOperationFactory factory = codec.getExtendedRequestFactories().get( requestName );
103                ExtendedRequest extendedRequest;
104                
105                if ( factory == null )
106                {
107                    // Create a default extended request operation
108                    extendedRequest = new OpaqueExtendedRequest();
109                }
110                else
111                {
112                    // Create the extended request
113                    extendedRequest = factory.newRequest();
114                }
115
116                extendedRequest.setMessageId( container.getMessageId() );
117                container.setMessage( extendedRequest );
118
119                if ( LOG.isDebugEnabled() )
120                {
121                    LOG.debug( I18n.msg( I18n.MSG_05126_OID_READ, extendedRequest.getRequestName() ) );
122                }
123            }
124            catch ( DecoderException de )
125            {
126                String msg = I18n.err( I18n.ERR_05121_INVALID_REQUEST_NAME_OID,
127                    requestName, Strings.dumpBytes( requestNameBytes ) );
128                LOG.error( I18n.err( I18n.ERR_05114_ERROR_MESSAGE, msg, de.getMessage() ) );
129
130                // Rethrow the exception, we will get a PROTOCOL_ERROR
131                throw de;
132            }
133        }
134
135        // We can have an END transition
136        container.setGrammarEndAllowed( true );
137    }
138}