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.ldap.codec.api.LdapApiService;
28  import org.apache.directory.api.ldap.model.message.Message;
29  import org.apache.directory.api.ldap.model.message.Response;
30  import org.apache.directory.api.ldap.model.message.SearchResultDone;
31  import org.apache.directory.api.ldap.model.message.SearchResultEntry;
32  import org.apache.directory.api.ldap.model.message.SearchResultReference;
33  import org.dom4j.Element;
34  import org.dom4j.tree.DefaultElement;
35  
36  
37  /**
38   * This class represents the Search Response Dsml Container. 
39   * It is used to store Search Responses (Search Result Entry, 
40   * Search Result Reference and SearchResultDone).
41   *
42   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
43   */
44  public class SearchResponseDsml extends AbstractResponseDsml<Response>
45  {
46      private static final String SEARCH_RESPONSE_TAG = "searchResponse";
47  
48      /** The responses */
49      private List<DsmlDecorator<? extends Response>> responses =
50          new ArrayList<DsmlDecorator<? extends Response>>();
51  
52  
53      /**
54       * Creates a new getDecoratedMessage() of SearchResponseDsml.
55       * 
56       * @param codec The LDAP Service to use
57       */
58      public SearchResponseDsml( LdapApiService codec )
59      {
60          super( codec, new SearchResponse() );
61      }
62  
63  
64      /**
65       * Creates a new getDecoratedMessage() of SearchResponseDsml.
66       *
67       * @param codec The LDAP Service to use
68       * @param response the LDAP response message to decorate
69       */
70      public SearchResponseDsml( LdapApiService codec, Message response )
71      {
72          super( codec, ( Response ) response );
73      }
74  
75  
76      /**
77       * Adds a response.
78       *
79       * @param response
80       *      the response to add
81       * @return
82       *      true (as per the general contract of the Collection.add method).
83       */
84      public boolean addResponse( DsmlDecorator<? extends Response> response )
85      {
86          if ( response instanceof SearchResultEntry )
87          {
88              ( ( SearchResponse ) getDecorated() ).addSearchResultEntry(
89                  ( SearchResultEntryDsml ) response );
90          }
91          else if ( response instanceof SearchResultReference )
92          {
93              ( ( SearchResponse ) getDecorated() ).addSearchResultReference(
94                  ( SearchResultReferenceDsml ) response );
95          }
96          else if ( response instanceof SearchResultDone )
97          {
98              ( ( SearchResponse ) getDecorated() ).setSearchResultDone(
99                  ( SearchResultDoneDsml ) response );
100         }
101         else
102         {
103             throw new IllegalArgumentException( "Unidentified search resp type" );
104         }
105 
106         return responses.add( response );
107     }
108 
109 
110     /**
111      * Removes a response.
112      *
113      * @param response
114      *      the response to remove
115      * @return
116      *      true if this list contained the specified element.
117      */
118     public boolean removeResponse( DsmlDecorator<? extends Response> response )
119     {
120         if ( response instanceof SearchResultEntry )
121         {
122             ( ( SearchResponse ) getDecorated() ).removeSearchResultEntry(
123                 ( SearchResultEntryDsml ) response );
124         }
125         else if ( response instanceof SearchResultReference )
126         {
127             ( ( SearchResponse ) getDecorated() ).removeSearchResultReference(
128                 ( SearchResultReferenceDsml ) response );
129         }
130         else if ( response instanceof SearchResultDone )
131         {
132             if ( response.equals( ( ( SearchResponse ) getDecorated() ).getSearchResultDone() ) )
133             {
134                 ( ( SearchResponse ) getDecorated() ).setSearchResultDone( null );
135             }
136         }
137         else
138         {
139             throw new IllegalArgumentException( "Unidentified search resp type" );
140         }
141 
142         return responses.remove( response );
143     }
144 
145 
146     /**
147      * {@inheritDoc}
148      */
149     public Element toDsml( Element root )
150     {
151         Element element = null;
152 
153         if ( root != null )
154         {
155             element = root.addElement( SEARCH_RESPONSE_TAG );
156         }
157         else
158         {
159             element = new DefaultElement( SEARCH_RESPONSE_TAG );
160         }
161 
162         // RequestID
163         if ( getDecorated() != null )
164         {
165             int requestID = getDecorated().getMessageId();
166             if ( requestID > 0 )
167             {
168                 element.addAttribute( "requestID", Integer.toString( requestID ) );
169             }
170         }
171 
172         for ( DsmlDecorator<? extends Response> response : responses )
173         {
174             response.toDsml( element );
175         }
176 
177         return element;
178     }
179 }