use of org.apache.http.impl.client.BasicResponseHandler in project cloudstack by apache.
the class PaloAltoResource method request.
/*
* XML API commands
*/
/* Function to make calls to the Palo Alto API. */
/* All API calls will end up going through this function. */
protected String request(PaloAltoMethod method, Map<String, String> params) throws ExecutionException {
if (method != PaloAltoMethod.GET && method != PaloAltoMethod.POST) {
throw new ExecutionException("Invalid http method used to access the Palo Alto API.");
}
String responseBody = "";
String debug_msg = "Palo Alto Request\n";
// a GET method...
if (method == PaloAltoMethod.GET) {
String queryString = "?";
for (String key : params.keySet()) {
if (!queryString.equals("?")) {
queryString = queryString + "&";
}
try {
queryString = queryString + key + "=" + URLEncoder.encode(params.get(key), "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new ExecutionException(e.getMessage());
}
}
if (_key != null) {
queryString = queryString + "&key=" + _key;
}
try {
debug_msg = debug_msg + "GET request: https://" + _ip + s_apiUri + URLDecoder.decode(queryString, "UTF-8") + "\n";
} catch (UnsupportedEncodingException e) {
debug_msg = debug_msg + "GET request: https://" + _ip + s_apiUri + queryString + "\n";
}
HttpGet get_request = new HttpGet("https://" + _ip + s_apiUri + queryString);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
try {
responseBody = s_httpclient.execute(get_request, responseHandler);
} catch (IOException e) {
throw new ExecutionException(e.getMessage());
}
}
// a POST method...
if (method == PaloAltoMethod.POST) {
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
for (String key : params.keySet()) {
nvps.add(new BasicNameValuePair(key, params.get(key)));
}
if (_key != null) {
nvps.add(new BasicNameValuePair("key", _key));
}
debug_msg = debug_msg + "POST request: https://" + _ip + s_apiUri + "\n";
for (NameValuePair nvp : nvps) {
debug_msg = debug_msg + "param: " + nvp.getName() + ", " + nvp.getValue() + "\n";
}
HttpPost post_request = new HttpPost("https://" + _ip + s_apiUri);
try {
post_request.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
} catch (UnsupportedEncodingException e) {
throw new ExecutionException(e.getMessage());
}
ResponseHandler<String> responseHandler = new BasicResponseHandler();
try {
responseBody = s_httpclient.execute(post_request, responseHandler);
} catch (IOException e) {
throw new ExecutionException(e.getMessage());
}
}
debug_msg = debug_msg + prettyFormat(responseBody);
// test cases
debug_msg = debug_msg + "\n" + responseBody.replace("\"", "\\\"") + "\n\n";
return responseBody;
}
use of org.apache.http.impl.client.BasicResponseHandler in project cloudstack by apache.
the class SspClient method executeMethod.
private String executeMethod(HttpRequestBase req, String path) {
try {
URI base = new URI(apiUrl);
req.setURI(new URI(base.getScheme(), base.getUserInfo(), base.getHost(), base.getPort(), path, null, null));
} catch (URISyntaxException e) {
s_logger.error("invalid API URL " + apiUrl + " path " + path, e);
return null;
}
try {
String content = null;
try {
content = getHttpClient().execute(req, new BasicResponseHandler());
s_logger.info("ssp api call: " + req);
} catch (HttpResponseException e) {
s_logger.info("ssp api call failed: " + req, e);
if (e.getStatusCode() == HttpStatus.SC_UNAUTHORIZED && login()) {
req.reset();
content = getHttpClient().execute(req, new BasicResponseHandler());
s_logger.info("ssp api retry call: " + req);
}
}
return content;
} catch (ClientProtocolException e) {
// includes HttpResponseException
s_logger.error("ssp api call failed: " + req, e);
} catch (IOException e) {
s_logger.error("ssp api call failed: " + req, e);
}
return null;
}
use of org.apache.http.impl.client.BasicResponseHandler in project lucene-solr by apache.
the class CheckBackupStatus method fetchStatus.
public void fetchStatus() throws IOException {
String masterUrl = client.getBaseURL() + "/" + coreName + ReplicationHandler.PATH + "?command=" + ReplicationHandler.CMD_DETAILS;
response = client.getHttpClient().execute(new HttpGet(masterUrl), new BasicResponseHandler());
if (pException.matcher(response).find()) {
fail("Failed to create backup");
}
if (response.contains("<str name=\"status\">success</str>")) {
Matcher m = p.matcher(response);
if (!m.find()) {
fail("could not find the completed timestamp in response.");
}
if (lastBackupTimestamp != null) {
backupTimestamp = m.group(1);
if (backupTimestamp.equals(lastBackupTimestamp)) {
success = true;
}
} else {
success = true;
}
}
}
use of org.apache.http.impl.client.BasicResponseHandler in project stanbol by apache.
the class RestfulNlpAnalysisEngine method initRESTfulNlpAnalysisService.
/**
* initialises the RESRfulNlpAnalysis if not yet done.
*/
private boolean initRESTfulNlpAnalysisService() {
if (serviceInitialised != null && serviceInitialised) {
//already initialised
return true;
}
if (serviceInitialised == null) {
log.info(" ... checking configured RESTful NLP Analysis service {}", analysisServiceUrl);
serviceInitialised = false;
} else {
log.info(" ... re-trying to initialise RESTful NLP Analysis service {}", analysisServiceUrl);
}
//get the supported languages
String supported;
try {
supported = AccessController.doPrivileged(new PrivilegedExceptionAction<String>() {
public String run() throws IOException {
HttpGet request = new HttpGet(analysisServiceUrl);
request.setHeader(HttpHeaders.ACCEPT, ContentType.APPLICATION_JSON.toString());
return httpClient.execute(request, new BasicResponseHandler());
}
});
serviceInitialised = true;
} catch (PrivilegedActionException pae) {
Exception e = pae.getException();
setRESTfulNlpAnalysisServiceUnavailable();
if (e instanceof IOException) {
log.warn("Unable to initialise RESTful NLP Analysis Service!", e);
return false;
} else {
throw RuntimeException.class.cast(e);
}
}
//NOTE: The list of supported languages is the combination of the
// languages enabled by the configuration (#languageConfig) and the
// languages supported by the RESTful NLP Analysis Service
// (#supportedLanguages)
//parse the supported languages from the initialization response
StringTokenizer st = new StringTokenizer(supported, "{[\",]}");
while (st.hasMoreElements()) {
supportedLanguages.add(st.nextToken());
}
return true;
}
Aggregations