Search in sources :

Example 36 with URIBuilder

use of org.apache.http.client.utils.URIBuilder in project opennms by OpenNMS.

the class GrafanaDashletConfigurationWindow method getGrafanaDashboards.

private Map<String, String> getGrafanaDashboards() throws GrafanaDashletException {
    /**
     * Loading the required properties...
     */
    final String grafanaApiKey = System.getProperty("org.opennms.grafanaBox.apiKey", "");
    final String grafanaProtocol = System.getProperty("org.opennms.grafanaBox.protocol", "http");
    final String grafanaHostname = System.getProperty("org.opennms.grafanaBox.hostname", "localhost");
    final int grafanaPort = Integer.parseInt(System.getProperty("org.opennms.grafanaBox.port", "3000"));
    final int grafanaConnectionTimeout = Integer.parseInt(System.getProperty("org.opennms.grafanaBox.connectionTimeout", "500"));
    final int grafanaSoTimeout = Integer.parseInt(System.getProperty("org.opennms.grafanaBox.soTimeout", "500"));
    if (!"".equals(grafanaApiKey) && !"".equals(grafanaHostname) && !"".equals(grafanaProtocol) && ("http".equals(grafanaProtocol) || "https".equals(grafanaProtocol))) {
        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            final RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(grafanaConnectionTimeout).setSocketTimeout(grafanaSoTimeout).build();
            final URI uri = new URIBuilder().setScheme(grafanaProtocol).setHost(grafanaHostname).setPort(grafanaPort).setPath("/api/search/").build();
            final HttpGet httpGet = new HttpGet(uri);
            httpGet.setConfig(requestConfig);
            /**
             * Adding the API key...
             */
            httpGet.setHeader("Authorization", "Bearer " + grafanaApiKey);
            final Map<String, String> resultSet = new TreeMap<>();
            try (CloseableHttpResponse httpResponse = httpClient.execute(httpGet)) {
                HttpEntity httpEntity = httpResponse.getEntity();
                if (httpEntity != null) {
                    /**
                     * Fill the result set...
                     */
                    final String responseString = IOUtils.toString(httpEntity.getContent(), StandardCharsets.UTF_8.name());
                    if (!Strings.isNullOrEmpty(responseString)) {
                        try {
                            final JSONArray arr = new JSONObject("{dashboards:" + responseString + "}").getJSONArray("dashboards");
                            for (int i = 0; i < arr.length(); i++) {
                                resultSet.put(arr.getJSONObject(i).getString("title"), arr.getJSONObject(i).getString("uri"));
                            }
                        } catch (JSONException e) {
                            throw new GrafanaDashletException(e.getMessage());
                        }
                    }
                }
            }
            return resultSet;
        } catch (Exception e) {
            throw new GrafanaDashletException(e.getMessage());
        }
    } else {
        throw new GrafanaDashletException("Invalid configuration");
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) RequestConfig(org.apache.http.client.config.RequestConfig) HttpEntity(org.apache.http.HttpEntity) HttpGet(org.apache.http.client.methods.HttpGet) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) TreeMap(java.util.TreeMap) URI(java.net.URI) JSONException(org.json.JSONException) URIBuilder(org.apache.http.client.utils.URIBuilder) JSONObject(org.json.JSONObject) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse)

Example 37 with URIBuilder

use of org.apache.http.client.utils.URIBuilder in project dal by ctripcorp.

the class HttpUtil method getJSONEntity.

public static <T> T getJSONEntity(Class<T> clazz, String url, Map<String, String> parameters, HttpMethod method) throws Exception {
    T result = null;
    if (url == null || url.trim().length() == 0)
        return result;
    URIBuilder builder = new URIBuilder(url);
    String content = null;
    if (method == HttpMethod.HttpGet) {
        if (parameters != null && parameters.size() > 0) {
            for (Map.Entry<String, String> parameter : parameters.entrySet()) {
                builder.addParameter(parameter.getKey(), parameter.getValue());
            }
        }
        URI uri = builder.build();
        HttpGet request = new HttpGet();
        request.setURI(uri);
        content = getHttpEntityContent(request);
    } else {
        URI uri = builder.build();
        HttpPost request = new HttpPost();
        request.setURI(uri);
        if (parameters != null && parameters.size() > 0) {
            JSONObject json = new JSONObject();
            for (Map.Entry<String, String> parameter : parameters.entrySet()) {
                json.put(parameter.getKey(), parameter.getValue());
            }
            StringEntity stringEntity = new StringEntity(json.toString());
            request.setEntity(stringEntity);
        }
        request.addHeader("content-type", "application/json");
        content = getHttpEntityContent(request);
    }
    try {
        result = JSON.parseObject(content, clazz);
    } catch (Throwable e) {
    }
    return result;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) JSONObject(com.alibaba.fastjson.JSONObject) HttpGet(org.apache.http.client.methods.HttpGet) Map(java.util.Map) URI(java.net.URI) URIBuilder(org.apache.http.client.utils.URIBuilder)

Example 38 with URIBuilder

use of org.apache.http.client.utils.URIBuilder in project dal by ctripcorp.

the class WebUtil method getAllInOneResponse.

public static Response getAllInOneResponse(String keyname, String environment) throws Exception {
    Response res = null;
    if (keyname == null || keyname.isEmpty())
        return res;
    if (SERVICE_RUL == null || SERVICE_RUL.isEmpty())
        return res;
    if (APP_ID == null || APP_ID.isEmpty())
        return res;
    try {
        URIBuilder builder = new URIBuilder(SERVICE_RUL).addParameter("ids", keyname).addParameter("appid", APP_ID);
        if (environment != null && !environment.isEmpty())
            builder.addParameter("envt", environment);
        URI uri = builder.build();
        HttpClient sslClient = initWeakSSLClient();
        if (sslClient != null) {
            HttpGet httpGet = new HttpGet();
            httpGet.setURI(uri);
            HttpResponse response = sslClient.execute(httpGet);
            HttpEntity entity = response.getEntity();
            String content = EntityUtils.toString(entity);
            res = JSON.parseObject(content, Response.class);
        }
        return res;
    } catch (Throwable e) {
        throw e;
    }
}
Also used : Response(com.ctrip.platform.dal.daogen.entity.Response) HttpResponse(org.apache.http.HttpResponse) HttpEntity(org.apache.http.HttpEntity) HttpClient(org.apache.http.client.HttpClient) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) URI(java.net.URI) URIBuilder(org.apache.http.client.utils.URIBuilder)

Example 39 with URIBuilder

use of org.apache.http.client.utils.URIBuilder in project cas by apereo.

the class OAuth20TokenAuthorizationResponseBuilder method buildCallbackUrlResponseType.

/**
 * Build callback url response type string.
 *
 * @param holder       the holder
 * @param redirectUri  the redirect uri
 * @param accessToken  the access token
 * @param params       the params
 * @param refreshToken the refresh token
 * @param context      the context
 * @return the string
 * @throws Exception the exception
 */
protected View buildCallbackUrlResponseType(final AccessTokenRequestDataHolder holder, final String redirectUri, final AccessToken accessToken, final List<NameValuePair> params, final RefreshToken refreshToken, final J2EContext context) throws Exception {
    final String state = holder.getAuthentication().getAttributes().get(OAuth20Constants.STATE).toString();
    final String nonce = holder.getAuthentication().getAttributes().get(OAuth20Constants.NONCE).toString();
    final URIBuilder builder = new URIBuilder(redirectUri);
    final StringBuilder stringBuilder = new StringBuilder();
    stringBuilder.append(OAuth20Constants.ACCESS_TOKEN).append('=').append(accessToken.getId()).append('&').append(OAuth20Constants.TOKEN_TYPE).append('=').append(OAuth20Constants.TOKEN_TYPE_BEARER).append('&').append(OAuth20Constants.EXPIRES_IN).append('=').append(accessTokenExpirationPolicy.getTimeToLive());
    if (refreshToken != null) {
        stringBuilder.append('&').append(OAuth20Constants.REFRESH_TOKEN).append('=').append(refreshToken.getId());
    }
    params.forEach(p -> stringBuilder.append('&').append(p.getName()).append('=').append(p.getValue()));
    if (StringUtils.isNotBlank(state)) {
        stringBuilder.append('&').append(OAuth20Constants.STATE).append('=').append(EncodingUtils.urlEncode(state));
    }
    if (StringUtils.isNotBlank(nonce)) {
        stringBuilder.append('&').append(OAuth20Constants.NONCE).append('=').append(EncodingUtils.urlEncode(nonce));
    }
    builder.setFragment(stringBuilder.toString());
    final String url = builder.toString();
    LOGGER.debug("Redirecting to URL [{}]", url);
    return new RedirectView(url);
}
Also used : RedirectView(org.springframework.web.servlet.view.RedirectView) URIBuilder(org.apache.http.client.utils.URIBuilder)

Example 40 with URIBuilder

use of org.apache.http.client.utils.URIBuilder in project cas by apereo.

the class WsFederationNavigationController method getRelativeRedirectUrlFor.

/**
 * Gets redirect url for.
 *
 * @param config  the config
 * @param service the service
 * @param request the request
 * @return the redirect url for
 */
@SneakyThrows
public static String getRelativeRedirectUrlFor(final WsFederationConfiguration config, final Service service, final HttpServletRequest request) {
    final URIBuilder builder = new URIBuilder(ENDPOINT_REDIRECT);
    builder.addParameter(PARAMETER_NAME, config.getId());
    if (service != null) {
        builder.addParameter(CasProtocolConstants.PARAMETER_SERVICE, service.getId());
    }
    final String method = request.getParameter(CasProtocolConstants.PARAMETER_METHOD);
    if (StringUtils.isNotBlank(method)) {
        builder.addParameter(CasProtocolConstants.PARAMETER_METHOD, method);
    }
    return builder.toString();
}
Also used : URIBuilder(org.apache.http.client.utils.URIBuilder) SneakyThrows(lombok.SneakyThrows)

Aggregations

URIBuilder (org.apache.http.client.utils.URIBuilder)107 URISyntaxException (java.net.URISyntaxException)42 URI (java.net.URI)37 HttpGet (org.apache.http.client.methods.HttpGet)22 IOException (java.io.IOException)21 NameValuePair (org.apache.http.NameValuePair)13 HttpEntity (org.apache.http.HttpEntity)10 NotNull (org.jetbrains.annotations.NotNull)9 Map (java.util.Map)8 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)8 ArrayList (java.util.ArrayList)7 HashMap (java.util.HashMap)7 HttpResponse (org.apache.http.HttpResponse)7 List (java.util.List)6 HttpClient (org.apache.http.client.HttpClient)5 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)5 Gson (com.google.gson.Gson)4 URL (java.net.URL)4 RequestConfig (org.apache.http.client.config.RequestConfig)4 HttpPost (org.apache.http.client.methods.HttpPost)4