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.response.intermediate;
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.IntermediateOperationFactory;
029import org.apache.directory.api.ldap.codec.api.LdapMessageContainer;
030import org.apache.directory.api.ldap.model.message.Control;
031import org.apache.directory.api.ldap.model.message.IntermediateResponse;
032import org.apache.directory.api.ldap.model.message.LdapResult;
033import org.apache.directory.api.util.Strings;
034import org.slf4j.Logger;
035import org.slf4j.LoggerFactory;
036
037
038/**
039 * The action used to store a IntermediateResponse Name
040 * <pre>
041 * IntermediateResponse ::= [APPLICATION 25] SEQUENCE {
042 *     responseName [0] LDAPOID OPTIONAL,
043 * </pre>
044 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
045 */
046public class StoreIntermediateResponseName extends GrammarAction<LdapMessageContainer<IntermediateResponse>>
047{
048    /** The logger */
049    private static final Logger LOG = LoggerFactory.getLogger( StoreIntermediateResponseName.class );
050
051    /**
052     * Instantiates a new response name action.
053     */
054    public StoreIntermediateResponseName()
055    {
056        super( "Store response name" );
057    }
058
059
060    /**
061     * {@inheritDoc}
062     */
063    public void action( LdapMessageContainer<IntermediateResponse> container ) throws DecoderException
064    {
065        // We can get the IntermediateResponse Object
066        IntermediateResponse intermediateResponse = container.getMessage();
067
068        // Get the Value and store it in the IntermediateResponse
069        TLV tlv = container.getCurrentTLV();
070
071        // We have to handle the special case of a 0 length matched
072        // OID.
073        if ( tlv.getLength() == 0 )
074        {
075            String msg = I18n.err( I18n.ERR_05122_NULL_NAME );
076            LOG.error( msg );
077            // This will generate a PROTOCOL_ERROR
078            throw new DecoderException( msg );
079        }
080        else
081        {
082            byte[] responseNameBytes = tlv.getValue().getData();
083
084            // Check if the OID is valid
085            String oidStr = Strings.utf8ToString( responseNameBytes );
086
087            if ( Oid.isOid( oidStr ) )
088            {
089                // Get the factory
090                IntermediateOperationFactory intermediateFactory = 
091                    container.getLdapCodecService().getIntermediateResponseFactories().get( oidStr );
092                
093                if ( intermediateFactory != null )
094                {
095                    // Ok, let's create the new operation, which will replace
096                    // the one created during the init phase
097                    IntermediateResponse newIntermediateResponse = intermediateFactory.newResponse();
098                    newIntermediateResponse.setMessageId( intermediateResponse.getMessageId() );
099                    
100                    // Copy the LdapResult 
101                    LdapResult ldapResult = intermediateResponse.getLdapResult();
102                    newIntermediateResponse.getLdapResult().setDiagnosticMessage( ldapResult.getDiagnosticMessage() );
103                    newIntermediateResponse.getLdapResult().setMatchedDn( ldapResult.getMatchedDn() );
104                    newIntermediateResponse.getLdapResult().setReferral( ldapResult.getReferral() );
105                    newIntermediateResponse.getLdapResult().setResultCode( ldapResult.getResultCode() );
106                    
107                    // Copy the controls
108                    for ( Control control : intermediateResponse.getControls().values() )
109                    {
110                        newIntermediateResponse.addControl( control );
111                    }
112                    
113                    container.setMessage( newIntermediateResponse );
114                    container.setIntermediateFactory( intermediateFactory );
115                }
116                else
117                {
118                    // We simply store the OID in teh existing message
119                    intermediateResponse.setResponseName( oidStr );
120                }
121            }
122            else
123            {
124                String msg = I18n.err( I18n.ERR_05133_INTERMEDIATE_RESPONSE_INVALID_OID, Strings.utf8ToString( responseNameBytes ), 
125                    Strings.dumpBytes( responseNameBytes ) );
126                LOG.error( I18n.err( I18n.ERR_05114_ERROR_MESSAGE, msg, oidStr ) );
127
128                // Rethrow the exception, we will get a PROTOCOL_ERROR
129                throw new DecoderException( msg );
130            }
131        }
132
133        // We can have an END transition
134        container.setGrammarEndAllowed( true );
135
136        if ( LOG.isDebugEnabled() )
137        {
138            LOG.debug( I18n.msg( I18n.MSG_05172_OID_READ, intermediateResponse.getResponseName() ) );
139        }
140    }
141}