📄 cswclient.java
字号:
package com.esri.solutions.jitk.datasources.ogc.csw;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethodBase;
import org.apache.commons.httpclient.methods.ByteArrayRequestEntity;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
/**
* CswClient class is used to submit CSW search request.
*
* @remark CswClient is a wrapper class of .NET HttpWebRequest and HttpWebResponse.
* It basically submits a HTTP request then return a text response.
* @version 1.0
* @created 25-Jul-2007 1:40:24 PM
*/
public class CswClient {
/**
* use static variable to be thread safe
* (FROM C# CODE: MAY BE REMOVED LATER)
*
*/
// private static CookieContainer _cookieContainer;
// private CredentialCache _credentialCache;
/**
* Constructor
*/
public CswClient() {
}
/**
* Encode PostBody
*
* @remark Encode special characters (such as %, space, <, >, \, and &) to percent
* values.
* @returns Encoded text.
*
* @param postbody Text to be encoded
*/
public String encodePostbody(String postbody) {
return "";
}
/**
* Submit HTTP Request
*
* @returns Response in plain text
*
* @param method HTTP Method. for example "POST", "GET"
* @param URL URL to send HTTP Request to
* @param postdata Data to be posted
* @throws IOException
*/
public InputStream submitHttpRequest(String method, String url,
String postdata) throws IOException {
return submitHttpRequest(method, url, postdata, "", "");
}
/**
* Submit HTTP Request (Both GET and POST). Return InputStream object from the response
*
* @remark Submit an HTTP request.
* @returns Response in plain text.
*
* @param method HTTP Method. for example "POST", "GET"
* @param URL URL to send HTTP Request to
* @param postdata Data to be posted
* @param usr Username
* @param pwd Password
* @throws IOException
*/
public InputStream submitHttpRequest(String method, String urlString,
String postdata, String usr, String pwd) throws IOException {
HttpClient httpClient = null;
HttpMethodBase httpMethod = null;
ByteArrayRequestEntity requestEntity = null;
try {
httpClient = new HttpClient();
if (method.equalsIgnoreCase("get")) {
httpMethod = new GetMethod(urlString);
} else {
httpMethod = new PostMethod(urlString);
PostMethod postMethod = (PostMethod) httpMethod;
requestEntity = new ByteArrayRequestEntity(postdata.getBytes(),
"application/xml");
postMethod.setRequestEntity(requestEntity);
}
httpClient.executeMethod(httpMethod);
return new ByteArrayInputStream(httpMethod.getResponseBody());
} finally {
if (httpMethod != null) {
httpMethod.releaseConnection();
}
}
}
/**
* Submit HTTP Request (Both GET and POST). Parse the response into an xml document element.
* @param method
* @param urlString
* @param postdata
* @param usr
* @param pwd
* @param response
* @throws IOException
* @throws ParserConfigurationException
* @throws SAXException
*/
public Element submitHttpRequest(String method, String urlString,
String postdata, String usr, String pwd, Element res)
throws IOException, ParserConfigurationException, SAXException {
Element response;
HttpClient httpClient = null;
HttpMethodBase httpMethod = null;
ByteArrayRequestEntity requestEntity = null;
DocumentBuilderFactory factory = null;
DocumentBuilder docBuilder = null;
Document doc = null;
try {
httpClient = new HttpClient();
if (method.equalsIgnoreCase("get")) {
httpMethod = new GetMethod(urlString);
} else {
httpMethod = new PostMethod(urlString);
PostMethod postMethod = (PostMethod) httpMethod;
requestEntity = new ByteArrayRequestEntity(postdata.getBytes(),
"application/xml");
postMethod.setRequestEntity(requestEntity);
}
httpClient.executeMethod(httpMethod);
// Get a response
factory = DocumentBuilderFactory.newInstance();
docBuilder = factory.newDocumentBuilder();
doc = docBuilder.parse(httpMethod.getResponseBodyAsStream());
response = doc.getDocumentElement();
response.normalize();
return response;
} finally {
if (httpMethod != null) {
httpMethod.releaseConnection();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -