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