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 *    https://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 */
020package org.apache.directory.api.dsmlv2.response;
021
022
023import java.util.ArrayList;
024import java.util.List;
025
026import org.apache.directory.api.dsmlv2.DsmlDecorator;
027import org.apache.directory.api.i18n.I18n;
028import org.apache.directory.api.ldap.codec.api.LdapApiService;
029import org.apache.directory.api.ldap.model.message.Message;
030import org.apache.directory.api.ldap.model.message.Response;
031import org.apache.directory.api.ldap.model.message.SearchResultDone;
032import org.apache.directory.api.ldap.model.message.SearchResultEntry;
033import org.apache.directory.api.ldap.model.message.SearchResultReference;
034import org.dom4j.Element;
035import org.dom4j.tree.DefaultElement;
036
037
038/**
039 * This class represents the Search Response Dsml Container. 
040 * It is used to store Search Responses (Search Result Entry, 
041 * Search Result Reference and SearchResultDone).
042 *
043 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
044 */
045public class SearchResponseDsml extends AbstractResponseDsml<Response>
046{
047    private static final String SEARCH_RESPONSE_TAG = "searchResponse";
048
049    /** The responses */
050    private List<DsmlDecorator<? extends Response>> responses =
051        new ArrayList<>();
052
053
054    /**
055     * Creates a new getDecoratedMessage() of SearchResponseDsml.
056     * 
057     * @param codec The LDAP Service to use
058     */
059    public SearchResponseDsml( LdapApiService codec )
060    {
061        super( codec, new SearchResponse() );
062    }
063
064
065    /**
066     * Creates a new getDecoratedMessage() of SearchResponseDsml.
067     *
068     * @param codec The LDAP Service to use
069     * @param response the LDAP response message to decorate
070     */
071    public SearchResponseDsml( LdapApiService codec, Message response )
072    {
073        super( codec, ( Response ) response );
074    }
075
076
077    /**
078     * Adds a response.
079     *
080     * @param response
081     *      the response to add
082     * @return
083     *      true (as per the general contract of the Collection.add method).
084     */
085    public boolean addResponse( DsmlDecorator<? extends Response> response )
086    {
087        if ( response instanceof SearchResultEntry )
088        {
089            ( ( SearchResponse ) getDecorated() ).addSearchResultEntry(
090                ( SearchResultEntryDsml ) response );
091        }
092        else if ( response instanceof SearchResultReference )
093        {
094            ( ( SearchResponse ) getDecorated() ).addSearchResultReference(
095                ( SearchResultReferenceDsml ) response );
096        }
097        else if ( response instanceof SearchResultDone )
098        {
099            ( ( SearchResponse ) getDecorated() ).setSearchResultDone(
100                ( SearchResultDoneDsml ) response );
101        }
102        else
103        {
104            throw new IllegalArgumentException( I18n.err( I18n.ERR_03045_UNIDENTIFIED_RESPONSE_TYPE ) );
105        }
106
107        return responses.add( response );
108    }
109
110
111    /**
112     * Removes a response.
113     *
114     * @param response
115     *      the response to remove
116     * @return
117     *      true if this list contained the specified element.
118     */
119    public boolean removeResponse( DsmlDecorator<? extends Response> response )
120    {
121        if ( response instanceof SearchResultEntry )
122        {
123            ( ( SearchResponse ) getDecorated() ).removeSearchResultEntry(
124                ( SearchResultEntryDsml ) response );
125        }
126        else if ( response instanceof SearchResultReference )
127        {
128            ( ( SearchResponse ) getDecorated() ).removeSearchResultReference(
129                ( SearchResultReferenceDsml ) response );
130        }
131        else if ( response instanceof SearchResultDone )
132        {
133            if ( response.equals( ( ( SearchResponse ) getDecorated() ).getSearchResultDone() ) )
134            {
135                ( ( SearchResponse ) getDecorated() ).setSearchResultDone( null );
136            }
137        }
138        else
139        {
140            throw new IllegalArgumentException( I18n.err( I18n.ERR_03045_UNIDENTIFIED_RESPONSE_TYPE ) );
141        }
142
143        return responses.remove( response );
144    }
145
146
147    /**
148     * {@inheritDoc}
149     */
150    @Override
151    public Element toDsml( Element root )
152    {
153        Element element;
154
155        if ( root != null )
156        {
157            element = root.addElement( SEARCH_RESPONSE_TAG );
158        }
159        else
160        {
161            element = new DefaultElement( SEARCH_RESPONSE_TAG );
162        }
163
164        // RequestID
165        if ( getDecorated() != null )
166        {
167            int requestID = getDecorated().getMessageId();
168            if ( requestID > 0 )
169            {
170                element.addAttribute( "requestID", Integer.toString( requestID ) );
171            }
172        }
173
174        for ( DsmlDecorator<? extends Response> response : responses )
175        {
176            response.toDsml( element );
177        }
178
179        return element;
180    }
181}