Example usage for org.apache.http.client.fluent Request Get

List of usage examples for org.apache.http.client.fluent Request Get

Introduction

In this page you can find the example usage for org.apache.http.client.fluent Request Get.

Prototype

public static Request Get(final String uri) 

Source Link

Usage

From source file:org.eclipse.epp.internal.logging.aeri.ui.v2.AeriServer.java

@VisibleForTesting
protected static Response request(URI target, Executor executor) throws ClientProtocolException, IOException {
    // max time until a connection to the server has to be established.
    int connectTimeout = (int) TimeUnit.SECONDS.toMillis(3);
    // max time between two packets sent back to the client. 10 seconds of silence will kill the session
    int socketTimeout = (int) TimeUnit.SECONDS.toMillis(10);
    Request request = Request.Get(target).viaProxy(getProxyHost(target).orNull()).connectTimeout(connectTimeout)
            .staleConnectionCheck(true).socketTimeout(socketTimeout);
    return proxyAuthentication(executor, target).execute(request);
}

From source file:org.eclipse.epp.internal.logging.aeri.ui.v2.AeriServer.java

/**
 *
 * @param monitor//from   w ww .  j  a  v  a2s .com
 * @return the {@link HttpStatus}
 */
public int downloadDatabase(File destination, IProgressMonitor monitor) throws IOException {
    URI target = newURI(configuration.getProblemsUrl());
    // @formatter:off
    Request request = Request.Get(target).viaProxy(getProxyHost(target).orNull())
            .connectTimeout(configuration.getConnectTimeoutMs()).staleConnectionCheck(true)
            .socketTimeout(configuration.getSocketTimeoutMs());
    // @formatter:on

    Response response = Proxies.proxyAuthentication(executor, target).execute(request);

    HttpResponse returnResponse = Responses.getResponseWithProgress(response, monitor);
    int statusCode = returnResponse.getStatusLine().getStatusCode();

    if (statusCode == HttpStatus.SC_OK) {
        configuration.setProblemsZipLastDownloadTimestamp(System.currentTimeMillis());
        saveConfiguration();
        try (FileOutputStream out = new FileOutputStream(destination)) {
            returnResponse.getEntity().writeTo(out);
        }
    }
    return statusCode;
}

From source file:org.talend.components.datastewardship.connection.TdsConnection.java

/**
 * Executes Http Get request// www  . ja  v  a2  s. c  o m
 * 
 * @param resource REST API resource. E. g. issue/{issueId}
 * @param parameters http query parameters
 * @return response result
 * @throws IOException
 */
public String get(String resource, Map<String, Object> parameters) throws IOException {
    try {
        URIBuilder builder = new URIBuilder(hostPort + resource);
        for (Map.Entry<String, Object> entry : parameters.entrySet()) {
            builder.addParameter(entry.getKey(), entry.getValue().toString());
        }
        URI uri = builder.build();
        Request get = Request.Get(uri).addHeader(authorization);
        return executor.execute(get).returnContent().asString();
    } catch (URISyntaxException e) {
        LOG.debug("Wrong URI. {}", e.getMessage()); //$NON-NLS-1$
        throw new IOException("Wrong URI", e); //$NON-NLS-1$
    }
}

From source file:org.talend.components.jira.connection.Rest.java

/**
 * Executes Http Get request//from ww  w.  ja v  a2 s  .c  o m
 * 
 * @param resource REST API resource. E. g. issue/{issueId}
 * @param parameters http query parameters
 * @return response result
 * @throws IOException
 */
public String get(String resource, Map<String, Object> parameters) throws IOException {
    try {
        URIBuilder builder = new URIBuilder(hostPort + resource);
        for (Map.Entry<String, Object> entry : parameters.entrySet()) {
            builder.addParameter(entry.getKey(), entry.getValue().toString());
        }
        URI uri = builder.build();
        Request get = Request.Get(uri);
        for (Header header : headers) {
            get.addHeader(header);
        }
        executor.clearCookies();
        return executor.execute(get).returnContent().asString();
    } catch (URISyntaxException e) {
        LOG.debug("Wrong URI. {}", e.getMessage());
        throw new IOException("Wrong URI", e);
    }
}

From source file:org.talend.librariesmanager.nexus.ArtifacoryRepositoryHandler.java

@Override
public List<MavenArtifact> search(String groupIdToSearch, String artifactId, String versionToSearch,
        boolean fromRelease, boolean fromSnapshot) throws Exception {
    String serverUrl = serverBean.getServer();
    if (!serverUrl.endsWith("/")) {
        serverUrl = serverUrl + "/";
    }/*from w  w  w  . j a  va 2s .  com*/
    String searchUrl = serverUrl + SEARCH_SERVICE;

    String repositoryId = "";
    if (fromRelease) {
        repositoryId = serverBean.getRepositoryId();
    }
    if (fromSnapshot) {
        if ("".equals(repositoryId)) {
            repositoryId = serverBean.getSnapshotRepId();
        } else {
            repositoryId = repositoryId + "," + serverBean.getSnapshotRepId();
        }
    }
    String query = "";//$NON-NLS-1$
    if (!"".equals(repositoryId)) {
        query = "repos=" + repositoryId;//$NON-NLS-1$
    }
    if (groupIdToSearch != null) {
        if (!"".equals(query)) {
            query = query + "&";
        }
        query = query + "g=" + groupIdToSearch;//$NON-NLS-1$
    }
    if (artifactId != null) {
        if (!"".equals(query)) {
            query = query + "&";
        }
        query = query + "a=" + artifactId;//$NON-NLS-1$
    }

    if (versionToSearch != null) {
        if (!"".equals(query)) {
            query = query + "&";
        }
        query = query + "v=" + versionToSearch;//$NON-NLS-1$
    }
    searchUrl = searchUrl + query;
    Request request = Request.Get(searchUrl);
    String userPass = serverBean.getUserName() + ":" + serverBean.getPassword();
    String basicAuth = "Basic " + new String(new Base64().encode(userPass.getBytes()));
    Header authority = new BasicHeader("Authorization", basicAuth);
    request.addHeader(authority);
    List<MavenArtifact> resultList = new ArrayList<MavenArtifact>();

    HttpResponse response = request.execute().returnResponse();
    String content = EntityUtils.toString(response.getEntity());
    if (content.isEmpty()) {
        return resultList;
    }
    JSONObject responseObject = new JSONObject().fromObject(content);
    String resultStr = responseObject.getString("results");
    JSONArray resultArray = null;
    try {
        resultArray = new JSONArray().fromObject(resultStr);
    } catch (Exception e) {
        throw new Exception(resultStr);
    }
    if (resultArray != null) {
        String resultUrl = serverUrl + SEARCH_RESULT_PREFIX;
        for (int i = 0; i < resultArray.size(); i++) {
            JSONObject jsonObject = resultArray.getJSONObject(i);
            String uri = jsonObject.getString("uri");
            uri = uri.substring(resultUrl.length(), uri.length());
            String[] split = uri.split("/");
            if (split.length > 4) {
                String fileName = split[split.length - 1];
                if (!fileName.endsWith("pom")) {
                    String type = null;
                    int dotIndex = fileName.lastIndexOf('.');
                    if (dotIndex > 0) {
                        type = fileName.substring(dotIndex + 1);

                    }
                    if (type != null) {
                        MavenArtifact artifact = new MavenArtifact();
                        String v = split[split.length - 2];
                        String a = split[split.length - 3];
                        String g = "";
                        for (int j = 1; j < split.length - 3; j++) {
                            if ("".equals(g)) {
                                g = split[j];
                            } else {
                                g = g + "." + split[j];
                            }
                        }
                        artifact.setGroupId(g);
                        artifact.setArtifactId(a);
                        artifact.setVersion(v);
                        artifact.setType(type);
                        resultList.add(artifact);
                    }
                }

            }

        }
    }

    return resultList;
}

From source file:org.usc.wechat.mp.sdk.util.HttpUtil.java

public static <T extends JsonRtn> T getRequest(WechatRequest request, License license,
        Map<String, String> paramMap, Class<T> jsonRtnClazz) {
    String requestUrl = request.getUrl();
    String requestName = request.getName();
    List<NameValuePair> nameValuePairs = buildNameValuePairs(license, paramMap);

    URI uri = buildURI(requestUrl, nameValuePairs);
    if (uri == null) {
        return JsonRtnUtil.buildFailureJsonRtn(jsonRtnClazz, "build request URI failed");
    }//from   w  ww .  j a va  2s.  c  o  m

    try {
        String json = Request.Get(uri).connectTimeout(CONNECT_TIMEOUT).socketTimeout(SOCKET_TIMEOUT).execute()
                .handleResponse(HttpUtil.UTF8_CONTENT_HANDLER);
        T jsonRtn = JsonRtnUtil.parseJsonRtn(json, jsonRtnClazz);
        log.info(requestName + " result:\n url={},\n rtn={},\n {}", uri, json, jsonRtn);
        return jsonRtn;
    } catch (Exception e) {
        String msg = requestName + " failed:\n url=" + uri;
        log.error(msg, e);
        return JsonRtnUtil.buildFailureJsonRtn(jsonRtnClazz, "get request server failed");
    }
}

From source file:org.usc.wechat.mp.sdk.util.platform.MediaUtil.java

public static File getMedia(License license, String mediaId, String path) {
    if (StringUtils.isEmpty(mediaId) || StringUtils.isEmpty(path)) {
        return null;
    }//from  w  ww  .  java  2 s  .co  m

    String accessToken = AccessTokenUtil.getAccessToken(license);
    String url = WechatRequest.GET_MEDIA.getUrl();
    try {
        URI uri = new URIBuilder(url).setParameter("access_token", accessToken)
                .setParameter("media_id", mediaId).build();

        HttpResponse response = Request.Get(uri).connectTimeout(HttpUtil.CONNECT_TIMEOUT)
                .socketTimeout(HttpUtil.SOCKET_TIMEOUT).execute().returnResponse();
        return downloadFile(response, mediaId, path, uri);
    } catch (Exception e) {
        String msg = "get media failed:\n " + "url=" + url + "?access_token=" + accessToken + "&media_id="
                + mediaId;
        log.error(msg, e);
        return null;
    }
}

From source file:org.usc.wechat.mp.sdk.util.platform.MenuUtil.java

public static Menu getMenu(License license) {
    String accessToken = AccessTokenUtil.getAccessToken(license);
    String url = WechatRequest.GET_MENU.getUrl();
    try {/* w w w. j  ava 2s  .  co m*/
        URI uri = new URIBuilder(url).setParameter("access_token", accessToken).build();

        String json = Request.Get(uri).connectTimeout(HttpUtil.CONNECT_TIMEOUT)
                .socketTimeout(HttpUtil.SOCKET_TIMEOUT).execute().handleResponse(HttpUtil.UTF8_CONTENT_HANDLER);
        Menu menu = buildMenu(json);
        log.info("get menu:\n url={},\n rtn={},{}", uri, json, menu);
        return menu;
    } catch (Exception e) {
        String msg = "get menu failed: url=" + url + "?access_token=" + accessToken;
        log.error(msg, e);
        return null;
    }
}