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 */ 020package org.apache.directory.shared.dsmlv2.reponse; 021 022 023import java.util.ArrayList; 024import java.util.List; 025 026import org.apache.directory.shared.dsmlv2.DsmlDecorator; 027import org.apache.directory.shared.ldap.codec.api.LdapApiService; 028import org.apache.directory.shared.ldap.model.message.Message; 029import org.apache.directory.shared.ldap.model.message.Response; 030import org.apache.directory.shared.ldap.model.message.SearchResultDone; 031import org.apache.directory.shared.ldap.model.message.SearchResultEntry; 032import org.apache.directory.shared.ldap.model.message.SearchResultReference; 033import org.dom4j.Element; 034import org.dom4j.tree.DefaultElement; 035 036 037/** 038 * This class represents the Search Response Dsml Container. 039 * It is used to store Search Responses (Search Result Entry, 040 * Search Result Reference and SearchResultDone). 041 * 042 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 043 */ 044public class SearchResponseDsml extends AbstractResponseDsml<Response> 045{ 046 private static final String SEARCH_RESPONSE_TAG = "searchResponse"; 047 048 /** The responses */ 049 private List<DsmlDecorator<? extends Response>> responses = 050 new ArrayList<DsmlDecorator<? extends Response>>(); 051 052 053 /** 054 * Creates a new getDecoratedMessage() of SearchResponseDsml. 055 */ 056 public SearchResponseDsml( LdapApiService codec ) 057 { 058 super( codec, null ); 059 } 060 061 062 /** 063 * Creates a new getDecoratedMessage() of SearchResponseDsml. 064 * 065 * @param response the LDAP response message to decorate 066 */ 067 public SearchResponseDsml( LdapApiService codec, Message response ) 068 { 069 super( codec, ( Response ) response ); 070 } 071 072 073 /** 074 * Adds a response. 075 * 076 * @param response 077 * the response to add 078 * @return 079 * true (as per the general contract of the Collection.add method). 080 */ 081 public boolean addResponse( DsmlDecorator<? extends Response> response ) 082 { 083 if ( response instanceof SearchResultEntry ) 084 { 085 ( ( SearchResponse ) getDecorated() ).addSearchResultEntry( 086 ( SearchResultEntryDsml ) response ); 087 } 088 else if ( response instanceof SearchResultReference ) 089 { 090 ( ( SearchResponse ) getDecorated() ).addSearchResultReference( 091 ( SearchResultReferenceDsml ) response ); 092 } 093 else if ( response instanceof SearchResultDone ) 094 { 095 ( ( SearchResponse ) getDecorated() ).setSearchResultDone( 096 ( SearchResultDoneDsml ) response ); 097 } 098 else 099 { 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<Response> response ) 116 { 117 return responses.remove( response ); 118 } 119 120 121 /** 122 * {@inheritDoc} 123 */ 124 public Element toDsml( Element root ) 125 { 126 Element element = null; 127 128 if ( root != null ) 129 { 130 element = root.addElement( SEARCH_RESPONSE_TAG ); 131 } 132 else 133 { 134 element = new DefaultElement( SEARCH_RESPONSE_TAG ); 135 } 136 137 138 // RequestID 139 if ( getDecorated() != null ) 140 { 141 int requestID = getDecorated().getMessageId(); 142 if ( requestID > 0 ) 143 { 144 element.addAttribute( "requestID", "" + requestID ); 145 } 146 } 147 148 for ( DsmlDecorator<? extends Response> response : responses ) 149 { 150 response.toDsml( element ); 151 } 152 153 return element; 154 } 155}