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   *    http://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.dsmlv2.response;
21  
22  
23  import org.apache.directory.api.asn1.util.Oid;
24  import org.apache.directory.api.dsmlv2.ParserUtils;
25  import org.apache.directory.api.ldap.codec.api.LdapApiService;
26  import org.apache.directory.api.ldap.model.message.ExtendedResponse;
27  import org.apache.directory.api.ldap.model.message.ExtendedResponseImpl;
28  import org.apache.directory.api.ldap.model.message.MessageTypeEnum;
29  import org.apache.directory.api.util.Strings;
30  import org.dom4j.Element;
31  import org.dom4j.Namespace;
32  import org.dom4j.QName;
33  import org.dom4j.tree.DefaultElement;
34  
35  
36  /**
37   * DSML Decorator for ExtendedResponse
38   *
39   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
40   */
41  public class ExtendedResponseDsml extends AbstractResultResponseDsml<ExtendedResponse>
42      implements ExtendedResponse
43  {
44      private static final String EXTENDED_RESPONSE_TAG = "extendedResponse";
45      private byte[] response;
46  
47  
48      /**
49       * Creates a new getDecoratedMessage() of ExtendedResponseDsml.
50       * 
51       * @param codec The LDAP Service to use
52       */
53      public ExtendedResponseDsml( LdapApiService codec )
54      {
55          super( codec, new ExtendedResponseImpl( "" ) );
56      }
57  
58  
59      /**
60       * Creates a new getDecoratedMessage() of ExtendedResponseDsml.
61       *
62       * @param codec The LDAP Service to use
63       * @param ldapMessage the message to decorate
64       */
65      public ExtendedResponseDsml( LdapApiService codec, ExtendedResponse ldapMessage )
66      {
67          super( codec, ldapMessage );
68      }
69  
70  
71      /**
72       * {@inheritDoc}
73       */
74      public MessageTypeEnum getType()
75      {
76          return getDecorated().getType();
77      }
78  
79  
80      /**
81       * {@inheritDoc}
82       */
83      public Element toDsml( Element root )
84      {
85          Element element = null;
86  
87          if ( root != null )
88          {
89              element = root.addElement( EXTENDED_RESPONSE_TAG );
90          }
91          else
92          {
93              element = new DefaultElement( EXTENDED_RESPONSE_TAG );
94          }
95  
96          ExtendedResponse extendedResponse = getDecorated();
97  
98          // LDAP Result
99          LdapResultDsml ldapResultDsml = new LdapResultDsml( getCodecService(),
100             getDecorated().getLdapResult(), getDecorated() );
101         ldapResultDsml.toDsml( element );
102 
103         // ResponseName
104         String responseName = extendedResponse.getResponseName();
105         if ( responseName != null )
106         {
107             element.addElement( "responseName" ).addText( responseName );
108         }
109 
110         // Response
111         Object responseValue = getResponseValue();
112 
113         if ( responseValue != null )
114         {
115             if ( ParserUtils.needsBase64Encoding( responseValue ) )
116             {
117                 Namespace xsdNamespace = new Namespace( ParserUtils.XSD, ParserUtils.XML_SCHEMA_URI );
118                 Namespace xsiNamespace = new Namespace( ParserUtils.XSI, ParserUtils.XML_SCHEMA_INSTANCE_URI );
119                 element.getDocument().getRootElement().add( xsdNamespace );
120                 element.getDocument().getRootElement().add( xsiNamespace );
121 
122                 Element responseElement = element.addElement( "response" )
123                     .addText( ParserUtils.base64Encode( responseValue ) );
124                 responseElement.addAttribute( new QName( "type", xsiNamespace ), ParserUtils.XSD + ":"
125                     + ParserUtils.BASE64BINARY );
126             }
127             else
128             {
129                 element.addElement( "response" ).addText( Strings.utf8ToString( ( byte[] ) responseValue ) );
130             }
131         }
132 
133         return element;
134     }
135 
136 
137     /**
138      * {@inheritDoc}
139      */
140     public void setResponseName( String oid )
141     {
142         getDecorated().setResponseName( oid );
143     }
144 
145 
146     /**
147      * Get the extended response name
148      * 
149      * @return Returns the name.
150      */
151     public String getResponseName()
152     {
153         return getDecorated().getResponseName();
154     }
155 
156 
157     /**
158      * Set the extended response name
159      * 
160      * @param responseName The name to set.
161      */
162     public void setResponseName( Oid responseName )
163     {
164         getDecorated().setResponseName( responseName.toString() );
165     }
166 
167 
168     /**
169      * Get the extended response
170      * 
171      * @return Returns the response.
172      */
173     public byte[] getResponseValue()
174     {
175         return this.response;
176     }
177 
178 
179     /**
180      * Set the extended response
181      * 
182      * @param responseValue The response to set.
183      */
184     public void setResponseValue( byte[] responseValue )
185     {
186         this.response = responseValue;
187     }
188 }