Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make DPI parameter parsing more compatible #1086

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -327,5 +328,89 @@ public static String repeat( int count, String str ) {
}
return sb.toString();
}

/**
* Split string into multiple elements and alow the delimiter to be escaped with a backslash
*
* @see OGC WFS 1.1.0 14.2.2
*
* @param input
* @param separator any character expect backslash
* @param limit maximum number of elements to return, zero is unlimited
* @return a list with all parts
*/
public static List<String> splitEscaped( String input, char delimiter, int limit ) {
List<String> res = new ArrayList<>();
if ( input == null ) {
return res;
}
if ( delimiter == '\\' ) {
throw new IllegalArgumentException( "The delimiter cannot be the escape character" );
}
if ( limit < 1 ) {
limit = Integer.MAX_VALUE;
}

StringBuilder sb = new StringBuilder();
boolean escaped = false;

for ( int i = 0; i < input.length(); i++ ) {
char c = input.charAt( i );
if ( c == delimiter && !escaped && res.size() < limit ) {
res.add( sb.toString() );
sb.setLength( 0 );
} else {
if ( escaped ) {
escaped = false;
sb.append( '\\' );
sb.append( c );
} else if ( c == '\\' ) {
escaped = true;
} else {
sb.append( c );
}
}
}

if ( escaped ) {
throw new IllegalArgumentException( "The specified String contains a incomplete escape sequence." );
}

res.add( sb.toString() );
return res;
}

/**
* Resolve escape sequences in a String.
*
* @see OGC WFS 1.1.0 14.2.2
*
* @param input the String to unescape
* @return resolved String
*/
public static String unescape(String input) {
Objects.requireNonNull( input, "The specified String cannot be null" );

StringBuilder sb = new StringBuilder();
boolean escaped = false;

for (int i = 0; i < input.length(); i++) {
char c = input.charAt(i);
if (escaped) {
escaped = false;
sb.append(c);
} else if (c == '\\') {
escaped = true;
} else {
sb.append(c);
}
}

if (escaped) {
throw new IllegalArgumentException(
"The specified String contains a incomplete escape sequence.");
}

return sb.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
/*----------------------------------------------------------------------------
This file is part of deegree, http://deegree.org/
Copyright (C) 2001-2010 by:
- Department of Geography, University of Bonn -
and
- lat/lon GmbH -
and
- grit graphische Informationstechnik Beratungsgesellschaft mbH -

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:

grit graphische Informationstechnik Beratungsgesellschaft mbH
Landwehrstr. 143, 59368 Werne
Germany
http://www.grit.de/

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.protocol.wms.filter;

import static org.deegree.filter.function.ParameterType.STRING;
import static org.deegree.filter.function.ParameterType.ANYTYPE;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.deegree.commons.tom.TypedObjectNode;
import org.deegree.commons.tom.primitive.BaseType;
import org.deegree.commons.tom.primitive.PrimitiveType;
import org.deegree.commons.tom.primitive.PrimitiveValue;
import org.deegree.commons.utils.StringUtils;
import org.deegree.cs.coordinatesystems.ICRS;
import org.deegree.filter.Expression;
import org.deegree.filter.FilterEvaluationException;
import org.deegree.filter.XPathEvaluator;
import org.deegree.filter.expression.Function;
import org.deegree.filter.function.FunctionProvider;
import org.deegree.filter.function.ParameterType;
import org.deegree.geometry.Envelope;
import org.deegree.workspace.Workspace;

/**
*
* @author <a href="mailto:reichhelm@grit.de">Stephan Reichhelm</a>
*/
public class EnvFunction implements FunctionProvider {

private static final String NAME = "env";

private static final List<ParameterType> INPUTS = new ArrayList<ParameterType>( 2 );

static {
INPUTS.add( STRING );
INPUTS.add( ANYTYPE );
}

static final ThreadLocal<Map<String, Object>> env = new ThreadLocal<Map<String, Object>>();

public static ThreadLocal<Map<String, Object>> getCurrentEnvValue() {
return env;
}

@Override
public void init( Workspace ws ) {
// nothing to do
}

@Override
public void destroy() {
// nothing to do
}

@Override
public String getName() {
return NAME;
}

@Override
public List<ParameterType> getArgs() {
return INPUTS;
}

@Override
public ParameterType getReturnType() {
return ANYTYPE;
}

@Override
public Function create( List<Expression> params ) {
return new Function( NAME, params ) {
@Override
public <T> TypedObjectNode[] evaluate( T obj, XPathEvaluator<T> xpathEvaluator )
throws FilterEvaluationException {
TypedObjectNode[] key = getParams()[0].evaluate( obj, xpathEvaluator );
TypedObjectNode[] def = getParams()[1].evaluate( obj, xpathEvaluator );
if ( key.length != 1 ) {
throw new FilterEvaluationException( "The " + NAME + " function's first argument must "
+ "evaluate to exactly one value." );
}
if ( def.length != 1 ) {
throw new FilterEvaluationException( "The " + NAME + " function's second argument must "
+ "evaluate to exactly one value." );
}

Map<String, Object> map = env.get();
Object result = map != null ? map.get( key[0].toString() ) : null;

if ( result == null ) {
result = def[0];
}

if ( result != null ) {
if ( result instanceof TypedObjectNode ) {
return new TypedObjectNode[] { (TypedObjectNode) result };
}
try {
return new TypedObjectNode[] { new PrimitiveValue( result ) };
} catch ( Exception ignored ) {
return new TypedObjectNode[] { new PrimitiveValue( result.toString(),
new PrimitiveType( BaseType.STRING ) ) };
}
} else {
return new TypedObjectNode[] {};
}
}
};
}

public static Map<String, Object> parse( Map<String, String> map, Envelope box, ICRS crs, int width, int height,
double scale ) {
Map<String, Object> res = new HashMap<>();
String env = map != null ? map.get( "ENV" ) : null;

for ( String word : StringUtils.splitEscaped( env, ';', 0 ) ) {
List<String> keyValue = StringUtils.splitEscaped( word, ':', 2 );
String raw = keyValue.size() == 1 ? "true" : StringUtils.unescape( keyValue.get( 1 ) );

// no further key based parsing is available at the moment
res.put( keyValue.get( 0 ), raw );
}

res.put( "wms_bbox", box );
res.put( "wms_crs", crs );
res.put( "wms_srs", crs != null ? crs.getName() : null );
res.put( "wms_width", width );
res.put( "wms_height", height );
res.put( "wms_scale_denominator", scale );

return res;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
package org.deegree.protocol.wms.map;

import static java.awt.Color.WHITE;
import static java.util.Collections.emptyMap;
import static org.deegree.commons.xml.CommonNamespaces.OWS_NS;
import static org.deegree.commons.xml.CommonNamespaces.SLDNS;
import static org.deegree.commons.xml.CommonNamespaces.WMSNS;
Expand Down Expand Up @@ -96,6 +97,16 @@ public class GetMapParser extends AbstractWmsParser {

private static final SimpleGeometryFactory GEOMETRY_FACTORY = new SimpleGeometryFactory();

private final Map<String, String> requestParameters;

public GetMapParser() {
requestParameters = emptyMap();
}

public GetMapParser( Map<String, String> requestParameters) {
this.requestParameters = requestParameters;
}

/**
* Parses a WMS <code>GetMap</code> document into a {@link GetMap} object.
*
Expand Down Expand Up @@ -164,7 +175,7 @@ private GetMap createGetMap( List<LayerRef> layers, List<StyleRef> styles, ICRS
boolean transparent = output.transparent;
Color color = output.bgcolor;
return new GetMap( layers, styles, width, height, envelope, crs, format, transparent, color, parameterMap,
dimensions );
dimensions, requestParameters );
}

private Output parseOutput( XMLStreamReader in )
Expand Down
Loading