1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.api.ldap.codec.actions.addRequest;
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.ldap.codec.api.LdapMessageContainer;
26 import org.apache.directory.api.ldap.codec.decorators.AddRequestDecorator;
27 import org.apache.directory.api.ldap.model.exception.LdapException;
28 import org.apache.directory.api.util.Strings;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32
33
34
35
36
37
38
39
40
41
42
43
44 public class AddAttributeValue extends GrammarAction<LdapMessageContainer<AddRequestDecorator>>
45 {
46
47 private static final Logger LOG = LoggerFactory.getLogger( AddAttributeValue.class );
48
49
50 private static final boolean IS_DEBUG = LOG.isDebugEnabled();
51
52
53
54
55
56 public AddAttributeValue()
57 {
58 super( "Store a value" );
59 }
60
61
62
63
64
65 public void action( LdapMessageContainer<AddRequestDecorator> container )
66 {
67 AddRequestDecorator addRequest = container.getMessage();
68
69 TLV tlv = container.getCurrentTLV();
70
71
72 Object value = null;
73
74 try
75 {
76 if ( tlv.getLength() == 0 )
77 {
78 addRequest.addAttributeValue( "" );
79 }
80 else
81 {
82 if ( container.isBinary( addRequest.getCurrentAttributeType() ) )
83 {
84 value = tlv.getValue().getData();
85
86 if ( IS_DEBUG )
87 {
88 LOG.debug( "Adding value {}", Strings.dumpBytes( ( byte[] ) value ) );
89 }
90
91 addRequest.addAttributeValue( ( byte[] ) value );
92 }
93 else
94 {
95 value = Strings.utf8ToString( tlv.getValue().getData() );
96
97 if ( IS_DEBUG )
98 {
99 LOG.debug( "Adding value {}" + value );
100 }
101
102 addRequest.addAttributeValue( ( String ) value );
103 }
104 }
105 }
106 catch ( LdapException le )
107 {
108
109 }
110
111
112 container.setGrammarEndAllowed( true );
113 }
114 }