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");
}
}
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;
}
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;
}
}
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);
}
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();
}
Aggregations