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 @SuppressWarnings("unchecked") 063 public WhoAmIResponseGrammar() 064 { 065 setName( WhoAmIResponseGrammar.class.getName() ); 066 067 // Create the transitions table 068 super.transitions = new GrammarTransition[WhoAmIResponseStatesEnum.LAST_WHO_AM_I_RESPONSE_STATE 069 .ordinal()][256]; 070 071 /** 072 * Transition from init state to WhoAmI Authzid Response Value 073 * 074 * authzId ::= OCTET STRING OPTIONAL 075 * 076 * Creates the authzid object 077 */ 078 super.transitions[WhoAmIResponseStatesEnum.START_STATE.ordinal()][UniversalTag.OCTET_STRING.getValue()] = 079 new GrammarTransition<WhoAmIResponseContainer>( 080 WhoAmIResponseStatesEnum.START_STATE, 081 WhoAmIResponseStatesEnum.AUTHZ_ID_RESPONSE_STATE, 082 UniversalTag.OCTET_STRING.getValue(), new GrammarAction<WhoAmIResponseContainer>( 083 "Store AuthzId" ) 084 { 085 public void action( WhoAmIResponseContainer container ) throws DecoderException 086 { 087 WhoAmIResponseDecorator whoAmIResponse = new WhoAmIResponseDecorator( 088 LdapApiServiceFactory.getSingleton(), new WhoAmIResponseImpl() ); 089 container.setWhoAmIResponse( whoAmIResponse ); 090 091 byte[] data = container.getCurrentTLV().getValue().getData(); 092 093 if ( data != null ) 094 { 095 switch ( data.length ) 096 { 097 case 0: 098 // Error 099 case 1: 100 // Error 101 String msg = "authzId too short. Must starts with either u: or dn:"; 102 LOG.error( msg ); 103 throw new DecoderException( msg ); 104 105 case 2 : 106 if ( ( data[0] == 'u' ) && ( data[1] == ':' ) ) 107 { 108 whoAmIResponse.setAuthzId( data ); 109 whoAmIResponse.setUserId( Strings.utf8ToString( data, 3, data.length - 3 ) ); 110 } 111 else 112 { 113 msg = "authzId Must starts with either u: or dn:, it starts with " + Strings.utf8ToString( data ); 114 LOG.error( msg ); 115 throw new DecoderException( msg ); 116 } 117 118 break; 119 120 default : 121 switch ( data[0] ) 122 { 123 case 'u' : 124 if ( data[1] == ':' ) 125 { 126 whoAmIResponse.setAuthzId( data ); 127 whoAmIResponse.setUserId( Strings.utf8ToString( data, 3, data.length - 3 ) ); 128 } 129 else 130 { 131 msg = "authzId Must starts with either u: or dn:, it starts with " + Strings.utf8ToString( data ); 132 LOG.error( msg ); 133 throw new DecoderException( msg ); 134 } 135 136 break; 137 138 case 'd' : 139 if ( ( data[1] == 'n' ) && ( data[2] == ':' ) ) 140 { 141 // Check that the remaining bytes are a valid DN 142 if ( Dn.isValid( Strings.utf8ToString( data, 3, data.length - 3 ) ) ) 143 { 144 whoAmIResponse.setAuthzId( data ); 145 146 try 147 { 148 whoAmIResponse.setDn( new Dn( Strings.utf8ToString( data, 3, data.length - 3 ) ) ); 149 } 150 catch ( LdapInvalidDnException e ) 151 { 152 // Should never happen 153 } 154 } 155 else 156 { 157 msg = "authzId Must starts with either u: or dn:, it starts with " + Strings.utf8ToString( data ); 158 LOG.error( msg ); 159 throw new DecoderException( msg ); 160 } 161 } 162 else 163 { 164 msg = "authzId Must starts with either u: or dn:, it starts with " + Strings.utf8ToString( data ); 165 LOG.error( msg ); 166 throw new DecoderException( msg ); 167 } 168 169 break; 170 171 default : 172 msg = "authzId Must starts with either u: or dn:, it starts with " + Strings.utf8ToString( data ); 173 LOG.error( msg ); 174 throw new DecoderException( msg ); 175 } 176 177 break; 178 } 179 } 180 else 181 { 182 whoAmIResponse.setAuthzId( null ); 183 } 184 185 // We may have nothing left 186 container.setGrammarEndAllowed( true ); 187 } 188 } ); 189 } 190 191 192 /** 193 * This class is a singleton. 194 * 195 * @return An instance on this grammar 196 */ 197 public static Grammar<WhoAmIResponseContainer> getInstance() 198 { 199 return instance; 200 } 201}