001 //$HeadURL: 002 /*---------------------------------------------------------------------------- 003 This file is part of deegree, http://deegree.org/ 004 Copyright (C) 2001-2010 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.IOException; 039 import java.net.URL; 040 import java.net.URLEncoder; 041 import java.nio.charset.Charset; 042 import java.util.Calendar; 043 import java.util.Enumeration; 044 import java.util.HashMap; 045 import java.util.List; 046 import java.util.Map; 047 048 import javax.servlet.http.HttpServletRequest; 049 050 import org.apache.commons.httpclient.HttpMethod; 051 import org.deegree.enterprise.control.ajax.ResponseHandler; 052 import org.deegree.enterprise.control.ajax.WebEvent; 053 import org.deegree.framework.log.ILogger; 054 import org.deegree.framework.log.LoggerFactory; 055 import org.deegree.framework.util.HttpUtils; 056 import org.deegree.framework.util.TimeTools; 057 import org.deegree.framework.xml.NamespaceContext; 058 import org.deegree.framework.xml.XMLFragment; 059 import org.deegree.framework.xml.XMLParsingException; 060 import org.deegree.framework.xml.XMLTools; 061 import org.deegree.ogcbase.CommonNamespaces; 062 import org.deegree.portal.cataloguemanager.model.ExceptionBean; 063 import org.deegree.portal.cataloguemanager.model.MetadataBean; 064 065 /** 066 * 067 * 068 * 069 * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a> 070 * @author last edited by: $Author: apoth $ 071 * 072 * @version $Revision: 30690 $, $Date: 2011-05-06 10:23:46 +0200 (Fr, 06 Mai 2011) $ 073 */ 074 public class LoadMetadataSetListener extends AbstractMetadataListener { 075 076 private static ILogger LOG = LoggerFactory.getLogger( LoadMetadataSetListener.class ); 077 078 private static String GRBID = "request=GetRecordById&OUTPUTSCHEMA=http://www.isotc211.org/2005/gmd&service=CSW&version=2.0.2&ID="; 079 080 private static String PARAM_ID = "ID"; 081 082 private XMLFragment xml; 083 084 private static NamespaceContext nsContext = CommonNamespaces.getNamespaceContext(); 085 086 @SuppressWarnings("unchecked") 087 @Override 088 public void actionPerformed( WebEvent event, ResponseHandler responseHandler ) 089 throws IOException { 090 091 CatalogueManagerConfiguration config = getCatalogueManagerConfiguration( event ); 092 Map<String, String> param = event.getParameter(); 093 LOG.logDebug( "load metadataset request param: ", param ); 094 String charset = event.getCharacterEncoding(); 095 if ( charset == null ) { 096 charset = Charset.defaultCharset().displayName(); 097 } 098 099 String s = URLEncoder.encode( param.get( PARAM_ID ), charset ); 100 URL url = new URL( config.getCatalogueURL() + '?' + GRBID + s ); 101 LOG.logDebug( "load metadataset: ", url ); 102 try { 103 Enumeration<String> en = ( (HttpServletRequest) getRequest() ).getHeaderNames(); 104 Map<String, String> map = new HashMap<String, String>(); 105 while ( en.hasMoreElements() ) { 106 String name = (String) en.nextElement(); 107 if ( !name.equalsIgnoreCase( "accept-encoding" ) && !name.equalsIgnoreCase( "content-length" ) 108 && !name.equalsIgnoreCase( "user-agent" ) ) { 109 map.put( name, ( (HttpServletRequest) getRequest() ).getHeader( name ) ); 110 } 111 } 112 HttpMethod get = HttpUtils.performHttpGet( url.toExternalForm(), null, 60000, null, null, map ); 113 xml = new XMLFragment(); 114 xml.load( get.getResponseBodyAsStream(), url.toExternalForm() ); 115 xml = new XMLFragment( XMLTools.getFirstChildElement( xml.getRootElement() ) ); 116 } catch ( Exception e ) { 117 LOG.logError( e.getMessage(), e ); 118 ExceptionBean eb = new ExceptionBean( getClass().getName(), e.getMessage() ); 119 responseHandler.writeAndClose( true, eb ); 120 return; 121 } 122 if ( LOG.getLevel() == ILogger.LOG_DEBUG ) { 123 LOG.logDebug( xml.getAsPrettyString() ); 124 } 125 126 event.getSession().setAttribute( "MD_TEMPLATE", xml ); 127 128 MetadataBean metadata = new MetadataBean(); 129 130 if ( param.get( "ISTEMPLATE" ) == null || "false".equalsIgnoreCase( param.get( "ISTEMPLATE" ) ) ) { 131 String tmp = getValue( config.getXPath( "datasetTitle" ) ); 132 if ( "".equals( tmp ) ) { 133 tmp = getValue( config.getXPath( "seviceTitle" ) ); 134 } 135 metadata.setDatasetTitle( tmp ); 136 metadata.setIdentifier( getValue( config.getXPath( "identifier" ) ) ); 137 metadata.setAbstract_( getValue( config.getXPath( "abstract_" ) ) ); 138 } 139 String tmp = getValue( config.getXPath( "begin" ) ); 140 if ( tmp != null && tmp.trim().length() > 0 ) { 141 Calendar cal = TimeTools.createCalendar( tmp ); 142 metadata.setBegin( toISO( cal ) ); 143 } 144 metadata.setContactCity( getValue( config.getXPath( "contactCity" ) ) ); 145 metadata.setContactCountry( getValue( config.getXPath( "contactCountry" ) ) ); 146 metadata.setContactDeliveryPoint( getValue( config.getXPath( "contactDeliveryPoint" ) ) ); 147 metadata.setContactEmailAddress( getValue( config.getXPath( "contactEmailAddress" ) ) ); 148 metadata.setContactFacsimile( getValue( config.getXPath( "contactFacsimile" ) ) ); 149 metadata.setContactIndividualName( getValue( config.getXPath( "contactIndividualName" ) ) ); 150 metadata.setContactOrganisationName( getValue( config.getXPath( "contactOrganisationName" ) ) ); 151 metadata.setContactPostalCode( getValue( config.getXPath( "contactPostalCode" ) ) ); 152 metadata.setContactRole( getValue( config.getXPath( "contactRole" ) ) ); 153 metadata.setContactVoice( getValue( config.getXPath( "contactVoice" ) ) ); 154 tmp = getValue( config.getXPath( "creation" ) ); 155 if ( tmp != null && tmp.trim().length() > 0 ) { 156 Calendar cal = TimeTools.createCalendar( tmp ); 157 metadata.setCreation( toISO( cal ) ); 158 } 159 metadata.setCrs( getValue( config.getXPath( "crs" ) ) ); 160 tmp = getValue( config.getXPath( "end" ) ); 161 if ( tmp != null && tmp.trim().length() > 0 ) { 162 Calendar cal = TimeTools.createCalendar( tmp ); 163 metadata.setEnd( toISO( cal ) ); 164 } 165 metadata.setGeogrDescription( getValue( config.getXPath( "geogrDescription" ) ) ); 166 metadata.setHlevel( getValue( config.getXPath( "hlevel" ) ) ); 167 168 metadata.setKeywords( getValues( config.getXPath( "keywords" ) ) ); 169 metadata.setParentId( getValue( config.getXPath( "parentId" ) ) ); 170 metadata.setPocCity( getValue( config.getXPath( "pocCity" ) ) ); 171 metadata.setPocCountry( getValue( config.getXPath( "pocCountry" ) ) ); 172 metadata.setPocDeliveryPoint( getValue( config.getXPath( "pocDeliveryPoint" ) ) ); 173 metadata.setPocEmailAddress( getValue( config.getXPath( "pocEmailAddress" ) ) ); 174 metadata.setPocFacsimile( getValue( config.getXPath( "pocFacsimile" ) ) ); 175 metadata.setPocIndividualName( getValue( config.getXPath( "pocIndividualName" ) ) ); 176 metadata.setPocOrganisationName( getValue( config.getXPath( "pocOrganisationName" ) ) ); 177 metadata.setPocPostalCode( getValue( config.getXPath( "pocPostalCode" ) ) ); 178 metadata.setPocRole( getValue( config.getXPath( "pocRole" ) ) ); 179 metadata.setPocVoice( getValue( config.getXPath( "pocVoice" ) ) ); 180 tmp = getValue( config.getXPath( "publication" ) ); 181 if ( tmp != null && tmp.trim().length() > 0 ) { 182 Calendar cal = TimeTools.createCalendar( tmp ); 183 metadata.setPublication( toISO( cal ) ); 184 } 185 tmp = getValue( config.getXPath( "revision" ) ); 186 if ( tmp != null && tmp.trim().length() > 0 ) { 187 Calendar cal = TimeTools.createCalendar( tmp ); 188 metadata.setRevision( toISO( cal ) ); 189 } 190 metadata.setScale( getValue( config.getXPath( "scale" ) ) ); 191 metadata.setTopCat( getValue( config.getXPath( "topCat" ) ) ); 192 193 tmp = getValue( config.getXPath( "begin" ) ); 194 if ( tmp.length() == 0 ) { 195 tmp = getValue( config.getXPath( "begin2" ) ); 196 } 197 Calendar cal = TimeTools.createCalendar( tmp ); 198 metadata.setBegin( toISO( cal ) ); 199 200 tmp = getValue( config.getXPath( "end" ) ); 201 if ( tmp.length() == 0 ) { 202 tmp = getValue( config.getXPath( "end2" ) ); 203 } 204 cal = TimeTools.createCalendar( tmp ); 205 metadata.setEnd( toISO( cal ) ); 206 metadata.setLineage( getValue( config.getXPath( "lineage" ) ) ); 207 208 metadata.setTransferOnline( getValue( config.getXPath( "transferOptOnline" ) ) ); 209 210 s = getValue( config.getXPath( "accessConstraints" ) ); 211 if ( s == null ) { 212 s = getValue( config.getXPath( "srvAccessConstraints" ) ); 213 } 214 metadata.setAccessConstraints( s ); 215 216 // result page uses UTF-8 encoding 217 String charEnc = Charset.defaultCharset().displayName(); 218 responseHandler.setContentType( "application/json; charset=" + charEnc ); 219 responseHandler.writeAndClose( true, metadata ); 220 } 221 222 private String toISO( Calendar cal ) { 223 StringBuffer sb = new StringBuffer(); 224 sb.append( Integer.toString( cal.get( Calendar.YEAR ) ) ).append( '-' ); 225 sb.append( Integer.toString( cal.get( Calendar.MONTH ) + 1 ) ).append( '-' ); 226 sb.append( Integer.toString( cal.get( Calendar.DAY_OF_MONTH ) ) ); 227 return sb.toString(); 228 } 229 230 private String getValue( String xPath ) 231 throws IOException { 232 try { 233 return XMLTools.getNodeAsString( xml.getRootElement(), xPath, nsContext, "" ); 234 } catch ( XMLParsingException e ) { 235 throw new IOException( e.getMessage() ); 236 } 237 } 238 239 private List<String> getValues( String xPath ) 240 throws IOException { 241 try { 242 return XMLTools.getNodesAsStringList( xml.getRootElement(), xPath, nsContext ); 243 } catch ( XMLParsingException e ) { 244 throw new IOException( e.getMessage() ); 245 } 246 } 247 }