/** * Whether the URL is relative, i.e. not absolute. * * @return true if the URL is relative. * @see {@link #isAbsolute()} */ public boolean isRelative() { return !isAbsolute(); }
/** * Return the encoded url string. * * @return the encoded url string */ public String encodedUri() { return toString(); }
/** * Convert this object to the {@link URL} class from the java standard library. * * @return a {@link URL} */ public URL toURL() { try { return toURI().toURL(); } catch (MalformedURLException e) { throw propagate(e); } }
@Test public void canDropThePath() throws Exception { Url fqUrl = url("/landing/de3445") .authority("example.com") .build(); Url newUrl = fqUrl.newBuilder() .dropHost() .build(); assertThat(newUrl.authority(), isAbsent()); assertThat(newUrl.toString(), is("/landing/de3445")); }
private void setHostHeader() { url.authority() .ifPresent(authority -> header(HOST, authority.hostAndPort())); }
private static Origin originFromRequest(HttpRequest request) { String hostAndPort = request.header(HOST) .orElseGet(() -> { checkArgument(request.url().isAbsolute(), "host header is not set for request=%s", request); return request.url().authority().map(Url.Authority::hostAndPort) .orElseThrow(() -> new IllegalArgumentException("Cannot send request " + request + " as URL is not absolute and no HOST header is present")); }); return newOriginBuilder(HostAndPort.fromString(hostAndPort)).build(); }
@Test public void identifiesCorrectlyIfUrlIsFullyQualified() throws Exception { Url fqUrl = url("http://example.com").build(); assertThat(fqUrl.isFullyQualified(), is(true)); assertThat(fqUrl.isAbsolute(), is(false)); assertThat(fqUrl.isRelative(), is(true)); Url nonFqUrl = url("/somepath").build(); assertThat(nonFqUrl.isFullyQualified(), is(false)); assertThat(nonFqUrl.isAbsolute(), is(true)); assertThat(nonFqUrl.isRelative(), is(false)); }
/** * Get all query parameters. * * @return all query parameters */ public Map<String, List<String>> queryParams() { return url.queryParams(); }
/** * Returns the requested path. * * @return the path being requested */ public String path() { return url.path(); }
@Test public void decodesMultipleQueryParams() { Url url = url("http://example.com?foo=bar&foo=hello&abc=def") .build(); assertThat(url.queryParamNames(), containsInAnyOrder("foo", "abc")); assertThat(url.queryParams(), isMap(ImmutableMap.of( "foo", asList("bar", "hello"), "abc", singletonList("def") ))); }
/** * Get a query parameter by name if present. * * @param name parameter name * @return query parameter if present */ public Optional<String> queryParam(String name) { return url.queryParam(name); }
/** * Get the names of all query parameters. * * @return the names of all query parameters. */ public Iterable<String> queryParamNames() { return url.queryParamNames(); }
@Test public void acceptsEmptyQueryString() { Url url = url("/webapp/assets/images/icons.eot?").build(); assertThat(url.encodedUri(), is("/webapp/assets/images/icons.eot?")); assertThat(url.queryParams().isEmpty(), is(true)); }
@Test public void decodesQueryParams() { Url url = url("http://example.com/?foo=bar") .build(); System.out.println(url.query()); assertThat(url.queryParam("foo"), isValue("bar")); }
@Test public void canEncodeTheModifiedUrl() throws Exception { Url url = url("/search.do?foo=a%2Bb%3Dc").build(); Url newUrl = url.newBuilder().path("/v1/search").build(); assertThat(newUrl.encodedUri(), is("/v1/search?foo=a%2Bb%3Dc")); }
/** * Whether the URL is fully-qualified, i.e. whether there is a host present. * * @return true if the URL is fully-qualified */ public boolean isFullyQualified() { Optional<String> host = host(); return host.isPresent() && !isNullOrEmpty(host.get()); }
/** * Rewrites the URL of the request according to the rewrite rules. * * @param request a request * @return a rewritten request */ public LiveHttpRequest rewrite(LiveHttpRequest request) { String path = request.path(); String newPath = newPath(path); if (!Objects.equals(newPath, path)) { Url newUrl = request.url().newBuilder().path(newPath).build(); return request.newBuilder() .url(newUrl) .build(); } return request; }
private void setHostHeader() { url.authority() .ifPresent(authority -> header(HOST, authority.hostAndPort())); }
private static Origin originFromRequest(LiveHttpRequest request, Boolean isHttps) { String hostAndPort = request.header(HOST) .orElseGet(() -> { checkArgument(request.url().isAbsolute(), "host header is not set for request=%s", request); return request.url().authority().map(Url.Authority::hostAndPort) .orElseThrow(() -> new IllegalArgumentException("Cannot send request " + request + " as URL is not absolute and no HOST header is present")); }); HostAndPort host = HostAndPort.fromString(hostAndPort); if (host.getPortOrDefault(-1) < 0) { host = host.withDefaultPort(isHttps ? DEFAULT_HTTPS_PORT : DEFAULT_HTTP_PORT); } return newOriginBuilder(host.getHostText(), host.getPort()).build(); }