use of org.apache.commons.httpclient.HttpClient in project zm-mailbox by Zimbra.
the class ZMailbox method getRESTResource.
private InputStream getRESTResource(String relativePath, String startTimeArg, String endTimeArg, int msecTimeout, String alternateUrl) throws ServiceException {
GetMethod get = null;
URI uri = null;
int statusCode;
try {
if (startTimeArg != null) {
String encodedArg = URLEncoder.encode(startTimeArg, "UTF-8");
if (!relativePath.contains("?")) {
relativePath = relativePath + "?start=" + encodedArg;
} else {
relativePath = relativePath + "&start=" + encodedArg;
}
}
if (endTimeArg != null) {
String encodedArg = URLEncoder.encode(endTimeArg, "UTF-8");
if (!relativePath.contains("?")) {
relativePath = relativePath + "?end=" + encodedArg;
} else {
relativePath = relativePath + "&end=" + encodedArg;
}
}
uri = getRestURI(relativePath, alternateUrl);
HttpClient client = getHttpClient(uri);
get = new GetMethod(uri.toString());
if (msecTimeout > -1) {
get.getParams().setSoTimeout(msecTimeout);
}
statusCode = HttpClientUtil.executeMethod(client, get);
// parse the response
if (statusCode == HttpServletResponse.SC_OK) {
return new GetMethodInputStream(get);
} else {
String msg = String.format("GET from %s failed, status=%d. %s", uri.toString(), statusCode, get.getStatusText());
throw ServiceException.FAILURE(msg, null);
}
} catch (IOException e) {
String fromUri = "";
if (uri != null) {
fromUri = " from " + uri.toString();
}
String msg = String.format("Unable to get REST resource%s: %s", fromUri, e.getMessage());
throw ZClientException.IO_ERROR(msg, e);
}
}
use of org.apache.commons.httpclient.HttpClient in project zm-mailbox by Zimbra.
the class ZimbraHttpConnectionManager method main.
public static void main(String[] args) {
// dump httpclient package defaults
System.out.println(dumpParams("httpclient package defaults", new HttpConnectionManagerParams(), new HttpClientParams()));
System.out.println(dumpParams("Internal ZimbraHttpConnectionManager", ZimbraHttpConnectionManager.getInternalHttpConnMgr().getParams().getConnMgrParams(), ZimbraHttpConnectionManager.getInternalHttpConnMgr().getDefaultHttpClient().getParams()));
System.out.println(dumpParams("External ZimbraHttpConnectionManager", ZimbraHttpConnectionManager.getExternalHttpConnMgr().getParams().getConnMgrParams(), ZimbraHttpConnectionManager.getExternalHttpConnMgr().getDefaultHttpClient().getParams()));
HttpClient httpClient = ZimbraHttpConnectionManager.getInternalHttpConnMgr().getDefaultHttpClient();
String connMgrName = httpClient.getHttpConnectionManager().getClass().getSimpleName();
long connMgrTimeout = httpClient.getParams().getConnectionManagerTimeout();
System.out.println("HttpConnectionManager for the HttpClient instance is: " + connMgrName);
System.out.println("connection manager timeout for the HttpClient instance is: " + connMgrTimeout);
}
use of org.apache.commons.httpclient.HttpClient in project tdi-studio-se by Talend.
the class ExchangeUtils method sendGetRequest.
public static String sendGetRequest(String urlAddress) throws Exception {
HttpClient httpclient = new HttpClient();
GetMethod getMethod = new GetMethod(urlAddress);
TransportClientProperties tcp = TransportClientPropertiesFactory.create("http");
if (tcp.getProxyHost().length() != 0) {
UsernamePasswordCredentials creds = new UsernamePasswordCredentials(tcp.getProxyUser() != null ? tcp.getProxyUser() : "", tcp.getProxyPassword() != null ? tcp.getProxyUser() : "");
httpclient.getState().setProxyCredentials(AuthScope.ANY, creds);
HostConfiguration hcf = new HostConfiguration();
hcf.setProxy(tcp.getProxyHost(), Integer.parseInt(tcp.getProxyPort()));
httpclient.executeMethod(hcf, getMethod);
} else {
httpclient.executeMethod(getMethod);
}
String response = getMethod.getResponseBodyAsString();
getMethod.releaseConnection();
return response;
}
use of org.apache.commons.httpclient.HttpClient in project tdi-studio-se by Talend.
the class ExchangeUtils method sendPostRequest.
public static String sendPostRequest(String urlAddress, Map<String, String> parameters) throws Exception {
HttpClient httpclient = new HttpClient();
PostMethod postMethod = new PostMethod(urlAddress);
if (parameters != null) {
NameValuePair[] postData = new NameValuePair[parameters.size()];
int i = 0;
for (String key : parameters.keySet()) {
String value = parameters.get(key);
postData[i++] = new NameValuePair(key, value);
}
postMethod.addParameters(postData);
}
httpclient.executeMethod(postMethod);
String response = postMethod.getResponseBodyAsString();
postMethod.releaseConnection();
return response;
}
use of org.apache.commons.httpclient.HttpClient in project spatial-portal by AtlasOfLivingAustralia.
the class SpeciesAutoComplete method getResults.
private JSONArray getResults(String nsurl) throws Exception {
HttpClient client = new HttpClient();
GetMethod get = new GetMethod(nsurl);
get.addRequestHeader(StringConstants.CONTENT_TYPE, StringConstants.TEXT_PLAIN);
client.executeMethod(get);
String rawJSON = get.getResponseBodyAsString();
//parse
JSONParser jp = new JSONParser();
JSONObject jo = (JSONObject) jp.parse(rawJSON);
//support search and auto bie webservices
if (jo.containsKey("searchResults")) {
return (JSONArray) ((JSONObject) jo.get("searchResults")).get("results");
} else {
return (JSONArray) jo.get("autoCompleteList");
}
}
Aggregations