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:utils.TestUtils.java

public static int httpGetCode(String url) throws Exception {
    CloseableHttpResponse resp = null;
    try {/*from   w w  w  .  j  a v a  2  s  .  co m*/
        HttpGet get = new HttpGet(url);
        resp = HTTP.execute(get);
        return resp.getStatusLine().getStatusCode();
    } finally {
        closeQuietly(resp);
    }
}

From source file:com.jaspersoft.studio.community.RESTCommunityHelper.java

/**
 * Tries to retrieve the content for the specified node ID.
 * //from  w w  w .  j  a v  a  2 s.  c  om
 * @param httpclient
 *            the http client
 * @param nodeID
 *            the node ID
 * @param authCookie
 *            the session cookie to use for authentication purpose
 * @return the node content as JSON
 * @throws CommunityAPIException
 */
public static JsonNode retrieveNodeContentAsJSON(CloseableHttpClient httpclient, String nodeID,
        Cookie authCookie) throws CommunityAPIException {
    try {
        HttpGet retrieveNodeContentGET = new HttpGet(
                CommunityConstants.NODE_CONTENT_URL_PREFIX + nodeID + ".json"); //$NON-NLS-1$
        CloseableHttpResponse resp = httpclient.execute(retrieveNodeContentGET);
        int httpRetCode = resp.getStatusLine().getStatusCode();
        String responseBodyAsString = EntityUtils.toString(resp.getEntity());

        if (HttpStatus.SC_OK == httpRetCode) {
            ObjectMapper mapper = new ObjectMapper();
            mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
            mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
            mapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
            JsonNode jsonRoot = mapper.readTree(responseBodyAsString);
            return jsonRoot;
        } else {
            CommunityAPIException ex = new CommunityAPIException(
                    Messages.RESTCommunityHelper_NodeContentRetrieveError);
            ex.setHttpStatusCode(httpRetCode);
            ex.setResponseBodyAsString(responseBodyAsString);
            throw ex;
        }
    } catch (IOException e) {
        JSSCommunityActivator.getDefault().logError(Messages.RESTCommunityHelper_GetMethodIOError, e);
        throw new CommunityAPIException(Messages.RESTCommunityHelper_NodeContentRetrieveError, e);
    }
}

From source file:org.owasp.benchmark.tools.BenchmarkCrawler.java

public static void sendPost(CloseableHttpClient httpclient, HttpPost post) throws Exception {
    CloseableHttpResponse response = httpclient.execute(post);
    System.out.println("POST " + post.getURI());
    try {/*from   www.  ja v a2 s  .c  om*/
        HttpEntity entity = response.getEntity();
        System.out.println("--> (" + response.getStatusLine().getStatusCode() + ") ");
        EntityUtils.consume(entity);
    } finally {
        response.close();
    }
}

From source file:com.ecofactor.qa.automation.drapi.HTTPSClient.java

/**
 * Put response./* w  w w . j  ava  2 s. c  o m*/
 * @param url the url
 * @param json the json
 * @param httpClient the http client
 * @return the http response
 */
public static synchronized HttpResponse putResponse(final String url, final String json,
        final CloseableHttpClient httpClient) {

    try {
        checkHTTPClient(httpClient);
        final HttpPut httpPut = new HttpPut(url);
        httpPut.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
        httpPut.setHeader(HttpHeaders.ACCEPT, "application/json");

        final StringEntity params = new StringEntity(json);
        httpPut.setEntity(params);

        final CloseableHttpResponse response = httpClient.execute(httpPut);
        setLogString("URL Values of the API \n" + url + "\n" + json, true);
        setLogString("Status == " + response.getStatusLine(), true);
        // setLogString("response " + response, true);
        return response;

    } catch (IOException | HTTPClientException e) {
        setLogString("Error executing HTTPS method. Reason ::: " + e, true);
        return null;
    }
}

From source file:com.meli.client.controller.AppController.java

private static void doApiCalls(String query, String countryCode) {
        List<Item> items = new ArrayList<Item>();
        Random r = new Random();

        Meli meliOb = new Meli(2072832875526076L, "1VZEFfhbSCy3vDDrh0Dp96NkfgNOWPGq");
        try {/*www  .j  a  v a2s.  c o  m*/

            FluentStringsMap params = new FluentStringsMap();
            params.add("q", query);

            String path = countryCode.isEmpty() ? "/sites/MLA/search/" : "/sites/" + countryCode + "/search";
            Response response = meliOb.get(path, params);

            ObjectMapper objectMapper = new ObjectMapper();
            JsonNode rootNode = objectMapper.readTree(response.getResponseBody());
            JsonNode resultNode = rootNode.findPath("results");

            if (resultNode.size() > 0) {
                JsonNode currNode = null;
                JsonNode dupNode = null;
                boolean dupNodeVal = false;

                CloseableHttpClient httpClient = HttpClientBuilder.create().build();

                Item item = null;

                int randomMins;

                String checkDupsUrl = null;

                HttpGet get = null;
                URIBuilder builder = null;
                URI uri = null;

                for (int i = 0; i < resultNode.size(); i++) {
                    currNode = resultNode.get(i);

                    builder = new URIBuilder();
                    builder.setScheme("http").setHost(apiUrl).setPath("/api/proxy/check")
                            .setParameter("host", "test").setParameter("itemID", currNode.get("id").asText());
                    uri = builder.build();

                    get = new HttpGet(uri);
                    get.addHeader("accept", "application/json");

                    CloseableHttpResponse res = httpClient.execute(get);
                    BufferedReader br = new BufferedReader(new InputStreamReader(res.getEntity().getContent()));
                    String content = "", line;

                    while ((line = br.readLine()) != null) {
                        content = content + line;
                    }

                    if (!content.isEmpty()) {
                        dupNode = objectMapper.readTree(content);
                        dupNodeVal = Boolean.parseBoolean(dupNode.get("isDuplicate").asText());

                        if (dupNodeVal && !allowDuplicates)
                            continue;

                        item = new Item(query, currNode.get("id").asText(), "", //currNode.get("host").asText(),?? 
                                currNode.get("site_id").asText(), currNode.get("title").asText(),
                                currNode.get("permalink").asText(), currNode.get("category_id").asText(),
                                currNode.get("seller").get("id").asText(), "", //currNode.get("seller").get("name").asText()
                                "", //currNode.get("seller").get("link").asText()
                                "", //currNode.get("seller").get("email").asText()
                                currNode.get("price").asText(), "", //currNode.get("auction_price").asText(),
                                "", //currNode.get("currency_id").asText(),
                                currNode.get("thumbnail").asText());
                        items.add(item);
                    }
                    randomMins = (int) (Math.random() * (maxWaitTime - minWaitTime)) + minWaitTime;
                    Thread.sleep(randomMins);
                }

                if (!items.isEmpty()) {
                    HttpPost post = new HttpPost(apiUrl + "/proxy/add");
                    StringEntity stringEntity = new StringEntity(objectMapper.writeValueAsString(items));

                    post.setEntity(stringEntity);
                    post.setHeader("Content-type", "application/json");

                    CloseableHttpResponse postResponse = httpClient.execute(post);
                    System.out.println("this is the reponse of the final request: "
                            + postResponse.getStatusLine().getStatusCode());
                }
            }

        } catch (MeliException ex) {
            Logger.getLogger(AppController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(AppController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InterruptedException ex) {
            Logger.getLogger(AppController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (URISyntaxException ex) {
            Logger.getLogger(AppController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

From source file:org.sead.repositories.reference.util.SEADGoogleLogin.java

static void getAuthCode() {
    access_token = null;/*  w w  w  .  j a v  a 2s . c o m*/
    expires_in = -1;
    token_start_time = -1;
    refresh_token = null;
    new File("refresh.txt").delete();

    if (gProps == null) {
        initGProps();
    }

    // Contact google for a user code
    CloseableHttpClient httpclient = HttpClients.createDefault();
    try {

        String codeUri = gProps.auth_uri.substring(0, gProps.auth_uri.length() - 4) + "device/code";

        HttpPost codeRequest = new HttpPost(codeUri);

        MultipartEntityBuilder meb = MultipartEntityBuilder.create();
        meb.addTextBody("client_id", gProps.client_id);
        meb.addTextBody("scope", "email profile");
        HttpEntity reqEntity = meb.build();

        codeRequest.setEntity(reqEntity);
        CloseableHttpResponse response = httpclient.execute(codeRequest);
        try {

            if (response.getStatusLine().getStatusCode() == 200) {
                HttpEntity resEntity = response.getEntity();
                if (resEntity != null) {
                    String responseJSON = EntityUtils.toString(resEntity);
                    ObjectNode root = (ObjectNode) new ObjectMapper().readTree(responseJSON);
                    device_code = root.get("device_code").asText();
                    user_code = root.get("user_code").asText();
                    verification_url = root.get("verification_url").asText();
                    expires_in = root.get("expires_in").asInt();
                }
            } else {
                log.error("Error response from Google: " + response.getStatusLine().getReasonPhrase());
            }
        } finally {
            response.close();
            httpclient.close();
        }
    } catch (IOException e) {
        log.error("Error reading sead-google.json or making http requests for code.");
        log.error(e.getMessage());
    }
}

From source file:com.networknt.light.server.handler.loader.FormLoader.java

private static void loadFormFile(String host, File file) {
    Scanner scan = null;//from w  w  w  .ja  v  a2 s  .  c om
    try {
        scan = new Scanner(file, Loader.encoding);
        // the content is only the data portion.
        String content = scan.useDelimiter("\\Z").next();
        // convert to map
        Map<String, Object> newMap = ServiceLocator.getInstance().getMapper().readValue(content,
                new TypeReference<HashMap<String, Object>>() {
                });
        String formId = (String) newMap.get("formId");
        Map<String, Object> oldMap = (Map<String, Object>) formMap.get(formId);
        boolean changed = false;
        if (oldMap == null) {
            // never loaded before.
            changed = true;
        } else {
            if (newMap.get("action") != null && !newMap.get("action").equals(oldMap.get("action"))) {
                changed = true;
            }
            if (!changed && newMap.get("schema") != null
                    && !newMap.get("schema").equals(oldMap.get("schema"))) {
                changed = true;
            }
            if (!changed && newMap.get("form") != null && !newMap.get("form").equals(oldMap.get("form"))) {
                changed = true;
            }
            if (!changed && newMap.get("modelData") != null
                    && !newMap.get("modelData").equals(oldMap.get("modelData"))) {
                changed = true;
            }
        }
        if (changed) {
            Map<String, Object> inputMap = new HashMap<String, Object>();
            inputMap.put("category", "form");
            inputMap.put("name", "impForm");
            inputMap.put("readOnly", false);

            Map<String, Object> data = new HashMap<String, Object>();
            data.put("content", content);
            inputMap.put("data", data);
            HttpPost httpPost = new HttpPost(host + "/api/rs");
            httpPost.addHeader("Authorization", "Bearer " + jwt);
            StringEntity input = new StringEntity(
                    ServiceLocator.getInstance().getMapper().writeValueAsString(inputMap));
            input.setContentType("application/json");
            httpPost.setEntity(input);
            CloseableHttpResponse response = httpclient.execute(httpPost);

            try {
                System.out.println("Form: " + file.getAbsolutePath() + " is loaded with status "
                        + response.getStatusLine());
                HttpEntity entity = response.getEntity();
                BufferedReader rd = new BufferedReader(new InputStreamReader(entity.getContent()));
                String json = "";
                String line = "";
                while ((line = rd.readLine()) != null) {
                    json = json + line;
                }
                //System.out.println("json = " + json);
                EntityUtils.consume(entity);
            } finally {
                response.close();
            }
        }
    } catch (IOException ioe) {
        ioe.printStackTrace();
    } finally {
        if (scan != null)
            scan.close();
    }
}

From source file:com.clonephpscrapper.utility.FetchPageWithProxy.java

public static String fetchPageSourcefromClientGoogle(URI newurl) throws IOException, InterruptedException {

    int portNo = generateRandomPort();
    CredentialsProvider credsprovider = new BasicCredentialsProvider();
    credsprovider.setCredentials(new AuthScope("195.154.161.103", portNo),
            new UsernamePasswordCredentials("mongoose", "Fjh30fi"));
    HttpHost proxy = new HttpHost("195.154.161.103", portNo);
    //-----------------------------------------------------------------------
    RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(5000).setConnectTimeout(5000)
            .setConnectionRequestTimeout(5000).build();

    CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsprovider)
            .setUserAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0")
            .setDefaultRequestConfig(requestConfig).setProxy(proxy).build();
    String responsebody = "";
    String responsestatus = null;
    int count = 0;
    try {// w  ww . j  a  v a  2 s .c o  m
        HttpGet httpget = new HttpGet(newurl);
        httpget.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
        httpget.addHeader("Accept-Encoding", "gzip, deflate");
        httpget.addHeader("Accept-Language", "en-US,en;q=0.5");
        httpget.addHeader("Connection", "keep-alive");

        System.out.println("Response status " + httpget.getRequestLine());
        CloseableHttpResponse resp = httpclient.execute(httpget);
        responsestatus = resp.getStatusLine().toString();
        if (responsestatus.contains("503") || responsestatus.contains("502") || responsestatus.contains("403")
                || responsestatus.contains("400") || responsestatus.contains("407")
                || responsestatus.contains("401") || responsestatus.contains("402")
                || responsestatus.contains("404") || responsestatus.contains("405")
                || responsestatus.contains("SSLHandshakeException") || responsestatus.contains("999")
                || responsestatus.contains("ClientProtocolException")
                || responsestatus.contains("SocketTimeoutException") || "".equals(responsestatus)) {
            Thread.sleep(10000);
            do {
                count++;
                responsebody = fetchPageSourcefromClientGoogleSecond(newurl);
                if (responsebody == null) {
                    Thread.sleep(10000);
                    System.out.println("PROX FAILURE");
                }
                if (count > 20) {
                    Thread.sleep(1000);
                    break;
                }
            } while (responsebody == null || "".equals(responsebody));
        } else {
            HttpEntity entity = resp.getEntity();
            System.out.println(resp.getStatusLine());
            if (entity != null) {
                System.out.println("Response content length: " + entity.getContentLength());
                BufferedReader in = new BufferedReader(new InputStreamReader(entity.getContent()));
                String inputLine;
                while ((inputLine = in.readLine()) != null) {
                    responsebody = new StringBuilder().append(responsebody).append(inputLine).toString();
                }
                // writeResponseFile(responsebody, pagename);
            }
            EntityUtils.consume(entity);
        }
    } catch (IOException | IllegalStateException e) {
        System.out.println("Exception = " + e);
        do {
            count++;
            responsebody = fetchPageSourcefromClientGoogleSecond(newurl);
            if (responsebody == null) {
                System.out.println("PROX FAILURE");
            }
            if (count > 15) {
                Thread.sleep(50000);
                //                    responsebody = fetchPageSourcefromClientGoogleSecond(newurl);
                break;
            }
        } while (responsebody == null || "".equals(responsebody));
    } finally {
        httpclient.close();
    }
    return responsebody;
}

From source file:com.jaspersoft.studio.community.RESTCommunityHelper.java

/**
 * Executes the authentication to the Jaspersoft community in order to
 * retrieve the session cookie to use later for all other operations.
 * /*w  w  w.  j a  va  2  s. co  m*/
 * @param httpclient
 *            the http client
 * 
 * @param cookieStore
 *            the Cookie Store instance
 * @param username
 *            the community user name (or email)
 * @param password
 *            the community user password
 * @return the authentication cookie if able to retrieve it,
 *         <code>null</code> otherwise
 * @throws CommunityAPIException
 */
public static Cookie getAuthenticationCookie(CloseableHttpClient httpclient, CookieStore cookieStore,
        String username, String password) throws CommunityAPIException {

    try {
        HttpPost loginPOST = new HttpPost(CommunityConstants.LOGIN_URL);
        EntityBuilder loginEntity = EntityBuilder.create();
        loginEntity.setText("{ \"username\": \"" + username + "\", \"password\":\"" + password + "\" }"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        loginEntity.setContentType(ContentType.create(CommunityConstants.JSON_CONTENT_TYPE));
        loginEntity.setContentEncoding(CommunityConstants.REQUEST_CHARSET);
        loginPOST.setEntity(loginEntity.build());

        CloseableHttpResponse resp = httpclient.execute(loginPOST);
        int httpRetCode = resp.getStatusLine().getStatusCode();
        String responseBodyAsString = EntityUtils.toString(resp.getEntity());
        if (HttpStatus.SC_OK == httpRetCode) {
            // Can proceed
            List<Cookie> cookies = cookieStore.getCookies();
            Cookie authCookie = null;
            for (Cookie cookie : cookies) {
                if (cookie.getName().startsWith("SESS")) { //$NON-NLS-1$
                    authCookie = cookie;
                    break;
                }
            }
            return authCookie;
        } else if (HttpStatus.SC_UNAUTHORIZED == httpRetCode) {
            // Unauthorized... wrong username or password
            CommunityAPIException unauthorizedEx = new CommunityAPIException(
                    Messages.RESTCommunityHelper_WrongUsernamePasswordError);
            unauthorizedEx.setHttpStatusCode(httpRetCode);
            unauthorizedEx.setResponseBodyAsString(responseBodyAsString);
            throw unauthorizedEx;
        } else {
            // Some other problem occurred
            CommunityAPIException generalEx = new CommunityAPIException(
                    Messages.RESTCommunityHelper_AuthInfoProblemsError);
            generalEx.setHttpStatusCode(httpRetCode);
            generalEx.setResponseBodyAsString(responseBodyAsString);
            throw generalEx;
        }
    } catch (UnsupportedEncodingException e) {
        JSSCommunityActivator.getDefault().logError(Messages.RESTCommunityHelper_EncodingNotValidError, e);
        throw new CommunityAPIException(Messages.RESTCommunityHelper_AuthenticationError, e);
    } catch (IOException e) {
        JSSCommunityActivator.getDefault().logError(Messages.RESTCommunityHelper_PostMethodIOError, e);
        throw new CommunityAPIException(Messages.RESTCommunityHelper_AuthenticationError, e);
    }
}

From source file:nl.knaw.dans.easy.sword2examples.SimpleDeposit.java

public static URI depositPackage(File bagDir, IRI colIri, String uid, String pw) throws Exception {
    // 0. Zip the bagDir
    File zipFile = new File(bagDir.getAbsolutePath() + ".zip");
    zipFile.delete();//www.j a  v a  2s  .  c  o m
    Common.zipDirectory(bagDir, zipFile);

    // 1. Set up stream for calculating MD5
    MessageDigest md = MessageDigest.getInstance("MD5");
    try (FileInputStream fis = new FileInputStream(zipFile);
            DigestInputStream dis = new DigestInputStream(fis, md)) {

        // 2. Post entire bag to Col-IRI
        CloseableHttpClient http = Common.createHttpClient(colIri.toURI(), uid, pw);
        CloseableHttpResponse response = Common.sendChunk(dis, (int) zipFile.length(), "POST", colIri.toURI(),
                "bag.zip", "application/zip", http, false);

        // 3. Check the response. If transfer corrupt (MD5 doesn't check out), report and exit.
        String bodyText = Common.readEntityAsString(response.getEntity());
        if (response.getStatusLine().getStatusCode() != 201) {
            System.err.println("FAILED. Status = " + response.getStatusLine());
            System.err.println("Response body follows:");
            System.err.println(bodyText);
            System.exit(2);
        }
        System.out.println("SUCCESS. Deposit receipt follows:");
        System.out.println(bodyText);

        // 4. Get the statement URL. This is the URL from which to retrieve the current status of the deposit.
        System.out.println("Retrieving Statement IRI (Stat-IRI) from deposit receipt ...");
        Entry receipt = Common.parse(bodyText);
        Link statLink = receipt.getLink("http://purl.org/net/sword/terms/statement");
        IRI statIri = statLink.getHref();
        System.out.println("Stat-IRI = " + statIri);

        // 5. Check statement every ten seconds (a bit too frantic, but okay for this test). If status changes:
        // report new status. If status is an error (INVALID, REJECTED, FAILED) or ARCHIVED: exit.
        return Common.trackDeposit(http, statIri.toURI());
    }
}