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.i18n.I18n;
29  import org.apache.directory.api.ldap.codec.api.LdapApiService;
30  import org.apache.directory.api.ldap.model.message.Message;
31  import org.apache.directory.api.ldap.model.message.Response;
32  import org.apache.directory.api.ldap.model.message.SearchResultDone;
33  import org.apache.directory.api.ldap.model.message.SearchResultEntry;
34  import org.apache.directory.api.ldap.model.message.SearchResultReference;
35  import org.dom4j.Element;
36  import org.dom4j.tree.DefaultElement;
37  
38  
39  /**
40   * This class represents the Search Response Dsml Container. 
41   * It is used to store Search Responses (Search Result Entry, 
42   * Search Result Reference and SearchResultDone).
43   *
44   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
45   */
46  public class SearchResponseDsml extends AbstractResponseDsml<Response>
47  {
48      /** The responses */
49      private List<DsmlDecorator<? extends Response>> responses =
50          new ArrayList<>();
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( I18n.err( I18n.ERR_03045_UNIDENTIFIED_RESPONSE_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( I18n.err( I18n.ERR_03045_UNIDENTIFIED_RESPONSE_TYPE ) );
140         }
141 
142         return responses.remove( response );
143     }
144 
145 
146     /**
147      * {@inheritDoc}
148      */
149     @Override
150     public Element toDsml( Element root )
151     {
152         Element element;
153 
154         if ( root != null )
155         {
156             element = root.addElement( DsmlLiterals.SEARCH_RESPONSE );
157         }
158         else
159         {
160             element = new DefaultElement( DsmlLiterals.SEARCH_RESPONSE );
161         }
162 
163         // RequestID
164         if ( getDecorated() != null )
165         {
166             int requestID = getDecorated().getMessageId();
167             if ( requestID > 0 )
168             {
169                 element.addAttribute( DsmlLiterals.REQUEST_ID, Integer.toString( requestID ) );
170             }
171         }
172 
173         for ( DsmlDecorator<? extends Response> response : responses )
174         {
175             response.toDsml( element );
176         }
177 
178         return element;
179     }
180 }