001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 *
019 */
020 package org.apache.directory.shared.dsmlv2.reponse;
021
022
023 import java.util.ArrayList;
024 import java.util.List;
025
026 import org.apache.directory.shared.dsmlv2.DsmlDecorator;
027 import org.apache.directory.shared.dsmlv2.ParserUtils;
028 import org.dom4j.Document;
029 import org.dom4j.DocumentHelper;
030 import org.dom4j.Element;
031
032
033 /**
034 * This class represents the Batch Response. It can be used to generate an the XML String of a BatchResponse.
035 *
036 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
037 * @version $Rev$, $Date$
038 */
039 public class BatchResponseDsml
040 {
041 /** The Responses list */
042 private List<DsmlDecorator> responses;
043
044 /** The ID of the response */
045 private int requestID;
046
047
048 /**
049 * Creates a new instance of BatchResponseDsml.
050 */
051 public BatchResponseDsml()
052 {
053 responses = new ArrayList<DsmlDecorator>();
054 }
055
056
057 /**
058 * Adds a request to the Batch Response DSML.
059 *
060 * @param response
061 * the request to add
062 * @return
063 * true (as per the general contract of the Collection.add method).
064 */
065 public boolean addResponse( DsmlDecorator response )
066 {
067 return responses.add( response );
068 }
069
070
071 /**
072 * Removes a request from the Batch Response DSML.
073 *
074 * @param response
075 * the request to remove
076 * @return
077 * true if this list contained the specified element.
078 */
079 public boolean removeResponse( DsmlDecorator response )
080 {
081 return responses.remove( response );
082 }
083
084
085 /**
086 * Gets the ID of the response
087 * @return
088 * the ID of the response
089 */
090 public int getRequestID()
091 {
092 return requestID;
093 }
094
095
096 /**
097 * Sets the ID of the response
098 *
099 * @param requestID
100 * the ID to set
101 */
102 public void setRequestID( int requestID )
103 {
104 this.requestID = requestID;
105 }
106
107
108 /**
109 * Converts the Batch Response to its XML representation in the DSMLv2 format.
110 */
111 public String toDsml()
112 {
113 Document document = DocumentHelper.createDocument();
114 Element element = document.addElement( "batchResponse" );
115
116 // RequestID
117 if ( requestID != 0 )
118 {
119 element.addAttribute( "requestID", "" + requestID );
120 }
121
122 for ( DsmlDecorator response : responses )
123 {
124 response.toDsml( element );
125 }
126
127 return ParserUtils.styleDocument( document ).asXML();
128 }
129 }