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.response.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.ExtendedResponse;
032import org.apache.directory.api.util.Strings;
033import org.slf4j.Logger;
034import org.slf4j.LoggerFactory;
035
036
037/**
038 * The action used to store a Response Name to an ExtendedResponse
039 * <pre>
040 * LdapMessage ::= ... ExtendedResponse ...
041 * ExtendedResponse ::= [APPLICATION 24] SEQUENCE {
042 *     COMPONENTS OF LDAPResult,
043 *     responseName   [10] LDAPOID OPTIONAL,
044 *     ...
045 * </pre>
046 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
047 */
048public class StoreExtendedResponseName extends GrammarAction<LdapMessageContainer<ExtendedResponse>>
049{
050    /** The logger */
051    private static final Logger LOG = LoggerFactory.getLogger( StoreExtendedResponseName.class );
052
053    /**
054     * Instantiates a new response name action.
055     */
056    public StoreExtendedResponseName()
057    {
058        super( "Store response name" );
059    }
060
061
062    /**
063     * {@inheritDoc}
064     */
065    public void action( LdapMessageContainer<ExtendedResponse> container ) throws DecoderException
066    {
067        // Get the Name and store it in the ExtendedResponse. That will
068        // allow us to find the proper extended response instance, if it's 
069        // already declared. Otherwise, we will use a default ExtendedResponse
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            throw new DecoderException( msg );
080        }
081        else
082        {
083            byte[] responseNameBytes = tlv.getValue().getData();
084            String responseName = Strings.asciiBytesToString( responseNameBytes );
085
086            try
087            {
088                // Check the OID first, if it's invalid, reject the operation
089                if ( !Oid.isOid( responseName ) )
090                {
091                    String msg = I18n.err( I18n.ERR_05159_INVALID_RESPONSE_NAME_OID,
092                        responseName, Strings.dumpBytes( responseNameBytes ) );
093                    LOG.error( msg );
094    
095                    // throw an exception, we will get a PROTOCOL_ERROR
096                    throw new DecoderException( msg );
097                }
098    
099                // Get the extended request factory from the LdapApiService, if it's registered
100                LdapApiService codec = container.getLdapCodecService();
101                ExtendedOperationFactory factory = codec.getExtendedResponseFactories().get( responseName );
102                ExtendedResponse extendedResponse = container.getMessage();
103                
104                if ( factory != null )
105                {
106                    // Create the extended response
107                    extendedResponse = factory.newResponse();
108
109                    // Move the LDAPResult in the newly created response
110                    LdapMessageContainer.copyLdapResult( container.getMessage(), extendedResponse );
111                    extendedResponse.setMessageId( container.getMessageId() );
112                    container.setMessage( extendedResponse );
113                }
114                else
115                {
116                   extendedResponse.setResponseName( responseName );
117                }
118
119                container.setExtendedFactory( factory );
120                
121                if ( LOG.isDebugEnabled() )
122                {
123                    LOG.debug( I18n.msg( I18n.MSG_05172_OID_READ, extendedResponse.getResponseName() ) );
124                }
125            }
126            catch ( DecoderException de )
127            {
128                String msg = I18n.err( I18n.ERR_05159_INVALID_RESPONSE_NAME_OID,
129                    responseName, Strings.dumpBytes( responseNameBytes ) );
130                LOG.error( I18n.err( I18n.ERR_05114_ERROR_MESSAGE, msg, de.getMessage() ) );
131
132                // Rethrow the exception, we will get a PROTOCOL_ERROR
133                throw de;
134            }
135        }
136
137        // We can have an END transition
138        container.setGrammarEndAllowed( true );
139    }
140}