Example usage for org.apache.http.client.methods RequestBuilder get

List of usage examples for org.apache.http.client.methods RequestBuilder get

Introduction

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

Prototype

public static RequestBuilder get() 

Source Link

Usage

From source file:org.outofbits.sesame.schemagen.SchemaGeneration.java

/**
 * Requests the RDF file of the vocabulary by using the given HTTP {@link java.net.URL}. The
 * header-entry ACCEPT contains all supported {@link RDFFormat}s, where the given
 * {@link RDFFormat} is the preferred one. The responded rdf file will be parsed and a
 * {@link Model} containing all statements will be returned. An {@link IOException} will be
 * thrown, if the rdf file cannot be accessed or read. {@link URISyntaxException} will be thrown,
 * if the given {@link java.net.URL} has a syntax error.
 *
 * @param url    the url, where the vocabulary is located and accessible by using HTTP. It must
 *               not be null./*from w  w  w . java2 s.  co m*/
 * @param format the format of the document representing the vocabulary. The format can be null.
 * @return {@link Model} containing all statements of the vocabulary located at the given
 * {@link java.net.URL}.
 * @throws IOException        if the rdf file cannot be accessed or read.
 * @throws URISyntaxException if the given {@link java.net.URL} has a syntax error.
 */
private Model readVocabularyFromHTTPSource(java.net.URL url, RDFFormat format)
        throws IOException, URISyntaxException {
    assert url != null && url.getProtocol().equals("http");
    HttpClientBuilder clientBuilder = HttpClientBuilder.create().setUserAgent(HTTP_USER_AGENT);
    HttpUriRequest vocabularyGETRequest = RequestBuilder.get().setUri(url.toURI())
            .setHeader(HttpHeaders.ACCEPT,
                    String.join(", ", RDFFormat.getAcceptParams(supportedRDFFormats.values(), false, format)))
            .build();
    try (CloseableHttpClient client = clientBuilder.build()) {
        CloseableHttpResponse response = client.execute(vocabularyGETRequest);
        if (response.getStatusLine().getStatusCode() != 200) {
            throw new IOException(String.format("The given vocabulary can not requested from '%s'. %d: %s", url,
                    response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase()));
        }
        String responseContentType = response.getEntity().getContentType().getValue();
        Optional<RDFFormat> optionalResponseRdfFormat = Rio.getParserFormatForMIMEType(responseContentType);
        try (InputStream vocabResponseStream = response.getEntity().getContent()) {
            if (optionalResponseRdfFormat.isPresent()) {
                return Rio.parse(vocabResponseStream, "", optionalResponseRdfFormat.get());
            } else {
                if (format == null) {
                    throw new IOException(
                            String.format("The returned content type (%s) from '%s' is not supported",
                                    responseContentType, url));
                }
                try {
                    return Rio.parse(vocabResponseStream, "", format);
                } catch (RDFParseException | UnsupportedRDFormatException e) {
                    throw new IOException(String.format(
                            "The returned content type (%s) from '%s' is not supported. Fallback to the given format %s, but an error occurred.",
                            responseContentType, url, format), e);
                }
            }
        }
    }
}

From source file:com.searchbox.collection.oppfin.IdealISTCollection.java

public ItemProcessor<String, FieldMap> itemProcessor() {
    return new ItemProcessor<String, FieldMap>() {
        @Override//  ww w .  j a  v  a  2  s.  co  m
        public FieldMap process(String uid) throws Exception {
            LOGGER.info("Fetching document uid={}", uid);
            Document document = httpGet(RequestBuilder.get()
                    .setUri(env.getProperty(IDEALIST_DOCUMENT_SERVICE, IDEALIST_DOCUMENT_SERVICE_DEFAULT))
                    .addParameter("uid", uid));
            LOGGER.debug("Got Document: {}", document);
            FieldMap fields = new FieldMap();

            fields.put("uid", uid);
            fields.put("docSource", "Ideal-Ist");
            fields.put("docType", "Collaboration");
            fields.put("programme", "H2020");

            addStringField(uid, document, fields, "title", "idealistTitle");
            addStringField(uid, document, fields, "PS_ID", "idealistPsId");
            addStringField(uid, document, fields, "Status", "idealistStatus");
            addDateField(uid, document, fields, "Date_of_last_Modification", "idealistUpdated");
            addDateField(uid, document, fields, "Date_of_Publication", "idealistPublished");
            addStringField(uid, document, fields, "Call_Identifier", "callIdentifier");
            addStringField(uid, document, fields, "Objective", "idealistObjective");
            addStringField(uid, document, fields, "Funding_Schemes", "idealistFundingScheme");
            addStringField(uid, document, fields, "Evaluation_Scheme", "idealistEvaluationScheme");
            addDateField(uid, document, fields, "Closure_Date", "idealistDeadline");
            addStringField(uid, document, fields, "Type_of_partner_sought", "idealistTypeOfPartnerSought");
            addStringField(uid, document, fields, "Coordinator_possible", "idealistCoordinationPossible");
            addStringField(uid, document, fields, "Organisation", "idealistOrganisation");
            addStringField(uid, document, fields, "Department", "idealistDepartement");
            addStringField(uid, document, fields, "Type_of_Organisation", "idealistTypeOfOrganisation");
            addStringField(uid, document, fields, "Country", "idealistCountry");
            addStringField(uid, document, fields, "Body", "idealistBody");
            addStringField(uid, document, fields, "outline", "idealistOutline");
            addStringField(uid, document, fields, "description_of_work", "idealistDescriptionOfWork");

            if (LOGGER.isDebugEnabled()) {
                for (String key : fields.keySet()) {
                    LOGGER.debug("field: {}\t{}", key, fields.get(key));
                }
            }

            //Filtering invalid ideal-ist, skip records where status is not open.
            if (fields.get("idealistStatus").toString().equals("[Open]")) {
                LOGGER.info("Found a ideal-ist with open status.");
                return fields;
            } else {
                LOGGER.info("The document has the following status {}",
                        fields.get("idealistStatus").toString());
                return null;
            }
        }
    };
}

From source file:edu.mit.scratch.ScratchCloudSession.java

public List<String> getVariables() throws ScratchProjectException {
    final List<String> list = new ArrayList<>();

    try {//from  w w w. ja  va  2s  .  c om
        final RequestConfig globalConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.DEFAULT).build();

        final CookieStore cookieStore = new BasicCookieStore();
        final BasicClientCookie lang = new BasicClientCookie("scratchlanguage", "en");
        lang.setDomain(".scratch.mit.edu");
        lang.setPath("/");
        cookieStore.addCookie(lang);

        final CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(globalConfig)
                .setUserAgent(Scratch.USER_AGENT).setDefaultCookieStore(cookieStore).build();
        CloseableHttpResponse resp;

        final HttpUriRequest update = RequestBuilder.get()
                .setUri("https://scratch.mit.edu/varserver/" + this.getProjectID())
                .addHeader("Accept",
                        "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*//*;q=0.8")
                .addHeader("Referer", "https://scratch.mit.edu").addHeader("Origin", "https://scratch.mit.edu")
                .addHeader("Accept-Encoding", "gzip, deflate, sdch")
                .addHeader("Accept-Language", "en-US,en;q=0.8").addHeader("Content-Type", "application/json")
                .addHeader("X-Requested-With", "XMLHttpRequest").build();
        try {
            resp = httpClient.execute(update);
        } catch (final IOException e) {
            e.printStackTrace();
            throw new ScratchProjectException();
        }

        BufferedReader rd;
        try {
            rd = new BufferedReader(new InputStreamReader(resp.getEntity().getContent()));
        } catch (UnsupportedOperationException | IOException e) {
            e.printStackTrace();
            throw new ScratchProjectException();
        }

        final StringBuffer result = new StringBuffer();
        String line = "";
        while ((line = rd.readLine()) != null)
            result.append(line);
        final JSONObject jsonOBJ = new JSONObject(result.toString().trim());

        final Iterator<?> keys = ((JSONArray) jsonOBJ.get("variables")).iterator();

        while (keys.hasNext()) {
            final JSONObject o = new JSONObject(StringEscapeUtils.unescapeJson("" + keys.next()));
            final String k = o.get("name") + "";
            list.add(k);
        }
    } catch (final UnsupportedEncodingException e) {
        e.printStackTrace();
        throw new ScratchProjectException();
    } catch (final IOException e) {
        e.printStackTrace();
        throw new ScratchProjectException();
    }

    return list;
}

From source file:edu.mit.scratch.ScratchUser.java

public ScratchUser update() throws ScratchUserException {
    try {//from  w  ww. ja va  2  s.c  om
        final RequestConfig globalConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.DEFAULT).build();

        final CookieStore cookieStore = new BasicCookieStore();
        final BasicClientCookie lang = new BasicClientCookie("scratchlanguage", "en");
        final BasicClientCookie debug = new BasicClientCookie("DEBUG", "true");
        lang.setDomain(".scratch.mit.edu");
        lang.setPath("/");
        debug.setDomain(".scratch.mit.edu");
        debug.setPath("/");
        cookieStore.addCookie(lang);
        cookieStore.addCookie(debug);

        final CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(globalConfig)
                .setUserAgent(Scratch.USER_AGENT).setDefaultCookieStore(cookieStore).build();
        CloseableHttpResponse resp;

        final HttpUriRequest update = RequestBuilder.get().setUri("https://api.scratch.mit.edu/users/arinerron")
                .addHeader("Accept",
                        "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
                .addHeader("Referer", "https://scratch.mit.edu/users/arinerron")
                .addHeader("Origin", "https://scratch.mit.edu")
                .addHeader("Accept-Encoding", "gzip, deflate, sdch")
                .addHeader("Accept-Language", "en-US,en;q=0.8").addHeader("Content-Type", "application/json")
                .addHeader("X-Requested-With", "XMLHttpRequest").build();
        try {
            resp = httpClient.execute(update);
        } catch (final IOException e) {
            e.printStackTrace();
            throw new ScratchUserException();
        }

        BufferedReader rd;
        try {
            rd = new BufferedReader(new InputStreamReader(resp.getEntity().getContent()));
        } catch (UnsupportedOperationException | IOException e) {
            e.printStackTrace();
            throw new ScratchUserException();
        }

        final StringBuffer result = new StringBuffer();
        String line = "";
        while ((line = rd.readLine()) != null)
            result.append(line);
        System.out.println("msgdata:" + result.toString()); // remove later!
        final JSONObject jsonObject = new JSONObject(result.toString().trim());

        this.join_date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss")
                .parse(jsonObject.getJSONObject("history").getString("joined"));
        final JSONObject profile = jsonObject.getJSONObject("profile");
        this.user_id = profile.getInt("id");
        this.status = profile.getString("status");
        this.bio = profile.getString("bio");
        this.country = profile.getString("country");
    } catch (final UnsupportedEncodingException e) {
        e.printStackTrace();
        throw new ScratchUserException();
    } catch (final IOException e) {
        e.printStackTrace();
        throw new ScratchUserException();
    } catch (final JSONException e) {
        e.printStackTrace();
        throw new ScratchUserException();
    } catch (final ParseException e) {
        e.printStackTrace();
        throw new ScratchUserException();
    }

    return this;
}

From source file:edu.mit.scratch.ScratchProject.java

public ScratchProject update() throws ScratchProjectException {
    try {/* www.  jav  a2s .  c  o  m*/
        final RequestConfig globalConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.DEFAULT).build();

        final CookieStore cookieStore = new BasicCookieStore();
        final BasicClientCookie lang = new BasicClientCookie("scratchlanguage", "en");
        lang.setDomain(".scratch.mit.edu");
        lang.setPath("/");
        cookieStore.addCookie(lang);

        final CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(globalConfig)
                .setUserAgent("Mozilla/5.0 (Windows NT 6.1; WOW64)"
                        + " AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/" + "537.36")
                .setDefaultCookieStore(cookieStore).build();
        CloseableHttpResponse resp;

        final HttpUriRequest update = RequestBuilder.get()
                .setUri("https://scratch.mit.edu/api/v1/project/" + this.getProjectID() + "/?format=json")
                .addHeader("Accept",
                        "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
                .addHeader("Referer", "https://scratch.mit.edu").addHeader("Origin", "https://scratch.mit.edu")
                .addHeader("Accept-Encoding", "gzip, deflate, sdch")
                .addHeader("Accept-Language", "en-US,en;q=0.8").addHeader("Content-Type", "application/json")
                .addHeader("X-Requested-With", "XMLHttpRequest").build();
        try {
            resp = httpClient.execute(update);
            System.out.println(resp.getStatusLine().toString());
        } catch (final IOException e) {
            e.printStackTrace();
            throw new ScratchProjectException();
        }

        BufferedReader rd;
        try {
            rd = new BufferedReader(new InputStreamReader(resp.getEntity().getContent()));
        } catch (UnsupportedOperationException | IOException e) {
            e.printStackTrace();
            throw new ScratchProjectException();
        }

        final StringBuffer result = new StringBuffer();
        String line = "";
        while ((line = rd.readLine()) != null)
            result.append(line);
        System.out.println("projdata:" + result.toString());
        final JSONObject jsonOBJ = new JSONObject(result.toString().trim());

        final Iterator<?> keys = jsonOBJ.keys();

        while (keys.hasNext()) {
            final String key = "" + keys.next();
            final Object o = jsonOBJ.get(key);
            if (o instanceof JSONObject)
                this.creator = "" + ((JSONObject) o).get("username");
            else {
                final String val = "" + o;

                switch (key) {
                case "creator":
                    this.creator = val;
                    break;
                case "datetime_shared":
                    this.share_date = val;
                    break;
                case "description":
                    this.description = val;
                    break;
                case "favorite_count":
                    this.favorite_count = val;
                    break;
                case "id":
                    this.ID = Integer.parseInt(val);
                    break;
                case "love_count":
                    this.love_count = val;
                    break;
                case "resource_uri":
                    this.resource_uri = val;
                    break;
                case "thumbnail":
                    this.thumbnail = val;
                    break;
                case "title":
                    this.title = val;
                    break;
                case "view_count":
                    this.view_count = val;
                    break;
                default:
                    System.out.println("Missing reference:" + key);
                    break;
                }
            }
        }
    } catch (final UnsupportedEncodingException e) {
        e.printStackTrace();
        throw new ScratchProjectException();
    } catch (final IOException e) {
        e.printStackTrace();
        throw new ScratchProjectException();
    }

    return this;
}

From source file:jp.classmethod.aws.brian.BrianClient.java

@Override
public Optional<BrianTrigger> describeTrigger(TriggerKey key)
        throws BrianClientException, BrianServerException {
    logger.debug("describe trigger: {}", key);
    HttpResponse httpResponse = null;/*from  w w w  .j ava 2  s.co m*/
    try {
        String path = String.format("/triggers/%s/%s", key.getGroup(), key.getName());
        URI uri = new URI(scheme, null, hostname, port, path, null, null);
        HttpUriRequest httpRequest = RequestBuilder.get().setUri(uri).build();
        httpResponse = httpClientExecute(httpRequest);
        int statusCode = httpResponse.getStatusLine().getStatusCode();
        JsonNode tree = mapper.readTree(httpResponse.getEntity().getContent());
        if (statusCode == HttpStatus.SC_OK) {
            if (tree.path("cronExpression").isMissingNode() == false) {
                return Optional.of(mapper.readValue(new TreeTraversingParser(tree), BrianCronTrigger.class));
            } else if (tree.path("repeatCount").isMissingNode() == false) {
                return Optional.of(mapper.readValue(new TreeTraversingParser(tree), BrianSimpleTrigger.class));
            }
            // TODO deserialize
            throw new Error("unknown scheduleType");
        } else if (statusCode >= 500) {
            throw new BrianServerException("status = " + statusCode);
        } else if (statusCode == HttpStatus.SC_NOT_FOUND) {
            return Optional.empty();
        } else if (statusCode >= 400) {
            throw new BrianClientException("status = " + statusCode);
        } else {
            throw new Error("status = " + statusCode);
        }
    } catch (JsonProcessingException e) {
        throw new BrianServerException(e);
    } catch (URISyntaxException e) {
        throw new IllegalStateException(e);
    } catch (IOException e) {
        throw new BrianServerException(e);
    } catch (IllegalStateException e) {
        throw new Error(e);
    } finally {
        if (httpResponse != null) {
            EntityUtils.consumeQuietly(httpResponse.getEntity());
        }
    }
}

From source file:edu.mit.scratch.Scratch.java

public static List<ScratchUser> getUsers(final int limit, final int offset) throws ScratchUserException {
    if ((offset < 0) || (limit < 0))
        throw new ScratchUserException();

    final List<ScratchUser> users = new ArrayList<>();

    try {//from  ww  w. j  av  a2 s .co  m
        final RequestConfig globalConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.DEFAULT).build();

        final CookieStore cookieStore = new BasicCookieStore();
        final BasicClientCookie lang = new BasicClientCookie("scratchlanguage", "en");
        final BasicClientCookie debug = new BasicClientCookie("DEBUG", "true");
        debug.setDomain(".scratch.mit.edu");
        debug.setPath("/");
        lang.setPath("/");
        lang.setDomain(".scratch.mit.edu");
        cookieStore.addCookie(lang);
        cookieStore.addCookie(debug);

        final CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(globalConfig)
                .setUserAgent(Scratch.USER_AGENT).setDefaultCookieStore(cookieStore).build();
        CloseableHttpResponse resp;

        final HttpUriRequest update = RequestBuilder.get()
                .setUri("https://scratch.mit.edu/api/v1/user/?format=json&limit=" + limit + "&offset=" + offset)
                .addHeader("Accept",
                        "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
                .addHeader("Referer", "https://scratch.mit.edu").addHeader("Origin", "https://scratch.mit.edu")
                .addHeader("Accept-Encoding", "gzip, deflate, sdch")
                .addHeader("Accept-Language", "en-US,en;q=0.8").addHeader("Content-Type", "application/json")
                .addHeader("X-Requested-With", "XMLHttpRequest").build();
        try {
            resp = httpClient.execute(update);
        } catch (final IOException e) {
            e.printStackTrace();
            throw new ScratchUserException();
        }

        BufferedReader rd;
        try {
            rd = new BufferedReader(new InputStreamReader(resp.getEntity().getContent()));
        } catch (UnsupportedOperationException | IOException e) {
            e.printStackTrace();
            throw new ScratchUserException();
        }

        final StringBuffer result = new StringBuffer();
        String line = "";
        while ((line = rd.readLine()) != null)
            result.append(line);
        final JSONObject jsonOBJ = new JSONObject(result.toString().trim());

        final Iterator<?> keys = jsonOBJ.keys();

        while (keys.hasNext()) {
            final String key = "" + keys.next();
            final Object o = jsonOBJ.get(key);
            final String val = "" + o;

            if (key.equals("objects")) {
                final JSONArray jsonArray = (JSONArray) o;
                for (int i = 0; i < jsonArray.length(); i++) {
                    final JSONObject jsonOBJ2 = jsonArray.getJSONObject(i);
                    users.add(new ScratchUser("" + jsonOBJ2.get("username")));
                }
            }
        }

        return users;
    } catch (final UnsupportedEncodingException e) {
        e.printStackTrace();
        throw new ScratchUserException();
    } catch (final Exception e) {
        e.printStackTrace();
        throw new ScratchUserException();
    }
}

From source file:edu.mit.scratch.ScratchUser.java

@Deprecated
public List<ScratchProject> getProjects() throws ScratchUserException { // DEPRECATED
    final List<ScratchProject> ids = new ArrayList<>();

    try {//ww w. ja v  a 2 s.com
        final RequestConfig globalConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.DEFAULT).build();

        final CookieStore cookieStore = new BasicCookieStore();
        final BasicClientCookie lang = new BasicClientCookie("scratchlanguage", "en");
        final BasicClientCookie debug = new BasicClientCookie("DEBUG", "true");
        debug.setDomain(".scratch.mit.edu");
        debug.setPath("/");
        lang.setPath("/");
        lang.setDomain(".scratch.mit.edu");
        cookieStore.addCookie(lang);
        cookieStore.addCookie(debug);

        final CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(globalConfig)
                .setUserAgent(Scratch.USER_AGENT).setDefaultCookieStore(cookieStore).build();
        CloseableHttpResponse resp;

        final HttpUriRequest update = RequestBuilder.get()
                .setUri("https://api.scratch.mit.edu/users/" + this.getUsername() + "/projects")
                .addHeader("Accept",
                        "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
                .addHeader("Referer", "https://scratch.mit.edu/users/" + this.getUsername() + "/")
                .addHeader("Origin", "https://scratch.mit.edu")
                .addHeader("Accept-Encoding", "gzip, deflate, sdch")
                .addHeader("Accept-Language", "en-US,en;q=0.8").addHeader("Content-Type", "application/json")
                .addHeader("X-Requested-With", "XMLHttpRequest").build();
        try {
            resp = httpClient.execute(update);
        } catch (final IOException e) {
            e.printStackTrace();
            throw new ScratchUserException();
        }

        BufferedReader rd;
        try {
            rd = new BufferedReader(new InputStreamReader(resp.getEntity().getContent()));
        } catch (UnsupportedOperationException | IOException e) {
            e.printStackTrace();
            throw new ScratchUserException();
        }

        final StringBuffer result = new StringBuffer();
        String line = "";
        while ((line = rd.readLine()) != null)
            result.append(line);
        final JSONArray jsonOBJ2 = new JSONArray(result.toString().trim());

        for (int i = 0; i < jsonOBJ2.length(); i++) {
            final JSONObject jsonOBJ = jsonOBJ2.getJSONObject(i);

            final Iterator<?> keys = jsonOBJ.keys();

            while (keys.hasNext()) {
                final String key = "" + keys.next();
                final Object o = jsonOBJ.get(key);
                final String val = "" + o;

                if (key.equalsIgnoreCase("id"))
                    ids.add(new ScratchProject(Integer.parseInt(val)));
            }
        }

        return ids;
    } catch (final UnsupportedEncodingException e) {
        e.printStackTrace();
        throw new ScratchUserException();
    } catch (final Exception e) {
        e.printStackTrace();
        throw new ScratchUserException();
    }
}

From source file:net.ymate.platform.module.wechat.support.HttpClientHelper.java

public IMediaFileWrapper doDownload(String url) throws Exception {
    CloseableHttpClient _httpClient = HttpClientHelper.create()
            .setConnectionTimeout(new Long(DateTimeUtils.MINUTE * 5).intValue()).__doBuildHttpClient();
    try {/* www .jav  a2  s.c  o m*/
        return _httpClient.execute(RequestBuilder.get().setUri(url).build(), __INNER_DOWNLOAD_HANDLER);
    } finally {
        _httpClient.close();
    }
}

From source file:net.ymate.framework.commons.HttpClientHelper.java

public void download(String url, Header[] headers, final IFileHandler handler) throws Exception {
    RequestBuilder _reqBuilder = RequestBuilder.get().setUri(url);
    if (headers != null && headers.length > 0) {
        for (Header _header : headers) {
            _reqBuilder.addHeader(_header);
        }/*w ww .j ava 2  s .co  m*/
    }
    __doExecHttpDownload(_reqBuilder, handler);
}