public String urlFor(String path) throws URISyntaxException { URIBuilder baseUri = new URIBuilder(baseUrl); String originalPath = baseUri.getPath(); if (originalPath == null) { originalPath = ""; } if (originalPath.endsWith(DELIMITER) && path.startsWith(DELIMITER)) { path = path.replaceFirst(DELIMITER, ""); } return baseUri.setPath(originalPath + path).toString(); }
/** * Adds the fragment to the end of the path and optionally adds an id param depending upon * the value of appendIdToRelativeURI. */ protected String buildRelativeUrlWithParam(String currentUrl, String fragment, String idParam, String idValue) { try { URIBuilder builder = new URIBuilder(currentUrl); builder.setPath(builder.getPath() + "/" + fragment); if (appendIdToRelativeURI) { builder.setParameter(idParam, String.valueOf(idValue)); } return builder.build().toString(); } catch (URISyntaxException e) { return currentUrl; } }
public HttpRequestBuilder withPath(String path) { try { URIBuilder uri = new URIBuilder(path); request.setServerName("test.host"); request.setContextPath(CONTEXT_PATH); request.setParameters(splitQuery(uri)); request.setRequestURI(CONTEXT_PATH + uri.getPath()); request.setServletPath(uri.getPath()); if (!uri.getQueryParams().isEmpty()) { request.setQueryString(URLEncodedUtils.format(uri.getQueryParams(), UTF_8)); } return this; } catch (Exception e) { throw new RuntimeException(e); } }
URIBuilder uriBuilder = new URIBuilder("http://example.com/test"); URI uri = uriBuilder.setPath(uriBuilder.getPath() + "/path/to/add") .build() .normalize(); // expected : http://example.com/test/path/to/add
@Override protected void buildPath(String path) { String temp = uriBuilder.getPath(); if (!temp.endsWith("/")) { temp = temp + "/"; } if (path.startsWith("/")) { path = path.substring(1); } uriBuilder.setPath(temp + path); build(); }
String url = "http://example.com/test"; URIBuilder builder = new URIBuilder(url); builder.setPath((builder.getPath() + "///example").replaceAll("//+", "/")); System.out.println("Result 2 -> " + builder.toString());
String url = "http://example.com/test"; URIBuilder builder = new URIBuilder(url); builder.setPath((builder.getPath() + "/example").replaceAll("//+", "/")); System.out.println("Result 1 -> " + builder.toString());
/** * Replaces the path in a given URL with a given path. * * @param currentUrl url to change * @param newPath path to replace the old path with * @return full URL of current request with given path appended */ @Contract("_, _ -> !null") @SneakyThrows(URISyntaxException.class) public static String replaceContextPath(@NotNull String currentUrl, @NotNull String newPath) { String pathToSet = new URIBuilder(newPath).getPath(); return new URIBuilder(currentUrl) .setPath(pathToSet.startsWith("/") ? pathToSet : ("/" + pathToSet)) .build().toString(); } }
/** * Build a URI from a path and query parmeters. * * @param path Path to append to the base URI * @param params Map of key value pairs * * @return the full URI * * @throws URISyntaxException when the path is invalid */ public URI buildURI(String path, Map<String, String> params) throws URISyntaxException { URIBuilder ub = new URIBuilder(uri); ub.setPath(ub.getPath() + path); if (params != null) { for (Map.Entry<String, String> ent : params.entrySet()) ub.addParameter(ent.getKey(), ent.getValue()); } return ub.build(); }
private void setHost(BenchmarkCommandOptions.VariantBenchmarkCommandOptions options) throws URISyntaxException { URIBuilder uriBuilder = null; String host = options.host; if (host == null) { uriBuilder = new URIBuilder(configuration.getBenchmark().getRest()); } else { uriBuilder = new URIBuilder(host); } String storageRest = commandOptions.commonCommandOptions.params.get("storage.rest"); if (StringUtils.isNotEmpty(storageRest) && storageRest.equals("true")) { uriBuilder.setPath(uriBuilder.getPath().concat(VariantStorageEngineRestSampler.STORAGE_REST_PATH)); } else { uriBuilder.setPath(uriBuilder.getPath().concat(VariantStorageEngineRestSampler.REST_PATH)); } configuration.getBenchmark().setRest(new URI(uriBuilder.toString())); }
/** * @since 4.1 */ protected URI createLocationURI(final String location) throws ProtocolException { try { final URIBuilder b = new URIBuilder(new URI(location).normalize()); final String host = b.getHost(); if (host != null) { b.setHost(host.toLowerCase(Locale.ROOT)); } final String path = b.getPath(); if (TextUtils.isEmpty(path)) { b.setPath("/"); } return b.build(); } catch (final URISyntaxException ex) { throw new ProtocolException("Invalid redirect URI: " + location, ex); } }
/** * @since 4.1 */ protected URI createLocationURI(final String location) throws ProtocolException { try { final URIBuilder b = new URIBuilder(new URI(location).normalize()); final String host = b.getHost(); if (host != null) { b.setHost(host.toLowerCase(Locale.ENGLISH)); } final String path = b.getPath(); if (TextUtils.isEmpty(path)) { b.setPath("/"); } return b.build(); } catch (final URISyntaxException ex) { throw new ProtocolException("Invalid redirect URI: " + location, ex); } }
private static UrlInfo getUrlInfo(String url, boolean retryValidPart) { try { if (!url.startsWith("http://") && !url.startsWith("https://")) { url = "https://" + url; } URIBuilder builder = new URIBuilder(url); return new UrlInfo(builder.getPath(), builder.getQueryParams().stream() .filter(it -> it.getValue() != null) .collect(Collectors.toMap(NameValuePair::getName, NameValuePair::getValue))); } catch (URISyntaxException e) { if (retryValidPart) { return getUrlInfo(url.substring(0, e.getIndex() - 1), false); } else { throw new FriendlyException("Not a valid URL: " + url, COMMON, e); } } }
/** * @since 4.1 */ protected URI createLocationURI(final String location) throws ProtocolException { try { final URIBuilder b = new URIBuilder(new URI(location).normalize()); final String host = b.getHost(); if (host != null) { b.setHost(host.toLowerCase(Locale.ROOT)); } final String path = b.getPath(); if (TextUtils.isEmpty(path)) { b.setPath("/"); } return b.build(); } catch (final URISyntaxException ex) { throw new ProtocolException("Invalid redirect URI: " + location, ex); } }
public static HttpPost createPost(String path, ClientConfiguration config) throws URISyntaxException { final URIBuilder uriBuilder = new URIBuilder(config.getMarmottaUri()); uriBuilder.setPath(uriBuilder.getPath() + path); if (StringUtils.isNotBlank(config.getMarmottaContext())) { uriBuilder.addParameter(CONTEXT, config.getMarmottaContext()); } final HttpPost post = new HttpPost(uriBuilder.build()); if (StringUtils.isNotBlank(config.getMarmottaUser())) { final String credentials = String.format("%s:%s", config.getMarmottaUser(), config.getMarmottaPassword()); try { final String encoded = DatatypeConverter.printBase64Binary(credentials.getBytes("UTF-8")); post.setHeader("Authorization", String.format("Basic %s", encoded)); } catch (UnsupportedEncodingException e) { System.err.println("Error encoding credentials: " + e.getMessage()); } } return post; }
@Override public long renewDelegationToken(Token<?> token) throws IOException { URIBuilder uriBuilder = new URIBuilder().setPath(DEFAULT_DELEGATION_TOKEN_MANAGER_ENDPOINT) .addParameter(OP_PARAM_KEY_NAME, RENEW_DELEGATION_TOKEN_OP) .addParameter(TOKEN_PARAM_KEY_NAME, token.encodeToUrlString()); String responseBody = remoteCallHelper .makeRemoteRequest(dtServiceUrls, uriBuilder.getPath(), uriBuilder.getQueryParams(), HttpPut.METHOD_NAME); Map<?, ?> parsedResp = JsonUtils.parse(responseBody); return ((Number) parsedResp.get("long")).longValue(); }
@Override public void cancelDelegationToken(Token<?> token) throws IOException { URIBuilder uriBuilder = new URIBuilder().setPath(DEFAULT_DELEGATION_TOKEN_MANAGER_ENDPOINT) .addParameter(OP_PARAM_KEY_NAME, CANCEL_DELEGATION_TOKEN_OP) .addParameter(TOKEN_PARAM_KEY_NAME, token.encodeToUrlString()); remoteCallHelper.makeRemoteRequest(dtServiceUrls, uriBuilder.getPath(), uriBuilder.getQueryParams(), HttpPut.METHOD_NAME); } }
private String fullUriString(String relativePath, boolean withTrailingSlash) { String baseUri = this.baseUri; if (!baseUri.endsWith("/")) { baseUri += "/"; } if (withTrailingSlash && !relativePath.equals("") && !relativePath.endsWith("/")) { relativePath += "/"; } try { URIBuilder builder = new URIBuilder(baseUri); return builder.setPath(builder.getPath() + relativePath).toString(); } catch (URISyntaxException e) { throw new RuntimeException("problem encoding fullUri", e); } } }
@Override public Token<DelegationTokenIdentifier> getDelegationToken( String renewer) throws IOException { URIBuilder uriBuilder = new URIBuilder().setPath(DEFAULT_DELEGATION_TOKEN_MANAGER_ENDPOINT) .addParameter(OP_PARAM_KEY_NAME, GET_DELEGATION_TOKEN_OP) .addParameter(RENEWER_PARAM_KEY_NAME, renewer) .addParameter(SERVICE_PARAM_KEY_NAME, WASB_DT_SERVICE_NAME.toString()); String responseBody = remoteCallHelper .makeRemoteRequest(dtServiceUrls, uriBuilder.getPath(), uriBuilder.getQueryParams(), HttpGet.METHOD_NAME); return TokenUtils.toDelegationToken(JsonUtils.parse(responseBody)); }
@Override public ClientResponse handle(ClientRequest cr) throws ClientHandlerException { URIBuilder uriBuilder = new URIBuilder(cr.getURI()); String path = uriBuilder.getPath(); uriBuilder.setPath(converter.convertCommandPath(path)); try { cr.setURI(uriBuilder.build()); ClientResponse response = getNext().handle(cr); String newEntity = converter.convertResponse(path, response.getEntity(String.class)); response.setEntityInputStream(new ByteArrayInputStream(newEntity.getBytes())); return response; } catch (Exception ex) { throw new ClientHandlerException(ex); } }