1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * https://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 *
19 */
20 package org.apache.directory.api.ldap.codec.actions.ldapMessage;
21
22
23 import org.apache.directory.api.asn1.DecoderException;
24 import org.apache.directory.api.asn1.ber.grammar.GrammarAction;
25 import org.apache.directory.api.asn1.ber.tlv.BerValue;
26 import org.apache.directory.api.asn1.ber.tlv.IntegerDecoder;
27 import org.apache.directory.api.asn1.ber.tlv.IntegerDecoderException;
28 import org.apache.directory.api.asn1.ber.tlv.TLV;
29 import org.apache.directory.api.i18n.I18n;
30 import org.apache.directory.api.ldap.codec.api.LdapMessageContainer;
31 import org.apache.directory.api.ldap.model.message.Message;
32 import org.apache.directory.api.util.Strings;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36
37 /**
38 * The action used to store the LdapMessage MessageID.
39 * <pre>
40 * LDAPMessage --> ... MessageId ...
41 *
42 * Checks that MessageId is in [0 .. 2147483647] and store the value in
43 * the LdapMessage Object
44 *
45 * (2147483647 = Integer.MAX_VALUE)
46 * The next state will be MESSAGE_ID_STATE
47 *
48 * The message ID will be temporarily stored in the container, because we can't store it
49 * into an object.
50 * </pre>
51 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
52 */
53 public class StoreMessageId extends GrammarAction<LdapMessageContainer<Message>>
54 {
55 /** The logger */
56 private static final Logger LOG = LoggerFactory.getLogger( StoreMessageId.class );
57
58
59 /**
60 * Instantiates a new action.
61 */
62 public StoreMessageId()
63 {
64 super( "Store MessageID" );
65 }
66
67
68 /**
69 * {@inheritDoc}
70 */
71 @Override
72 public void action( LdapMessageContainer<Message> container ) throws DecoderException
73 {
74 // The current TLV should be a integer
75 // We get it and store it in MessageId
76 TLV tlv = container.getCurrentTLV();
77
78 // The Length should not be null
79 if ( tlv.getLength() == 0 )
80 {
81 LOG.error( I18n.err( I18n.ERR_05100_ZERO_LENGTH_MESSAGE_ID_NOT_ALLOWED ) );
82
83 // This will generate a PROTOCOL_ERROR
84 throw new DecoderException( I18n.err( I18n.ERR_05101_NULL_MESSAGE_ID_NOT_ALLOWED ) );
85 }
86
87 BerValue value = tlv.getValue();
88
89 try
90 {
91 int messageId = IntegerDecoder.parse( value, 0, Integer.MAX_VALUE );
92
93 container.setMessageId( messageId );
94
95 if ( LOG.isDebugEnabled() )
96 {
97 LOG.debug( I18n.msg( I18n.MSG_05102_LDAP_MESSAGE_ID_DECODED, messageId ) );
98 }
99 }
100 catch ( IntegerDecoderException ide )
101 {
102 LOG.error( I18n.err( I18n.ERR_05102_INVALID_MESSAGE_ID, Strings.dumpBytes( value.getData() ), ide
103 .getLocalizedMessage() ) );
104
105 // This will generate a PROTOCOL_ERROR
106 throw new DecoderException( ide.getMessage(), ide );
107 }
108 }
109 }