Skip to content

Commit

Permalink
Merge pull request #216 from copierrj/feature-info
Browse files Browse the repository at this point in the history
GetFeatureInfo improvements
  • Loading branch information
MrSnyder committed Feb 21, 2014
2 parents 69c2f4b + 54576d3 commit 975f972
Show file tree
Hide file tree
Showing 15 changed files with 529 additions and 213 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//$HeadURL$
/*----------------------------------------------------------------------------
This file is part of deegree, http://deegree.org/
Copyright (C) 2014 by:
IDgis bv
This library is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation; either version 2.1 of the License, or (at your option)
any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
details.
You should have received a copy of the GNU Lesser General Public License
along with this library; if not, write to the Free Software Foundation, Inc.,
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Contact information:
IDgis bv
Boomkamp 16
7461 AX Rijssen
The Netherlands
http://idgis.nl/
lat/lon GmbH
Aennchenstr. 19, 53177 Bonn
Germany
http://lat-lon.de/
Department of Geography, University of Bonn
Prof. Dr. Klaus Greve
Postfach 1147, 53001 Bonn
Germany
http://www.geographie.uni-bonn.de/deegree/
e-mail: info@deegree.org
----------------------------------------------------------------------------*/

package org.deegree.featureinfo;

import java.io.IOException;
import java.io.OutputStream;

import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;

public interface FeatureInfoContext {

OutputStream getOutputStream()
throws IOException;

XMLStreamWriter getXmlWriter()
throws IOException, XMLStreamException;

void sendRedirect( String location )
throws IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,29 +41,20 @@ Occam Labs UG (haftungsbeschränkt)
----------------------------------------------------------------------------*/
package org.deegree.featureinfo;

import static org.deegree.featureinfo.templating.TemplatingUtils.runTemplate;
import static org.slf4j.LoggerFactory.getLogger;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.xml.stream.XMLStreamWriter;
import javax.xml.stream.XMLStreamException;

import org.deegree.commons.tom.gml.property.Property;
import org.deegree.feature.Feature;
import org.deegree.featureinfo.serializing.FeatureInfoGmlWriter;
import org.deegree.featureinfo.serializing.FeatureInfoSerializer;
import org.deegree.featureinfo.serializing.PlainTextFeatureInfoSerializer;
import org.deegree.featureinfo.serializing.TemplateFeatureInfoSerializer;
import org.deegree.featureinfo.serializing.XsltFeatureInfoSerializer;
import org.deegree.gml.GMLOutputFactory;
import org.deegree.gml.GMLStreamWriter;
import org.deegree.gml.GMLVersion;
import org.deegree.workspace.Workspace;
import org.slf4j.Logger;
Expand All @@ -72,6 +63,7 @@ Occam Labs UG (haftungsbeschränkt)
* Responsible for managing feature info output formats and their serializers.
*
* @author <a href="mailto:schmitz@occamlabs.de">Andreas Schmitz</a>
* @author <a href="mailto:reijer.copier@idgis.nl">Reijer Copier</a>
* @author last edited by: $Author: stranger $
*
* @version $Revision: $, $Date: $
Expand All @@ -80,144 +72,64 @@ public class FeatureInfoManager {

private static final Logger LOG = getLogger( FeatureInfoManager.class );

private final HashSet<String> defaultGMLGFIFormats = new LinkedHashSet<String>();
private static final String[] GML_FORMATS = { "application/gml+xml; version=2.1",
"application/gml+xml; version=3.0",
"application/gml+xml; version=3.1",
"application/gml+xml; version=3.2", "text/xml; subtype=gml/2.1.2",
"text/xml; subtype=gml/3.0.1", "text/xml; subtype=gml/3.1.1",
"text/xml; subtype=gml/3.2.1", "application/vnd.ogc.gml", "text/xml" };

private final HashMap<String, FeatureInfoSerializer> featureInfoSerializers = new HashMap<String, FeatureInfoSerializer>();

private final LinkedHashMap<String, String> supportedFeatureInfoFormats = new LinkedHashMap<String, String>();

public FeatureInfoManager( boolean addDefaultFormats ) {
if ( addDefaultFormats ) {
defaultGMLGFIFormats.add( "application/gml+xml; version=2.1" );
defaultGMLGFIFormats.add( "application/gml+xml; version=3.0" );
defaultGMLGFIFormats.add( "application/gml+xml; version=3.1" );
defaultGMLGFIFormats.add( "application/gml+xml; version=3.2" );
defaultGMLGFIFormats.add( "text/xml; subtype=gml/2.1.2" );
defaultGMLGFIFormats.add( "text/xml; subtype=gml/3.0.1" );
defaultGMLGFIFormats.add( "text/xml; subtype=gml/3.1.1" );
defaultGMLGFIFormats.add( "text/xml; subtype=gml/3.2.1" );
supportedFeatureInfoFormats.put( "application/vnd.ogc.gml", "" );
supportedFeatureInfoFormats.put( "text/xml", "" );
supportedFeatureInfoFormats.put( "text/plain", "" );
supportedFeatureInfoFormats.put( "text/html", "" );
LOG.debug( "Adding default feature info formats" );

final FeatureInfoGmlWriter gmlWriter = new FeatureInfoGmlWriter();
for ( final String gmlFormat : GML_FORMATS ) {
featureInfoSerializers.put( gmlFormat, gmlWriter );
}

featureInfoSerializers.put( "text/plain", new PlainTextFeatureInfoSerializer() );
featureInfoSerializers.put( "text/html", new TemplateFeatureInfoSerializer() );
}
}

public void addOrReplaceCustomFormat( String format, FeatureInfoSerializer serializer ) {
LOG.debug( "Adding custom feature info format" );

featureInfoSerializers.put( format, serializer );
}

public void addOrReplaceFormat( String format, String file ) {
defaultGMLGFIFormats.remove( format );
supportedFeatureInfoFormats.put( format, file );
LOG.debug( "Adding template feature info format" );

featureInfoSerializers.put( format, new TemplateFeatureInfoSerializer( file ) );
}

public void addOrReplaceXsltFormat( String format, URL xsltUrl, GMLVersion version, Workspace workspace ) {
defaultGMLGFIFormats.remove( format );
LOG.debug( "Adding xslt feature info format" );

XsltFeatureInfoSerializer xslt = new XsltFeatureInfoSerializer( version, xsltUrl, workspace );
featureInfoSerializers.put( format, xslt );
}

public void finalizeConfiguration() {
for ( String f : defaultGMLGFIFormats ) {
supportedFeatureInfoFormats.put( f, "" );
}
}

public Set<String> getSupportedFormats() {
return supportedFeatureInfoFormats.keySet();
return featureInfoSerializers.keySet();
}

public void serializeFeatureInfo( FeatureInfoParams params )
throws IOException {
public void serializeFeatureInfo( FeatureInfoParams params, FeatureInfoContext context )
throws IOException, XMLStreamException {

String format = params.getFormat();
FeatureInfoSerializer serializer = featureInfoSerializers.get( format );
if ( serializer != null ) {
serializer.serialize( params.getNsBindings(), params.getFeatureCollection(), params.getOutputStream() );
return;
}

String fiFile = supportedFeatureInfoFormats.get( format );
if ( fiFile != null && !fiFile.isEmpty() ) {
runTemplate( params.getOutputStream(), fiFile, params.getFeatureCollection(), params.isWithGeometries() );
} else if ( isGml( format ) ) {
handleGmlOutput( format, params );
} else if ( format.equalsIgnoreCase( "text/plain" ) ) {
handlePlainTextOutput( params );
} else if ( format.equalsIgnoreCase( "text/html" ) ) {
runTemplate( params.getOutputStream(), null, params.getFeatureCollection(), params.isWithGeometries() );
LOG.debug( "Generating feature info output for format: {}", format );

FeatureInfoSerializer serializer = featureInfoSerializers.get( format.toLowerCase() );
if ( serializer != null ) {
serializer.serialize( params, context );
} else {
throw new IOException( "FeatureInfo format '" + format + "' is unknown." );
}
}

private boolean isGml( String format ) {
return format.equalsIgnoreCase( "application/vnd.ogc.gml" ) || format.equalsIgnoreCase( "text/xml" )
|| defaultGMLGFIFormats.contains( format.toLowerCase() );
}

private void handlePlainTextOutput( FeatureInfoParams params )
throws UnsupportedEncodingException {
PrintWriter out = new PrintWriter( new OutputStreamWriter( params.getOutputStream(), "UTF-8" ) );
for ( Feature f : params.getFeatureCollection() ) {
out.println( f.getName().getLocalPart() + ":" );
for ( Property p : f.getProperties() ) {
out.println( " " + p.getName().getLocalPart() + ": " + p.getValue() );
}
out.println();
}
out.close();
}

private void handleGmlOutput( String format, FeatureInfoParams params ) {
try {
XMLStreamWriter xmlWriter = params.getXmlWriter();

// for more than just quick 'hacky' schemaLocation attributes one should use a proper WFS
HashMap<String, String> bindings = new HashMap<String, String>();
String ns = determineNamespace( params );
if ( ns != null ) {
bindings.put( ns, params.getSchemaLocation() );
if ( !params.getNsBindings().containsValue( ns ) ) {
params.getNsBindings().put( "app", ns );
}
}
if ( !params.getNsBindings().containsKey( "app" ) ) {
params.getNsBindings().put( "app", "http://www.deegree.org/app" );
}
bindings.put( "http://www.opengis.net/wfs", "http://schemas.opengis.net/wfs/1.0.0/WFS-basic.xsd" );

GMLVersion gmlVersion = getGmlVersion( format );

GMLStreamWriter gmlWriter = GMLOutputFactory.createGMLStreamWriter( gmlVersion, xmlWriter );
gmlWriter.setOutputCrs( params.getCrs() );
gmlWriter.setNamespaceBindings( params.getNsBindings() );
gmlWriter.setExportGeometries( params.isWithGeometries() );
new FeatureInfoGmlWriter( gmlVersion ).export( params.getFeatureCollection(), gmlWriter,
ns == null ? params.getSchemaLocation() : null, bindings );
} catch ( Throwable e ) {
LOG.warn( "Error when writing GetFeatureInfo GML response '{}'.", e.getLocalizedMessage() );
LOG.trace( "Stack trace:", e );
}
}

private String determineNamespace( FeatureInfoParams params ) {
String ns = params.getFeatureType() == null ? null : params.getFeatureType().getName().getNamespaceURI();
if ( ns != null && ns.isEmpty() ) {
ns = null;
}
return ns;
}

private GMLVersion getGmlVersion( String format ) {
GMLVersion gmlVersion = GMLVersion.GML_2;
if ( format.endsWith( "3.0" ) || format.endsWith( "3.0.1" ) ) {
gmlVersion = GMLVersion.GML_30;
}
if ( format.endsWith( "3.1" ) || format.endsWith( "3.1.1" ) ) {
gmlVersion = GMLVersion.GML_31;
}
if ( format.endsWith( "3.2" ) || format.endsWith( "3.2.1" ) ) {
gmlVersion = GMLVersion.GML_32;
}
return gmlVersion;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,8 @@ Occam Labs UG (haftungsbeschränkt)
----------------------------------------------------------------------------*/
package org.deegree.featureinfo;

import java.io.OutputStream;
import java.util.Map;

import javax.xml.stream.XMLStreamWriter;

import org.deegree.cs.coordinatesystems.ICRS;
import org.deegree.feature.FeatureCollection;
import org.deegree.feature.types.FeatureType;
Expand All @@ -66,8 +63,6 @@ public class FeatureInfoParams {

private String format;

private OutputStream outputStream;

private boolean withGeometries;

private String schemaLocation;
Expand All @@ -76,20 +71,15 @@ public class FeatureInfoParams {

private ICRS crs;

private XMLStreamWriter xmlWriter;

public FeatureInfoParams( Map<String, String> nsBindings, FeatureCollection col, String format, OutputStream out,
boolean withGeometries, String schemaLocation, FeatureType type, ICRS crs,
XMLStreamWriter xmlWriter ) {
public FeatureInfoParams( Map<String, String> nsBindings, FeatureCollection col, String format,
boolean withGeometries, String schemaLocation, FeatureType type, ICRS crs ) {
this.nsBindings = nsBindings;
this.featureCollection = col;
this.format = format;
this.outputStream = out;
this.withGeometries = withGeometries;
this.schemaLocation = schemaLocation;
this.featureType = type;
this.crs = crs;
this.xmlWriter = xmlWriter;
}

/**
Expand All @@ -113,13 +103,6 @@ public String getFormat() {
return format;
}

/**
* @return the outputStream
*/
public OutputStream getOutputStream() {
return outputStream;
}

/**
* @return the withGeometries
*/
Expand Down Expand Up @@ -147,12 +130,4 @@ public FeatureType getFeatureType() {
public ICRS getCrs() {
return crs;
}

/**
* @return the xmlWriter
*/
public XMLStreamWriter getXmlWriter() {
return xmlWriter;
}

}
Loading

0 comments on commit 975f972

Please sign in to comment.