/** * Same as {@link #fromContextPath(HttpServletRequest)} except the * request is obtained through {@link RequestContextHolder}. */ public static ServletUriComponentsBuilder fromCurrentContextPath() { return fromContextPath(getCurrentRequest()); }
/** * Same as {@link #fromContextPath(HttpServletRequest)} except the * request is obtained through {@link RequestContextHolder}. */ public static ServletUriComponentsBuilder fromCurrentContextPath() { return fromContextPath(getCurrentRequest()); }
/** * Prepare a builder from the host, port, scheme, context path, and * servlet mapping of the given HttpServletRequest. * <p>If the servlet is mapped by name, e.g. {@code "/main/*"}, the path * will end with "/main". If the servlet is mapped otherwise, e.g. * {@code "/"} or {@code "*.do"}, the result will be the same as * if calling {@link #fromContextPath(HttpServletRequest)}. */ public static ServletUriComponentsBuilder fromServletMapping(HttpServletRequest request) { ServletUriComponentsBuilder builder = fromContextPath(request); if (StringUtils.hasText(new UrlPathHelper().getPathWithinServletMapping(request))) { builder.path(request.getServletPath()); } return builder; }
/** * Prepare a builder from the host, port, scheme, context path, and * servlet mapping of the given HttpServletRequest. * <p>If the servlet is mapped by name, e.g. {@code "/main/*"}, the path * will end with "/main". If the servlet is mapped otherwise, e.g. * {@code "/"} or {@code "*.do"}, the result will be the same as * if calling {@link #fromContextPath(HttpServletRequest)}. */ public static ServletUriComponentsBuilder fromServletMapping(HttpServletRequest request) { ServletUriComponentsBuilder builder = fromContextPath(request); if (StringUtils.hasText(new UrlPathHelper().getPathWithinServletMapping(request))) { builder.path(request.getServletPath()); } return builder; }
builder.append("<form id=\"confirmationForm\" name=\"confirmationForm\" action=\""); String requestPath = ServletUriComponentsBuilder.fromContextPath(request).build().getPath(); if (requestPath == null) { requestPath = "";
@Test public void fromContextPath() { this.request.setRequestURI("/mvc-showcase/data/param"); this.request.setQueryString("foo=123"); String result = ServletUriComponentsBuilder.fromContextPath(this.request).build().toUriString(); assertEquals("http://localhost/mvc-showcase", result); }
@Test // SPR-16650 public void fromContextPathWithForwardedPrefix() throws Exception { this.request.addHeader("X-Forwarded-Prefix", "/prefix"); this.request.setContextPath("/mvc-showcase"); this.request.setRequestURI("/mvc-showcase/simple"); HttpServletRequest requestToUse = adaptFromForwardedHeaders(this.request); String result = ServletUriComponentsBuilder.fromContextPath(requestToUse).build().toUriString(); assertEquals("http://localhost/prefix", result); }
private static Optional<String> buildUrl(HttpServletRequest request, URI path) { Assert.notNull(request, "request is required; it must not be null"); if (!path.isAbsolute()) { return Optional.of(fromContextPath(request).path(path.toASCIIString()).toUriString()); } return Optional.empty(); } }
private String buildSignupUrl(HttpServletRequest request) { if (signupUrl.startsWith("http://") || signupUrl.startsWith("https://")) { return signupUrl; } if (!signupUrl.startsWith("/")) { return ServletUriComponentsBuilder.fromContextPath(request).path("/" + signupUrl).build().toUriString(); } return ServletUriComponentsBuilder.fromContextPath(request).path(signupUrl).build().toUriString(); }
private String buildSignupUrl(HttpServletRequest request) { if (signupUrl.startsWith("http://") || signupUrl.startsWith("https://")) { return signupUrl; } if (!signupUrl.startsWith("/")) { return ServletUriComponentsBuilder.fromContextPath(request).path("/" + signupUrl).build().toUriString(); } return ServletUriComponentsBuilder.fromContextPath(request).path(signupUrl).build().toUriString(); }
@Override public String getProxyCallbackUrl() { if (proxyCallbackUri == null) { return null; } if (proxyCallbackUri.isAbsolute()) { return proxyCallbackUri.toASCIIString(); } return fromContextPath(context).path(proxyCallbackUri.toASCIIString()).toUriString(); }
private String buildSignupUrl(HttpServletRequest request) { if (signupUrl.startsWith("http://") || signupUrl.startsWith("https://")) { return signupUrl; } if (!signupUrl.startsWith("/")) { return ServletUriComponentsBuilder.fromContextPath(request).path("/" + signupUrl).build().toUriString(); } return ServletUriComponentsBuilder.fromContextPath(request).path(signupUrl).build().toUriString(); }
@PostMapping() @ApiOperation(nickname = "create-post", value = "Cretae a new post") public ResponseEntity<Void> createPost(@RequestBody @Valid PostForm post, HttpServletRequest request) { log.debug("create a new post@" + post); PostDetails saved = blogService.savePost(post); log.debug("saved post id is @" + saved.getId()); URI loacationHeader = ServletUriComponentsBuilder .fromContextPath(request) .path("/api/posts/{id}") .buildAndExpand(saved.getId()) .toUri(); HttpHeaders headers = new HttpHeaders(); headers.setLocation(loacationHeader); return new ResponseEntity<>(headers, HttpStatus.CREATED); }
@PostMapping(value = {""}) public ResponseEntity<Void> createUser( @RequestBody @Valid UserForm form, HttpServletRequest req) { log.debug("user data@" + form); User saved = this.userService.createUser(form); URI createdUri = ServletUriComponentsBuilder .fromContextPath(req) .path("/users/{username}") .buildAndExpand(saved.getId()).toUri(); return created(createdUri).build(); }
@PostMapping() public ResponseEntity<Void> saveUser(@RequestBody @Valid UserForm form, BindingResult errors, HttpServletRequest req) { log.debug("save user data @" + form); if (errors.hasErrors()) { throw new InvalidRequestException(errors); } UserDetails userDetails = userService.saveUser(form); log.debug("created user@" + userDetails); HttpHeaders headers = new HttpHeaders(); headers.setLocation( ServletUriComponentsBuilder .fromContextPath(req) .path(Constants.URI_API_PREFIX + Constants.URI_USERS + "/{id}") .buildAndExpand(userDetails.getId()).toUri() ); return new ResponseEntity<>(headers, HttpStatus.CREATED); }
private static ServletUriComponentsBuilder fromServletMapping(HttpServletRequest request, String basePath) { ServletUriComponentsBuilder builder = fromContextPath(request); builder.replacePath(prependForwardedPrefix(request, basePath)); if (hasText(new UrlPathHelper().getPathWithinServletMapping(request))) { builder.path(request.getServletPath()); } return builder; }
private static ServletUriComponentsBuilder fromServletMapping( HttpServletRequest request, String basePath) { ServletUriComponentsBuilder builder = fromContextPath(request); SwaggerBootstrapUiXForwardPrefixPathAdjuster adjuster = new SwaggerBootstrapUiXForwardPrefixPathAdjuster(request); builder.replacePath(adjuster.adjustedPath(basePath)); if (hasText(new UrlPathHelper().getPathWithinServletMapping(request))) { builder.path(request.getServletPath()); } return builder; } }
private static ServletUriComponentsBuilder fromServletMapping( HttpServletRequest request, String basePath) { ServletUriComponentsBuilder builder = fromContextPath(request); XForwardPrefixPathAdjuster adjuster = new XForwardPrefixPathAdjuster(request); builder.replacePath(adjuster.adjustedPath(basePath)); if (hasText(new UrlPathHelper().getPathWithinServletMapping(request))) { builder.path(request.getServletPath()); } return builder; } }
@Override protected String determineTargetUrl(HttpServletRequest request, HttpServletResponse response) { UriComponentsBuilder builder = UriComponentsBuilder.fromUri(casLogout); addLogoutServiceParameter(builder, fromContextPath(request).build().toUriString()); return builder.build().toUriString(); } }
@PostMapping("") public ResponseEntity save(@RequestBody VehicleForm form, HttpServletRequest request) { Vehicle saved = this.vehicles.save(Vehicle.builder().name(form.getName()).build()); return created( ServletUriComponentsBuilder .fromContextPath(request) .path("/v1/vehicles/{id}") .buildAndExpand(saved.getId()) .toUri()) .build(); }