Revision: 4737 http://sourceforge.net/p/jump-pilot/code/4737 Author: edso Date: 2016-01-02 16:19:28 +0000 (Sat, 02 Jan 2016) Log Message: ----------- - finetuned wfs httpclient access, unified proxy and auth handling - wfs request is now buffered to disk for optimal performance - wfs item count is now limited by RAM only NOTE: we should probably switch to deegree3+ instead of patching things up
Modified Paths: -------------- core/trunk/ChangeLog core/trunk/src/com/vividsolutions/jump/util/FileUtil.java core/trunk/src/de/latlon/deejump/wfs/auth/UserData.java core/trunk/src/de/latlon/deejump/wfs/client/AbstractWFSWrapper.java core/trunk/src/de/latlon/deejump/wfs/client/WFSClientHelper.java core/trunk/src/de/latlon/deejump/wfs/client/WFSHttpClient.java core/trunk/src/de/latlon/deejump/wfs/client/WFServiceWrapper_1_0_0.java core/trunk/src/de/latlon/deejump/wfs/client/WFServiceWrapper_1_1_0.java core/trunk/src/de/latlon/deejump/wfs/data/JUMPFeatureFactory.java core/trunk/src/de/latlon/deejump/wfs/deegree2mods/GMLSchemaDocument.java core/trunk/src/de/latlon/deejump/wfs/deegree2mods/WFSCapabilitiesDocument.java core/trunk/src/de/latlon/deejump/wfs/deegree2mods/XMLFragment.java core/trunk/src/de/latlon/deejump/wfs/plugin/UpdateWFSLayerPlugIn.java core/trunk/src/de/latlon/deejump/wfs/plugin/WFSPlugIn.java core/trunk/src/de/latlon/deejump/wfs/ui/RequestPanel.java core/trunk/src/de/latlon/deejump/wfs/ui/WFSPanel.java Added Paths: ----------- core/trunk/src/de/latlon/deejump/wfs/client/WFSGetMethod.java core/trunk/src/de/latlon/deejump/wfs/client/WFSMethod.java core/trunk/src/de/latlon/deejump/wfs/client/WFSPostMethod.java core/trunk/src/de/latlon/deejump/wfs/deegree2mods/GMLFeatureCollectionDocument.java Modified: core/trunk/ChangeLog =================================================================== --- core/trunk/ChangeLog 2016-01-02 15:46:40 UTC (rev 4736) +++ core/trunk/ChangeLog 2016-01-02 16:19:28 UTC (rev 4737) @@ -3,6 +3,12 @@ # 2. make sure that lines break at 80 chars for constricted display situations #<-------------------------------- 80 chars ----------------------------------># +2016-01-02 ede + * finetuned wfs httpclient access, unified proxy and auth handling + * wfs request is now buffered to disk for optimal performance + * wfs item count is now limited by RAM only + * NOTE: we should probably switch to deegree3+ instead of patching things up + 2015-12-31 ede * fix FeatureInfoTool did not respect wms 1.3.0 axis order * EditWMSQueryPlugin allows now properly changing of _all_ parameters Modified: core/trunk/src/com/vividsolutions/jump/util/FileUtil.java =================================================================== --- core/trunk/src/com/vividsolutions/jump/util/FileUtil.java 2016-01-02 15:46:40 UTC (rev 4736) +++ core/trunk/src/com/vividsolutions/jump/util/FileUtil.java 2016-01-02 16:19:28 UTC (rev 4737) @@ -27,7 +27,17 @@ package com.vividsolutions.jump.util; -import java.io.*; +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; +import java.io.BufferedReader; +import java.io.Closeable; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; import java.net.URI; import java.nio.charset.Charset; import java.util.ArrayList; @@ -37,239 +47,330 @@ import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; +import com.vividsolutions.jump.I18N; import com.vividsolutions.jump.io.CompressedFile; +import com.vividsolutions.jump.task.TaskMonitor; /** * File-related utility functions. */ public class FileUtil { - /** - * Gets the content of filename as a list of lines. - * - * @param filename name of the input file - * @return a list of strings representing the file's lines - * @throws IOException - */ - public static List getContents(String filename) throws IOException { - return getContents(new FileInputStream(filename)); - } + public static final String I18NPREFIX = FileUtil.class.getName() + "."; - /** - * Gets the content of filename as a list of lines. - * - * @param filename name of the input file - * @param encoding charset to use to decode filename - * @return a list of strings representing the file's lines - * @throws IOException - */ - public static List getContents(String filename, String encoding) throws IOException { - return getContents(new FileInputStream(filename), encoding); - } + /** + * Gets the content of filename as a list of lines. + * + * @param filename + * name of the input file + * @return a list of strings representing the file's lines + * @throws IOException + */ + public static List getContents(String filename) throws IOException { + return getContents(new FileInputStream(filename)); + } - /** - * Gets the content a compressed file passed as an URI. - * - * @param uri uri of the input resource - * @return a list of strings representing the compressed file's lines - * @throws IOException - */ - public static List getContents(URI uri) throws IOException { - return getContents(CompressedFile.openFile(uri)); - } + /** + * Gets the content of filename as a list of lines. + * + * @param filename + * name of the input file + * @param encoding + * charset to use to decode filename + * @return a list of strings representing the file's lines + * @throws IOException + */ + public static List getContents(String filename, String encoding) + throws IOException { + return getContents(new FileInputStream(filename), encoding); + } - /** - * Gets the content of an inputSteam as a list of lines. - * @param inputStream - * @return a list of lines - * @throws IOException - */ - public static List getContents(InputStream inputStream) throws IOException { - return getContents(inputStream, Charset.defaultCharset().name()); - } + /** + * Gets the content a compressed file passed as an URI. + * + * @param uri + * uri of the input resource + * @return a list of strings representing the compressed file's lines + * @throws IOException + */ + public static List getContents(URI uri) throws IOException { + return getContents(CompressedFile.openFile(uri)); + } - /** - * Gets the content of an inputSteam as a list of lines. inputStream is decoded - * with the specified charset. - * @param inputStream - * @return a list of lines - * @throws IOException - */ - public static List getContents(InputStream inputStream, String encoding) throws IOException { - ArrayList contents = new ArrayList(); - InputStreamReader inputStreamReader = new InputStreamReader(inputStream, encoding); - try { - BufferedReader bufferedReader = new BufferedReader( - inputStreamReader); - try { - String line = bufferedReader.readLine(); - while (line != null) { - contents.add(line); - line = bufferedReader.readLine(); - } - } finally { - bufferedReader.close(); - } - } finally { - inputStreamReader.close(); + /** + * Gets the content of an inputSteam as a list of lines. + * + * @param inputStream + * @return a list of lines + * @throws IOException + */ + public static List getContents(InputStream inputStream) throws IOException { + return getContents(inputStream, Charset.defaultCharset().name()); + } + + /** + * Gets the content of an inputSteam as a list of lines. inputStream is + * decoded with the specified charset. + * + * @param inputStream + * @return a list of lines + * @throws IOException + */ + public static List getContents(InputStream inputStream, String encoding) + throws IOException { + ArrayList contents = new ArrayList(); + InputStreamReader inputStreamReader = new InputStreamReader(inputStream, + encoding); + try { + BufferedReader bufferedReader = new BufferedReader(inputStreamReader); + try { + String line = bufferedReader.readLine(); + while (line != null) { + contents.add(line); + line = bufferedReader.readLine(); } - return contents; + } finally { + bufferedReader.close(); + } + } finally { + inputStreamReader.close(); } - - /** - * Saves the String to a file with the given filename. - * - * @param filename the pathname of the file to create (or overwrite) - * @param contents the data to save - * @throws IOException if an I/O error occurs. - */ - public static void setContents(String filename, String contents) - throws IOException { - setContents(filename, contents, Charset.defaultCharset().name()); - } + return contents; + } + /** + * Saves the String to a file with the given filename. + * + * @param filename + * the pathname of the file to create (or overwrite) + * @param contents + * the data to save + * @throws IOException + * if an I/O error occurs. + */ + public static void setContents(String filename, String contents) + throws IOException { + setContents(filename, contents, Charset.defaultCharset().name()); + } + /** + * Saves the List of Strings to a file with the given filename. + * + * @param filename + * the pathname of the file to create (or overwrite) + * @param lines + * the Strings to save as lines in the file + * @throws IOException + * if an I/O error occurs. + */ + public static void setContents(String filename, List lines) + throws IOException { + setContents(filename, lines, Charset.defaultCharset().name()); + } - /** - * Saves the List of Strings to a file with the given filename. - * - * @param filename the pathname of the file to create (or overwrite) - * @param lines the Strings to save as lines in the file - * @throws IOException if an I/O error occurs. - */ - public static void setContents(String filename, List lines) - throws IOException { - setContents(filename, lines, Charset.defaultCharset().name()); - } + /** + * Saves lines into a file named filename, using encoding charset. + * + * @param filename + * the pathname of the file to create (or overwrite) + * @param contents + * the data to save + * @throws IOException + * if an I/O error occurs. + */ + public static void setContents(String filename, String contents, + String encoding) throws IOException { + List<String> lines = new ArrayList<String>(); + lines.add(contents); + setContents(filename, lines, encoding); + } - /** - * Saves lines into a file named filename, using encoding charset. - * - * @param filename the pathname of the file to create (or overwrite) - * @param contents the data to save - * @throws IOException if an I/O error occurs. - */ - public static void setContents(String filename, String contents, String encoding) - throws IOException { - List<String> lines = new ArrayList<String>(); - lines.add(contents); - setContents(filename, lines, encoding); - } - - /** - * Saves lines into a file named filename, using encoding charset. - * - * @param filename the pathname of the file to create (or overwrite) - * @param lines the data to save - * @throws IOException if an I/O error occurs. - */ - public static void setContents(String filename, List<String> lines, String encoding) - throws IOException { - OutputStreamWriter osw = null; + /** + * Saves lines into a file named filename, using encoding charset. + * + * @param filename + * the pathname of the file to create (or overwrite) + * @param lines + * the data to save + * @throws IOException + * if an I/O error occurs. + */ + public static void setContents(String filename, List<String> lines, + String encoding) throws IOException { + OutputStreamWriter osw = null; + try { + osw = new OutputStreamWriter(new FileOutputStream(filename), encoding); + String lineSep = System.lineSeparator(); + for (Iterator<String> it = lines.iterator(); it.hasNext();) { + osw.write(it.next()); + if (it.hasNext()) { + osw.write(lineSep); + } + } + } finally { + if (osw != null) { try { - osw = new OutputStreamWriter(new FileOutputStream(filename), encoding); - String lineSep = System.lineSeparator(); - for (Iterator<String> it = lines.iterator() ; it.hasNext() ; ) { - osw.write(it.next()); - if (it.hasNext()) { - osw.write(lineSep); - } - } - } finally { - if (osw != null) { - try {osw.close();} catch(IOException e) {} - } + osw.close(); + } catch (IOException e) { } + } } + } - public static void zip(Collection files, File zipFile) throws IOException { - FileOutputStream fos = new FileOutputStream(zipFile); + public static void zip(Collection files, File zipFile) throws IOException { + FileOutputStream fos = new FileOutputStream(zipFile); + try { + BufferedOutputStream bos = new BufferedOutputStream(fos); + try { + ZipOutputStream zos = new ZipOutputStream(bos); try { - BufferedOutputStream bos = new BufferedOutputStream(fos); + for (Iterator i = files.iterator(); i.hasNext();) { + File file = (File) i.next(); + zos.putNextEntry(new ZipEntry(file.getName())); + FileInputStream fis = new FileInputStream(file); try { - ZipOutputStream zos = new ZipOutputStream(bos); - try { - for (Iterator i = files.iterator(); i.hasNext(); ) { - File file = (File) i.next(); - zos.putNextEntry(new ZipEntry(file.getName())); - FileInputStream fis = new FileInputStream(file); - try { - BufferedInputStream bis = new BufferedInputStream( - fis); - try { - while (true) { - int j = bis.read(); - if (j == -1) { - break; - } - zos.write(j); - } - } finally { - bis.close(); - } - } finally { - fis.close(); - zos.closeEntry(); - } - } - } finally { - zos.close(); + BufferedInputStream bis = new BufferedInputStream(fis); + try { + while (true) { + int j = bis.read(); + if (j == -1) { + break; + } + zos.write(j); } + } finally { + bis.close(); + } } finally { - bos.close(); + fis.close(); + zos.closeEntry(); } + } } finally { - fos.close(); + zos.close(); } + } finally { + bos.close(); + } + } finally { + fos.close(); } - - public static String getExtension(String s) { - String ext = ""; - int i = s.lastIndexOf('.'); + } - if ((i > 0) && (i < (s.length() - 1))) { - ext = s.substring(i + 1).toLowerCase(); - } + public static String getExtension(String s) { + String ext = ""; + int i = s.lastIndexOf('.'); - return ext; + if ((i > 0) && (i < (s.length() - 1))) { + ext = s.substring(i + 1).toLowerCase(); } - - public static String getExtension(File f) { - String s = f.getName(); - return getExtension(s); + + return ext; } - public static File addExtensionIfNone(File file, String extension) { - if (getExtension(file).length() > 0) { - return file; - } - String path = file.getAbsolutePath(); - if (!path.endsWith(".")) { - path += "."; - } - path += extension; - return new File(path); + public static String getExtension(File f) { + String s = f.getName(); + return getExtension(s); + } + + public static File addExtensionIfNone(File file, String extension) { + if (getExtension(file).length() > 0) { + return file; } - - public static File removeExtensionIfAny(File file) { - int i = file.getName().lastIndexOf('.'); - if (i > 0) { - String path = file.getAbsolutePath(); - path = path.substring(0, path.length()-file.getName().length()+i); - return new File(path); - } - else return file; + String path = file.getAbsolutePath(); + if (!path.endsWith(".")) { + path += "."; } - - public static String getFileNameFromLayerName(String layerName) { - return layerName.replaceAll("[/\\\\\\?%\\*:\\|\"<> ]+","_") - .replaceAll("^_","").replaceAll("_$",""); + path += extension; + return new File(path); + } + + public static File removeExtensionIfAny(File file) { + int i = file.getName().lastIndexOf('.'); + if (i > 0) { + String path = file.getAbsolutePath(); + path = path.substring(0, path.length() - file.getName().length() + i); + return new File(path); + } else + return file; + } + + public static String getFileNameFromLayerName(String layerName) { + return layerName.replaceAll("[/\\\\\\?%\\*:\\|\"<> ]+", "_") + .replaceAll("^_", "").replaceAll("_$", ""); + } + + public static void close(Closeable is) { + try { + if (is instanceof Closeable) + is.close(); + } catch (IOException e) { } + } - public static void close(Closeable is){ - try { - if (is instanceof Closeable) - is.close(); - } catch (IOException e) {} + public static final String PREFIX = "openjump"; + public static final String SUFFIX = ".tmp"; + + /** + * utility method to copy an inputstream to a temp file for further processing + * NOTE: prefix, suffix, monitor are optional and may be {@link <code>null</code>} + * + * @param in + * @param prefix + * - default "openjump" + * @param suffix + * - default ".tmp" + * @param monitor + * @return + * @throws IOException + */ + public static File copyInputStreamToTempFile(InputStream in, String prefix, + String suffix, TaskMonitor monitor) throws IOException { + final File tempFile = File.createTempFile(prefix != null ? prefix : PREFIX, + suffix != null ? suffix : SUFFIX); + tempFile.deleteOnExit(); + // Files.copy(in, tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING); + BufferedOutputStream fout = new BufferedOutputStream(new FileOutputStream( + tempFile)); + + long counter = 0; + int r = 0; + byte[] b = new byte[1024]; + long startCounter = counter; + long stopCounter = counter; + long startTime = System.currentTimeMillis(); + long stopTime = 0; + int i = 0; + float speed = 0; + while ((r = in.read(b)) != -1) { + counter += r; + + i += 1; + if (i % 1023 == 0) { + stopTime = System.currentTimeMillis(); + stopCounter = counter; + speed = (stopCounter - startCounter) / 1024f / 1024 + / ((stopTime - startTime) / 1000f); + } + + // only report if monitor is set + if (monitor != null) { + monitor.report(I18N.getMessage( + I18NPREFIX + "receiving-{0}-MiB-(at-{1}-{2})", + String.format("%.2f", counter / 1024f / 1024), + String.format("%.2f", speed < 1 ? speed * 1024 : speed), + speed < 1 ? I18N.getMessage(I18NPREFIX + "KiB/s") : I18N + .getMessage(I18NPREFIX + "MiB/s"))); + } + fout.write(b, 0, r); + if (i >= 1023) { + startTime = System.currentTimeMillis(); + startCounter = counter + 1; + i = 0; + } } + in.close(); + fout.close(); + return tempFile; + } } Modified: core/trunk/src/de/latlon/deejump/wfs/auth/UserData.java =================================================================== --- core/trunk/src/de/latlon/deejump/wfs/auth/UserData.java 2016-01-02 15:46:40 UTC (rev 4736) +++ core/trunk/src/de/latlon/deejump/wfs/auth/UserData.java 2016-01-02 16:19:28 UTC (rev 4737) @@ -78,4 +78,8 @@ public String getPassword() { return password; } + + public boolean isEmpty(){ + return username == null || username.isEmpty(); + } } Modified: core/trunk/src/de/latlon/deejump/wfs/client/AbstractWFSWrapper.java =================================================================== --- core/trunk/src/de/latlon/deejump/wfs/client/AbstractWFSWrapper.java 2016-01-02 15:46:40 UTC (rev 4736) +++ core/trunk/src/de/latlon/deejump/wfs/client/AbstractWFSWrapper.java 2016-01-02 16:19:28 UTC (rev 4737) @@ -9,297 +9,336 @@ package de.latlon.deejump.wfs.client; -import java.io.*; -import java.net.*; -import java.util.*; +import java.io.StringReader; +import java.net.URL; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; -import org.apache.log4j.*; -import org.deegree.datatypes.*; -import org.deegree.framework.xml.*; -import org.deegree.model.feature.schema.*; -import org.deegree.ogcwebservices.*; -import org.deegree.ogcwebservices.wfs.capabilities.*; +import org.apache.log4j.Logger; +import org.deegree.datatypes.QualifiedName; +import org.deegree.datatypes.Types; +import org.deegree.framework.xml.DOMPrinter; +import org.deegree.model.feature.schema.FeatureType; +import org.deegree.model.feature.schema.GMLSchema; +import org.deegree.model.feature.schema.PropertyType; +import org.deegree.ogcwebservices.OWSUtils; +import org.deegree.ogcwebservices.wfs.capabilities.WFSFeatureType; +import org.openjump.util.UriUtil; -import de.latlon.deejump.wfs.*; -import de.latlon.deejump.wfs.auth.*; +import de.latlon.deejump.wfs.DeeJUMPException; +import de.latlon.deejump.wfs.auth.UserData; +import de.latlon.deejump.wfs.deegree2mods.GMLSchemaDocument; /** - * Superclass that wraps the basic functionality of a (simple) WFS. This class encapsulates the - * behaviour of a WFS, and allows subclasses to change behaviour according to WFS version. + * Superclass that wraps the basic functionality of a (simple) WFS. This class + * encapsulates the behaviour of a WFS, and allows subclasses to change + * behaviour according to WFS version. * * @author <a href="mailto:tad...@lat-lon.de">Ugo Taddei</a> * @author last edited by: $Author: stranger $ * - * @version $Revision: 1438 $, $Date: 2008-05-29 15:00:02 +0200 (Do, 29 Mai 2008) $ + * @version $Revision: 1438 $, $Date: 2008-05-29 15:00:02 +0200 (Do, 29 Mai + * 2008) $ */ public abstract class AbstractWFSWrapper { - /** + /** * */ - public static final String WFS_PREFIX = "wfs"; + public static final String WFS_PREFIX = "wfs"; - private static Logger LOG = Logger.getLogger( AbstractWFSWrapper.class ); + private static Logger LOG = Logger.getLogger(AbstractWFSWrapper.class); - protected String baseURL; + protected String baseURL; - protected Map<String, WFSFeatureType> ftNameToWfsFT; + protected Map<String, WFSFeatureType> ftNameToWfsFT; - private Map<String, GMLSchema> featureTypeToSchema; + private Map<String, GMLSchema> featureTypeToSchema; - // hmmm, this is repeating the above, really... - private Map<String, String> featureTypeToSchemaXML; + // hmmm, this is repeating the above, really... + private Map<String, String> featureTypeToSchemaXML; - private Map<String, QualifiedName[]> geoPropsNameToQNames; + private Map<String, QualifiedName[]> geoPropsNameToQNames; - protected UserData logins; + protected UserData logins; - /** - * @return the version as String - */ - public abstract String getServiceVersion(); + /** + * @return the version as String + */ + public abstract String getServiceVersion(); - /** - * @return the list of feature types - */ - public abstract String[] getFeatureTypes(); + /** + * @return the list of feature types + */ + public abstract String[] getFeatureTypes(); - /** - * @return the GET GetFeature URL - */ - public abstract String getGetFeatureURL(); + /** + * @return the GET GetFeature URL + */ + public abstract String getGetFeatureURL(); - abstract protected String createDescribeFTOnlineResource(); + abstract protected String createDescribeFTOnlineResource(); - /** - * @return the base WFS URL - */ - public String getBaseWfsURL() { - return this.baseURL; - } + /** + * @return the base WFS URL + */ + public String getBaseWfsURL() { + return this.baseURL; + } - /** - * @return the capabilities as String - */ - public abstract String getCapabilitesAsString(); + /** + * get login data + */ + public UserData getLogins() { + return logins; + } - protected AbstractWFSWrapper( UserData logins, String baseUrl ) { - if ( baseUrl == null || baseUrl.length() == 0 ) { - throw new IllegalArgumentException( "The URL for the WFServer cannot be null or empty." ); - } - this.baseURL = baseUrl; - this.logins = logins; - this.featureTypeToSchema = new HashMap<String, GMLSchema>(); - this.featureTypeToSchemaXML = new HashMap<String, String>(); - this.geoPropsNameToQNames = new HashMap<String, QualifiedName[]>(); + /** + * set login userdata + */ + public void setLogins(UserData logins) { + this.logins = logins; + } - } + /** + * @return the capabilities as String + */ + public abstract String getCapabilitesAsString(); - /** - * @return the capabilities URL - */ - public String getCapabilitiesURL() { - - StringBuffer sb = new StringBuffer( OWSUtils.validateHTTPGetBaseURL( this.baseURL ) ); - sb.append( "SERVICE=WFS&REQUEST=GetCapabilities&VERSION=" ); - sb.append( getServiceVersion() ); - if ( logins != null && logins.getUsername() != null && logins.getPassword() != null ) { - sb.append( "&user=" + logins.getUsername() + "&password=" + logins.getPassword() ); - } - return sb.toString(); + protected AbstractWFSWrapper(UserData logins, String baseUrl) { + if (baseUrl == null || baseUrl.length() == 0) { + throw new IllegalArgumentException( + "The URL for the WFServer cannot be null or empty."); } + this.baseURL = baseUrl; + this.logins = logins; + this.featureTypeToSchema = new HashMap<String, GMLSchema>(); + this.featureTypeToSchemaXML = new HashMap<String, String>(); + this.geoPropsNameToQNames = new HashMap<String, QualifiedName[]>(); - /** - * @param baseURL - * the base URL to use - * @param typename - * @return the full request String - */ - public String getDescribeTypeURL( String baseURL, QualifiedName typename ) { - String url; + } - if ( typename.getPrefix() == null || typename.getPrefix().length() == 0 ) { - url = baseURL + "SERVICE=WFS&REQUEST=DescribeFeatureType&version=" + getServiceVersion() + "&TYPENAME=" - + typename.getLocalName(); - } else { - url = baseURL + "SERVICE=WFS&REQUEST=DescribeFeatureType&version=" + getServiceVersion() + "&TYPENAME=" - + typename.getPrefix() + ":" + typename.getLocalName() + "&NAMESPACE=xmlns(" + typename.getPrefix() - + "=" + typename.getNamespace() + ")"; - } + /** + * @return the capabilities URL + */ + public String getCapabilitiesURL() { - if ( logins != null && logins.getUsername() != null && logins.getPassword() != null ) { - url += "&user=" + logins.getUsername() + "&password=" + logins.getPassword(); - } + StringBuffer sb = new StringBuffer( + OWSUtils.validateHTTPGetBaseURL(this.baseURL)); + sb.append("SERVICE=WFS&REQUEST=GetCapabilities&VERSION="); + sb.append(getServiceVersion()); + // if ( logins != null && logins.getUsername() != null && + // logins.getPassword() != null ) { + // sb.append( "&user=" + logins.getUsername() + "&password=" + + // logins.getPassword() ); + // } + // + String url = sb.toString(); + if (logins != null && !logins.isEmpty()) + url = UriUtil.urlAddCredentials(url, logins.getUsername(), + logins.getPassword()); + return url; + } - return url; - } + /** + * @param baseURL + * the base URL to use + * @param typename + * @return the full request String + */ + public String getDescribeTypeURL(String baseURL, QualifiedName typename) { + String url; - /** - * @param typename - * @return the full request String - */ - public String getDescribeTypeURL( QualifiedName typename ) { - String url = getDescribeTypeURL( OWSUtils.validateHTTPGetBaseURL( createDescribeFTOnlineResource() ), typename ); - LOG.debug( "Describe Feature Type request:\n" + url ); - return url; + if (typename.getPrefix() == null || typename.getPrefix().length() == 0) { + url = baseURL + "SERVICE=WFS&REQUEST=DescribeFeatureType&version=" + + getServiceVersion() + "&TYPENAME=" + typename.getLocalName(); + } else { + url = baseURL + "SERVICE=WFS&REQUEST=DescribeFeatureType&version=" + + getServiceVersion() + "&TYPENAME=" + typename.getPrefix() + ":" + + typename.getLocalName() + "&NAMESPACE=xmlns(" + + typename.getPrefix() + "=" + typename.getNamespace() + ")"; } - /** - * @param featureType - * @return the GMLSchema object for the feature type - */ - public synchronized GMLSchema getSchemaForFeatureType( String featureType ) { - GMLSchema res = this.featureTypeToSchema.get( featureType ); - if ( res != null ) { - return res; - } + if (logins != null && !logins.isEmpty()) + url = UriUtil.urlAddCredentials(url, logins.getUsername(), + logins.getPassword()); - createSchemaForFeatureType( featureType ); - return this.featureTypeToSchema.get( featureType ); - } + return url; + } - /** - * @param featureType - * @return a String encoding of the Schema for the feature type - */ - public String getRawSchemaForFeatureType( String featureType ) { - return this.featureTypeToSchemaXML.get( featureType ); + /** + * @param typename + * @return the full request String + */ + public String getDescribeTypeURL(QualifiedName typename) { + String url = getDescribeTypeURL( + OWSUtils.validateHTTPGetBaseURL(createDescribeFTOnlineResource()), + typename); + LOG.debug("Describe Feature Type request:\n" + url); + + return url; + } + + /** + * @param featureType + * @return the GMLSchema object for the feature type + */ + public synchronized GMLSchema getSchemaForFeatureType(String featureType) { + GMLSchema res = this.featureTypeToSchema.get(featureType); + if (res != null) { + return res; } - protected String loadSchemaForFeatureType( String featureType ) - throws DeeJUMPException { + createSchemaForFeatureType(featureType); + return this.featureTypeToSchema.get(featureType); + } - String descrFtUrl = createDescribeFTOnlineResource(); + /** + * @param featureType + * @return a String encoding of the Schema for the feature type + */ + public String getRawSchemaForFeatureType(String featureType) { + return this.featureTypeToSchemaXML.get(featureType); + } - if ( descrFtUrl == null ) { - throw new RuntimeException( - "Service does not have a DescribeFeatureType operation accessible by HTTP GET or POST." ); - } + protected String loadSchemaForFeatureType(String featureType) + throws DeeJUMPException { - WFSFeatureType wfsFt = getFeatureTypeByName( featureType ); - if ( wfsFt == null ) { - return null; - } + String descrFtUrl = createDescribeFTOnlineResource(); - QualifiedName ft = wfsFt.getName(); - String serverReq = getDescribeTypeURL( ft ); + if (descrFtUrl == null) { + throw new RuntimeException( + "Service does not have a DescribeFeatureType operation accessible by HTTP GET or POST."); + } - try { - GMLSchemaDocument xsdDoc = new de.latlon.deejump.wfs.deegree2mods.GMLSchemaDocument(); - xsdDoc.load( new URL( serverReq ) ); - return DOMPrinter.nodeToString( xsdDoc.getRootElement(), null ); - } catch ( Exception e ) { - e.printStackTrace(); - String mesg = "Error fetching FeatureType description"; - LOG.error( mesg + " for " + featureType + " using " + serverReq ); - throw new DeeJUMPException( mesg, e ); - } + WFSFeatureType wfsFt = getFeatureTypeByName(featureType); + if (wfsFt == null) { + return null; + } + QualifiedName ft = wfsFt.getName(); + String serverReq = getDescribeTypeURL(ft); + + try { + GMLSchemaDocument xsdDoc = new de.latlon.deejump.wfs.deegree2mods.GMLSchemaDocument(); + xsdDoc.load(new URL(serverReq)); + return DOMPrinter.nodeToString(xsdDoc.getRootElement(), null); + } catch (Exception e) { + e.printStackTrace(); + String mesg = "Error fetching FeatureType description"; + LOG.error(mesg + " for " + featureType + " using " + serverReq); + throw new DeeJUMPException(mesg, e); } - /** - * Creates an String[] containing the attributes of a given feature type - * - * @param featureTypeName - * the name of the feature type - */ - protected synchronized void createSchemaForFeatureType( String featureTypeName ) { + } - try { - // GMLSchema xsd = loadSchemaForFeatureType( featureTypeName ); - String rawXML = loadSchemaForFeatureType( featureTypeName ); - if ( rawXML == null ) { - return; - } - GMLSchemaDocument xsdDoc = new GMLSchemaDocument(); - xsdDoc.load( new StringReader( rawXML ), "http://empty" ); - GMLSchema xsd = xsdDoc.parseGMLSchema(); + /** + * Creates an String[] containing the attributes of a given feature type + * + * @param featureTypeName + * the name of the feature type + */ + protected synchronized void createSchemaForFeatureType(String featureTypeName) { - this.featureTypeToSchema.put( featureTypeName, xsd ); - this.featureTypeToSchemaXML.put( featureTypeName, rawXML ); + try { + // GMLSchema xsd = loadSchemaForFeatureType( featureTypeName ); + String rawXML = loadSchemaForFeatureType(featureTypeName); + if (rawXML == null) { + return; + } + GMLSchemaDocument xsdDoc = new GMLSchemaDocument(); + xsdDoc.load(new StringReader(rawXML), "http://empty"); + GMLSchema xsd = xsdDoc.parseGMLSchema(); - QualifiedName[] geoProp = guessGeomProperty( xsd, featureTypeName ); + this.featureTypeToSchema.put(featureTypeName, xsd); + this.featureTypeToSchemaXML.put(featureTypeName, rawXML); - this.geoPropsNameToQNames.put( featureTypeName, geoProp ); + QualifiedName[] geoProp = guessGeomProperty(xsd, featureTypeName); - } catch ( Exception e ) { - e.printStackTrace(); - } + this.geoPropsNameToQNames.put(featureTypeName, geoProp); + + } catch (Exception e) { + e.printStackTrace(); } + } - /** - * @param featureType - * @return a list of property names - */ - public String[] getProperties( String featureType ) { + /** + * @param featureType + * @return a list of property names + */ + public String[] getProperties(String featureType) { - List<String> propsList = new ArrayList<String>(); - try { - createSchemaForFeatureType( featureType ); + List<String> propsList = new ArrayList<String>(); + try { + createSchemaForFeatureType(featureType); - GMLSchema schema = featureTypeToSchema.get( featureType ); - if ( schema != null ) { - FeatureType[] fts = schema.getFeatureTypes(); - for ( int i = 0; i < fts.length; i++ ) { - if ( fts[i].getName().getLocalName().equals( featureType ) ) { - PropertyType[] props = fts[i].getProperties(); - for ( int j = 0; j < props.length; j++ ) { - if ( !( props[j].getType() == Types.GEOMETRY || props[j].getType() == 10014 ) ) { - propsList.add( props[j].getName().getPrefixedName() ); - } - } - } - } + GMLSchema schema = featureTypeToSchema.get(featureType); + if (schema != null) { + FeatureType[] fts = schema.getFeatureTypes(); + for (int i = 0; i < fts.length; i++) { + if (fts[i].getName().getLocalName().equals(featureType)) { + PropertyType[] props = fts[i].getProperties(); + for (int j = 0; j < props.length; j++) { + if (!(props[j].getType() == Types.GEOMETRY || props[j].getType() == 10014)) { + propsList.add(props[j].getName().getPrefixedName()); + } } - } catch ( Exception e ) { - e.printStackTrace(); - propsList = new ArrayList<String>(); + } } - - return propsList.toArray( new String[propsList.size()] ); + } + } catch (Exception e) { + e.printStackTrace(); + propsList = new ArrayList<String>(); } - /** - * @param ftName - * @return the WFSFeatureType object - */ - public synchronized WFSFeatureType getFeatureTypeByName( String ftName ) { - if ( ftNameToWfsFT == null ) { - getFeatureTypes(); // side effects in functions that return lists are wonderful - } - return ftNameToWfsFT.get( ftName ); + return propsList.toArray(new String[propsList.size()]); + } + + /** + * @param ftName + * @return the WFSFeatureType object + */ + public synchronized WFSFeatureType getFeatureTypeByName(String ftName) { + if (ftNameToWfsFT == null) { + getFeatureTypes(); // side effects in functions that return lists are + // wonderful } + return ftNameToWfsFT.get(ftName); + } - private static QualifiedName[] guessGeomProperty( GMLSchema schema, String featureTypeName ) { - QualifiedName[] geoPropNames = null; - List<QualifiedName> tmpList = new ArrayList<QualifiedName>( 20 ); + private static QualifiedName[] guessGeomProperty(GMLSchema schema, + String featureTypeName) { + QualifiedName[] geoPropNames = null; + List<QualifiedName> tmpList = new ArrayList<QualifiedName>(20); - FeatureType[] fts = schema.getFeatureTypes(); - for ( int i = 0; i < fts.length; i++ ) { - if ( !fts[i].getName().getLocalName().equals( featureTypeName ) ) { - continue; - } + FeatureType[] fts = schema.getFeatureTypes(); + for (int i = 0; i < fts.length; i++) { + if (!fts[i].getName().getLocalName().equals(featureTypeName)) { + continue; + } - PropertyType[] props = fts[i].getProperties(); - for ( int j = 0; j < props.length; j++ ) { - if ( props[j].getType() == Types.GEOMETRY || props[j].getType() == 10014 ) { - tmpList.add( props[j].getName() ); + PropertyType[] props = fts[i].getProperties(); + for (int j = 0; j < props.length; j++) { + if (props[j].getType() == Types.GEOMETRY || props[j].getType() == 10014) { + tmpList.add(props[j].getName()); - } - } } + } + } - geoPropNames = tmpList.toArray( new QualifiedName[tmpList.size()] ); + geoPropNames = tmpList.toArray(new QualifiedName[tmpList.size()]); - return geoPropNames; - } + return geoPropNames; + } - /** - * @param featureType - * @return a list of geometry property names - */ - public QualifiedName[] getGeometryProperties( String featureType ) { - return this.geoPropsNameToQNames.get( featureType ); - } + /** + * @param featureType + * @return a list of geometry property names + */ + public QualifiedName[] getGeometryProperties(String featureType) { + return this.geoPropsNameToQNames.get(featureType); + } } Modified: core/trunk/src/de/latlon/deejump/wfs/client/WFSClientHelper.java =================================================================== --- core/trunk/src/de/latlon/deejump/wfs/client/WFSClientHelper.java 2016-01-02 15:46:40 UTC (rev 4736) +++ core/trunk/src/de/latlon/deejump/wfs/client/WFSClientHelper.java 2016-01-02 16:19:28 UTC (rev 4737) @@ -42,18 +42,23 @@ package de.latlon.deejump.wfs.client; -import java.io.*; -import java.net.*; +import java.io.IOException; +import java.io.InputStream; +import java.io.PushbackInputStream; import java.util.LinkedList; +import java.util.zip.GZIPInputStream; -import org.apache.commons.httpclient.*; -import org.apache.commons.httpclient.methods.*; +import org.apache.commons.httpclient.Header; +import org.apache.commons.httpclient.HttpClient; +import org.apache.commons.httpclient.HttpMethod; +import org.apache.commons.httpclient.methods.PostMethod; +import org.apache.commons.httpclient.methods.StringRequestEntity; import org.apache.commons.io.IOUtils; -import org.deegree.enterprise.*; -import org.deegree.framework.log.*; +import org.deegree.framework.log.ILogger; +import org.deegree.framework.log.LoggerFactory; import org.deegree.framework.util.CharsetUtils; -import de.latlon.deejump.wfs.*; +import de.latlon.deejump.wfs.DeeJUMPException; /** * Does the posting and getting of requests/reponses for the WFSPanel. @@ -61,106 +66,133 @@ * @author <a href="mailto:tad...@lat-lon.de">Ugo Taddei</a> * @author last edited by: $Author: stranger $ * - * @version $Revision: 1438 $, $Date: 2008-05-29 15:00:02 +0200 (Do, 29 Mai 2008) $ + * @version $Revision: 1438 $, $Date: 2008-05-29 15:00:02 +0200 (Do, 29 Mai + * 2008) $ */ public class WFSClientHelper { - private static ILogger LOG = LoggerFactory.getLogger( WFSClientHelper.class ); + private static ILogger LOG = LoggerFactory.getLogger(WFSClientHelper.class); - /** - * @param serverUrl - * @param request - * @return the response as String - * @throws DeeJUMPException - */ - public static String createResponseStringfromWFS(String serverUrl, - String request) throws DeeJUMPException { - - PushbackInputStream pbis = new PushbackInputStream( - createResponseStreamfromWFS(serverUrl, request), 1024); + /** + * convenience method, big datasets tend to flood your memory + * + * @param serverUrl + * @param request + * @return the response as String + * @throws DeeJUMPException + */ + public static String createResponseStringfromWFS(String serverUrl, + String request) throws DeeJUMPException { - try { - String encoding = readEncoding(pbis); - return IOUtils.toString(pbis, encoding); - } catch (IOException e) { - String mesg = "Error reading " + serverUrl; - LOG.logError(mesg, e); - throw new DeeJUMPException(mesg, e); - } + PushbackInputStream pbis = new PushbackInputStream( + createResponseStreamfromWFS(serverUrl, request), 1024); + + try { + String encoding = readEncoding(pbis); + return IOUtils.toString(pbis, encoding); + } catch (IOException e) { + String mesg = "Error reading " + serverUrl; + LOG.logError(mesg, e); + throw new DeeJUMPException(mesg, e); } - - public static InputStream createResponseStreamfromWFS( String serverUrl, String request ) - throws DeeJUMPException { - LOG.logDebug( "WFS GetFeature: " + serverUrl + " -> " + request ); + } - HttpClient httpclient = new WFSHttpClient(); + /** + * creates and InputStream for memory efficient handling + * + * if postData == null, a get request is executed + * this method supports gzip content encoding + * + * @param serverUrl + * @param postData + * @return + * @throws DeeJUMPException + */ + public static InputStream createResponseStreamfromWFS(String serverUrl, + String postData) throws DeeJUMPException { + LOG.logDebug("WFS GetFeature: " + serverUrl + " -> " + postData); + //System.out.println(serverUrl); - PostMethod httppost = new PostMethod( serverUrl ); - try { - httppost.setRequestEntity( new StringRequestEntity( request, "text/xml", "UTF-8" ) ); - } catch (UnsupportedEncodingException e1) { - throw new DeeJUMPException(e1); - } + HttpClient httpclient = new WFSHttpClient(); + try { + HttpMethod method; + if (postData != null) { + PostMethod pm = new WFSPostMethod(serverUrl); + pm.setRequestEntity(new StringRequestEntity(postData, "text/xml", + "UTF-8")); + method = pm; + } else { + method = new WFSGetMethod(serverUrl); + } - try { - WebUtils.enableProxyUsage( httpclient, new URL(serverUrl) ); - httpclient.executeMethod( httppost ); + method.addRequestHeader("Accept-Encoding", "gzip"); - return httppost.getResponseBodyAsStream(); - } catch ( Exception e ) { - String mesg = "Error opening connection with " + serverUrl; - LOG.logError( mesg, e ); - throw new DeeJUMPException( mesg, e ); - } + // WebUtils.enableProxyUsage(httpclient, new URL(serverUrl)); + httpclient.executeMethod(method); + Header encHeader = method.getResponseHeader("Content-Encoding"); + InputStream is = method.getResponseBodyAsStream(); + if (encHeader != null) { + String encValue = encHeader.getValue(); + if (encValue != null && encValue.toLowerCase().equals("gzip")) { + is = new GZIPInputStream(is); + } + } + + return is; + } catch (Exception e) { + String mesg = "Error opening connection with " + serverUrl; + LOG.logError(mesg, e); + throw new DeeJUMPException(mesg, e); } - - /** - * reads the encoding of a XML document from its header. If no header available - * <code>CharsetUtils.getSystemCharset()</code> will be returned - * - * @param pbis - * @return encoding of a XML document - * @throws IOException - */ - private static String readEncoding( PushbackInputStream pbis ) - throws IOException { - byte[] b = new byte[80]; - String s = ""; - int rd = 0; + } - LinkedList<byte[]> bs = new LinkedList<byte[]>(); - LinkedList<Integer> rds = new LinkedList<Integer>(); - while ( rd < 80 ) { - rds.addFirst( pbis.read( b ) ); - if ( rds.peek() == -1 ) { - rds.poll(); - break; - } - rd += rds.peek(); - s += new String( b, 0, rds.peek() ).toLowerCase(); - bs.addFirst( b ); - b = new byte[80]; - } + /** + * reads the encoding of a XML document from its header. If no header + * available <code>CharsetUtils.getSystemCharset()</code> will be returned + * + * @param pbis + * @return encoding of a XML document + * @throws IOException + */ + public static String readEncoding(PushbackInputStream pbis) + throws IOException { + byte[] b = new byte[80]; + String s = ""; + int rd = 0; - String encoding = CharsetUtils.getSystemCharset(); - if ( s.indexOf( "?>" ) > -1 ) { - int p = s.indexOf( "encoding=" ); - if ( p > -1 ) { - StringBuffer sb = new StringBuffer(); - int k = p + 1 + "encoding=".length(); - while ( s.charAt( k ) != '"' && s.charAt( k ) != '\'' ) { - sb.append( s.charAt( k++ ) ); - } - encoding = sb.toString(); - } + LinkedList<byte[]> bs = new LinkedList<byte[]>(); + LinkedList<Integer> rds = new LinkedList<Integer>(); + while (rd < 80) { + rds.addFirst(pbis.read(b)); + if (rds.peek() == -1) { + rds.poll(); + break; + } + rd += rds.peek(); + s += new String(b, 0, rds.peek()).toLowerCase(); + bs.addFirst(b); + b = new byte[80]; + } + + String encoding = CharsetUtils.getSystemCharset(); + if (s.indexOf("?>") > -1) { + int p = s.indexOf("encoding="); + if (p > -1) { + StringBuffer sb = new StringBuffer(); + int k = p + 1 + "encoding=".length(); + while (s.charAt(k) != '"' && s.charAt(k) != '\'') { + sb.append(s.charAt(k++)); } - while ( !bs.isEmpty() ) { - pbis.unread( bs.poll(), 0, rds.poll() ); - } + encoding = sb.toString(); + } + } + while (!bs.isEmpty()) { + pbis.unread(bs.poll(), 0, rds.poll()); + } - return encoding; - } - + return encoding; + } + } Added: core/trunk/src/de/latlon/deejump/wfs/client/WFSGetMethod.java =================================================================== --- core/trunk/src/de/latlon/deejump/wfs/client/WFSGetMethod.java (rev 0) +++ core/trunk/src/de/latlon/deejump/wfs/client/WFSGetMethod.java 2016-01-02 16:19:28 UTC (rev 4737) @@ -0,0 +1,21 @@ +package de.latlon.deejump.wfs.client; + +import org.apache.commons.httpclient.methods.GetMethod; + +public class WFSGetMethod extends GetMethod implements WFSMethod{ + + private String uri; + + public WFSGetMethod(String uri) { + super(uri); + this.uri = uri; + } + + @Override + public String getUri() { + return this.uri; + } + + + +} Property changes on: core/trunk/src/de/latlon/deejump/wfs/client/WFSGetMethod.java ___________________________________________________________________ Added: svn:mime-type ## -0,0 +1 ## +text/plain \ No newline at end of property Modified: core/trunk/src/de/latlon/deejump/wfs/client/WFSHttpClient.java =================================================================== --- core/trunk/src/de/latlon/deejump/wfs/client/WFSHttpClient.java 2016-01-02 15:46:40 UTC (rev 4736) +++ core/trunk/src/de/latlon/deejump/wfs/client/WFSHttpClient.java 2016-01-02 16:19:28 UTC (rev 4737) @@ -1,29 +1,52 @@ package de.latlon.deejump.wfs.client; -import org.apache.commons.httpclient.*; -import org.apache.commons.httpclient.params.*; +import java.io.IOException; +import java.net.URL; +import org.apache.commons.httpclient.Credentials; +import org.apache.commons.httpclient.HostConfiguration; +import org.apache.commons.httpclient.HttpClient; +import org.apache.commons.httpclient.HttpConnectionManager; +import org.apache.commons.httpclient.HttpException; +import org.apache.commons.httpclient.HttpMethod; +import org.apache.commons.httpclient.HttpState; +import org.apache.commons.httpclient.UsernamePasswordCredentials; +import org.apache.commons.httpclient.auth.AuthScope; +import org.apache.commons.httpclient.params.HttpClientParams; +import org.deegree.enterprise.WebUtils; +import org.openjump.util.UriUtil; + +import com.vividsolutions.wms.WMService; + +/** + * a client specifically tailored to do our WFS requests + */ public class WFSHttpClient extends HttpClient { - HttpClient nonProxyClient; - String nonProxyHosts = ""; - + AbstractWFSWrapper wfsService; + + public WFSHttpClient(AbstractWFSWrapper wfsService) { + super(); + this.wfsService = wfsService; + this._init(); + } + public WFSHttpClient() { super(); this._init(); } - public WFSHttpClient(HttpClientParams params, + private WFSHttpClient(HttpClientParams params, HttpConnectionManager httpConnectionManager) { super(params, httpConnectionManager); this._init(); } - public WFSHttpClient(HttpClientParams arg0) { + private WFSHttpClient(HttpClientParams arg0) { super(arg0); this._init(); } - public WFSHttpClient(HttpConnectionManager httpConnectionManager) { + private WFSHttpClient(HttpConnectionManager httpConnectionManager) { super(httpConnectionManager); this._init(); } @@ -31,67 +54,44 @@ private void _init() { HttpClientParams clientPars = new HttpClientParams(); // set timeout to 5s - clientPars.setConnectionManagerTimeout( 5000 ); - this.setParams( clientPars ); - -// nonProxyClient = new HttpClient(); - -// // Recover the proxy settings from the System, if they exist -// Properties systemSettings = System.getProperties(); -// if (systemSettings != null) { -// -// String proxySet = systemSettings.getProperty("http.proxySet", "false"); -// if (StringUtils.isNotEmpty(proxySet) && proxySet.equals("true")) { -// String proxyHost = systemSettings.getProperty("http.proxyHost"); -// String proxyPort = systemSettings.getProperty("http.proxyPort"); -// -// this.getHostConfiguration().setProxy(proxyHost, -// Integer.valueOf(proxyPort)); -// -// String proxyUser = systemSettings.getProperty("http.proxyUser"); -// String proxyPass = systemSettings.getProperty("http.proxyPass"); -// -// if (StringUtils.isNotEmpty(proxyUser)) { -// Credentials credentials = new UsernamePasswordCredentials( -// proxyUser, proxyPass); -// AuthScope scope = new AuthScope(AuthScope.ANY_HOST, -// AuthScope.ANY_PORT); -// this.getState().setProxyCredentials(scope, credentials); -// } -// -// nonProxyHosts = systemSettings.getProperty("http.nonProxyHosts"); -// } -// } + clientPars.setConnectionManagerTimeout(WMService.TIMEOUT_OPEN); + clientPars.setSoTimeout(WMService.TIMEOUT_READ); + clientPars.setContentCharset("UTF-8"); + this.setParams(clientPars); + + // always add auth, we use this client for this specific wfs server only + // anyway + if (wfsService != null) { + Credentials creds = new UsernamePasswordCredentials(wfsService + .getLogins().getUsername(), wfsService.getLogins().getPassword()); + getState().setCredentials(AuthScope.ANY, creds); + } } -// @Override -// public int executeMethod(HostConfiguration hostconfig, HttpMethod method, -// HttpState state) throws IOException, HttpException { -// System.out.println("e1"+(hostconfig!=null?hostconfig.getHost():getHost())); -// System.out.println(method.getHostConfiguration().getHost()); -// try { -// String host = method.getHostConfiguration().getHost(); -// System.out.println(nonProxyHosts.split("\\|")); -// } catch (NullPointerException e) { -// } -// // TODO Auto-generated method stub -// return super.executeMethod(hostconfig, method, state); -// } + /** + * fiddle in some default processing before firing the request eg. setting + * proxies, auth + */ + @Override + public int executeMethod(HostConfiguration hostconfig, HttpMethod method, + HttpState state) throws IOException, HttpException { -// @Override -// public int executeMethod(HostConfiguration hostConfiguration, -// HttpMethod method) throws IOException, HttpException { -// System.out.println("e2"+(hostConfiguration!=null?hostConfiguration.getHost():getHost())); -// // TODO Auto-generated method stub -// return super.executeMethod(hostConfiguration, method); -// } -// -// @Override -// public int executeMethod(HttpMethod method) throws IOException, HttpException { -// System.out.println("e3"+method.getHostConfiguration().getHost()); -// // TODO Auto-generated method stub -// return super.executeMethod(method); -// } + if (!(method instanceof WFSMethod)) + throw new IllegalArgumentException( + "WFSHttpClient only executes WFSMethod's"); - + String url = ((WFSMethod) method).getUri(); + // enable proxy usage + WebUtils.enableProxyUsage(this, new URL(url)); + + // set auth from url + if (!UriUtil.urlGetUser(url).isEmpty()) { + Credentials creds = new UsernamePasswordCredentials( + UriUtil.urlGetUser(url), UriUtil.urlGetPassword(url)); + getState().setCredentials(AuthScope.ANY, creds); + } + + return super.executeMethod(hostconfig, method, state); + } + } Added: core/trunk/src/de/latlon/deejump/wfs/client/WFSMethod.java =================================================================== --- core/trunk/src/de/latlon/deejump/wfs/client/WFSMethod.java (rev 0) +++ core/trunk/src/de/latlon/deejump/wfs/client/WFSMethod.java 2016-01-02 16:19:28 UTC (rev 4737) @@ -0,0 +1,7 @@ +package de.latlon.deejump.wfs.client; + +public interface WFSMethod { + + public String getUri(); + +} Property changes on: core/trunk/src/de/latlon/deejump/wfs/client/WFSMethod.java ___________________________________________________________________ Added: svn:mime-type ## -0,0 +1 ## +text/plain \ No newline at end of property Added: core/trunk/src/de/latlon/deejump/wfs/client/WFSPostMethod.java =================================================================== --- core/trunk/src/de/latlon/deejump/wfs/client/WFSPostMethod.java (rev 0) +++ core/trunk/src/de/latlon/deejump/wfs/client/WFSPostMethod.java 2016-01-02 16:19:28 UTC (rev 4737) @@ -0,0 +1,21 @@ +package de.latlon.deejump.wfs.client; + +import org.apache.commons.httpclient.methods.PostMethod; + +public class WFSPostMethod extends PostMethod implements WFSMethod{ + + private String uri; + + public WFSPostMethod(String uri) { + super(uri); + this.uri = uri; + } + + @Override + public String getUri() { + return this.uri; + } + + + +} Property changes on: core/trunk/src/de/latlon/deejump/wfs/client/WFSPostMethod.java ___________________________________________________________________ Added: svn:mime-type ## -0,0 +1 ## +text/plain \ No newline at end of property Modified: core/trunk/src/de/latlon/deejump/wfs/client/WFServiceWrapper_1_0_0.java =================================================================== --- core/trunk/src/de/latlon/deejump/wfs/client/WFServiceWrapper_1_0_0.java 2016-01-02 15:46:40 UTC (rev 4736) +++ core/trunk/src/de/latlon/deejump/wfs/client/WFServiceWrapper_1_0_0.java 2016-01-02 16:19:28 UTC (rev 4737) @@ -17,15 +17,16 @@ import org.deegree.datatypes.QualifiedName; import org.deegree.framework.xml.DOMPrinter; -import org.deegree.framework.xml.XMLFragment; import org.deegree.framework.xml.XMLParsingException; import org.deegree.framework.xml.XMLTools; import org.deegree.ogcbase.CommonNamespaces; import org.deegree.ogcwebservices.wfs.capabilities.WFSFeatureType; +import org.openjump.util.UriUtil; import org.w3c.dom.Element; import org.w3c.dom.Node; import de.latlon.deejump.wfs.auth.UserData; +import de.latlon.deejump.wfs.deegree2mods.XMLFragment; /** * The WFS 1.0.0 client. @@ -33,113 +34,122 @@ * @author <a href="mailto:tad...@lat-lon.de">Ugo Taddei</a> * @author last edited by: $Author: stranger $ * - * @version $Revision: 1438 $, $Date: 2008-05-29 15:00:02 +0200 (Do, 29 Mai 2008) $ + * @version $Revision: 1438 $, $Date: 2008-05-29 15:00:02 +0200 (Do, 29 Mai + * 2008) $ */ public class WFServiceWrapper_1_0_0 extends AbstractWFSWrapper { - private XMLFragment capsDoc; + private XMLFragment capsDoc; - private String[] featureTypes; + private String[] featureTypes; - /** - * @param logins - * @param baseUrl - */ - public WFServiceWrapper_1_0_0( UserData logins, String baseUrl ) { - super( logins, baseUrl ); - init(); - } + /** + * @param logins + * @param baseUrl + */ + public WFServiceWrapper_1_0_0(UserData logins, String baseUrl) { + super(logins, baseUrl); + init(); + } - private void init() { + private void init() { - capsDoc = new de.latlon.deejump.wfs.deegree2mods.XMLFragment(); + capsDoc = new de.latlon.deejump.wfs.deegree2mods.XMLFragment(); - try { - capsDoc.load( new URL( getCapabilitiesURL() ) ); - } catch ( Exception e ) { - e.printStackTrace(); - } - + try { + capsDoc.load(new URL(getCapabilitiesURL())); + } catch (Exception e) { + e.printStackTrace(); } - @Override - public String getCapabilitesAsString() { - return DOMPrinter.nodeToString( capsDoc.getRootElement(), " " ); - } + } - @Override - public synchronized String[] getFeatureTypes() { - if ( featureTypes == null ) { - featureTypes = extractFeatureTypes(); - } - return featureTypes; + @Override + public String getCapabilitesAsString() { + return DOMPrinter.nodeToString(capsDoc.getRootElement(), " "); + } + + @Override + public synchronized String[] getFeatureTypes() { + if (featureTypes == null) { + featureTypes = extractFeatureTypes(); } + return featureTypes; + } - private synchronized String[] extractFeatureTypes() { + private synchronized String[] extractFeatureTypes() { - String[] fts = null; + String[] fts = null; - ftNameToWfsFT = new HashMap<String, WFSFeatureType>(); + ftNameToWfsFT = new HashMap<String, WFSFeatureType>(); - Element root = this.capsDoc.getRootElement(); + Element root = this.capsDoc.getRootElement(); - try { - List<Element> nodes = XMLTools.getElements( root, "wfs:FeatureTypeList/wfs:FeatureType", - CommonNamespaces.getNamespaceContext() ); + try { + List<Element> nodes = XMLTools.getElements(root, + "wfs:FeatureTypeList/wfs:FeatureType", + CommonNamespaces.getNamespaceContext()); - List<String> ftList = new ArrayList<String>( nodes.size() ); - for ( Element n : nodes ) { - Node node = XMLTools.getNode( n, "wfs:Name/text()", CommonNamespaces.getNamespaceContext() ); + List<String> ftList = new ArrayList<String>(nodes.size()); + for (Element n : nodes) { + Node node = XMLTools.getNode(n, "wfs:Name/text()", + CommonNamespaces.getNamespaceContext()); - QualifiedName qualiName = XMLFragment.parseQualifiedName( node ); + QualifiedName qualiName = XMLFragment.parseQualifiedName(node); - ftList.add( qualiName.getLocalName() ); + ftList.add(qualiName.getLocalName()); - URI uri = XMLTools.getNodeAsURI( n, "wfs:SRS/text()", CommonNamespaces.getNamespaceContext(), null ); - WFSFeatureType wfsFt = new WFSFeatureType( qualiName, null, null, null, uri, null, null, null, null, - null ); + URI uri = XMLTools.getNodeAsURI(n, "wfs:SRS/text()", + CommonNamespaces.getNamespaceContext(), null); + WFSFeatureType wfsFt = new WFSFeatureType(qualiName, null, null, null, + uri, null, null, null, null, null); - ftNameToWfsFT.put( qualiName.getLocalName(), wfsFt ); + ftNameToWfsFT.put(qualiName.getLocalName(), wfsFt); - } - fts = ftList.toArray( new String[ftList.size()] ); - } catch ( XMLParsingException e ) { - e.printStackTrace(); - } - return fts; - + } + fts = ftList.toArray(new String[ftList.size()]); + } catch (XMLParsingException e) { + e.printStackTrace(); } + return fts; - private String createOnlineResourceForOperation( String operationName, String httpMeth ) { - String value = null; - Element root = this.capsDoc.getRootElement(); + } - try { - value = XMLTools.getRequiredNodeAsString( root, "wfs:Capability/wfs:Request/wfs:" + operationName - + "/wfs:DCPType/wfs:HTTP/wfs:" + httpMeth - + "/@onlineResource", - CommonNamespaces.getNamespaceContext() ); + private String createOnlineResourceForOperation(String operationName, + String httpMeth) { + String value = null; + Element root = this.capsDoc.getRootElement(); - } catch ( XMLParsingException e ) { - e.printStackTrace(); - } - return value; + try { + value = XMLTools.getRequiredNodeAsString(root, + "wfs:Capability/wfs:Request/wfs:" + operationName + + "/wfs:DCPType/wfs:HTTP/wfs:" + httpMeth + "/@onlineResource", + CommonNamespaces.getNamespaceContext()); + } catch (XMLParsingException e) { + e.printStackTrace(); } + return value; - @Override - public String createDescribeFTOnlineResource() { - return createOnlineResourceForOperation( "DescribeFeatureType", "Get" ); - } + } - @Override - public String getServiceVersion() { - return "1.0.0"; - } + @Override + public String createDescribeFTOnlineResource() { + return createOnlineResourceForOperation("DescribeFeatureType", "Get"); + } - @Override - public String getGetFeatureURL() { - return createOnlineResourceForOperation( "GetFeature", "Post" ); - } + @Override + public String getServiceVersion() { + return "1.0.0"; + } + @Override + public String getGetFeatureURL() { + String url = createOnlineResourceForOperation("GetFeature", "Post"); + if (logins != null && !logins.isEmpty()) + url = UriUtil.urlAddCredentials(url, logins.getUsername(), + logins.getPassword()); + return url; + } + } Modified: core/trunk/src/de/latlon/deejump/wfs/client/WFServiceWrapper_1_1_0.java =================================================================== --- core/trunk/src/de/latlon/deejump/wfs/client/WFServiceWrapper_1_1_0.java 2016-01-02 15:46:40 UTC (rev 4736) +++ core/trunk/src/de/latlon/deejump/wfs/client/WFServiceWrapper_1_1_0.java 2016-01-02 16:19:28 UTC (rev 4737) @@ -26,12 +26,13 @@ import org.deegree.ogcwebservices.getcapabilities.HTTP; import org.deegree.ogcwebservices.getcapabilities.InvalidCapabilitiesException; import org.deegree.ogcwebservices.wfs.capabilities.WFSCapabilities; -import org.deegree.ogcwebservices.wfs.capabilities.WFSCapabilitiesDocument; import org.deegree.ogcwebservices.wfs.capabilities.WFSFeatureType; +import org.openjump.util.UriUtil; import org.xml.sax.SAXException; import de.latlon.deejump.wfs.DeeJUMPException; import de.latlon.deejump.wfs.auth.UserData; +import de.latlon.deejump.wfs.deegree2mods.WFSCapabilitiesDocument; /** * This class represents a WFService. It handles connection with the server behind the given URL. It also caches Feature @@ -157,6 +158,10 @@ throw new RuntimeException( "Service does not have a GetFeature operation accessible by HTTP POST." ); } + if (logins != null && !logins.isEmpty()) + getFeatureUrl = UriUtil.urlAddCredentials(getFeatureUrl, logins.getUsername(), + logins.getPassword()); + return getFeatureUrl; } Modified: core/trunk/src/de/latlon/deejump/wfs/data/JUMPFeatureFactory.java =================================================================== --- core/trunk/src/de/latlon/deejump/wfs/data/JUMPFeatureFactory.java 2016-01-02 15:46:40 UTC (rev 4736) +++ core/trunk/src/de/latlon/deejump/wfs/data/JUMPFeatureFactory.java 2016-01-02 16:19:28 UTC (rev 4737) @@ -11,8 +11,16 @@ import static com.vividsolutions.jump.feature.AttributeType.GEOMETRY; import static org.deegree.model.spatialschema.JTSAdapter.export; +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; import java.io.IOException; -import java.io.StringReader; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.PushbackInputStream; +import java.io.PushbackReader; +import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; import java.net.URI; import java.net.URL; @@ -20,6 +28,7 @@ import java.util.Iterator; import java.util.Map; +import org.apache.commons.io.IOUtils; import org.apache.log4j.Logger; import org.deegree.datatypes.QualifiedName; import org.deegree.datatypes.Types; @@ -31,11 +40,9 @@ import org.deegree.model.crs.UnknownCRSException; import org.deegree.model.feature.FeatureFactory; import org.deegree.model.feature.FeatureProperty; -import org.deegree.model.feature.GMLFeatureCollectionDocument; import org.deegree.model.feature.schema.AbstractPropertyType; import org.deegree.model.feature.schema.FeatureType; import org.deegree.model.feature.schema.GMLSchema; -import org.deegree.model.feature.schema.GMLSchemaDocument; import org.deegree.model.feature.schema.PropertyType; import org.deegree.model.filterencoding.ComplexFilter; import org.deegree.model.filterencoding.Filter; @@ -58,11 +65,16 @@ import com.vividsolutions.jump.feature.FeatureCollection; import com.vividsolutions.jump.feature.FeatureDataset; import com.vividsolutions.jump.feature.FeatureSchema; +import com.vividsolutions.jump.task.TaskMonitor; +import com.vividsolutions.jump.util.FileUtil; import de.latlon.deejump.wfs.DeeJUMPException; import de.latlon.deejump.wfs.client.AbstractWFSWrapper; import de.latlon.deejump.wfs.client.WFSClientHelper; +import de.latlon.deejump.wfs.deegree2mods.GMLFeatureCollectionDocument; +import de.latlon.deejump.wfs.deegree2mods.GMLSchemaDocument; import de.latlon.deejump.wfs.jump.WFSFeature; +import de.latlon.deejump.wfs.plugin.WFSPlugIn; /** * Utility functions to create different kinds of FeatureDatasets. <br/> @@ -72,537 +84,608 @@ */ public class JUMPFeatureFactory { - private static Logger LOG = Logger.getLogger( JUMPFeatureFactory.class ); + private static Logger LOG = Logger.getLogger(JUMPFeatureFactory.class); - private static int maxFeatures = 1000; + private static int maxFeatures = 1000; + private TaskMonitor monitor; - /** - * Creates a JUMP FeatureCollection from a deegree FeatureCollection [UT] - * - * @author <a href="mailto:tad...@lat-lon.de">Ugo Taddei </a> - * @param deegreeFeatCollec - * the deegree FeatureCollection - * @return the new JUMP FeatureCollection - * @throws DeeJUMPException - */ - public static FeatureCollection createFromDeegreeFC( org.deegree.model.feature.FeatureCollection deegreeFeatCollec ) - throws DeeJUMPException { + public JUMPFeatureFactory(TaskMonitor monitor) { + this.monitor = monitor; + } - return createFromDeegreeFC( deegreeFeatCollec, null ); - } + /** + * Creates a JUMP FeatureCollection from a deegree FeatureCollection [UT] + * + * @author <a href="mailto:tad...@lat-lon.de">Ugo Taddei </a> + * @param deegreeFeatCollec + * the deegree FeatureCollection + * @return the new JUMP FeatureCollection + * @throws DeeJUMPException + */ + public FeatureCollection createFromDeegreeFC( + org.deegree.model.feature.FeatureCollection deegreeFeatCollec) + throws DeeJUMPException { - /** - * Creates a deegree <code>WFSGetFeatureRequest</code> based on the WFS version, the feature name ( - * <code>typeName</code>) and the <code>envelope</code>. <br/> - * This method was adapted from <code>DownloadListener</code>. - * - * @author <a href="mailto:tad...@lat-lon.de">Ugo Taddei </a> - * @param version - * the WFS version - * @param qualName - * the feature (type) name - * @param envelope - * the box inside of which data has been requested - * @return a wfs GetFeature request - */ - public static GetFeature createFeatureRequest( String version, QualifiedName qualName, - org.deegree.model.spatialschema.Envelope envelope ) { + return createFromDeegreeFC(deegreeFeatCollec, null); + } - CoordinateSystem cs = null; + /** + * Creates a deegree <code>WFSGetFeatureRequest</code> based on the WFS + * version, the feature name ( <code>typeName</code>) and the + * <code>envelope</code>. <br/> + * This method was adapted from <code>DownloadListener</code>. + * + * @author <a href="mailto:tad...@lat-lon.de">Ugo Taddei </a> + * @param version + * the WFS version + * @param qualName + * the feature (type) name + * @param envelope + * the box inside of which data has been requested + * @return a wfs GetFeature request + */ + public GetFeature createFeatureRequest(String version, + QualifiedName qualName, org.deegree.model.spatialschema.Envelope envelope) { - Filter filter = null; - if ( envelope != null ) { - org.deegree.model.spatialschema.Geometry boxGeom = null; - try { - boxGeom = GeometryFactory.createSurface( envelope, cs ); - } catch ( GeometryException e ) { - e.printStackTrace(); - throw new RuntimeException( "Cannot create surface from bbox." + e.getMessage() ); - } + CoordinateSystem cs = null; - Operation op = new SpatialOperation( OperationDefines.BBOX, - new PropertyName( new QualifiedName( "GEOM" ) ), boxGeom ); //$NON-NLS-1$ + Filter filter = null; + if (envelope != null) { + org.deegree.model.spatialschema.Geometry boxGeom = null; + try { + boxGeom = GeometryFactory.createSurface(envelope, cs); + } catch (GeometryException e) { + e.printStackTrace(); + throw new RuntimeException("Cannot create surface from bbox." + + e.getMessage()); + } - filter = new ComplexFilter( op ); - } + Operation op = new SpatialOperation(OperationDefines.BBOX, + new PropertyName(new QualifiedName("GEOM")), boxGeom); //$NON-NLS-1$ - Query query = Query.create( null, null, null, null, version, new QualifiedName[] { qualName }, null, null, - filter, maxFeatures, 0, RESULT_TYPE.RESULTS ); - IDGenerator idg = IDGenerator.getInstance(); + filter = new ComplexFilter(op); + } - int maxDepth = 100; - int traverseExpiry = -999; - GetFeature gfr = GetFeature.create( version, "" + idg.generateUniqueID(), RESULT_TYPE.RESULTS, - "text/xml; subtype=gml/3.1.1", null, maxFeatures, 0, maxDepth, - traverseExpiry, new Query[] { query } ); + Query query = Query.create(null, null, null, null, version, + new QualifiedName[] { qualName }, null, null, filter, maxFeatures, 0, + RESULT_TYPE.RESULTS); + IDGenerator idg = IDGenerator.getInstance(); - return gfr; - } + int maxDepth = 100; + int traverseExpiry = -999; + GetFeature gfr = GetFeature.create(version, "" + idg.generateUniqueID(), + RESULT_TYPE.RESULTS, "text/xml; subtype=gml/3.1.1", null, maxFeatures, + 0, maxDepth, traverseExpiry, new Query[] { query }); - /** - * Creates a deegree <code>FeatureCollection</code> from a given GetFeature request to a server. - * - * @author <a href="mailto:tad...@lat-lon.de">Ugo Taddei </a> - * @param serverUrl - * the URL of the WFS server - * @param request - * the GetFeature request - * @return a deegree FeatureCollection - * @throws XMLParsingException - * @throws IOException - * @throws DeeJUMPException - */ - public static org.deegree.model.feature.FeatureCollection createDeegreeFCfromWFS( AbstractWFSWrapper serverUrl, - GetFeature request ) - throws DeeJUMPException, IOException, XMLParsingException { + return gfr; + } - return createDeegreeFCfromWFS( serverUrl, XMLFactory.export( request ).getAsString(), null ); - } + /** + * Creates a deegree <code>FeatureCollection</code> from a given GetFeature + * request to a server. + * + * @author <a href="mailto:tad...@lat-lon.de">Ugo Taddei </a> + * @param serverUrl + * the URL of the WFS server + * @param request + * the GetFeature request + * @return a deegree FeatureCollection + * @throws XMLParsingException + * @throws IOException + * @throws DeeJUMPException + */ + public org.deegree.model.feature.FeatureCollection createDeegreeFCfromWFS( + AbstractWFSWrapper serverUrl, GetFeature request) + throws DeeJUMPException, IOException, XMLParsingException { - /** - * Creates a deegree <code>FeatureCollection</code> from a given GetFeature request to a server. - * - * @author <a href="mailto:tad...@lat-lon.de">Ugo Taddei </a> - * @param server - * the URL of the WFS server - * @param request - * the GetFeature request - * @param featureType - * if non null, a DescribeFeatureType will be performed to get the correct schema - * @return a deegree FeatureCollection - * @throws DeeJUMPException - */ - public static org.deegree.model.feature.FeatureCollection createDeegreeFCfromWFS( AbstractWFSWrapper server, - String request, - QualifiedName featureType ) - throws DeeJUMPException { + return createDeegreeFCfromWFS(serverUrl, XMLFactory.export(request) + .getAsString(), null); + } - String s = WFSClientHelper.createResponseStringfromWFS( server.getGetFeatureURL(), request ); - - if ( s.indexOf( "<Exception>" ) >= 0 || s.indexOf( "<ServiceExceptionReport" ) >= 0 ) { //$NON-NLS-1$ //$NON-NLS-2$ - RuntimeException re = new RuntimeException( "Couldn't get data from WFS:\n" //$NON-NLS-1$ - + s ); - LOG.debug( "Couldn't get data from WFS.", re ); - throw re; - } + public org.deegree.model.feature.FeatureCollection createDeegreeFCfromFile( + AbstractWFSWrapper server, File file, QualifiedName featureType) + throws DeeJUMPException, FileNotFoundException { - LOG.debug( "WFS FC: " + s ); + InputStream is = new FileInputStream(file); + org.deegree.model.feature.FeatureCollection fc = createDeegreeFCfromInputStream( + server, is, featureType); + FileUtil.close(is); + return fc; + } - StringReader sr = new StringReader( s ); - GMLFeatureCollectionDocument gfDoc = new GMLFeatureCollectionDocument(); + public org.deegree.model.feature.FeatureCollection createDeegreeFCfromWFS( + AbstractWFSWrapper server, String request, QualifiedName featureType) + throws DeeJUMPException { + InputStream is = WFSClientHelper.createResponseStreamfromWFS( + server.getGetFeatureURL(), request); + org.deegree.model.feature.FeatureCollection fc = createDeegreeFCfromInputStream( + server, is, featureType); + FileUtil.close(is); + return fc; + } - // get schema from server - if ( featureType != null ) { - Map<URI, GMLSchema> schemaMap = new HashMap<URI, GMLSchema>(); - String dft = server.getDescribeTypeURL( featureType ); - GMLSchemaDocument doc = new de.latlon.deejump.wfs.deegree2mods.GMLSchemaDocument(); - try { - doc.load( new URL( dft ) ); - LOG.debug( "Feature type schema:\n" + doc.getAsPrettyString() ); - GMLSchema schema = doc.parseGMLSchema(); - schemaMap.put( featureType.getNamespace(), schema ); - gfDoc.setSchemas( schemaMap ); - } catch ( XMLSchemaException e ) { - LOG.debug( "DescribeFeatureType did not work." ); - } catch ( UnknownCRSException e ) { - LOG.debug( "DescribeFeatureType did not work." ); - } catch ( XMLParsingException e ) { - LOG.debug( "DescribeFeatureType did not work." ); - } catch ( MalformedURLException e ) { - LOG.debug( "DescribeFeatureType did not work." ); - } catch ( IOException e ) { - LOG.debug( "DescribeFeatureType did not work." ); - } catch ( SAXException e ) { - LOG.debug( "DescribeFeatureType did not work." ); - } finally { - LOG.debug( "Trying to use base url of server for DescribeFeatureType..." ); - try { - String baseURL = server.getBaseWfsURL(); - baseURL += baseURL.endsWith( "?" ) ? "" : "?"; - doc.load( new URL( server.getDescribeTypeURL( baseURL, featureType ) ) ); + /** + * Creates a deegree <code>FeatureCollection</code> from a given GetFeature + * request to a server. + * + * @author <a href="mailto:tad...@lat-lon.de">Ugo Taddei </a> + * @param server + * the URL of the WFS server + * @param request + * the GetFeature request + * @param featureType + * if non null, a DescribeFeatureType will be performed to get the + * correct schema + * @return a deegree FeatureCollection + * @throws DeeJUMPException + */ + public org.deegree.model.feature.FeatureCollection createDeegreeFCfromInputStream( + AbstractWFSWrapper server, InputStream is, QualifiedName featureType) + throws DeeJUMPException { - LOG.debug( "Feature type schema:\n" + doc.getAsPrettyString() ); + monitor.report(WFSPlugIn.i18n("detect-encoding")); + // detect encoding or error + PushbackReader rd = null; + try { + PushbackInputStream pbis = new PushbackInputStream(is, 1024); - GMLSchema schema = doc.parseGMLSchema(); - schemaMap.put( featureType.getNamespace(), schema ); - gfDoc.setSchemas( schemaMap ); - } catch ( XMLSchemaException e ) { - LOG.debug( "DescribeFeatureType did not work." ); - } catch ( UnknownCRSException e ) { - LOG.debug( "DescribeFeatureType did not work." ); - } catch ( XMLParsingException e ) { - LOG.debug( "DescribeFeatureType did not work." ); - } catch ( MalformedURLException e ) { - LOG.debug( "DescribeFeatureType did not work." ); - } catch ( IOException e ) { - LOG.debug( "DescribeFeatureType did not work." ); - } catch ( SAXException e ) { - LOG.debug( "DescribeFeatureType did not work." ); - } - } - } + String encoding = WFSClientHelper.readEncoding(pbis); + InputStreamReader isrd = new InputStreamReader(pbis, encoding); + rd = new PushbackReader(isrd, 1024); - org.deegree.model.feature.FeatureCollection newFeatCollec = null; - try { - gfDoc.load( sr, "http://dummySysId" ); - newFeatCollec = gfDoc.parse(); - } catch ( SAXException e ) { - String mesg = "Error parsing response."; - LOG.error( mesg, e ); - throw new DeeJUMPException( mesg, e ); - } catch ( IOException e ) { - String mesg = "Error parsing response."; - LOG.error( mesg, e ); - throw new DeeJUMPException( mesg, e ); - } catch ( XMLParsingException e ) { - String mesg = "Error parsing response."; - LOG.error( mesg, e ); - try { - LOG.error( "Schema could not be used to validate FeatureCollection." ); - LOG.error( "Trying once again with crude guessing method." ); - gfDoc = new GMLFeatureCollectionDocument( true ); - gfDoc.load( sr, "http://www.systemid.org" ); - newFeatCollec = gfDoc.parse(); - } catch ( SAXException e1 ) { - LOG.error( mesg, e ); - throw new DeeJUMPException( mesg, e ); - } catch ( IOException e1 ) { - LOG.error( mesg, e ); - throw new DeeJUMPException( mesg, e ); - } catch ( XMLParsingException e1 ) { - LOG.error( mesg, e ); - throw new DeeJUMPException( mesg, e ); - } - throw new DeeJUMPException( mesg, e ); - } + // read the first 1024 bytes + char[] chunk = new char[1024]; + rd.read(chunk); + // unread and reuse later + rd.unread(chunk); + // System.out.println(new String(chunk)); - return newFeatCollec; + // did we receive a wfs exception? + String outputStart = new String(chunk); + if (outputStart.matches("(?i).*<Exception>.*") + || outputStart.matches("(?i).*<ServiceExceptionReport")) { + RuntimeException re = new RuntimeException( + "Couldn't get data from WFS:\n" + IOUtils.toString(rd)); + LOG.debug("Couldn't get data from WFS.", re); + throw re; + } + } catch (UnsupportedEncodingException e2) { + throw new DeeJUMPException(e2); + } catch (IOException e) { + throw new DeeJUMPException(e); } - /** - * @param fc - * @param geom - * @return a JUMP feature collection - * @throws DeeJUMPException - */ - public static FeatureCollection createFromDeegreeFC( org.deegree.model.feature.FeatureCollection fc, Geometry geom ) - throws DeeJUMPException { - return createFromDeegreeFC( fc, geom, null, null ); - } + BufferedReader bfrd = new BufferedReader(rd); - /** - * Creates a JUMP FeatureCollection from a deegree FeatureCollection [UT] and a specified JUMP/JTS Geometry object. - * The new JUMP FeatureCollection returned will have the <code>defaultGeometry</code> as its <code>GEOM</code> - * attribute - * - * @author <a href="mailto:tad...@lat-lon.de">Ugo Taddei </a> - * @param deegreeFeatCollec - * the deegree FeatureCollection - * @param defaultGeometry - * the geometry of the returned FeatureCollection - * @param wfs - * if the data came from a wfs, this can be used to determine the feature type even without any features - * @param ftName - * the requested feature type from the above wfs - * @return the new JUMP FeatureCollection - * @throws DeeJUMPException - */ - public static FeatureCollection createFromDeegreeFC( org.deegree.model.feature.FeatureCollection deegreeFeatCollec, - Geometry defaultGeometry, AbstractWFSWrapper wfs, - QualifiedName ftName ) - throws DeeJUMPException + GMLFeatureCollectionDocument gfDoc = new GMLFeatureCollectionDocument(); - { + monitor.report(WFSPlugIn.i18n("build-schema-from-server")); + // get schema from server + if (featureType != null) { + Map<URI, GMLSchema> schemaMap = new HashMap<URI, GMLSchema>(); + String dft = server.getDescribeTypeURL(featureType); + GMLSchemaDocument doc = new de.latlon.deejump.wfs.deegree2mods.GMLSchemaDocument(); + try { + doc.load(new URL(dft)); + LOG.debug("Feature type schema:\n" + doc.getAsPrettyString()); + GMLSchema schema = doc.parseGMLSchema(); + schemaMap.put(featureType.getNamespace(), schema); + gfDoc.setSchemas(schemaMap); + } catch (XMLSchemaException e) { + LOG.debug("DescribeFeatureType did not work."); + } catch (UnknownCRSException e) { + LOG.debug("DescribeFeatureType did not work."); + } catch (XMLParsingException e) { + LOG.debug("DescribeFeatureType did not work."); + } catch (MalformedURLException e) { + LOG.debug("DescribeFeatureType did not work."); + } catch (IOException e) { + LOG.debug("DescribeFeatureType did not work."); + } catch (SAXException e) { + LOG.debug("DescribeFeatureType did not work."); + } finally { + LOG.debug("Trying to use base url of server for DescribeFeatureType..."); + try { + String baseURL = server.getBaseWfsURL(); + baseURL += baseURL.endsWith("?") ? "" : "?"; + doc.load(new URL(server.getDescribeTypeURL(baseURL, featureType))); - FeatureSchema fs = new FeatureSchema(); + LOG.debug("Feature type schema:\n" + doc.getAsPrettyString()); - com.vividsolutions.jump.feature.FeatureCollection jumpFC = new FeatureDataset( fs ); + GMLSchema schema = doc.parseGMLSchema(); + schemaMap.put(featureType.getNamespace(), schema); + gfDoc.setSchemas(schemaMap); + } catch (XMLSchemaException e) { + LOG.debug("DescribeFeatureType did not work."); + } catch (UnknownCRSException e) { + LOG.debug("DescribeFeatureType did not work."); + } catch (XMLParsingException e) { + LOG.debug("DescribeFeatureType did not work."); + } catch (MalformedURLException e) { + LOG.debug("DescribeFeatureType did not work."); + } catch (IOException e) { + LOG.debug("DescribeFeatureType did not work."); + } catch (SAXException e) { + LOG.debug("DescribeFeatureType did not work."); + } + } + } - org.deegree.model.feature.Feature[] feats = deegreeFeatCollec.toArray(); + monitor.report(WFSPlugIn.i18n("create-deegree-feature-collection")); + org.deegree.model.feature.FeatureCollection newFeatCollec = null; + try { + gfDoc.load(bfrd, "http://dummySysId"); + newFeatCollec = gfDoc.parse(); + } catch (SAXException e) { + String mesg = "Error parsing response."; + LOG.error(mesg, e); + throw new DeeJUMPException(mesg, e); + } catch (IOException e) { + String mesg = "Error parsing response."; + LOG.error(mesg, e); + throw new DeeJUMPException(mesg, e); + } catch (XMLParsingException e) { + String mesg = "Error parsing response."; + LOG.error(mesg, e); + try { + LOG.error("Schema could not be used to validate FeatureCollection."); + LOG.error("Trying once again with crude guessing method."); + gfDoc = new GMLFeatureCollectionDocument(true); + gfDoc.load(bfrd, "http://www.systemid.org"); + newFeatCollec = gfDoc.parse(); + } catch (SAXException e1) { + LOG.error(mesg, e); + throw new DeeJUMPException(mesg, e); + } catch (IOException e1) { + LOG.error(mesg, e); + throw new DeeJUMPException(mesg, e); + } catch (XMLParsingException e1) { + LOG.error(mesg, e); + throw new DeeJUMPException(mesg, e); + } + throw new DeeJUMPException(mesg, e); + } - if ( wfs == null && ( feats == null || feats.length < 1 ) ) { - throw new DeeJUMPException( "No data found" ); - } + return newFeatCollec; + } - FeatureType ft = null; - if ( wfs != null && ftName != null ) { - GMLSchema schema = wfs.getSchemaForFeatureType( ftName.getLocalName() ); - if ( schema != null ) { - ft = schema.getFeatureType( ftName ); - } else { - throw new DeeJUMPException( "No data found" ); - } - } else { - if ( feats.length > 0 ) { - ft = feats[0].getFeatureType(); - } - } + /** + * @param fc + * @param geom + * @return a JUMP feature collection + * @throws DeeJUMPException + */ + public FeatureCollection createFromDeegreeFC( + org.deegree.model.feature.FeatureCollection fc, Geometry geom) + throws DeeJUMPException { + return createFromDeegreeFC(fc, geom, null, null); + } - AbstractPropertyType[] geoTypeProps = ft.getGeometryProperties(); + /** + * Creates a JUMP FeatureCollection from a deegree FeatureCollection [UT] and + * a specified JUMP/JTS Geometry object. The new JUMP FeatureCollection + * returned will have the <code>defaultGeometry</code> as its + * <code>GEOM</code> attribute + * + * @author <a href="mailto:tad...@lat-lon.de">Ugo Taddei </a> + * @param deegreeFeatCollec + * the deegree FeatureCollection + * @param defaultGeometry + * the geometry of the returned FeatureCollection + * @param wfs + * if the data came from a wfs, this can be used to determine the + * feature type even without any features + * @param ftName + * the requested feature type from the above wfs + * @return the new JUMP FeatureCollection + * @throws DeeJUMPException + */ + public FeatureCollection createFromDeegreeFC( + org.deegree.model.feature.FeatureCollection deegreeFeatCollec, + Geometry defaultGeometry, AbstractWFSWrapper wfs, QualifiedName ftName) + throws DeeJUMPException - String geoProName = null; + { - if ( geoTypeProps.length > 1 ) { - LOG.warn( "This feature type has more than one geometry property. Only the first one will be used." ); - } + FeatureSchema fs = new FeatureSchema(); - if ( geoTypeProps == null || geoTypeProps.length == 0 ) { - LOG.debug( "Guessing geometry property name." ); - geoProName = "GEOMETRY"; //$NON-NLS-1$ - } else { - geoProName = geoTypeProps[0].getName().getLocalName(); - LOG.debug( "Geometry property name: " + geoProName ); - } + com.vividsolutions.jump.feature.FeatureCollection jumpFC = new FeatureDataset( + fs); - PropertyType[] featTypeProps = ft.getProperties(); + org.deegree.model.feature.Feature[] feats = deegreeFeatCollec.toArray(); - boolean addedGeometry = false; + if (wfs == null && (feats == null || feats.length < 1)) { + throw new DeeJUMPException("No data found"); + } - // populate JUMP schema - for ( int j = 0; j < featTypeProps.length; j++ ) { - String name = featTypeProps[j].getName().getLocalName(); - AttributeType type = findType( featTypeProps[j].getType() ); - if ( type == GEOMETRY ) { - if ( !addedGeometry ) { - addedGeometry = true; - } else { - continue; - } - } - fs.addAttribute( name, type ); - } + FeatureType ft = null; + if (wfs != null && ftName != null) { + GMLSchema schema = wfs.getSchemaForFeatureType(ftName.getLocalName()); + if (schema != null) { + ft = schema.getFeatureType(ftName); + } else { + throw new DeeJUMPException("No data found"); + } + } else { + if (feats.length > 0) { + ft = feats[0].getFeatureType(); + } + } - if ( defaultGeometry == null && fs.getGeometryIndex() == -1 ) { - throw new RuntimeException( "No geometry property found!" ); - } else if ( defaultGeometry != null && fs.getGeometryIndex() == -1 ) { - fs.addAttribute( "FAKE_GEOMETRY", GEOMETRY ); - } + AbstractPropertyType[] geoTypeProps = ft.getGeometryProperties(); - // populate FC with data - for ( int i = 0; i < feats.length; i++ ) { + String geoProName = null; - com.vividsolutions.jump.feature.Feature jf = new WFSFeature( fs, feats[i].getId() ); - org.deegree.model.spatialschema.Geometry geoObject = feats[i].getDefaultGeometryPropertyValue(); + if (geoTypeProps.length > 1) { + LOG.warn("This feature type has more than one geometry property. Only the first one will be used."); + } - if ( addedGeometry ) { - try { - Geometry geom = export( geoObject ); - jf.setGeometry( geom ); + if (geoTypeProps == null || geoTypeProps.length == 0) { + LOG.debug("Guessing geometry property name."); + geoProName = "GEOMETRY"; //$NON-NLS-1$ + } else { + geoProName = geoTypeProps[0].getName().getLocalName(); + LOG.debug("Geometry property name: " + geoProName); + } - } catch ( Exception e ) { - throw new RuntimeException( e ); - } - } else { - jf.setGeometry( defaultGeometry ); - } + PropertyType[] featTypeProps = ft.getProperties(); - int geoIndex = fs.getGeometryIndex(); + boolean addedGeometry = false; - for ( int j = 0; j < fs.getAttributeCount(); j++ ) { - if ( j != geoIndex ) { - QualifiedName qn = new QualifiedName( fs.getAttributeName( j ), - featTypeProps[j].getName().getNamespace() ); - - FeatureProperty fp = feats[i].getDefaultProperty( qn ); - Object value = null; - if ( fp != null ) { - value = fp.getValue(); - } - // OpenJUMP only knows int values - if ( value instanceof Long ) { - jf.setAttribute( j, (int) ( (Long) value ).longValue() ); - } else { - jf.setAttribute( j, value ); - } - } - } - jumpFC.add( jf ); + // populate JUMP schema + for (int j = 0; j < featTypeProps.length; j++) { + String name = featTypeProps[j].getName().getLocalName(); + AttributeType type = findType(featTypeProps[j].getType()); + if (type == GEOMETRY) { + if (!addedGeometry) { + addedGeometry = true; + } else { + continue; } + } + fs.addAttribute(name, type); + } - return jumpFC; + if (defaultGeometry == null && fs.getGeometryIndex() == -1) { + throw new RuntimeException("No geometry property found!"); + } else if (defaultGeometry != null && fs.getGeometryIndex() == -1) { + fs.addAttribute("FAKE_GEOMETRY", GEOMETRY); } - /** - * @param type - * an SQL type code as in deegree Types class - * @return the JUMP type - */ - public static AttributeType findType( int type ) { - // assumes integer for SQL's NUMERIC - String xsd = Types.getXSDTypeForSQLType( type, 0 ); + // populate FC with data + for (int i = 0; i < feats.length; i++) { - if ( xsd.equals( "dateTime" ) || xsd.equals( "date" ) ) { - return AttributeType.DATE; - } + com.vividsolutions.jump.feature.Feature jf = new WFSFeature(fs, + feats[i].getId()); + org.deegree.model.spatialschema.Geometry geoObject = feats[i] + .getDefaultGeometryPropertyValue(); - if ( xsd.equals( "gml:GeometryPropertyType" ) ) { - return AttributeType.GEOMETRY; - } + if (addedGeometry) { + try { + Geometry geom = export(geoObject); + jf.setGeometry(geom); - if ( xsd.equals( "integer" ) ) { - return AttributeType.INTEGER; + } catch (Exception e) { + throw new RuntimeException(e); } + } else { + jf.setGeometry(defaultGeometry); + } - if ( xsd.equals( "double" ) || xsd.equals( "decimal" ) || xsd.equals( "float" ) ) { - return AttributeType.DOUBLE; - } + int geoIndex = fs.getGeometryIndex(); - if ( xsd.equals( "gml:FeaturePropertyType" ) ) { - return AttributeType.OBJECT; // unknown what happens in this case + for (int j = 0; j < fs.getAttributeCount(); j++) { + if (j != geoIndex) { + QualifiedName qn = new QualifiedName(fs.getAttributeName(j), + featTypeProps[j].getName().getNamespace()); + + FeatureProperty fp = feats[i].getDefaultProperty(qn); + Object value = null; + if (fp != null) { + value = fp.getValue(); + } + // OpenJUMP only knows int values + if (value instanceof Long) { + jf.setAttribute(j, (int) ((Long) value).longValue()); + } else { + jf.setAttribute(j, value); + } } + } + jumpFC.add(jf); + } - // default is string, should work for booleans as well - return AttributeType.STRING; + return jumpFC; + } + + /** + * @param type + * an SQL type code as in deegree Types class + * @return the JUMP type + */ + public AttributeType findType(int type) { + // assumes integer for SQL's NUMERIC + String xsd = Types.getXSDTypeForSQLType(type, 0); + + if (xsd.equals("dateTime") || xsd.equals("date")) { + return AttributeType.DATE; } - /** - * @param jumpFeatureCollection - * @return a deegree FC - * @throws UnknownTypeException - * @throws GeometryException - */ - public static org.deegree.model.feature.FeatureCollection createFromJUMPFeatureCollection( - com.vividsolutions.jump.feature.FeatureCollection jumpFeatureCollection ) - throws UnknownTypeException, GeometryException { + if (xsd.equals("gml:GeometryPropertyType")) { + return AttributeType.GEOMETRY; + } - if ( jumpFeatureCollection.size() == 0 || jumpFeatureCollection == null ) { - throw new IllegalArgumentException( "FeatureCollection cannot be null and must have at least one feature" ); - } - org.deegree.model.feature.FeatureCollection fc = FeatureFactory.createFeatureCollection( - "id", - jumpFeatureCollection.size() ); + if (xsd.equals("integer")) { + return AttributeType.INTEGER; + } - FeatureSchema schema = jumpFeatureCollection.getFeatureSchema(); - // for (int i = 0; i < schema.getAttributeCount(); i++) { - // System.out.println(schema.getAttributeName(i) + " " - // + schema.getAttributeType(i)); - // } - int count = 0; + if (xsd.equals("double") || xsd.equals("decimal") || xsd.equals("float")) { @@ Diff output truncated at 100000 characters. @@ ------------------------------------------------------------------------------ _______________________________________________ Jump-pilot-devel mailing list Jump-pilot-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/jump-pilot-devel