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.modify;
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.ModifyRequest;
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 modifyRequest
37   * 
38   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
39   */
40  public class StoreModifyRequestAttributeValue extends GrammarAction<LdapMessageContainer<ModifyRequest>>
41  {
42      /** The logger */
43      private static final Logger LOG = LoggerFactory.getLogger( StoreModifyRequestAttributeValue.class );
44  
45      /**
46       * Instantiates a new modify attribute value action.
47       */
48      public StoreModifyRequestAttributeValue()
49      {
50          super( "Stores AttributeValue" );
51      }
52  
53  
54      /**
55       * {@inheritDoc}
56       */
57      @Override
58      public void action( LdapMessageContainer<ModifyRequest> container )
59      {
60          TLV tlv = container.getCurrentTLV();
61  
62          // Store the value. It can't be null
63          byte[] value = Strings.EMPTY_BYTES;
64          Attribute currentAttribute = container.getCurrentAttribute();
65  
66          try
67          {
68              if ( tlv.getLength() == 0 )
69              {
70                  currentAttribute.add( "" );
71              }
72              else
73              {
74                  value = tlv.getValue().getData();
75  
76                  if ( container.isBinary( currentAttribute.getId() ) )
77                  {
78                      container.getCurrentAttribute().add( value );
79                  }
80                  else
81                  {
82                      currentAttribute.add( Strings.utf8ToString( ( byte[] ) value ) );
83                  }
84              }
85          }
86          catch ( LdapException le )
87          {
88              // Just swallow the exception, it can't occur here
89          }
90  
91          // We can have an END transition
92          container.setGrammarEndAllowed( true );
93  
94          if ( LOG.isDebugEnabled() )
95          {
96              LOG.debug( I18n.msg( I18n.MSG_05131_VALUE_MODIFIED, value ) );
97          }
98      }
99  }