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.ByteArrayOutputStream; 039 import java.io.IOException; 040 import java.io.OutputStream; 041 import java.net.URI; 042 import java.net.URL; 043 import java.nio.charset.Charset; 044 import java.util.ArrayList; 045 import java.util.Enumeration; 046 import java.util.HashMap; 047 import java.util.List; 048 import java.util.Locale; 049 import java.util.Map; 050 import java.util.Properties; 051 import java.util.UUID; 052 053 import javax.servlet.http.HttpServletRequest; 054 import javax.servlet.http.HttpServletResponse; 055 import javax.xml.transform.dom.DOMSource; 056 import javax.xml.transform.stream.StreamResult; 057 058 import net.sf.jasperreports.engine.JRDataSource; 059 import net.sf.jasperreports.engine.JREmptyDataSource; 060 import net.sf.jasperreports.engine.JasperRunManager; 061 062 import org.apache.commons.httpclient.HttpMethod; 063 import org.deegree.datatypes.QualifiedName; 064 import org.deegree.enterprise.control.ajax.ResponseHandler; 065 import org.deegree.enterprise.control.ajax.WebEvent; 066 import org.deegree.framework.log.ILogger; 067 import org.deegree.framework.log.LoggerFactory; 068 import org.deegree.framework.util.HttpUtils; 069 import org.deegree.framework.xml.XMLFragment; 070 import org.deegree.framework.xml.XMLParsingException; 071 import org.deegree.framework.xml.XMLTools; 072 import org.deegree.framework.xml.XSLTDocument; 073 import org.deegree.i18n.Messages; 074 import org.deegree.model.filterencoding.ComplexFilter; 075 import org.deegree.model.filterencoding.Filter; 076 import org.deegree.model.filterencoding.Literal; 077 import org.deegree.model.filterencoding.LogicalOperation; 078 import org.deegree.model.filterencoding.Operation; 079 import org.deegree.model.filterencoding.OperationDefines; 080 import org.deegree.model.filterencoding.PropertyIsLikeOperation; 081 import org.deegree.model.filterencoding.PropertyName; 082 import org.deegree.ogcbase.CommonNamespaces; 083 import org.deegree.ogcbase.PropertyPath; 084 import org.deegree.ogcwebservices.csw.discovery.GetRecords; 085 import org.deegree.ogcwebservices.csw.discovery.Query; 086 import org.deegree.ogcwebservices.csw.discovery.XMLFactory; 087 import org.deegree.ogcwebservices.csw.discovery.GetRecords.RESULT_TYPE; 088 import org.deegree.portal.cataloguemanager.model.ExceptionBean; 089 import org.w3c.dom.Element; 090 import org.w3c.dom.Node; 091 092 /** 093 * TODO add class documentation here 094 * 095 * @author <a href="mailto:name@deegree.org">Andreas Poth</a> 096 * @author last edited by: $Author: admin $ 097 * 098 * @version $Revision: $, $Date: $ 099 */ 100 public class FullMetadataSetListener extends AbstractMetadataListener { 101 102 private static final ILogger LOG = LoggerFactory.getLogger( FullMetadataSetListener.class ); 103 104 private CatalogueManagerConfiguration config; 105 106 private Locale loc; 107 108 /* 109 * (non-Javadoc) 110 * 111 * @see 112 * org.deegree.enterprise.control.ajax.AbstractListener#actionPerformed(org.deegree.enterprise.control.ajax.WebEvent 113 * , org.deegree.enterprise.control.ajax.ResponseHandler) 114 */ 115 @SuppressWarnings("unchecked") 116 public void actionPerformed( WebEvent event, ResponseHandler responseHandler ) 117 throws IOException { 118 119 loc = getRequest().getLocale(); 120 121 config = getCatalogueManagerConfiguration( event ); 122 Map<String, String> param = event.getParameter(); 123 String id = param.get( "ID" ); 124 String format = param.get( "FORMAT" ); 125 String cswAddress = param.get( "CSW" ); 126 GetRecords getRecords = createGetRecords( id ); 127 XMLFragment resultXML = null; 128 try { 129 resultXML = performQuery( getRecords, cswAddress ); 130 } catch ( Exception e ) { 131 LOG.logError( e ); 132 ExceptionBean bean = new ExceptionBean( this.getClass().getName(), e.getMessage() ); 133 responseHandler.writeAndClose( true, bean ); 134 return; 135 } 136 137 if ( "XML".equalsIgnoreCase( format ) ) { 138 writeXML( responseHandler, resultXML ); 139 } else if ( "PDF".equalsIgnoreCase( format ) ) { 140 writePDF( event, responseHandler, resultXML ); 141 } else { 142 writeHTML( responseHandler, cswAddress, resultXML ); 143 } 144 145 } 146 147 private void writePDF( WebEvent event, ResponseHandler responseHandler, XMLFragment resultXML ) 148 throws IOException { 149 Properties p = Messages.getProperties( loc ); 150 XSLTDocument xsl = null; 151 try { 152 xsl = new XSLTDocument( config.getPdfXSL() ); 153 XMLFragment xml = xsl.transform( resultXML ); 154 List<Node> nodes = XMLTools.getNodes( xml.getRootElement(), "./*", CommonNamespaces.getNamespaceContext() ); 155 for ( Node node : nodes ) { 156 if ( node instanceof Element ) { 157 String key = node.getLocalName(); 158 String value = XMLTools.getStringValue( node ); 159 p.put( key, value ); 160 } 161 } 162 163 JRDataSource ds = new JREmptyDataSource(); 164 String path = event.getAbsolutePath( "./WEB-INF/conf/cataloguemanager/reports/report1.jasper" ); 165 byte[] result = JasperRunManager.runReportToPdf( path, p, ds ); 166 HttpServletResponse resp = responseHandler.getHttpServletResponse(); 167 resp.setContentType( "application/pdf" ); 168 resp.setContentLength( result.length ); 169 OutputStream os = resp.getOutputStream(); 170 os.write( result ); 171 os.flush(); 172 os.close(); 173 } catch ( Exception e ) { 174 LOG.logError( e ); 175 ExceptionBean bean = new ExceptionBean( this.getClass().getName(), e.getMessage() ); 176 responseHandler.writeAndClose( true, bean ); 177 return; 178 } 179 180 } 181 182 private void writeHTML( ResponseHandler responseHandler, String cswAddress, XMLFragment resultXML ) 183 throws IOException { 184 String html; 185 try { 186 html = formatResult( resultXML, cswAddress ); 187 } catch ( Exception e ) { 188 LOG.logError( e ); 189 ExceptionBean bean = new ExceptionBean( this.getClass().getName(), e.getMessage() ); 190 responseHandler.writeAndClose( true, bean ); 191 return; 192 } 193 String charEnc = Charset.defaultCharset().displayName(); 194 responseHandler.setContentType( "application/json; charset=" + charEnc ); 195 responseHandler.writeAndClose( html ); 196 } 197 198 private void writeXML( ResponseHandler responseHandler, XMLFragment resultXML ) 199 throws IOException { 200 String charEnc = Charset.defaultCharset().displayName(); 201 responseHandler.setContentType( "text/xml; charset=" + charEnc ); 202 Element md; 203 try { 204 md = XMLTools.getElement( resultXML.getRootElement(), "csw202:SearchResults/gmd:MD_Metadata", 205 CommonNamespaces.getNamespaceContext() ); 206 } catch ( XMLParsingException e ) { 207 LOG.logError( e ); 208 throw new IOException( e.getMessage() ); 209 } 210 String s = "<table width='900' cellpadding='2' border='0'><tr><td width='80'></td>"; 211 s += ( "<td><textarea cols='97' rows='35'>" + new XMLFragment( md ).getAsPrettyString() + "</textarea></td>" ); 212 s += "</tr><tr><td></td><td><a class='button' href='javascript:closeFullMD()'>"; 213 s += ( Messages.get( loc, "CATMANAGE_RESULT_CLOSE" ) + "</a></td>" ); 214 s += "</tr></table>"; 215 responseHandler.writeAndClose( s ); 216 } 217 218 /** 219 * @param resultXML 220 * @return 221 */ 222 private String formatResult( XMLFragment resultXML, String cswAddress ) 223 throws Exception { 224 URL xslURL = config.getFullHTMLXSL(); 225 XSLTDocument xsl = new XSLTDocument( xslURL ); 226 ByteArrayOutputStream bos = new ByteArrayOutputStream( 10000 ); 227 DOMSource xmlSource = new DOMSource( resultXML.getRootElement() ); 228 DOMSource xslSource = new DOMSource( xsl.getRootElement().getOwnerDocument(), 229 xsl.getSystemId() == null ? null : xsl.getSystemId().toString() ); 230 StreamResult sr = new StreamResult( bos ); 231 Map<String, String> param = new HashMap<String, String>(); 232 param.put( "TITLE", Messages.get( loc, "CATMANAGE_RESULT_TITLE" ) ); 233 param.put( "ABSTRACT", Messages.get( loc, "CATMANAGE_RESULT_ABSTRACT" ) ); 234 param.put( "TOPICCATEGORY", Messages.get( loc, "CATMANAGE_RESULT_TOPICCATEGORY" ) ); 235 param.put( "HIERARCHYLEVEL", Messages.get( loc, "CATMANAGE_RESULT_HIERARCHYLEVEL" ) ); 236 param.put( "GEOGRDESC", Messages.get( loc, "CATMANAGE_RESULT_GEOGRDESC" ) ); 237 param.put( "CREATIONDATE", Messages.get( loc, "CATMANAGE_RESULT_CREATIONDATE" ) ); 238 param.put( "PUBLICATIONDATE", Messages.get( loc, "CATMANAGE_RESULT_PUBLICATIONDATE" ) ); 239 param.put( "REVISIONDATE", Messages.get( loc, "CATMANAGE_RESULT_REVISIONDATE" ) ); 240 param.put( "CONTACT", Messages.get( loc, "CATMANAGE_RESULT_CONTACT" ) ); 241 param.put( "NAME", Messages.get( loc, "CATMANAGE_RESULT_NAME" ) ); 242 param.put( "ORGANISATION", Messages.get( loc, "CATMANAGE_RESULT_ORGANISATION" ) ); 243 param.put( "ADDRESS", Messages.get( loc, "CATMANAGE_RESULT_ADDRESS" ) ); 244 param.put( "VOICE", Messages.get( loc, "CATMANAGE_RESULT_VOICE" ) ); 245 param.put( "FAX", Messages.get( loc, "CATMANAGE_RESULT_FAX" ) ); 246 param.put( "EMAIL", Messages.get( loc, "CATMANAGE_RESULT_EMAIL" ) ); 247 param.put( "CLOSE", Messages.get( loc, "CATMANAGE_RESULT_CLOSE" ) ); 248 param.put( "XML", Messages.get( loc, "CATMANAGE_RESULT_XML" ) ); 249 param.put( "PDF", Messages.get( loc, "CATMANAGE_RESULT_PDF" ) ); 250 param.put( "SERVICES", Messages.get( loc, "CATMANAGE_RESULT_SERVICES" ) ); 251 param.put( "CSW", cswAddress ); 252 253 XSLTDocument.transform( xmlSource, xslSource, sr, null, param ); 254 return new String( bos.toByteArray() ); 255 } 256 257 /** 258 * @param cswAddress 259 * @return 260 */ 261 @SuppressWarnings("unchecked") 262 private XMLFragment performQuery( GetRecords getRecords, String cswAddress ) 263 throws Exception { 264 XMLFragment gr = XMLFactory.exportWithVersion( getRecords ); 265 LOG.logDebug( "GetRecords: ", gr.getAsPrettyString() ); 266 Enumeration<String> en = ( (HttpServletRequest) getRequest() ).getHeaderNames(); 267 Map<String, String> map = new HashMap<String, String>(); 268 while ( en.hasMoreElements() ) { 269 String name = (String) en.nextElement(); 270 if ( !name.equalsIgnoreCase( "accept-encoding" ) && !name.equalsIgnoreCase( "content-length" ) 271 && !name.equalsIgnoreCase( "user-agent" ) ) { 272 map.put( name, ( (HttpServletRequest) getRequest() ).getHeader( name ) ); 273 } 274 } 275 HttpMethod method = HttpUtils.performHttpPost( cswAddress, gr, 60000, null, null, map ); 276 XMLFragment xml = new XMLFragment(); 277 xml.load( method.getResponseBodyAsStream(), cswAddress ); 278 if ( LOG.getLevel() == ILogger.LOG_DEBUG ) { 279 LOG.logDebug( "GetRecords result: ", xml.getAsPrettyString() ); 280 } 281 return xml; 282 } 283 284 /** 285 * @return 286 */ 287 private GetRecords createGetRecords( String id ) { 288 QualifiedName qn = new QualifiedName( "ResourceIdentifier", 289 URI.create( "http://www.opengis.net/cat/csw/apiso/1.0" ) ); 290 PropertyName propertyName = new PropertyName( qn ); 291 PropertyIsLikeOperation operation1 = new PropertyIsLikeOperation( propertyName, new Literal( '*' + id + '*' ), 292 '*', '?', '/' ); 293 qn = new QualifiedName( "Identifier", URI.create( "http://www.opengis.net/cat/csw/apiso/1.0" ) ); 294 propertyName = new PropertyName( qn ); 295 PropertyIsLikeOperation operation2 = new PropertyIsLikeOperation( propertyName, new Literal( '*' + id + '*' ), 296 '*', '?', '/' ); 297 List<Operation> list = new ArrayList<Operation>(); 298 list.add( operation1 ); 299 list.add( operation2 ); 300 Filter filter = new ComplexFilter( new LogicalOperation( OperationDefines.OR, list ) ); 301 List<QualifiedName> typeNames = new ArrayList<QualifiedName>(); 302 typeNames.add( new QualifiedName( "{http://www.isotc211.org/2005/gmd}:MD_Metadata" ) ); 303 Query query = new Query( "full", new ArrayList<QualifiedName>(), new HashMap<String, QualifiedName>(), 304 new ArrayList<PropertyPath>(), filter, null, typeNames, 305 new HashMap<String, QualifiedName>() ); 306 return new GetRecords( UUID.randomUUID().toString(), "2.0.2", null, null, RESULT_TYPE.RESULTS, 307 "application/xml", "http://www.isotc211.org/2005/gmd", 1, config.getStepSize() * 2, -1, 308 null, query ); 309 } 310 311 }