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.controls.search.pagedSearch; 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.BerValue; 029import org.apache.directory.api.asn1.ber.tlv.IntegerDecoder; 030import org.apache.directory.api.asn1.ber.tlv.IntegerDecoderException; 031import org.apache.directory.api.asn1.ber.tlv.UniversalTag; 032import org.apache.directory.api.i18n.I18n; 033import org.apache.directory.api.util.Strings; 034import org.slf4j.Logger; 035import org.slf4j.LoggerFactory; 036 037 038/** 039 * This class implements the PagedSearchControl. All the actions are declared in 040 * this class. As it is a singleton, these declaration are only done once. 041 * 042 * The decoded grammar is the following : 043 * 044 * realSearchControlValue ::= SEQUENCE { 045 * size INTEGER, 046 * cookie OCTET STRING, 047 * } 048 * 049 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 050 */ 051public final class PagedResultsGrammar extends AbstractGrammar<PagedResultsContainer> 052{ 053 /** The logger */ 054 static final Logger LOG = LoggerFactory.getLogger( PagedResultsGrammar.class ); 055 056 /** Speedup for logs */ 057 static final boolean IS_DEBUG = LOG.isDebugEnabled(); 058 059 /** The instance of grammar. PagedSearchControlGrammar is a singleton */ 060 private static Grammar<?> instance = new PagedResultsGrammar(); 061 062 063 /** 064 * Creates a new PagedSearchControlGrammar object. 065 */ 066 @SuppressWarnings("unchecked") 067 private PagedResultsGrammar() 068 { 069 setName( PagedResultsGrammar.class.getName() ); 070 071 // Create the transitions table 072 super.transitions = new GrammarTransition[PagedResultsStates.LAST_PAGED_SEARCH_STATE.ordinal()][256]; 073 074 /** 075 * Transition from initial state to PagedSearch sequence 076 * realSearchControlValue ::= SEQUENCE OF { 077 * ... 078 * 079 * Nothing to do 080 */ 081 super.transitions[PagedResultsStates.START_STATE.ordinal()][UniversalTag.SEQUENCE.getValue()] = 082 new GrammarTransition<PagedResultsContainer>( PagedResultsStates.START_STATE, 083 PagedResultsStates.PAGED_SEARCH_SEQUENCE_STATE, 084 UniversalTag.SEQUENCE.getValue(), null ); 085 086 /** 087 * Transition from PagedSearch sequence to size 088 * 089 * realSearchControlValue ::= SEQUENCE OF { 090 * size INTEGER, -- INTEGER (0..maxInt), 091 * ... 092 * 093 * Stores the size value 094 */ 095 super.transitions[PagedResultsStates.PAGED_SEARCH_SEQUENCE_STATE.ordinal()][UniversalTag.INTEGER.getValue()] = 096 new GrammarTransition<PagedResultsContainer>( PagedResultsStates.PAGED_SEARCH_SEQUENCE_STATE, 097 PagedResultsStates.SIZE_STATE, 098 UniversalTag.INTEGER.getValue(), 099 new GrammarAction<PagedResultsContainer>( "Set PagedSearchControl size" ) 100 { 101 public void action( PagedResultsContainer container ) throws DecoderException 102 { 103 BerValue value = container.getCurrentTLV().getValue(); 104 105 try 106 { 107 // Check that the value is into the allowed interval 108 int size = IntegerDecoder.parse( value, Integer.MIN_VALUE, Integer.MAX_VALUE ); 109 110 // We allow negative value to absorb a bug in some M$ client. 111 // Those negative values will be transformed to Integer.MAX_VALUE. 112 if ( size < 0 ) 113 { 114 size = Integer.MAX_VALUE; 115 } 116 117 if ( IS_DEBUG ) 118 { 119 LOG.debug( "size = " + size ); 120 } 121 122 container.getDecorator().setSize( size ); 123 } 124 catch ( IntegerDecoderException ide ) 125 { 126 String msg = I18n.err( I18n.ERR_04050 ); 127 LOG.error( msg, ide ); 128 throw new DecoderException( msg, ide ); 129 } 130 } 131 } ); 132 133 /** 134 * Transition from size to cookie 135 * realSearchControlValue ::= SEQUENCE OF { 136 * ... 137 * cookie OCTET STRING 138 * } 139 * 140 * Stores the cookie flag 141 */ 142 super.transitions[PagedResultsStates.SIZE_STATE.ordinal()][UniversalTag.OCTET_STRING.getValue()] = 143 new GrammarTransition<PagedResultsContainer>( PagedResultsStates.SIZE_STATE, 144 PagedResultsStates.COOKIE_STATE, UniversalTag.OCTET_STRING.getValue(), 145 new GrammarAction<PagedResultsContainer>( "Set PagedSearchControl cookie" ) 146 { 147 public void action( PagedResultsContainer container ) throws DecoderException 148 { 149 BerValue value = container.getCurrentTLV().getValue(); 150 151 if ( container.getCurrentTLV().getLength() == 0 ) 152 { 153 container.getDecorator().setCookie( Strings.EMPTY_BYTES ); 154 } 155 else 156 { 157 container.getDecorator().setCookie( value.getData() ); 158 } 159 160 // We can have an END transition 161 container.setGrammarEndAllowed( true ); 162 } 163 } ); 164 } 165 166 167 /** 168 * This class is a singleton. 169 * 170 * @return An instance on this grammar 171 */ 172 public static Grammar<?> getInstance() 173 { 174 return instance; 175 } 176}