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 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 the current response
61       */
62      public DsmlDecorator<? extends Response> getCurrentResponse()
63      {
64          return responses.get( responses.size() - 1 );
65      }
66  
67  
68      /**
69       * Adds a request to the Batch Response DSML.
70       *
71       * @param response the request to add
72       * @return true (as per the general contract of the Collection.add method).
73       */
74      public boolean addResponse( DsmlDecorator<? extends Response> response )
75      {
76          return responses.add( response );
77      }
78  
79  
80      /**
81       * Removes a request from the Batch Response DSML.
82       *
83       * @param response the request to remove
84       * @return true if this list contained the specified element.
85       */
86      public boolean removeResponse( DsmlDecorator<Response> response )
87      {
88          return responses.remove( response );
89      }
90  
91  
92      /**
93       * Gets the ID of the response
94       * 
95       * @return the ID of the response
96       */
97      public int getRequestID()
98      {
99          return requestID;
100     }
101 
102 
103     /**
104      * Sets the ID of the response
105      *
106      * @param requestID
107      *      the ID to set
108      */
109     public void setRequestID( int requestID )
110     {
111         this.requestID = requestID;
112     }
113 
114 
115     /**
116      * Gets the List of all the responses
117      *
118      * @return
119      *      the List of all the responses
120      */
121     public List<DsmlDecorator<? extends Response>> getResponses()
122     {
123         return responses;
124     }
125 
126 
127     /**
128      * Converts this Batch Response to its XML representation in the DSMLv2 format.
129      * The XML document will be formatted for pretty printing by default. 
130      * 
131      * @return the XML representation in DSMLv2 format
132      */
133     public String toDsml()
134     {
135        return toDsml( true ); 
136     }
137     
138     
139     /**
140      * Converts this Batch Response to its XML representation in the DSMLv2 format.
141      * 
142      * @param prettyPrint if true, formats the document for pretty printing
143      * @return the XML representation in DSMLv2 format
144      */
145     public String toDsml( boolean prettyPrint )
146     {
147         Document document = DocumentHelper.createDocument();
148         Element element = document.addElement( "batchResponse" );
149 
150         element.add( ParserUtils.DSML_NAMESPACE );
151         element.add( ParserUtils.XSD_NAMESPACE );
152         element.add( ParserUtils.XSI_NAMESPACE );
153 
154         // RequestID
155         if ( requestID != 0 )
156         {
157             element.addAttribute( "requestID", Integer.toString( requestID ) );
158         }
159 
160         for ( DsmlDecorator<? extends Response> response : responses )
161         {
162             response.toDsml( element );
163         }
164 
165         if ( prettyPrint )
166         {
167             document = ParserUtils.styleDocument( document );
168         }
169         
170         return document.asXML();
171     }
172 }