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