?? wfsgclient.java
字號:
package com.esri.solutions.jitk.common.gazetteer;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.ByteArrayRequestEntity;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import com.esri.solutions.jitk.common.gazetteer.IGazetteerService.SearchMethod;
import org.apache.log4j.Logger;
import org.apache.xerces.parsers.SAXParser;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.StringCharacterIterator;
/**
* Class that will interact with the WFS-G service via HTTP.
*/
public class WFSGClient {
/**
* {@link Logger} used to log messages for this class.
*/
private static final Logger _logger = Logger.getLogger(WFSGClient.class);
/**
* URL of the WFS-G service.
*/
private String m_serviceUrl = null;
/**
* {@link String} representation of the WFS-G get features
* request.
*/
private String m_getFeaturesRequest = null;
/**
* {@link String} representation of the WFS-G get features
* response.
*/
private String m_getFeaturesResponse = null;
/**
* Reference to the {@link WFSCapabilities} object.
*/
private WFSCapabilities m_capabilities = null;
/**
* Constructor that initializes the instance with the URL
* to the WFS-G service. Also makes a get capabilities call
* to the WFS-G service to retrieve the capabilities document via
* {@link #getCapabilities()}.
*
* @param url {@link String} Url to the WFS-G service.
*/
public WFSGClient(String url) {
m_serviceUrl = url;
m_capabilities = getCapabilities();
}
/**
* Gets the Url to the WFS-G service.
*
* @return {@link String} Url to the WFS-G service.
*/
public String getServiceUrl() {
return m_serviceUrl;
}
/**
* Sets the Url to the WFS-G service.
*
* @param url {@link String} Url to the WFS-G service.
*/
public void setServiceUrl(String url) {
m_serviceUrl = url;
}
/**
* Gets the {@link String} representation of the WFS get
* features request generated by {@link #createGetFeaturesRequest(String, int)}.
*
* @return {@link String} representation of the WFS get features request.
*/
public String getGetFeaturesRequest() {
return m_getFeaturesRequest;
}
/**
* Gets the {@link String} representation of the WFS get
* features response generated by {@link #submitGetFeatureRequest(String)}.
*
* @return {@link String} representation of the WFS get features response.
*/
public String getGetFeaturesResponse() {
return m_getFeaturesResponse;
}
/**
* Creates a WFS get feature request with the given <code>placeName</code>
* and <code>numFeatures</code> arguments. The output of this method can be
* retrieved via {@link #getGetFeaturesRequest()}.
*
* @param placeName {@link String} feature name for searching.
* @param numFeatures Max number of requested features returned.
*/
public void createGetFeaturesRequest(String placeName, int numFeatures,
SearchMethod searchMethod) {
String strXML = "";
if (placeName.length() > 0) {
strXML = "<?xml version='1.0' ?>";
strXML += ("<wfs:GetFeature service='WFS' version=\"1.1.0\" outputFormat=\"GML3\" maxFeatures=\"" +
numFeatures + "\" ");
strXML += "xmlns:iso19112=\"http://www.opengis.net/iso19112\" ";
strXML += "xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" ";
strXML += "xmlns:gml=\"http://www.opengis.net/gml\" ";
strXML += "xmlns:wfs=\"http://www.opengis.net/wfs\" ";
strXML += "xmlns:ogc=\"http://www.opengis.net/ogc\" ";
strXML += "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" ";
strXML += "xsi:schemaLocation=\"http://www.opengis.net/wfs../wfs/1.0.0/WFS-basic.xsd\">";
strXML += " <wfs:Query typeName=\"iso19112:SI_LOCATIONINSTANCE\">";
strXML += " <ogc:Filter>";
strXML += " <ogc:PropertyIsLike wildCard=\"*\" singleChar=\"?\" escapeChar=\"!\">";
strXML += " <ogc:PropertyName>geographicIdentifier</ogc:PropertyName>";
if (searchMethod.equals(SearchMethod.CONTAINS))
strXML += (" <ogc:Literal>%" + placeName + "*</ogc:Literal>");
else if (searchMethod.equals(SearchMethod.BEGINS_WITH))
strXML += (" <ogc:Literal>" + placeName + "*</ogc:Literal>");
strXML += " </ogc:PropertyIsLike>";
strXML += " </ogc:Filter>";
strXML += " </wfs:Query>";
strXML += "</wfs:GetFeature>";
}
_logger.debug(strXML);
m_getFeaturesRequest = strXML;
}
/**
* Submits a WFS get feature request to the WFS service using HTTP. The
* output of this method can be retrieved via {@link #getGetFeaturesResponse()}.
*
* @param theRequest {@link String} representation of the WFS get feature request.
*/
public void submitGetFeatureRequest(String theRequest) {
HttpClient httpClient = null;
PostMethod method = null;
ByteArrayRequestEntity requestEntity = null;
BufferedReader rd = null;
String line = null;
StringBuilder sb = null;
try {
httpClient = new HttpClient();
method = new PostMethod(m_capabilities.getGetFeatureUrlPost());
requestEntity = new ByteArrayRequestEntity(theRequest.getBytes(),
"text/xml");
method.setRequestEntity(requestEntity);
httpClient.executeMethod(method);
// Get the response
rd = new BufferedReader(new InputStreamReader(
method.getResponseBodyAsStream()));
sb = new StringBuilder();
sb.append("<?xml version='1.0' ?>");
while ((line = rd.readLine()) != null) {
StringCharacterIterator iterator = new StringCharacterIterator(line);
char character = iterator.current();
while (character != StringCharacterIterator.DONE) {
// if (character == '<') {
// sb.append("<");
// } else if (character == '>') {
// sb.append(">");
// } else if (character == '\"') {
// sb.append(""");
// } else if (character == '\'') {
// sb.append("'");
// } else if (character == '\\') {
// sb.append("\");
// } else if (character == '&') {
// sb.append("&");
// } else {
sb.append(character);
// }
character = iterator.next();
}
}
rd.close();
m_getFeaturesResponse = sb.toString();
} catch (Exception ex) {
_logger.warn("Error submitting request", ex);
} finally {
if (method != null) {
method.releaseConnection();
}
}
}
/**
* Submits a WFS get capabilities request to the WFS service using HTTP.
* The response is then parsed using the Simple API for XML (SAX).
*
* @return Reference to the {@link WFSCapabilities} object.
*/
private WFSCapabilities getCapabilities() {
WFSCapabilities capabilities = null;
String capabilitiesUrl = null;
HttpClient httpClient = null;
GetMethod method = null;
CapabilitiesHandler handler = null;
SAXParser parser = null;
if (m_serviceUrl.charAt(m_serviceUrl.length() - 1) != '?') {
capabilitiesUrl = m_serviceUrl + "?";
} else {
capabilitiesUrl = m_serviceUrl;
}
capabilitiesUrl += "request=GetCapabilities&service=WFS&version=1.1.0";
try {
httpClient = new HttpClient();
method = new GetMethod(capabilitiesUrl);
httpClient.executeMethod(method);
handler = new CapabilitiesHandler();
parser = new SAXParser();
parser.setContentHandler(handler);
parser.parse(new InputSource(method.getResponseBodyAsStream()));
capabilities = handler.getWFSCapabilities();
} catch (IOException ex) {
_logger.warn("IOException occurred getting WFS capabilities.", ex);
} catch (SAXException ex) {
_logger.warn("SAXException occurred parsing WFS capabilities document.",
ex);
} finally {
if (method != null) {
method.releaseConnection();
}
}
return capabilities;
}
/**
* SAX content handler class for parsing the XML capabilities document
* of a WFS service. Extends {@link DefaultHandler}.
*/
private class CapabilitiesHandler extends DefaultHandler {
/**
* Reference to the {@link WFSCapabilities} object.
*/
WFSCapabilities capabilities = new WFSCapabilities();
/**
* Flag used when parsing the XML capabilities document.
*/
boolean isGetFeatureOperation = Boolean.FALSE;
/**
* Gets the parsed {@link WFSCapabilities} document.
*
* @return Parsed {@link WFSCapabilities} object.
*/
public WFSCapabilities getWFSCapabilities() {
return capabilities;
}
/*
* (non-Javadoc)
* @see org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
*/
public void endElement(String uri, String localName, String rawName)
throws SAXException {
if (localName.equalsIgnoreCase("Operation") &&
isGetFeatureOperation) {
isGetFeatureOperation = Boolean.FALSE;
}
}
/*
* (non-Javadoc)
* @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
*/
public void startElement(String uri, String localName, String rawName,
Attributes attributes) throws SAXException {
if (localName.equalsIgnoreCase("Operation")) {
if (attributes.getValue("name").equalsIgnoreCase("GetFeature")) {
isGetFeatureOperation = Boolean.TRUE;
}
}
if (isGetFeatureOperation && localName.equalsIgnoreCase("Post")) {
capabilities.setGetFeatureUrlPost(attributes.getValue(
"xlink:href"));
}
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -