Example usage for org.apache.http.client.methods CloseableHttpResponse getStatusLine

List of usage examples for org.apache.http.client.methods CloseableHttpResponse getStatusLine

Introduction

In this page you can find the example usage for org.apache.http.client.methods CloseableHttpResponse getStatusLine.

Prototype

StatusLine getStatusLine();

Source Link

Usage

From source file:eu.diacron.crawlservice.app.Util.java

public static void getAllCrawls() {
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
            new UsernamePasswordCredentials(Configuration.REMOTE_CRAWLER_USERNAME,
                    Configuration.REMOTE_CRAWLER_PASS));
    CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
    try {//from w  ww  .  ja  v  a 2 s  .co m
        //HttpGet httpget = new HttpGet("http://diachron.hanzoarchives.com/crawl");
        HttpGet httpget = new HttpGet(Configuration.REMOTE_CRAWLER_URL_CRAWL);

        System.out.println("Executing request " + httpget.getRequestLine());
        CloseableHttpResponse response = httpclient.execute(httpget);
        try {
            System.out.println("----------------------------------------");
            System.out.println(response.getStatusLine());
            System.out.println("----------------------------------------");

            System.out.println(response.getEntity().getContent());

            BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            String inputLine;
            while ((inputLine = in.readLine()) != null) {
                System.out.println(inputLine);
            }
            in.close();
            EntityUtils.consume(response.getEntity());
        } finally {
            response.close();
        }
    } catch (IOException ex) {
        Logger.getLogger(Util.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        try {
            httpclient.close();
        } catch (IOException ex) {
            Logger.getLogger(Util.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

From source file:org.apache.ofbiz.passport.user.GitHubAuthenticator.java

public static Map<String, Object> getUserInfo(HttpGet httpGet, String accessToken, String tokenType,
        Locale locale) throws AuthenticatorException {
    JSON userInfo = null;/*w  w w. j  a v  a  2s.  co  m*/
    httpGet.setConfig(PassportUtil.StandardRequestConfig);
    CloseableHttpClient jsonClient = HttpClients.custom().build();
    httpGet.setHeader(PassportUtil.AUTHORIZATION_HEADER, tokenType + " " + accessToken);
    httpGet.setHeader(PassportUtil.ACCEPT_HEADER, "application/json");
    CloseableHttpResponse getResponse = null;
    try {
        getResponse = jsonClient.execute(httpGet);
        String responseString = new BasicResponseHandler().handleResponse(getResponse);
        if (getResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            // Debug.logInfo("Json Response from GitHub: " + responseString, module);
            userInfo = JSON.from(responseString);
        } else {
            String errMsg = UtilProperties.getMessage(resource, "GetOAuth2AccessTokenError",
                    UtilMisc.toMap("error", responseString), locale);
            throw new AuthenticatorException(errMsg);
        }
    } catch (ClientProtocolException e) {
        throw new AuthenticatorException(e.getMessage());
    } catch (IOException e) {
        throw new AuthenticatorException(e.getMessage());
    } finally {
        if (getResponse != null) {
            try {
                getResponse.close();
            } catch (IOException e) {
                // do nothing
            }
        }
    }
    JSONToMap jsonMap = new JSONToMap();
    Map<String, Object> userMap;
    try {
        userMap = jsonMap.convert(userInfo);
    } catch (ConversionException e) {
        throw new AuthenticatorException(e.getMessage());
    }
    return userMap;
}

From source file:org.coding.git.api.CodingNetConnection.java

@NotNull
private static CodingNetStatusCodeException getStatusCodeException(@NotNull CloseableHttpResponse response) {
    StatusLine statusLine = response.getStatusLine();
    try {//from   www  .  j  a v a2  s  .c  o  m
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            CodingNetErrorMessage error = fromJson(parseResponse(entity.getContent()),
                    CodingNetErrorMessage.class);
            String message = statusLine.getReasonPhrase() + " - " + error.getMessage();
            return new CodingNetStatusCodeException(message, error, statusLine.getStatusCode());
        }
    } catch (IOException e) {
        LOG.info(e);
    }
    return new CodingNetStatusCodeException(statusLine.getReasonPhrase(), statusLine.getStatusCode());
}

From source file:org.dashbuilder.dataprovider.backend.elasticsearch.ElasticSearchDataSetTestBase.java

protected static Object[] doGet(String url) throws Exception {
    Object[] response = null;//  w w w. j  a  va 2  s .co  m
    if (url == null || url.trim().length() == 0)
        return response;

    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpGet httpGet = new HttpGet(url);
    CloseableHttpResponse response1 = httpclient.execute(httpGet);
    try {
        HttpEntity entity1 = response1.getEntity();
        String responseBody = responseAsString(response1);
        int responseStatus = response1.getStatusLine().getStatusCode();
        response = new Object[] { responseStatus, responseBody };

        // do something useful with the response body
        // and ensure it is fully consumed
        EntityUtils.consume(entity1);
    } finally {
        response1.close();
    }

    return response;
}

From source file:org.wuspba.ctams.ws.ITBandResultController.java

protected static void delete() throws Exception {
    CloseableHttpClient httpclient = HttpClients.createDefault();

    URI uri = new URIBuilder().setScheme(PROTOCOL).setHost(HOST).setPort(PORT).setPath(PATH)
            .setParameter("id", TestFixture.INSTANCE.bandResult.getId()).build();

    HttpDelete httpDelete = new HttpDelete(uri);

    CloseableHttpResponse response = null;

    try {//  w w  w .  j av a 2  s.c om
        response = httpclient.execute(httpDelete);

        assertEquals(IntegrationTestUtils.OK_STRING, response.getStatusLine().toString());

        HttpEntity responseEntity = response.getEntity();

        EntityUtils.consume(responseEntity);
    } catch (UnsupportedEncodingException ex) {
        LOG.error("Unsupported coding", ex);
    } catch (IOException ioex) {
        LOG.error("IOException", ioex);
    } finally {
        if (response != null) {
            try {
                response.close();
            } catch (IOException ex) {
                LOG.error("Could not close response", ex);
            }
        }
    }

    ITBandController.delete();
    ITBandContestController.delete();
}

From source file:eu.diacron.crawlservice.app.Util.java

public static String getCrawlid(URL urltoCrawl) {
    String crawlid = "";
    System.out.println("start crawling page");

    CredentialsProvider credsProvider = new BasicCredentialsProvider();

    credsProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
            new UsernamePasswordCredentials(Configuration.REMOTE_CRAWLER_USERNAME,
                    Configuration.REMOTE_CRAWLER_PASS));
    CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
    try {/*from  w  w  w.ja va 2  s .co m*/
        //HttpPost httppost = new HttpPost("http://diachron.hanzoarchives.com/crawl");
        HttpPost httppost = new HttpPost(Configuration.REMOTE_CRAWLER_URL_CRAWL_INIT);

        List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
        urlParameters.add(new BasicNameValuePair("name", UUID.randomUUID().toString()));
        urlParameters.add(new BasicNameValuePair("scope", "page"));
        urlParameters.add(new BasicNameValuePair("seed", urltoCrawl.toString()));

        httppost.setEntity(new UrlEncodedFormEntity(urlParameters));

        System.out.println("Executing request " + httppost.getRequestLine());
        CloseableHttpResponse response = httpclient.execute(httppost);
        try {
            System.out.println("----------------------------------------");
            System.out.println(response.getStatusLine());
            System.out.println("----------------------------------------");

            BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            String inputLine;
            while ((inputLine = in.readLine()) != null) {
                crawlid = inputLine;
            }
            in.close();
            EntityUtils.consume(response.getEntity());
        } finally {
            response.close();
        }
    } catch (IOException ex) {
        Logger.getLogger(Util.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        try {
            httpclient.close();
        } catch (IOException ex) {
            Logger.getLogger(Util.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    return crawlid;
}

From source file:org.wuspba.ctams.ws.ITBandController.java

private static void add(Band band) throws Exception {
    CTAMSDocument doc = new CTAMSDocument();
    doc.getBands().add(band);/*from   w w  w  .j ava2s  .  c  o  m*/
    String xml = XMLUtils.marshal(doc);

    CloseableHttpClient httpclient = HttpClients.createDefault();

    URI uri = new URIBuilder().setScheme(PROTOCOL).setHost(HOST).setPort(PORT).setPath(PATH).build();

    HttpPost httpPost = new HttpPost(uri);

    StringEntity xmlEntity = new StringEntity(xml, ContentType.APPLICATION_XML);

    CloseableHttpResponse response = null;

    try {
        httpPost.setEntity(xmlEntity);
        response = httpclient.execute(httpPost);

        assertEquals(IntegrationTestUtils.OK_STRING, response.getStatusLine().toString());

        HttpEntity responseEntity = response.getEntity();

        doc = IntegrationTestUtils.convertEntity(responseEntity);

        TestFixture.INSTANCE.skye.setId(doc.getBands().get(0).getId());

        EntityUtils.consume(responseEntity);
    } catch (UnsupportedEncodingException ex) {
        LOG.error("Unsupported coding", ex);
    } catch (IOException ioex) {
        LOG.error("IOException", ioex);
    } finally {
        if (response != null) {
            try {
                response.close();
            } catch (IOException ex) {
                LOG.error("Could not close response", ex);
            }
        }
    }
}

From source file:com.ibm.mobilefirstplatform.serversdk.java.push.PushNotifications.java

protected static void sendResponseToListener(CloseableHttpResponse response,
        PushNotificationsResponseListener listener) throws IOException {
    String responseBody = null;//from w w w . j  a  va 2s. c o m

    if (response.getEntity() != null) {
        ByteArrayOutputStream outputAsByteArray = new ByteArrayOutputStream();
        response.getEntity().writeTo(outputAsByteArray);

        responseBody = new String(outputAsByteArray.toByteArray());
    }

    Integer statusCode = null;

    if (response.getStatusLine() != null) {
        statusCode = response.getStatusLine().getStatusCode();
    }

    if (statusCode != null && statusCode == HttpStatus.SC_ACCEPTED) {
        listener.onSuccess(statusCode, responseBody);
    } else {
        listener.onFailure(statusCode, responseBody, null);
    }
}

From source file:eu.diacron.crawlservice.app.Util.java

public static String getCrawlStatusById(String crawlid) {

    String status = "";
    System.out.println("get crawlid");

    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
            new UsernamePasswordCredentials(Configuration.REMOTE_CRAWLER_USERNAME,
                    Configuration.REMOTE_CRAWLER_PASS));

    CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
    try {/* w w w.  ja  v  a  2 s .c o m*/
        //HttpGet httpget = new HttpGet("http://diachron.hanzoarchives.com/crawl/" + crawlid);
        HttpGet httpget = new HttpGet(Configuration.REMOTE_CRAWLER_URL_CRAWL + crawlid);

        System.out.println("Executing request " + httpget.getRequestLine());
        CloseableHttpResponse response = httpclient.execute(httpget);
        try {
            System.out.println("----------------------------------------");
            System.out.println(response.getStatusLine());
            System.out.println("----------------------------------------");

            String result = "";

            BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            String inputLine;
            while ((inputLine = in.readLine()) != null) {
                System.out.println(inputLine);
                result += inputLine;
            }
            in.close();

            // TO-DO should be removed in the future and handle it more gracefully
            result = result.replace("u'", "'");
            result = result.replace("'", "\"");

            JSONObject crawljson = new JSONObject(result);
            System.out.println("myObject " + crawljson.toString());

            status = crawljson.getString("status");

            EntityUtils.consume(response.getEntity());
        } catch (JSONException ex) {
            Logger.getLogger(Util.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            response.close();
        }
    } catch (IOException ex) {
        Logger.getLogger(Util.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        try {
            httpclient.close();
        } catch (IOException ex) {
            Logger.getLogger(Util.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    return status;
}

From source file:eu.diacron.crawlservice.app.Util.java

public static JSONArray getwarcsByCrawlid(String crawlid) {

    JSONArray warcsArray = null;//from  www. j a  va2s.  c o  m
    System.out.println("get crawlid");

    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    /*        credsProvider.setCredentials(
     new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
     new UsernamePasswordCredentials("diachron", "7nD9dNGshTtficn"));
     */

    credsProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
            new UsernamePasswordCredentials(Configuration.REMOTE_CRAWLER_USERNAME,
                    Configuration.REMOTE_CRAWLER_PASS));
    CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
    try {

        //HttpGet httpget = new HttpGet("http://diachron.hanzoarchives.com/warcs/" + crawlid);
        HttpGet httpget = new HttpGet(Configuration.REMOTE_CRAWLER_URL + crawlid);

        System.out.println("Executing request " + httpget.getRequestLine());
        CloseableHttpResponse response = httpclient.execute(httpget);
        try {
            System.out.println("----------------------------------------");
            System.out.println(response.getStatusLine());
            System.out.println("----------------------------------------");

            String result = "";

            BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            String inputLine;
            while ((inputLine = in.readLine()) != null) {
                System.out.println(inputLine);
                result += inputLine;
            }
            in.close();

            result = result.replace("u'", "'");
            result = result.replace("'", "\"");

            warcsArray = new JSONArray(result);

            for (int i = 0; i < warcsArray.length(); i++) {

                System.out.println("url to download: " + warcsArray.getString(i));

            }

            EntityUtils.consume(response.getEntity());
        } catch (JSONException ex) {
            Logger.getLogger(Util.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            response.close();
        }
    } catch (IOException ex) {
        Logger.getLogger(Util.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        try {
            httpclient.close();
        } catch (IOException ex) {
            Logger.getLogger(Util.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    return warcsArray;
}