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.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      public SearchResponseDsml( LdapApiService codec )
57      {
58          super( codec, new SearchResponse() );
59      }
60  
61  
62      /**
63       * Creates a new getDecoratedMessage() of SearchResponseDsml.
64       *
65       * @param response the LDAP response message to decorate
66       */
67      public SearchResponseDsml( LdapApiService codec, Message response )
68      {
69          super( codec, ( Response ) response );
70      }
71  
72  
73      /**
74       * Adds a response.
75       *
76       * @param response
77       *      the response to add
78       * @return
79       *      true (as per the general contract of the Collection.add method).
80       */
81      public boolean addResponse( DsmlDecorator<? extends Response> response )
82      {
83          if ( response instanceof SearchResultEntry )
84          {
85              ( ( SearchResponse ) getDecorated() ).addSearchResultEntry(
86                  ( SearchResultEntryDsml ) response );
87          }
88          else if ( response instanceof SearchResultReference )
89          {
90              ( ( SearchResponse ) getDecorated() ).addSearchResultReference(
91                  ( SearchResultReferenceDsml ) response );
92          }
93          else if ( response instanceof SearchResultDone )
94          {
95              ( ( SearchResponse ) getDecorated() ).setSearchResultDone(
96                  ( SearchResultDoneDsml ) response );
97          }
98          else
99          {
100             throw new IllegalArgumentException( "Unidentified search resp type" );
101         }
102 
103         return responses.add( response );
104     }
105 
106 
107     /**
108      * Removes a response.
109      *
110      * @param response
111      *      the response to remove
112      * @return
113      *      true if this list contained the specified element.
114      */
115     public boolean removeResponse( DsmlDecorator<? extends Response> response )
116     {
117         if ( response instanceof SearchResultEntry )
118         {
119             ( ( SearchResponse ) getDecorated() ).removeSearchResultEntry(
120                 ( SearchResultEntryDsml ) response );
121         }
122         else if ( response instanceof SearchResultReference )
123         {
124             ( ( SearchResponse ) getDecorated() ).removeSearchResultReference(
125                 ( SearchResultReferenceDsml ) response );
126         }
127         else if ( response instanceof SearchResultDone )
128         {
129             if ( response.equals( ( ( SearchResponse ) getDecorated() ).getSearchResultDone() ) )
130             {
131                 ( ( SearchResponse ) getDecorated() ).setSearchResultDone( null );
132             }
133         }
134         else
135         {
136             throw new IllegalArgumentException( "Unidentified search resp type" );
137         }
138 
139         return responses.remove( response );
140     }
141 
142 
143     /**
144      * {@inheritDoc}
145      */
146     public Element toDsml( Element root )
147     {
148         Element element = null;
149 
150         if ( root != null )
151         {
152             element = root.addElement( SEARCH_RESPONSE_TAG );
153         }
154         else
155         {
156             element = new DefaultElement( SEARCH_RESPONSE_TAG );
157         }
158 
159         // RequestID
160         if ( getDecorated() != null )
161         {
162             int requestID = getDecorated().getMessageId();
163             if ( requestID > 0 )
164             {
165                 element.addAttribute( "requestID", "" + requestID );
166             }
167         }
168 
169         for ( DsmlDecorator<? extends Response> response : responses )
170         {
171             response.toDsml( element );
172         }
173 
174         return element;
175     }
176 }