Search in sources :

Example 21 with BasicResponseHandler

use of org.apache.http.impl.client.BasicResponseHandler in project ddf by codice.

the class LogoutMessageImpl method sendSamlLogoutRequest.

@Override
public String sendSamlLogoutRequest(@NotNull LogoutRequest request, @NotNull String targetUri) throws IOException, WSSecurityException {
    Element requestElement = getElementFromSaml(request);
    String requestMessage = DOM2Writer.nodeToString(requestElement);
    try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
        HttpPost post = new HttpPost(targetUri);
        post.addHeader("Cache-Control", "no-cache, no-store");
        post.addHeader("Pragma", "no-cache");
        post.addHeader("SOAPAction", SAML_SOAP_ACTION);
        post.setEntity(new StringEntity(requestMessage, "utf-8"));
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        return httpClient.execute(post, responseHandler);
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) Element(org.w3c.dom.Element) BasicResponseHandler(org.apache.http.impl.client.BasicResponseHandler)

Example 22 with BasicResponseHandler

use of org.apache.http.impl.client.BasicResponseHandler in project libresonic by Libresonic.

the class VersionService method readLatestVersion.

/**
     * Resolves the latest available Libresonic version by screen-scraping a web page.
     *
     * @throws IOException If an I/O error occurs.
     */
private void readLatestVersion() throws IOException {
    RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(10000).setSocketTimeout(10000).build();
    HttpGet method = new HttpGet(VERSION_URL + "?v=" + getLocalVersion());
    method.setConfig(requestConfig);
    String content;
    try (CloseableHttpClient client = HttpClients.createDefault()) {
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        content = client.execute(method, responseHandler);
    }
    Pattern finalPattern = Pattern.compile("LIBRESONIC_FULL_VERSION_BEGIN(.*)LIBRESONIC_FULL_VERSION_END");
    Pattern betaPattern = Pattern.compile("LIBRESONIC_BETA_VERSION_BEGIN(.*)LIBRESONIC_BETA_VERSION_END");
    try (BufferedReader reader = new BufferedReader(new StringReader(content))) {
        String line = reader.readLine();
        while (line != null) {
            Matcher finalMatcher = finalPattern.matcher(line);
            if (finalMatcher.find()) {
                latestFinalVersion = new Version(finalMatcher.group(1));
                LOG.info("Resolved latest Libresonic final version to: " + latestFinalVersion);
            }
            Matcher betaMatcher = betaPattern.matcher(line);
            if (betaMatcher.find()) {
                latestBetaVersion = new Version(betaMatcher.group(1));
                LOG.info("Resolved latest Libresonic beta version to: " + latestBetaVersion);
            }
            line = reader.readLine();
        }
    }
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) Version(org.libresonic.player.domain.Version) HttpGet(org.apache.http.client.methods.HttpGet) BasicResponseHandler(org.apache.http.impl.client.BasicResponseHandler)

Example 23 with BasicResponseHandler

use of org.apache.http.impl.client.BasicResponseHandler in project weixin-java-tools by chanjarster.

the class WxCpServiceImpl method getAccessToken.

public String getAccessToken(boolean forceRefresh) throws WxErrorException {
    if (forceRefresh) {
        wxCpConfigStorage.expireAccessToken();
    }
    if (wxCpConfigStorage.isAccessTokenExpired()) {
        synchronized (globalAccessTokenRefreshLock) {
            if (wxCpConfigStorage.isAccessTokenExpired()) {
                String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?" + "&corpid=" + wxCpConfigStorage.getCorpId() + "&corpsecret=" + wxCpConfigStorage.getCorpSecret();
                try {
                    HttpGet httpGet = new HttpGet(url);
                    if (httpProxy != null) {
                        RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
                        httpGet.setConfig(config);
                    }
                    CloseableHttpClient httpclient = getHttpclient();
                    String resultContent = null;
                    try (CloseableHttpResponse response = httpclient.execute(httpGet)) {
                        resultContent = new BasicResponseHandler().handleResponse(response);
                    }
                    WxError error = WxError.fromJson(resultContent);
                    if (error.getErrorCode() != 0) {
                        throw new WxErrorException(error);
                    }
                    WxAccessToken accessToken = WxAccessToken.fromJson(resultContent);
                    wxCpConfigStorage.updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
                } catch (ClientProtocolException e) {
                    throw new RuntimeException(e);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
    return wxCpConfigStorage.getAccessToken();
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) WxError(me.chanjar.weixin.common.bean.result.WxError) WxAccessToken(me.chanjar.weixin.common.bean.WxAccessToken) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) BasicResponseHandler(org.apache.http.impl.client.BasicResponseHandler) IOException(java.io.IOException) WxErrorException(me.chanjar.weixin.common.exception.WxErrorException) ClientProtocolException(org.apache.http.client.ClientProtocolException)

Example 24 with BasicResponseHandler

use of org.apache.http.impl.client.BasicResponseHandler in project cw-andtutorials by commonsguy.

the class Patchy method updateStatus.

private void updateStatus() {
    try {
        String s = status.getText().toString();
        HttpPost post = new HttpPost("https://identi.ca/api/statuses/update.json");
        post.addHeader("Authorization", "Basic " + getCredentials());
        List<NameValuePair> form = new ArrayList<NameValuePair>();
        form.add(new BasicNameValuePair("status", s));
        post.setEntity(new UrlEncodedFormEntity(form, HTTP.UTF_8));
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        String responseBody = client.execute(post, responseHandler);
        JSONObject response = new JSONObject(responseBody);
    } catch (Throwable t) {
        Log.e("Patchy", "Exception in updateStatus()", t);
        goBlooey(t);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) JSONObject(org.json.JSONObject) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) ArrayList(java.util.ArrayList) BasicResponseHandler(org.apache.http.impl.client.BasicResponseHandler) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity)

Example 25 with BasicResponseHandler

use of org.apache.http.impl.client.BasicResponseHandler in project musiccabinet by hakko.

the class AbstractWSGetClient method invokeSingleCall.

/*
	 * Make a single call to a Last.fm web service, and return a packaged result.
	 */
private WSResponse invokeSingleCall(List<NameValuePair> params) throws ApplicationException {
    if (throttleService != null) {
        throttleService.awaitAllowance();
    }
    WSResponse wsResponse;
    HttpClient httpClient = getHttpClient();
    try {
        HttpGet httpGet = new HttpGet(getURI(params));
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        String responseBody = httpClient.execute(httpGet, responseHandler);
        wsResponse = new WSResponse(responseBody);
    } catch (HttpResponseException e) {
        wsResponse = new WSResponse(isHttpRecoverable(e.getStatusCode()), e.getStatusCode(), e.getMessage());
    } catch (IOException e) {
        LOG.warn("Could not fetch data from Last.fm!", e);
        wsResponse = new WSResponse(true, -1, "Call failed due to " + e.getMessage());
    }
    return wsResponse;
}
Also used : HttpClient(org.apache.http.client.HttpClient) HttpGet(org.apache.http.client.methods.HttpGet) BasicResponseHandler(org.apache.http.impl.client.BasicResponseHandler) HttpResponseException(org.apache.http.client.HttpResponseException) IOException(java.io.IOException)

Aggregations

BasicResponseHandler (org.apache.http.impl.client.BasicResponseHandler)40 HttpGet (org.apache.http.client.methods.HttpGet)27 Test (org.junit.Test)15 IOException (java.io.IOException)11 HttpPost (org.apache.http.client.methods.HttpPost)10 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)8 ArrayList (java.util.ArrayList)7 HttpClient (org.apache.http.client.HttpClient)7 NameValuePair (org.apache.http.NameValuePair)6 ClientProtocolException (org.apache.http.client.ClientProtocolException)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 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