protected URI getURI(String headerValue) { if (headerValue == null) return null; URI toReturn = URI.create(headerValue); if (!"127.0.0.1".equals(toReturn.getHost())) return toReturn; return uriBuilder(toReturn).host(hostToReplace).build(); }
private static String slash(CharSequence left, CharSequence right) { return delimit(left, right, '/'); }
private static String delimit(CharSequence left, CharSequence right, char token) { if (left.length() == 0) return right.toString(); if (right.length() == 0) return left.toString(); StringBuilder builder = new StringBuilder(left); if (lastChar(left) == token) { if (firstChar(right) == token) // left/ + /right return builder.append(right.subSequence(1, right.length())).toString(); return builder.append(right).toString(); // left/ + right } else if (firstChar(right) == token) { return builder.append(right).toString(); // left + /right } // left + / + right return new StringBuilder(left).append(token).append(right).toString(); }
public boolean shouldRetryRequest(HttpCommand command, HttpResponse response) { closeClientButKeepContentStream(response); if (!command.isReplayable()) { logger.error("Cannot retry after redirect, command is not replayable: %s", command); URI redirect = URI.create(location); if (!redirect.isAbsolute()) { if (redirect.getPath() == null) { logger.error("Cannot retry after redirect, no path in location header %s", command); return false; redirect = uriBuilder(current.getEndpoint()).path(redirect.getPath()).query(redirect.getQuery()).build(); if (redirect.equals(current.getEndpoint())) { backoffHandler.imposeBackoffExponentialDelay(command.getRedirectCount(), "redirect: " + command.toString()); } else if (current.getFirstHeaderOrNull(HOST) != null && redirect.getHost() != null) { String host = redirect.getPort() > 0 ? redirect.getHost() + ":" + redirect.getPort() : redirect.getHost(); command.setCurrentRequest(current.toBuilder().replaceHeader(HOST, host).endpoint(redirect).build()); } else { command.setCurrentRequest(current.toBuilder().endpoint(redirect).build());
if (from.getPayload().getInput() == null) throw new HttpResponseException("no content", null, from); String toParse = Strings2.toStringAndClose(from.getPayload().getInput()); return URI.create(toParse.trim()); } catch (IOException e) { throw new HttpResponseException("couldn't parse uri from content", null, from, e); } finally { releasePayload(from); releasePayload(from); String location = from.getFirstHeaderOrNull(LOCATION); if (location == null) location = from.getFirstHeaderOrNull("location"); if (location != null) { URI locationUri = URI.create(location); if (locationUri.getHost() != null) return locationUri; checkState(request != null, "request should have been initialized"); location = "/" + location; locationUri = URI.create(location); return Uris.uriBuilder(request.getEndpoint()).path(locationUri.getPath()).query(locationUri.getQuery()).build(); } else { return null;
if (from.getPayload().getInput() == null) throw new HttpResponseException("no content", null, from); String toParse = Strings2.toString(from.getPayload()); return URI.create(toParse.trim()); } catch (IOException e) { throw new HttpResponseException("couldn't parse uri from content", null, from, e); } finally { releasePayload(from); releasePayload(from); String location = from.getFirstHeaderOrNull(LOCATION); if (location == null) location = from.getFirstHeaderOrNull("location"); if (location != null) { URI locationUri = URI.create(location); if (locationUri.getHost() != null) return locationUri; checkState(request != null, "request should have been initialized"); location = "/" + location; locationUri = URI.create(location); return Uris.uriBuilder(request.getEndpoint()).path(locationUri.getPath()).query(locationUri.getQuery()).build(); } else { throw new HttpResponseException("no uri in headers or content", null, from);
@Test(dataProvider = "strings") public void testPath(String path) { assertEquals(uriBuilder("https://foo.com:8080").path(path).toString(), "https://foo.com:8080/" + path); assertEquals(uriBuilder("https://foo.com:8080").path(path).build().toString(), "https://foo.com:8080/" + urlEncode(path, '/', ':', ';', '=')); assertEquals(uriBuilder("https://api.github.com/repos/user?foo=bar&kung=fu").path(path).toString(), "https://api.github.com/" + path + "?foo=bar&kung=fu"); assertEquals(uriBuilder("https://api.github.com/repos/user?foo=bar&kung=fu").path(path).build().toString(), "https://api.github.com/" + urlEncode(path, '/', ':', ';', '=') + "?foo=bar&kung=fu"); assertEquals(uriBuilder("https://api.github.com/repos/{user}").path(path).toString(), "https://api.github.com/" + path); assertEquals(uriBuilder("https://api.github.com/repos/{user}").path(path).build(templateParams).toASCIIString(), "https://api.github.com/" + urlEncode(path, '/', ':', ';', '=')); }
public void testAddQuery() { assertEquals(uriBuilder("http://localhost:8080/client/api").addQuery("response", "json").toString(), "http://localhost:8080/client/api?response=json"); assertEquals( uriBuilder(URI.create("http://localhost:8080/client/api")).addQuery("response", "json") .addQuery("command", "queryAsyncJobResult").build().toString(), "http://localhost:8080/client/api?response=json&command=queryAsyncJobResult"); } }
@Override @SuppressWarnings("unchecked") public <R extends HttpRequest> R bindToRequest(R request, Object payload) { checkNotNull(payload, "hostprefix"); checkArgument(isValid(request.getEndpoint().getHost()), "this is only valid for hostnames: " + request); InternetDomainName name = from(request.getEndpoint().getHost()).child(payload.toString()); return (R) request.toBuilder().endpoint(uriBuilder(request.getEndpoint()).host(name.name()).build()).build(); } }
public void testScheme() { assertEquals(uriBuilder("https://foo.com:8080").scheme("http").toString(), "http://foo.com:8080"); assertEquals(uriBuilder("https://foo.com:8080").scheme("http").build().toString(), "http://foo.com:8080"); assertEquals(uriBuilder("https://api.github.com/repos/user?foo=bar&kung=fu").scheme("http").toString(), "http://api.github.com/repos/user?foo=bar&kung=fu"); assertEquals(uriBuilder("https://api.github.com/repos/user?foo=bar&kung=fu").scheme("http").build().toString(), "http://api.github.com/repos/user?foo=bar&kung=fu"); assertEquals(uriBuilder("https://api.github.com/repos/{user}").scheme("http").toString(), "http://api.github.com/repos/{user}"); assertEquals(uriBuilder("https://api.github.com/repos/{user}").scheme("http").build(templateParams) .toASCIIString(), "http://api.github.com/repos/bob"); }
private HttpRequest sign(String method, String path, long timeInSeconds) { Date now = dateService.rfc1123DateParse(timeStampProvider.get()); String expires = String.valueOf(now.getTime() / 1000 + timeInSeconds); String stringToSign = method + "\n" + path + "\n" + identity + "\n" + expires; return HttpRequest.builder() .method(method) .endpoint(Uris.uriBuilder(endpoint.getScheme() + "://" + endpoint.getHost()).appendPath(path).build()) .addQueryParam("uid", identity) .addQueryParam("expires", expires) .addQueryParam("signature", signer.calculateSignature(stringToSign)) .build(); } }
@Test(dataProvider = "strings") public void testReplaceQueryIsEncoded(String key) { assertEquals(uriBuilder("/redirect").addQuery("foo", key).toString(), "/redirect?foo=" + key); assertEquals(uriBuilder("/redirect").addQuery("foo", key).build().toString(), "/redirect?foo=" + urlEncode(key, '/', ',')); }
@Test(dataProvider = "strings") public void testReplaceQueryIsEncoded(String key) { assertThat(uriBuilder("/redirect").addQuery("foo", key).build().getQuery()).isEqualTo("foo=" + key); assertThat(uriBuilder("/redirect").addQuery("foo", key).build().getRawQuery()) .isEqualTo("foo=" + urlEncode(key, '/', ',')); }
private HttpRequest sign(String method, String container, String name, GetOptions options, long expires) { checkNotNull(container, "container"); checkNotNull(name, "name"); URI url = Uris.uriBuilder(storageUrl).appendPath(container).appendPath(name).build(); String signature = signer.sign(method, url.getPath(), expires); return HttpRequest.builder() .method(method) .endpoint(url) .addQueryParams(options.buildQueryParameters()) .addQueryParam("temp_url_sig", signature) .addQueryParam("temp_url_expires", String.valueOf(expires)) .headers(options.buildRequestHeaders()).build(); } }
@SuppressWarnings("unchecked") @Override public <R extends HttpRequest> R bindToRequest(R request, Object input) { checkArgument(input instanceof Multimap<?, ?>, "this binder is only valid for Multimaps!"); Multimap<String, String> pairs = (Multimap<String, String>) checkNotNull(input, "account group pairs"); checkArgument(pairs.size() > 0, "you must specify at least one account, group pair"); Multimap<String, String> existingParams = queryParser().apply(request.getEndpoint().getQuery()); Builder<String, String> map = ImmutableMultimap.<String, String> builder().putAll(existingParams); int i = 0; for (Entry<String, String> entry : pairs.entries()) map.put(String.format("usersecuritygrouplist[%d].account", i), entry.getKey()).put( String.format("usersecuritygrouplist[%d].group", i++), entry.getValue()); URI endpoint = uriBuilder(request.getEndpoint()).query(map.build()).build(); return (R) request.toBuilder().endpoint(endpoint).build(); } }
@Test public void testWhenMatrixOnPath() { assertEquals( uriBuilder("https://api.rimuhosting.com/r").appendPath("orders;include_inactive=N").build(templateParams) .toASCIIString(), "https://api.rimuhosting.com/r/orders;include_inactive=N"); }
public HttpRequest filter(HttpRequest request) throws HttpException { checkNotNull(request, "request must be present"); Multimap<String, String> decodedParams = queryParser().apply(request.getEndpoint().getRawQuery()); addSigningParams(decodedParams); String stringToSign = createStringToSign(request, decodedParams); String signature = sign(stringToSign); addSignature(decodedParams, signature); request = request.toBuilder().endpoint(uriBuilder(request.getEndpoint()).query(decodedParams).build()).build(); utils.logRequest(signatureLog, request, "<<"); return request; }
@SuppressWarnings("unchecked") @Override public <R extends HttpRequest> R bindToRequest(R request, Object recordId) { RecordId valueToAppend = RecordId.class.cast(checkNotNull(recordId, "recordId")); URI path = uriBuilder(request.getEndpoint()).appendPath("/{type}Record/{zone}/{fqdn}/{id}").build( ImmutableMap.<String, Object> builder() .put("type", valueToAppend.getType()) .put("zone", valueToAppend.getZone()) .put("fqdn", valueToAppend.getFQDN()) .put("id", valueToAppend.getId()).build()); return (R) request.toBuilder().endpoint(path).build(); } }
builder.uri(uriBuilder(getRequest().getEndpoint()).clearQuery().appendPath(Strings2.urlEncode(currentKey)) .build()); } else if (qName.equals("LastModified")) { builder.lastModified(dateParser
currentKey = currentOrNull(currentText); builder.key(currentKey); builder.uri(uriBuilder(getRequest().getEndpoint()).clearQuery().appendPath(currentKey).build()); } else if (qName.equals("LastModified")) { builder.lastModified(dateParser.iso8601DateParse(currentOrNull(currentText))); String currentETag = currentOrNull(currentText); builder.eTag(currentETag); currentETag = Strings2.replaceAll(currentETag, '"', ""); if (!MULTIPART_BLOB_ETAG.matcher(currentETag).matches()) { builder.contentMD5(base16().lowerCase().decode(currentETag));