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.extras.extended.ads_impl.whoAmI;
021
022
023import org.apache.directory.api.asn1.DecoderException;
024import org.apache.directory.api.asn1.ber.grammar.AbstractGrammar;
025import org.apache.directory.api.asn1.ber.grammar.Grammar;
026import org.apache.directory.api.asn1.ber.grammar.GrammarAction;
027import org.apache.directory.api.asn1.ber.grammar.GrammarTransition;
028import org.apache.directory.api.asn1.ber.tlv.UniversalTag;
029import org.apache.directory.api.ldap.codec.api.LdapApiServiceFactory;
030import org.apache.directory.api.ldap.extras.extended.whoAmI.WhoAmIResponseImpl;
031import org.apache.directory.api.ldap.model.exception.LdapInvalidDnException;
032import org.apache.directory.api.ldap.model.name.Dn;
033import org.apache.directory.api.util.Strings;
034import org.slf4j.Logger;
035import org.slf4j.LoggerFactory;
036
037
038/**
039 * This class implements the WhoAmIResponse extended operation's ASN.1 grammer. 
040 * All the actions are declared in this class. As it is a singleton, 
041 * these declaration are only done once. The grammar is :
042 * 
043 * <pre>
044 *  WhoAmIResponseValue ::= OCTET STRING OPTIONAL }
045 * </pre>
046 * 
047 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
048 */
049
050public class WhoAmIResponseGrammar extends AbstractGrammar<WhoAmIResponseContainer>
051{
052    /** logger */
053    private static final Logger LOG = LoggerFactory.getLogger( WhoAmIResponseGrammar.class );
054
055    /** Speedup for logs */
056    static final boolean IS_DEBUG = LOG.isDebugEnabled();
057
058    /** The instance of grammar. WhoAmIResponseGrammar is a singleton */
059    private static Grammar<WhoAmIResponseContainer> instance = new WhoAmIResponseGrammar();
060
061
062    /**
063     * Creates a new WhoAmIResponseGrammar object.
064     */
065    @SuppressWarnings("unchecked")
066    public WhoAmIResponseGrammar()
067    {
068        setName( WhoAmIResponseGrammar.class.getName() );
069
070        // Create the transitions table
071        super.transitions = new GrammarTransition[WhoAmIResponseStatesEnum.LAST_WHO_AM_I_RESPONSE_STATE
072            .ordinal()][256];
073
074        /**
075         * Transition from init state to WhoAmI Authzid Response Value
076         * 
077         * authzId ::= OCTET STRING OPTIONAL
078         *     
079         * Creates the authzid object
080         */
081        super.transitions[WhoAmIResponseStatesEnum.START_STATE.ordinal()][UniversalTag.OCTET_STRING.getValue()] =
082            new GrammarTransition<WhoAmIResponseContainer>(
083                WhoAmIResponseStatesEnum.START_STATE,
084                WhoAmIResponseStatesEnum.AUTHZ_ID_RESPONSE_STATE,
085                UniversalTag.OCTET_STRING.getValue(), new GrammarAction<WhoAmIResponseContainer>(
086                    "Store AuthzId" )
087                {
088                    public void action( WhoAmIResponseContainer container ) throws DecoderException
089                    {
090                        WhoAmIResponseDecorator whoAmIResponse = new WhoAmIResponseDecorator(
091                            LdapApiServiceFactory.getSingleton(), new WhoAmIResponseImpl() );
092                        container.setWhoAmIResponse( whoAmIResponse );
093                        
094                        byte[] data = container.getCurrentTLV().getValue().getData();
095                        
096                        if ( data != null )
097                        {
098                            switch ( data.length )
099                            {
100                                case 0:
101                                    // Error
102                                case 1:
103                                    // Error
104                                    String msg = "authzId too short. Must starts with either u: or dn:";
105                                    LOG.error( msg );
106                                    throw new DecoderException( msg );
107
108                                case 2 :
109                                    if ( ( data[0] == 'u' ) && ( data[1] == ':' ) )
110                                    {
111                                        whoAmIResponse.setAuthzId( data );
112                                        whoAmIResponse.setUserId( Strings.utf8ToString( data, 3, data.length - 3 ) );
113                                    }
114                                    else
115                                    {
116                                        msg = "authzId Must starts with either u: or dn:, it starts with " + Strings.utf8ToString( data );
117                                        LOG.error( msg );
118                                        throw new DecoderException( msg );
119                                    }
120                                    
121                                    break;
122                                    
123                                default :
124                                    switch ( data[0] )
125                                    {
126                                        case 'u' :
127                                            if ( data[1] == ':' )
128                                            {
129                                                whoAmIResponse.setAuthzId( data );
130                                                whoAmIResponse.setUserId( Strings.utf8ToString( data, 3, data.length - 3 ) );
131                                            }
132                                            else
133                                            {
134                                                msg = "authzId Must starts with either u: or dn:, it starts with " + Strings.utf8ToString( data );
135                                                LOG.error( msg );
136                                                throw new DecoderException( msg );
137                                            }
138                                            
139                                            break;
140                                            
141                                        case 'd' :
142                                            if ( ( data[1] == 'n' ) && ( data[2] == ':' ) )
143                                            {
144                                                // Check that the remaining bytes are a valid DN
145                                                if ( Dn.isValid( Strings.utf8ToString( data, 3, data.length - 3 ) ) )
146                                                {
147                                                    whoAmIResponse.setAuthzId( data );
148                                                    
149                                                    try
150                                                    {
151                                                        whoAmIResponse.setDn( new Dn( Strings.utf8ToString( data, 3, data.length - 3 ) ) );
152                                                    }
153                                                    catch ( LdapInvalidDnException e )
154                                                    {
155                                                        // Should never happen
156                                                    }
157                                                }
158                                                else
159                                                {
160                                                    msg = "authzId Must starts with either u: or dn:, it starts with " + Strings.utf8ToString( data );
161                                                    LOG.error( msg );
162                                                    throw new DecoderException( msg );
163                                                }
164                                            }
165                                            else
166                                            {
167                                                msg = "authzId Must starts with either u: or dn:, it starts with " + Strings.utf8ToString( data );
168                                                LOG.error( msg );
169                                                throw new DecoderException( msg );
170                                            }
171                                            
172                                            break;
173
174                                        default :
175                                            msg = "authzId Must starts with either u: or dn:, it starts with " + Strings.utf8ToString( data );
176                                            LOG.error( msg );
177                                            throw new DecoderException( msg );
178                                    }
179                                    
180                                    break;
181                            }
182                        }
183                        else
184                        {
185                            whoAmIResponse.setAuthzId( null );
186                        }
187
188                        // We may have nothing left
189                        container.setGrammarEndAllowed( true );
190                    }
191                } );
192    }
193
194
195    /**
196     * This class is a singleton.
197     * 
198     * @return An instance on this grammar
199     */
200    public static Grammar<WhoAmIResponseContainer> getInstance()
201    {
202        return instance;
203    }
204}