View Javadoc
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.request.add;
21  
22  
23  import org.apache.directory.api.asn1.ber.grammar.GrammarAction;
24  import org.apache.directory.api.asn1.ber.tlv.TLV;
25  import org.apache.directory.api.i18n.I18n;
26  import org.apache.directory.api.ldap.codec.api.LdapMessageContainer;
27  import org.apache.directory.api.ldap.model.entry.Attribute;
28  import org.apache.directory.api.ldap.model.exception.LdapException;
29  import org.apache.directory.api.ldap.model.message.AddRequest;
30  import org.apache.directory.api.util.Strings;
31  import org.slf4j.Logger;
32  import org.slf4j.LoggerFactory;
33  
34  
35  /**
36   * The action used to store a Value to an AddRequest
37   * <pre>
38   * AttributeList ::= SEQUENCE OF SEQUENCE {
39   *     ...
40   *     vals SET OF AttributeValue }
41   *
42   * AttributeValue OCTET STRING
43   * </pre>
44   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
45   */
46  public class AddAttributeValue extends GrammarAction<LdapMessageContainer<AddRequest>>
47  {
48      /** The logger */
49      private static final Logger LOG = LoggerFactory.getLogger( AddAttributeValue.class );
50  
51      /**
52       * Instantiates a new value action.
53       */
54      public AddAttributeValue()
55      {
56          super( "Store a value" );
57      }
58  
59  
60      /**
61       * {@inheritDoc}
62       */
63      @Override
64      public void action( LdapMessageContainer<AddRequest> container )
65      {
66          Attribute currentAttribute = container.getCurrentAttribute();
67  
68          TLV tlv = container.getCurrentTLV();
69  
70          // Store the value. It can't be null
71          Object value = null;
72  
73          try
74          {
75              if ( tlv.getLength() == 0 )
76              {
77                  currentAttribute.add( "" );
78              }
79              else
80              {
81                  if ( container.isBinary( currentAttribute.getId() ) )
82                  {
83                      value = tlv.getValue().getData();
84  
85                      if ( LOG.isDebugEnabled() )
86                      {
87                          LOG.debug( I18n.msg( I18n.MSG_05112_ADDING_VALUE, Strings.dumpBytes( ( byte[] ) value ) ) );
88                      }
89  
90                      currentAttribute.add( ( byte[] ) value );
91                  }
92                  else
93                  {
94                      value = Strings.utf8ToString( tlv.getValue().getData() );
95  
96                      if ( LOG.isDebugEnabled() )
97                      {
98                          LOG.debug( I18n.msg( I18n.MSG_05112_ADDING_VALUE, value ) );
99                      }
100 
101                     currentAttribute.add( ( String ) value );
102                 }
103             }
104         }
105         catch ( LdapException le )
106         {
107             // Just swallow the exception, it can't occur here
108         }
109 
110         // We can have an END transition
111         container.setGrammarEndAllowed( true );
112     }
113 }