private static String checkValidUrlItem(final String id, final String item) { checkNotNullOrEmpty(id, item + " should not be null or empty"); if (id.contains(URLs.SEPARATOR)) { throw new IllegalArgumentException(item + " should not contain '/'"); } if (!URLs.isValidUrl(id)) { throw new IllegalArgumentException(item + " should not contains invalid URI character"); } return id; }
@Override public String apply(final String uri) { return join(context, uri); } }
public static String join(final String base, final String... paths) { String target = base; for (String path : paths) { target = doJoin(target, path); } return target; }
private String subResourceUri(final RestIdMatcher resourceName) { return join(resourceRoot(resourceName.resourceUri()), this.uriPart); } }
@Override public URL apply(final HttpRequest input) { return toUrl(url.readFor(Optional.of(input)).toString()); } };
@Override protected RequestMatcher getBaseRequestMatcher(final RestIdMatcher resourceName) { return by(uri(resourceRoot(resourceName.resourceUri()))); } }
public MountTo(final String target) { this.target = toBase(target); }
private static String doJoin(final String base, final String path) { String joinPath = toJoinPath(path); if (base.endsWith(SEPARATOR)) { return base + joinPath; } if (joinPath.isEmpty()) { return base; } return base + SEPARATOR + joinPath; }
public static ResponseHandler proxy(final ContentResource url, final Failover failover) { return new ProxyResponseHandler(toUrlFunction(checkNotNull(url, "URL should not be null")), checkNotNull(failover, "Failover should not be null")); }
public RestRequestDispatcher(final String name, final Iterable<RestSetting> settings) { this.name = eq(name); this.getAllSettings = filterSettings(settings, RestAllSetting.class, HttpMethod.GET); this.getSingleSettings = filterSettings(settings, RestSingleSetting.class, HttpMethod.GET); this.postSettings = filterSettings(settings, RestAllSetting.class, HttpMethod.POST); this.putSettings = filterSettings(settings, RestSingleSetting.class, HttpMethod.PUT); this.deleteSettings = filterSettings(settings, RestSingleSetting.class, HttpMethod.DELETE); this.headSettings = filterSettings(settings, RestSingleSetting.class, HttpMethod.HEAD); this.headAllSettings = filterSettings(settings, RestAllSetting.class, HttpMethod.HEAD); this.patchSettings = filterSettings(settings, RestSingleSetting.class, HttpMethod.PATCH); this.subResourceSettings = filter(settings, SubResourceSetting.class); this.allMatcher = by(uri(resourceRoot(name))); this.singleMatcher = Moco.match(uri(join(resourceRoot(name), "[^/]*"))); }
private Optional<URL> remoteUrl(final HttpRequest request) { Optional<String> remoteUrl = this.doRemoteUrl(request); if (!remoteUrl.isPresent()) { return absent(); } QueryStringEncoder encoder = new QueryStringEncoder(remoteUrl.get()); for (Map.Entry<String, String[]> entry : request.getQueries().entrySet()) { for (String value : entry.getValue()) { encoder.addParam(entry.getKey(), value); } } try { return of(toUrl(encoder.toString())); } catch (IllegalArgumentException e) { return absent(); } }
@Override public void resource(final String name, final RestSetting setting, final RestSetting... settings) { checkResourceName(name); RestHandler handler = new RestHandler(name, asIterable( checkNotNull(setting, "Rest setting should not be null"), checkNotNull(settings, "Rest settings should not be null"))); this.request(InternalApis.context(resourceRoot(name))).response(handler); } }
public String remoteUrl(final String uri) { String relative = nullToEmpty(uri.replaceFirst(this.localBase, "")); return join(remoteBase, relative); }
@Test public void should_know_valid_url_character() { assertThat(URLs.isValidUrl("base"), is(true)); assertThat(URLs.isValidUrl("base path"), is(false)); } }
@Override public Optional<ResponseHandler> getMatched(final RestIdMatcher resourceName, final HttpRequest httpRequest) { for (RestSetting setting : settings) { RestIdMatcher idMatcher = RestIdMatchers.match(join(resourceName.resourceUri(), this.id.resourceUri(), this.name)); Optional<ResponseHandler> responseHandler = setting.getMatched(idMatcher, httpRequest); if (responseHandler.isPresent()) { return responseHandler; } } return Optional.absent(); } }
@Test public void should_join_path() { assertThat(URLs.join("base", "path"), is("base/path")); assertThat(URLs.join("base/", "path"), is("base/path")); assertThat(URLs.join("base", ""), is("base")); assertThat(URLs.join("base", "/path"), is("base/path")); assertThat(URLs.join("base/", "/path"), is("base/path")); assertThat(URLs.join("base", "path", "sub"), is("base/path/sub")); }
public static RequestMatcher context(final String context) { return or(by(uri(context)), match(uri(URLs.join(context, ".*")))); }