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 java.util.ArrayList;
24  import java.util.List;
25  
26  import org.apache.directory.api.dsmlv2.DsmlDecorator;
27  import org.apache.directory.api.dsmlv2.ParserUtils;
28  import org.apache.directory.api.ldap.model.message.Response;
29  import org.dom4j.Document;
30  import org.dom4j.DocumentHelper;
31  import org.dom4j.Element;
32  
33  
34  /**
35   * This class represents the Batch Response. It can be used to generate an the XML String of a BatchResponse.
36   *
37   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
38   */
39  public class BatchResponseDsml
40  {
41      /** The Responses list */
42      private List<DsmlDecorator<? extends Response>> responses;
43  
44      /** The ID of the response */
45      private int requestID;
46  
47  
48      /**
49       * Creates a new instance of BatchResponseDsml.
50       */
51      public BatchResponseDsml()
52      {
53          responses = new ArrayList<DsmlDecorator<? extends Response>>();
54      }
55  
56  
57      /**
58       * Gets the current response
59       *
60       * @return
61       *      the current response
62       */
63      public DsmlDecorator<? extends Response> getCurrentResponse()
64      {
65          return responses.get( responses.size() - 1 );
66      }
67  
68  
69      /**
70       * Adds a request to the Batch Response DSML.
71       *
72       * @param response
73       *      the request to add
74       * @return
75       *      true (as per the general contract of the Collection.add method).
76       */
77      public boolean addResponse( DsmlDecorator<? extends Response> response )
78      {
79          return responses.add( response );
80      }
81  
82  
83      /**
84       * Removes a request from the Batch Response DSML.
85       *
86       * @param response
87       *      the request to remove
88       * @return
89       *      true if this list contained the specified element.
90       */
91      public boolean removeResponse( DsmlDecorator<Response> response )
92      {
93          return responses.remove( response );
94      }
95  
96  
97      /**
98       * Gets the ID of the response
99       * @return
100      *      the ID of the response
101      */
102     public int getRequestID()
103     {
104         return requestID;
105     }
106 
107 
108     /**
109      * Sets the ID of the response
110      *
111      * @param requestID
112      *      the ID to set
113      */
114     public void setRequestID( int requestID )
115     {
116         this.requestID = requestID;
117     }
118 
119 
120     /**
121      * Gets the List of all the responses
122      *
123      * @return
124      *      the List of all the responses
125      */
126     public List<DsmlDecorator<? extends Response>> getResponses()
127     {
128         return responses;
129     }
130 
131 
132     /**
133      * Converts this Batch Response to its XML representation in the DSMLv2 format.
134      * The XML document will be formatted for pretty printing by default. 
135      * 
136      * @see {@link #toDsml(boolean)}
137      * 
138      * @return the XML representation in DSMLv2 format
139      */
140     public String toDsml()
141     {
142        return toDsml( true ); 
143     }
144     
145     
146     /**
147      * Converts this Batch Response to its XML representation in the DSMLv2 format.
148      * 
149      * @param prettyPrint if true, formats the document for pretty printing
150      * @return the XML representation in DSMLv2 format
151      */
152     public String toDsml( boolean prettyPrint )
153     {
154         Document document = DocumentHelper.createDocument();
155         Element element = document.addElement( "batchResponse" );
156 
157         element.add( ParserUtils.DSML_NAMESPACE );
158         element.add( ParserUtils.XSD_NAMESPACE );
159         element.add( ParserUtils.XSI_NAMESPACE );
160 
161         // RequestID
162         if ( requestID != 0 )
163         {
164             element.addAttribute( "requestID", "" + requestID );
165         }
166 
167         for ( DsmlDecorator<? extends Response> response : responses )
168         {
169             response.toDsml( element );
170         }
171 
172         if( prettyPrint )
173         {
174             document = ParserUtils.styleDocument( document );
175         }
176         
177         return document.asXML();
178     }
179 }