private static AnnotatedValueResolver ofPathVariable(String name, AnnotatedElement annotatedElement, AnnotatedElement typeElement, Class<?> type, @Nullable String description) { return builder(annotatedElement, type) .annotationType(Param.class) .httpElementName(name) .typeElement(typeElement) .supportOptional(true) .pathVariable(true) .description(description) .resolver(resolver(ctx -> ctx.context().pathParam(name))) .build(); }
ctx.pathParam("bindingProtocol"); return CompletableFuture.completedFuture( configurator.idpConfigs().get(idpEntityId));
@Override public HttpResponse serve(ServiceRequestContext ctx, HttpRequest req) throws Exception { final MetadataService mds = MetadataServiceInjector.getMetadataService(ctx); final User user = AuthUtil.currentUser(ctx); final String projectName = ctx.pathParam("projectName"); checkArgument(!isNullOrEmpty(projectName), "no project name is specified"); final String repoName = ctx.pathParam("repoName"); checkArgument(!isNullOrEmpty(repoName), "no repository name is specified"); if (Project.REPO_DOGMA.equals(repoName)) { return serveInternalRepo(ctx, req, mds, user, projectName); } else { return serveUserRepo(ctx, req, mds, user, projectName, repoName); } }
private static String getPath(ServiceRequestContext ctx) { // check the path param first final String path = ctx.pathParam("path"); if (!isNullOrEmpty(path)) { return path; } // then check HTTP query final String query = ctx.query(); if (query != null) { final List<String> params = new QueryStringDecoder(query, false).parameters().get("path"); if (params != null) { return params.get(0); } } // return empty string if there's no path return ""; }
private static String getPath(ServiceRequestContext ctx) { // check the path param first final String path = ctx.pathParam("path"); if (!isNullOrEmpty(path)) { return path; } // then check HTTP query final String query = ctx.query(); if (query != null) { final List<String> params = new QueryStringDecoder(query, false).parameters().get("path"); if (params != null) { return params.get(0); } } // return empty string if there's no path return ""; }
private static String getPath(ServiceRequestContext ctx) { // check the path param first final String path = ctx.pathParam("path"); if (!isNullOrEmpty(path)) { return path; } // then check HTTP query final String query = ctx.query(); if (query != null) { final List<String> params = new QueryStringDecoder(query, false).parameters().get("path"); if (params != null) { return params.get(0); } } // return empty string if there's no path return ""; }
@Test public void invalidPath() throws Exception { final ServiceRequestContext ctx = mock(ServiceRequestContext.class); final String filePath = "/"; // a directory when(ctx.pathParam("path")).thenReturn(filePath); final Optional<Query<?>> query = convert(ctx); assertThat(query.isPresent()).isFalse(); }
Class<?> expectedResultType) throws Exception { if (expectedResultType == Project.class) { final String projectName = ctx.pathParam("projectName"); checkArgument(!isNullOrEmpty(projectName), "project name should not be null or empty."); final String projectName = ctx.pathParam("projectName"); checkArgument(!isNullOrEmpty(projectName), "project name should not be null or empty."); final String repositoryName = ctx.pathParam("repoName"); checkArgument(!isNullOrEmpty(repositoryName), "repository name should not be null or empty.");
@Override public HttpResponse serve(ServiceRequestContext ctx, HttpRequest req) throws Exception { final MetadataService mds = MetadataServiceInjector.getMetadataService(ctx); final User user = AuthUtil.currentUser(ctx); final String projectName = ctx.pathParam("projectName"); checkArgument(!isNullOrEmpty(projectName), "no project name is specified"); final String repoName = ctx.pathParam("repoName"); checkArgument(!isNullOrEmpty(repoName), "no repository name is specified"); if (Project.REPO_DOGMA.equals(repoName)) { return serveInternalRepo(ctx, req, mds, user, projectName); } else { return serveUserRepo(ctx, req, mds, user, projectName, repoName); } }
@Override public final HttpResponse serve(Service<HttpRequest, HttpResponse> delegate, ServiceRequestContext ctx, HttpRequest req) throws Exception { final MetadataService mds = MetadataServiceInjector.getMetadataService(ctx); final User user = AuthenticationUtil.currentUser(ctx); final String projectName = ctx.pathParam("projectName"); checkArgument(!isNullOrEmpty(projectName), "no project name is specified"); final String repoName = ctx.pathParam("repoName"); checkArgument(!isNullOrEmpty(repoName), "no repository name is specified"); if (Project.REPO_DOGMA.equals(repoName)) { return serveInternalRepo(delegate, ctx, req, mds, user, projectName); } else { return serveUserRepo(delegate, ctx, req, mds, user, projectName, repoName); } }
@Test public void convertIdentityQuery() throws Exception { final ServiceRequestContext ctx = mock(ServiceRequestContext.class); final String filePath = "/a.txt"; when(ctx.pathParam("path")).thenReturn(filePath); final Optional<Query<?>> query = convert(ctx); assert query.isPresent(); assertThat(query.get().type()).isSameAs(QueryType.IDENTITY); assertThat(query.get().path()).isEqualTo(filePath); }
@Test public void convertJsonPathQuery() throws Exception { final ServiceRequestContext ctx = mock(ServiceRequestContext.class); final String jsonFilePath = "/a.json"; when(ctx.pathParam("path")).thenReturn(jsonFilePath); final String httpQuery = "?jsonpath=%22%24.a%22"; // "$.a" when(ctx.query()).thenReturn(httpQuery); final Optional<Query<?>> query = convert(ctx); assert query.isPresent(); assertThat(query.get().type()).isSameAs(QueryType.JSON_PATH); assertThat(query.get().path()).isEqualTo(jsonFilePath); assertThat(query.get().expressions().get(0)).isEqualTo("\"$.a\""); }
@Test public void withoutExpression() throws Exception { final ServiceRequestContext ctx = mock(ServiceRequestContext.class); // even though the file is a JSON file, the converted query's type will be IDENTITY because there's no // expression final String jsonFilePath = "/a.json"; when(ctx.pathParam("path")).thenReturn(jsonFilePath); when(ctx.query()).thenReturn(""); final Optional<Query<?>> query = convert(ctx); assert query.isPresent(); assertThat(query.get().type()).isSameAs(QueryType.IDENTITY); assertThat(query.get().path()).isEqualTo(jsonFilePath); }
@Override public HttpResponse serve(ServiceRequestContext ctx, HttpRequest req) throws Exception { final MetadataService mds = MetadataServiceInjector.getMetadataService(ctx); final User user = AuthUtil.currentUser(ctx); final String projectName = ctx.pathParam("projectName"); checkArgument(!isNullOrEmpty(projectName), "no project name is specified"); try { return HttpResponse.from(mds.findRole(projectName, user).handle((role, cause) -> { if (cause != null) { return handleException(ctx, cause); } if (!user.isAdmin() && !accessibleRoles.contains(role)) { return HttpApiUtil.throwResponse( ctx, HttpStatus.FORBIDDEN, "You must have one of the following roles to access the project '%s': %s", projectName, roleNames); } try { return delegate().serve(ctx, req); } catch (Exception e) { return Exceptions.throwUnsafely(e); } })); } catch (Throwable cause) { return handleException(ctx, cause); } }
Class<?> expectedResultType) throws Exception { if (expectedResultType == Project.class) { final String projectName = ctx.pathParam("projectName"); checkArgument(!isNullOrEmpty(projectName), "project name should not be null or empty."); final String projectName = ctx.pathParam("projectName"); checkArgument(!isNullOrEmpty(projectName), "project name should not be null or empty."); final String repositoryName = ctx.pathParam("repoName"); checkArgument(!isNullOrEmpty(repositoryName), "repository name should not be null or empty.");
Class<?> expectedResultType) throws Exception { if (expectedResultType == Project.class) { final String projectName = ctx.pathParam("projectName"); checkArgument(!isNullOrEmpty(projectName), "project name should not be null or empty."); final String projectName = ctx.pathParam("projectName"); checkArgument(!isNullOrEmpty(projectName), "project name should not be null or empty."); final String repositoryName = ctx.pathParam("repoName"); checkArgument(!isNullOrEmpty(repositoryName), "repository name should not be null or empty.");
@Override public HttpResponse serve(ServiceRequestContext ctx, HttpRequest req) throws Exception { final MetadataService mds = MetadataServiceInjector.getMetadataService(ctx); final User user = AuthUtil.currentUser(ctx); final String projectName = ctx.pathParam("projectName"); checkArgument(!isNullOrEmpty(projectName), "no project name is specified"); try { return HttpResponse.from(mds.findRole(projectName, user).handle((role, cause) -> { if (cause != null) { return handleException(ctx, cause); } if (!user.isAdmin() && !accessibleRoles.contains(role)) { return HttpApiUtil.throwResponse( ctx, HttpStatus.FORBIDDEN, "You must have one of the following roles to access the project '%s': %s", projectName, roleNames); } try { return delegate().serve(ctx, req); } catch (Exception e) { return Exceptions.throwUnsafely(e); } })); } catch (Throwable cause) { return handleException(ctx, cause); } }
@Override public final HttpResponse serve(Service<HttpRequest, HttpResponse> delegate, ServiceRequestContext ctx, HttpRequest req) throws Exception { final MetadataService mds = MetadataServiceInjector.getMetadataService(ctx); final User user = AuthenticationUtil.currentUser(ctx); final String projectName = ctx.pathParam("projectName"); checkArgument(!isNullOrEmpty(projectName), "no project name is specified"); try { return HttpResponse.from(mds.findRole(projectName, user).handle((role, cause) -> { if (cause != null) { return handleException(cause); } if (!isAccessAllowed(ctx, req, user, role)) { return HttpApiUtil.throwResponse( HttpStatus.FORBIDDEN, "You must be %s of project '%s'.", role, projectName); } try { return delegate.serve(ctx, req); } catch (Exception e) { return Exceptions.throwUnsafely(e); } })); } catch (Throwable cause) { return handleException(cause); } }