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.reponse;
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      public ExtendedResponseDsml( LdapApiService codec )
52      {
53          super( codec, new ExtendedResponseImpl( "" ) );
54      }
55  
56  
57      /**
58       * Creates a new getDecoratedMessage() of ExtendedResponseDsml.
59       *
60       * @param ldapMessage
61       *      the message to decorate
62       */
63      public ExtendedResponseDsml( LdapApiService codec, ExtendedResponse ldapMessage )
64      {
65          super( codec, ldapMessage );
66      }
67  
68  
69      /**
70       * {@inheritDoc}
71       */
72      public MessageTypeEnum getType()
73      {
74          return getDecorated().getType();
75      }
76  
77  
78      /**
79       * {@inheritDoc}
80       */
81      public Element toDsml( Element root )
82      {
83          Element element = null;
84  
85          if ( root != null )
86          {
87              element = root.addElement( EXTENDED_RESPONSE_TAG );
88          }
89          else
90          {
91              element = new DefaultElement( EXTENDED_RESPONSE_TAG );
92          }
93  
94          ExtendedResponse extendedResponse = getDecorated();
95  
96          // LDAP Result
97          LdapResultDsml ldapResultDsml = new LdapResultDsml( getCodecService(),
98              getDecorated().getLdapResult(), getDecorated() );
99          ldapResultDsml.toDsml( element );
100 
101         // ResponseName
102         String responseName = extendedResponse.getResponseName();
103         if ( responseName != null )
104         {
105             element.addElement( "responseName" ).addText( responseName );
106         }
107 
108         // Response
109         Object response = getResponseValue();
110 
111         if ( response != null )
112         {
113             if ( ParserUtils.needsBase64Encoding( response ) )
114             {
115                 Namespace xsdNamespace = new Namespace( ParserUtils.XSD, ParserUtils.XML_SCHEMA_URI );
116                 Namespace xsiNamespace = new Namespace( ParserUtils.XSI, ParserUtils.XML_SCHEMA_INSTANCE_URI );
117                 element.getDocument().getRootElement().add( xsdNamespace );
118                 element.getDocument().getRootElement().add( xsiNamespace );
119 
120                 Element responseElement = element.addElement( "response" )
121                     .addText( ParserUtils.base64Encode( response ) );
122                 responseElement.addAttribute( new QName( "type", xsiNamespace ), ParserUtils.XSD + ":"
123                     + ParserUtils.BASE64BINARY );
124             }
125             else
126             {
127                 element.addElement( "response" ).addText( Strings.utf8ToString( ( byte[] ) response ) );
128             }
129         }
130 
131         return element;
132     }
133 
134 
135     /**
136      * {@inheritDoc}
137      */
138     public void setResponseName( String oid )
139     {
140         getDecorated().setResponseName( oid );
141     }
142 
143 
144     /**
145      * Get the extended response name
146      * 
147      * @return Returns the name.
148      */
149     public String getResponseName()
150     {
151         return getDecorated().getResponseName();
152     }
153 
154 
155     /**
156      * Set the extended response name
157      * 
158      * @param responseName The name to set.
159      */
160     public void setResponseName( Oid responseName )
161     {
162         getDecorated().setResponseName( responseName.toString() );
163     }
164 
165 
166     /**
167      * Get the extended response
168      * 
169      * @return Returns the response.
170      */
171     public byte[] getResponseValue()
172     {
173         return this.response;
174     }
175 
176 
177     /**
178      * Set the extended response
179      * 
180      * @param response The response to set.
181      */
182     public void setResponseValue( byte[] response )
183     {
184         this.response = response;
185     }
186 }