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.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.ldap.codec.api.LdapApiService; 028import org.apache.directory.api.ldap.model.message.Message; 029import org.apache.directory.api.ldap.model.message.Response; 030import org.apache.directory.api.ldap.model.message.SearchResultDone; 031import org.apache.directory.api.ldap.model.message.SearchResultEntry; 032import org.apache.directory.api.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 * @param codec The LDAP Service to use 057 */ 058 public SearchResponseDsml( LdapApiService codec ) 059 { 060 super( codec, new SearchResponse() ); 061 } 062 063 064 /** 065 * Creates a new getDecoratedMessage() of SearchResponseDsml. 066 * 067 * @param codec The LDAP Service to use 068 * @param response the LDAP response message to decorate 069 */ 070 public SearchResponseDsml( LdapApiService codec, Message response ) 071 { 072 super( codec, ( Response ) response ); 073 } 074 075 076 /** 077 * Adds a response. 078 * 079 * @param response 080 * the response to add 081 * @return 082 * true (as per the general contract of the Collection.add method). 083 */ 084 public boolean addResponse( DsmlDecorator<? extends Response> response ) 085 { 086 if ( response instanceof SearchResultEntry ) 087 { 088 ( ( SearchResponse ) getDecorated() ).addSearchResultEntry( 089 ( SearchResultEntryDsml ) response ); 090 } 091 else if ( response instanceof SearchResultReference ) 092 { 093 ( ( SearchResponse ) getDecorated() ).addSearchResultReference( 094 ( SearchResultReferenceDsml ) response ); 095 } 096 else if ( response instanceof SearchResultDone ) 097 { 098 ( ( SearchResponse ) getDecorated() ).setSearchResultDone( 099 ( 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}