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.response.search.entry;
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.SearchResultEntry;
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 search result entry
37   * <pre>
38   * PartialAttributeList ::= 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 StoreSearchResultAttributeValue extends GrammarAction<LdapMessageContainer<SearchResultEntry>>
47  {
48      /** The logger */
49      private static final Logger LOG = LoggerFactory.getLogger( StoreSearchResultAttributeValue.class );
50  
51      /**
52       * Instantiates a new search result attribute value action.
53       */
54      public StoreSearchResultAttributeValue()
55      {
56          super( "Stores AttributeValue" );
57      }
58  
59  
60      /**
61       * {@inheritDoc}
62       */
63      public void action( LdapMessageContainer<SearchResultEntry> container )
64      {
65          Attribute currentAttribute = container.getCurrentAttribute();
66  
67          TLV tlv = container.getCurrentTLV();
68  
69          // Store the value
70          try
71          {
72              if ( tlv.getLength() == 0 )
73              {
74                  currentAttribute.add( "" );
75  
76                  if ( LOG.isDebugEnabled() )
77                  {
78                      LOG.debug( I18n.msg( I18n.MSG_05180_NULL_ATTRIBUTE_VALUE ) );
79                  }
80              }
81              else
82              {
83                  if ( container.isBinary( container.getCurrentAttribute().getId() ) )
84                  {
85                      byte[] value = tlv.getValue().getData();
86                      currentAttribute.add( value );
87  
88                      if ( LOG.isDebugEnabled() )
89                      {
90                          LOG.debug( I18n.msg( I18n.MSG_05181_ATTRIBUTE_VALUE, Strings.dumpBytes( ( byte[] ) value ) ) );
91                      }
92                  }
93                  else
94                  {
95                      String value = Strings.utf8ToString( tlv.getValue().getData() );
96                      currentAttribute.add( value );
97  
98                      if ( LOG.isDebugEnabled() )
99                      {
100                         LOG.debug( I18n.msg( I18n.MSG_05181_ATTRIBUTE_VALUE, value ) );
101                     }
102                 }
103 
104             }
105         }
106         catch ( LdapException le )
107         {
108             // Just swallow the exception, it can't occur here
109         }
110 
111         // We can have an END transition
112         container.setGrammarEndAllowed( true );
113     }
114 }