001 //$HeadURL: svn+ssh://developername@svn.wald.intevation.org/deegree/base/trunk/resources/eclipse/files_template.xml $ 002 /*---------------------------------------------------------------------------- 003 This file is part of deegree, http://deegree.org/ 004 Copyright (C) 2001-2009 by: 005 - Department of Geography, University of Bonn - 006 and 007 - lat/lon GmbH - 008 009 This library is free software; you can redistribute it and/or modify it under 010 the terms of the GNU Lesser General Public License as published by the Free 011 Software Foundation; either version 2.1 of the License, or (at your option) 012 any later version. 013 This library is distributed in the hope that it will be useful, but WITHOUT 014 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 015 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 016 details. 017 You should have received a copy of the GNU Lesser General Public License 018 along with this library; if not, write to the Free Software Foundation, Inc., 019 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 020 021 Contact information: 022 023 lat/lon GmbH 024 Aennchenstr. 19, 53177 Bonn 025 Germany 026 http://lat-lon.de/ 027 028 Department of Geography, University of Bonn 029 Prof. Dr. Klaus Greve 030 Postfach 1147, 53001 Bonn 031 Germany 032 http://www.geographie.uni-bonn.de/deegree/ 033 034 e-mail: info@deegree.org 035 ----------------------------------------------------------------------------*/ 036 package org.deegree.portal.cataloguemanager.control; 037 038 import java.io.StringReader; 039 import java.util.ArrayList; 040 import java.util.Enumeration; 041 import java.util.HashMap; 042 import java.util.List; 043 import java.util.Map; 044 import java.util.concurrent.Callable; 045 046 import javax.servlet.http.HttpServletRequest; 047 048 import org.apache.commons.httpclient.HttpMethod; 049 import org.deegree.framework.concurrent.ExecutionFinishedEvent; 050 import org.deegree.framework.concurrent.Executor; 051 import org.deegree.framework.log.ILogger; 052 import org.deegree.framework.log.LoggerFactory; 053 import org.deegree.framework.util.HttpUtils; 054 import org.deegree.framework.util.Pair; 055 import org.deegree.framework.util.StringTools; 056 import org.deegree.framework.xml.NamespaceContext; 057 import org.deegree.framework.xml.XMLFragment; 058 import org.deegree.framework.xml.XMLTools; 059 import org.deegree.ogcbase.CommonNamespaces; 060 import org.deegree.ogcwebservices.csw.discovery.GetRecords; 061 import org.deegree.ogcwebservices.csw.discovery.XMLFactory; 062 import org.w3c.dom.Element; 063 import org.w3c.dom.Node; 064 065 /** 066 * TODO add class documentation here 067 * 068 * @author <a href="mailto:name@deegree.org">Andreas Poth</a> 069 * @author last edited by: $Author: admin $ 070 * 071 * @version $Revision: $, $Date: $ 072 */ 073 abstract class AbstractSearchListener extends AbstractMetadataListener { 074 075 private static final ILogger LOG = LoggerFactory.getLogger( AbstractSearchListener.class ); 076 077 protected CatalogueManagerConfiguration config; 078 079 protected List<SearchResultInfo> searchResultInfos = new ArrayList<SearchResultInfo>(); 080 081 protected static NamespaceContext nsc = CommonNamespaces.getNamespaceContext(); 082 083 /** 084 * 085 * @param getRecords 086 * @return 087 * @throws Throwable 088 */ 089 protected List<Pair<String, XMLFragment>> performQuery( GetRecords getRecords ) 090 throws Throwable { 091 XMLFragment gr = XMLFactory.exportWithVersion( getRecords ); 092 LOG.logDebug( "GetRecords request: ", gr.getAsPrettyString() ); 093 List<String> csw = config.getSearchableCSW(); 094 List<Callable<XMLFragment>> callables = new ArrayList<Callable<XMLFragment>>(); 095 callables.add( new GetRecordsCallable( config.getCatalogueURL(), gr ) ); 096 for ( String address : csw ) { 097 callables.add( new GetRecordsCallable( address, gr ) ); 098 } 099 100 Executor exe = Executor.getInstance(); 101 List<ExecutionFinishedEvent<XMLFragment>> events = exe.performSynchronously( callables ); 102 103 List<Pair<String, XMLFragment>> result = new ArrayList<Pair<String, XMLFragment>>(); 104 105 String xpath1 = "csw202:SearchResults/@numberOfRecordsReturned"; 106 String xpath2 = "csw202:SearchResults/@numberOfRecordsMatched"; 107 for ( ExecutionFinishedEvent<XMLFragment> executionFinishedEvent : events ) { 108 SearchResultInfo sri = new SearchResultInfo(); 109 XMLFragment xml = executionFinishedEvent.getResult(); 110 String cswAddress = ( (GetRecordsCallable) executionFinishedEvent.getTask() ).getCsw(); 111 // store meta information for search result 112 sri.setNumberOfRecordsReturned( XMLTools.getNodeAsInt( xml.getRootElement(), xpath1, nsc, 0 ) ); 113 sri.setNumberOfRecordsMatched( XMLTools.getNodeAsInt( xml.getRootElement(), xpath2, nsc, 0 ) ); 114 sri.setCswURL( cswAddress ); 115 // TODO 116 // set catalogue name to search result info 117 searchResultInfos.add( sri ); 118 result.add( new Pair<String, XMLFragment>( cswAddress, xml ) ); 119 if ( LOG.getLevel() == ILogger.LOG_DEBUG ) { 120 LOG.logDebug( "queried catalogue: ", cswAddress ); 121 if ( LOG.getLevel() == ILogger.LOG_DEBUG ) { 122 LOG.logDebug( xml.getAsPrettyString() ); 123 } 124 } 125 } 126 return result; 127 } 128 129 /** 130 * @param result 131 * first parameter of pair contains CSW URL second one contains GetRecords result 132 * @return first string of pair contains CSW URL second one contains formatted GetRecords result 133 * @throws Exception 134 */ 135 protected List<SearchResultBean> formatResult( List<Pair<String, XMLFragment>> result ) 136 throws Exception { 137 List<SearchResultBean> formattedResult = new ArrayList<SearchResultBean>(); 138 139 for ( Pair<String, XMLFragment> pair : result ) { 140 List<Node> nodes = XMLTools.getNodes( pair.second.getRootElement(), "./csw202:SearchResults/*", nsc ); 141 for ( Node node : nodes ) { 142 Element root = (Element) node; 143 try { 144 SearchResultBean srb = new SearchResultBean(); 145 srb.setCsw( pair.first ); 146 String s = XMLTools.getNodeAsString( root, config.getXPath( "abstract_" ), nsc, "-" ); 147 srb.setAbstr( s ); 148 149 s = XMLTools.getNodeAsString( root, config.getXPath( "identifier" ), nsc, "" ); 150 if ( s == null ) { 151 s = XMLTools.getNodeAsString( root, config.getXPath( "resourceIdentifier1" ), nsc, null ); 152 } 153 if ( s == null ) { 154 s = XMLTools.getNodeAsString( root, config.getXPath( "resourceIdentifier2" ), nsc, null ); 155 } 156 if ( s == null ) { 157 s = XMLTools.getNodeAsString( root, config.getXPath( "srvResourceIdentifier1" ), nsc, null ); 158 } 159 if ( s == null ) { 160 s = XMLTools.getNodeAsString( root, config.getXPath( "srvResourceIdentifier2" ), nsc, null ); 161 } 162 163 srb.setId( s ); 164 s = XMLTools.getNodeAsString( root, config.getXPath( "serviceTitle" ), nsc, null ); 165 if ( s == null ) { 166 s = XMLTools.getRequiredNodeAsString( root, config.getXPath( "datasetTitle" ), nsc ); 167 } 168 srb.setTitle( s ); 169 s = XMLTools.getNodeAsString( root, config.getXPath( "modified" ), nsc, "-" ); 170 srb.setModified( s ); 171 s = XMLTools.getRequiredNodeAsString( root, config.getXPath( "hlevel" ), nsc ); 172 srb.setHierarchyLevel( s ); 173 s = XMLTools.getNodeAsString( root, config.getXPath( "overview" ), nsc, null ); 174 srb.setOverview( s ); 175 String west = XMLTools.getNodeAsString( root, config.getXPath( "srvWest" ), nsc, null ); 176 if ( west == null ) { 177 west = XMLTools.getRequiredNodeAsString( root, config.getXPath( "west" ), nsc ); 178 } 179 String east = XMLTools.getNodeAsString( root, config.getXPath( "srvEast" ), nsc, null ); 180 if ( east == null ) { 181 east = XMLTools.getRequiredNodeAsString( root, config.getXPath( "east" ), nsc ); 182 } 183 String south = XMLTools.getNodeAsString( root, config.getXPath( "srvSouth" ), nsc, null ); 184 if ( south == null ) { 185 south = XMLTools.getRequiredNodeAsString( root, config.getXPath( "south" ), nsc ); 186 } 187 String north = XMLTools.getNodeAsString( root, config.getXPath( "srvNorth" ), nsc, null ); 188 if ( north == null ) { 189 north = XMLTools.getRequiredNodeAsString( root, config.getXPath( "north" ), nsc ); 190 } 191 srb.setBbox( west + ' ' + south + ' ' + east + ' ' + north ); 192 formattedResult.add( srb ); 193 } catch ( Exception e ) { 194 LOG.logError( e ); 195 } 196 } 197 } 198 return formattedResult; 199 } 200 201 // //////////////////////////////////////////////////////////////////////////////////////////////////// 202 // inner classes 203 // //////////////////////////////////////////////////////////////////////////////////////////////////// 204 205 private class GetRecordsCallable implements Callable<XMLFragment> { 206 207 private String csw; 208 209 private XMLFragment getRecords; 210 211 /** 212 * 213 * @param csw 214 * @param getRecords 215 */ 216 GetRecordsCallable( String csw, XMLFragment getRecords ) { 217 this.csw = csw; 218 this.getRecords = getRecords; 219 } 220 221 /** 222 * @return the csw 223 */ 224 public String getCsw() { 225 return csw; 226 } 227 228 /* 229 * (non-Javadoc) 230 * 231 * @see java.util.concurrent.Callable#call() 232 */ 233 @SuppressWarnings("unchecked") 234 // public XMLFragment call() 235 // throws Exception { 236 // LOG.logDebug( "connect to: ", csw ); 237 // Enumeration<String> en = ( (HttpServletRequest) getRequest() ).getHeaderNames(); 238 // Map<String, String> map = new HashMap<String, String>(); 239 // while ( en.hasMoreElements() ) { 240 // String name = (String) en.nextElement(); 241 // map.put( name, ( (HttpServletRequest) getRequest() ).getHeader( name ) ); 242 // } 243 // HttpMethod method = HttpUtils.performHttpPost( csw, getRecords, 60000, null, null, map ); 244 // XMLFragment xml = new XMLFragment(); 245 // xml.load( method.getResponseBodyAsStream(), csw ); 246 // return xml; 247 // } 248 249 public XMLFragment call() 250 throws Exception { 251 LOG.logDebug( "connect to: ", csw ); 252 Enumeration<String> en = ( (HttpServletRequest) getRequest() ).getHeaderNames(); 253 Map<String, String> map = new HashMap<String, String>(); 254 255 HashMap hm = (HashMap) map; 256 257 while ( en.hasMoreElements() ) { 258 String name = (String) en.nextElement(); 259 if ( !name.equalsIgnoreCase( "accept-encoding" ) && !name.equalsIgnoreCase( "content-length" ) 260 && !name.equalsIgnoreCase( "user-agent" ) ) { 261 map.put( name, ( (HttpServletRequest) getRequest() ).getHeader( name ) ); 262 } 263 } 264 if ( LOG.isDebug() ) { 265 LOG.logDebug( "header: " ); 266 StringTools.printMap( map, null ); 267 } 268 HttpMethod method = HttpUtils.performHttpPost( csw, getRecords, 60000, null, null, map ); 269 270 XMLFragment xml = new XMLFragment(); 271 if ( LOG.isDebug() ) { 272 LOG.logDebug( "response content: " ); 273 String s = method.getResponseBodyAsString(); 274 LOG.logDebug( s ); 275 xml.load( new StringReader( s ), csw ); 276 } else { 277 xml.load( method.getResponseBodyAsStream(), csw ); 278 } 279 return xml; 280 } 281 282 } 283 284 }