Search in sources :

Example 36 with BasicResponseHandler

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;
}
Also used : NameValuePair(org.apache.http.NameValuePair) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) HttpPost(org.apache.http.client.methods.HttpPost) HttpGet(org.apache.http.client.methods.HttpGet) BasicResponseHandler(org.apache.http.impl.client.BasicResponseHandler) ArrayList(java.util.ArrayList) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) ExecutionException(com.cloud.utils.exception.ExecutionException)

Example 37 with BasicResponseHandler

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;
}
Also used : BasicResponseHandler(org.apache.http.impl.client.BasicResponseHandler) HttpResponseException(org.apache.http.client.HttpResponseException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) URI(java.net.URI) ClientProtocolException(org.apache.http.client.ClientProtocolException)

Example 38 with BasicResponseHandler

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;
        }
    }
}
Also used : Matcher(java.util.regex.Matcher) HttpGet(org.apache.http.client.methods.HttpGet) BasicResponseHandler(org.apache.http.impl.client.BasicResponseHandler)

Example 39 with BasicResponseHandler

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;
}
Also used : StringTokenizer(java.util.StringTokenizer) PrivilegedActionException(java.security.PrivilegedActionException) HttpGet(org.apache.http.client.methods.HttpGet) BasicResponseHandler(org.apache.http.impl.client.BasicResponseHandler) PrivilegedExceptionAction(java.security.PrivilegedExceptionAction) IOException(java.io.IOException) HttpException(org.apache.http.HttpException) ClientProtocolException(org.apache.http.client.ClientProtocolException) PrivilegedActionException(java.security.PrivilegedActionException) IOException(java.io.IOException) EngineException(org.apache.stanbol.enhancer.servicesapi.EngineException) URISyntaxException(java.net.URISyntaxException) ConfigurationException(org.osgi.service.cm.ConfigurationException) HttpResponseException(org.apache.http.client.HttpResponseException)

Aggregations

BasicResponseHandler (org.apache.http.impl.client.BasicResponseHandler)39 HttpGet (org.apache.http.client.methods.HttpGet)26 Test (org.junit.Test)15 IOException (java.io.IOException)10 HttpPost (org.apache.http.client.methods.HttpPost)10 ArrayList (java.util.ArrayList)7 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)7 NameValuePair (org.apache.http.NameValuePair)6 HttpClient (org.apache.http.client.HttpClient)6 HttpResponseException (org.apache.http.client.HttpResponseException)6 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)6 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)6 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)6 ClientProtocolException (org.apache.http.client.ClientProtocolException)5 RequestConfig (org.apache.http.client.config.RequestConfig)5 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)4 BasicClientCookie (org.apache.http.impl.cookie.BasicClientCookie)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2