📄 wcshttpclientbase.java
字号:
/**
*
*/
package com.esri.solutions.jitk.datasources.ogc.wcs;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Iterator;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import com.esri.solutions.jitk.datasources.exceptions.ParserNotFoundException;
import com.esri.solutions.jitk.datasources.exceptions.ResponseParsingException;
import com.esri.solutions.jitk.datasources.exceptions.WCSException;
import com.esri.solutions.jitk.datasources.ogc.wcs.info.WCSCapabilities;
import com.esri.solutions.jitk.datasources.ogc.wcs.parsing.IWCSCapabilitiesParser;
import com.esri.solutions.jitk.datasources.ogc.wcs.parsing.WCSCapabilitiesParserFactory;
/**
* @author vlad2928
*
*/
public class WCSHTTPClientBase {
protected static Logger logger = LogManager.getLogger(WCSHTTPClientBase.class.getName());
protected GetMethod method;
protected String version;
public String getVersion() {
return this.version;
}
protected void doGET(IURL url) throws HttpException, IOException {
// Release previously opened connection if any
releaseConnection();
// Create an instance of HttpClient.
HttpClient client = new HttpClient();
// Create a method instance.
method = new GetMethod(url.toString());
// Provide custom retry handler is necessary
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));
// Execute the method.
if(client.executeMethod(method) != HttpStatus.SC_OK) {
logger.error("HTTP request failed for '" + url + "': " + method.getStatusLine());
throw new HttpException("HTTP request failed for '" + url + "': " + method.getStatusLine());
}
}
protected boolean isConnectionOpen() {
return (method != null);
}
protected String getResponseContentType() {
if(isConnectionOpen()) {
return method.getResponseHeader("Content-Type").getValue();
}
return "";
}
protected void releaseConnection() {
if(isConnectionOpen()) {
method.releaseConnection();
}
}
public WCSCapabilities getCapabilities(String getCapabilitiesURL) throws WCSException {
WCSCapabilities wcsCapabilities = null;
String version = "";
try {
IURL url = new URLBuilder(getCapabilitiesURL);
if(url.isValid()) {
url.setParam("request", "GetCapabilities");
url.setParam("service", "WCS");
for(Iterator<String> i = WCSCapabilitiesParserFactory.getSupportedVersions().iterator(); i.hasNext();) {
version = i.next();
if(WCSClientFactory.isSupportedVersion(version)) {
IWCSCapabilitiesParser parser = WCSCapabilitiesParserFactory.getInstance(version);
url.setParam("version", version);
try {
doGET(url);
if(isConnectionOpen()) {
wcsCapabilities = parser.parse(method.getResponseBodyAsStream());
}
} catch(ResponseParsingException e) {
} finally {
releaseConnection();
}
if(wcsCapabilities != null) {
break;
}
}
}
} else {
throw new MalformedURLException(url.getErrorMessage());
}
} catch(MalformedURLException e) {
logger.error("Malformed url " + getCapabilitiesURL + " provided.");
logger.error(e.getMessage());
throw new WCSException("Malformed url " + getCapabilitiesURL + " provided.");
} catch(ParserNotFoundException e) {
logger.error("GetCapabilities parser ver. " + version + " not found.");
logger.error(e.getMessage());
throw new WCSException("GetCapabilities parser ver. " + version + " not found.");
} catch(HttpException e) {
logger.error("Fatal protocol violation: " + e.getMessage());
throw new WCSException("Fatal protocol violation.");
} catch(IOException e) {
logger.error("Fatal transport error: " + e.getMessage());
throw new WCSException("Fatal transport error.");
}
return wcsCapabilities;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -