Refine search
public static UriComponentsBuilder getURIBuilder(String path, boolean zoneSwitchPossible) { UriComponentsBuilder builder = ServletUriComponentsBuilder.fromCurrentContextPath().path(path); if (zoneSwitchPossible) { String host = builder.build().getHost(); IdentityZone current = IdentityZoneHolder.get(); if (host != null && !IdentityZoneHolder.isUaa()) { if (!host.startsWith(current.getSubdomain() + ".")) { host = current.getSubdomain() + "." + host; builder.host(host); } } } return builder; }
@RequestMapping(path = "/", produces = "text/html") public String home(HttpServletRequest request, Map<String, Object> model) { if (isForceSsl() && !request.isSecure()) { String securedUrl = ServletUriComponentsBuilder.fromCurrentRequest() .scheme("https").build().toUriString(); return "redirect:" + securedUrl; } renderHome(model); return "home"; }
@Test public void fromRequestEncodedPath() { this.request.setRequestURI("/mvc-showcase/data/foo%20bar"); String result = ServletUriComponentsBuilder.fromRequest(this.request).build().toUriString(); assertEquals("http://localhost/mvc-showcase/data/foo%20bar", result); }
/** * Same as {@link #fromServletMapping(HttpServletRequest)} except the * request is obtained through {@link RequestContextHolder}. */ public static ServletUriComponentsBuilder fromCurrentServletMapping() { return fromServletMapping(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, and context path of the * given HttpServletRequest. */ public static ServletUriComponentsBuilder fromContextPath(HttpServletRequest request) { ServletUriComponentsBuilder builder = initFromRequest(request); builder.replacePath(request.getContextPath()); return builder; }
@PostMapping public ResponseEntity<?> postBook(@RequestBody Book book) { bookService.saveBook(book); URI location = ServletUriComponentsBuilder .fromCurrentRequest() .path("/{id}") .buildAndExpand(book.getId()) .toUri(); return ResponseEntity .created(location) .body(book); }
ServletUriComponentsBuilder.fromCurrentRequestUri().replacePath( ServletUriComponentsBuilder.fromCurrentRequest().build().getPath().replace("-old","") ); uriComponentsBuilder.build() : uriComponentsBuilder .path("/{id}") .buildAndExpand(addedItemId); return ResponseEntity.created(uriComponents.toUri()).build();
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(); }
/** * Build a downLoadUrl for the file identified by the given fileId. If this method is called * outside of a request context ( system initialization for example) the server context is used * for generating a relative path */ private String buildFileUrl(String fileId) { if (RequestContextHolder.getRequestAttributes() != null) { ServletUriComponentsBuilder currentRequest = ServletUriComponentsBuilder.fromCurrentRequest(); UriComponents downloadUri = currentRequest .replacePath(FileDownloadController.URI + '/' + fileId) .replaceQuery(null) .build(); return downloadUri.toUriString(); } else { // TODO this is a workaround for the situation where the File url needs to be created without // a request // context, in order to fix the properly we would need to add a deploy time property // defining the file download server location return FileDownloadController.URI + '/' + fileId; } } }
@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(); }
HttpServletResponse response ) throws IOException { ServletUriComponentsBuilder builder = ServletUriComponentsBuilder.fromRequestUri(request); String redirectUri = builder.replacePath(pkceProperties.getTokenEndpoint()).build().toUriString();
@RequestMapping(path = "/workbooks/{workbookId}/comments", method = RequestMethod.POST) public @ResponseBody ResponseEntity<?> createComments(@PathVariable("workbookId") String workbookId, @RequestBody Comment comment) { comment.setDomainType(WORKBOOK); comment.setDomainId(workbookId); commentRepository.saveAndFlush(comment); final URI location = ServletUriComponentsBuilder .fromCurrentServletMapping().path("/api/workbooks/{workbookId}/comments/{commentId}").build() .expand(workbookId, comment.getId()).toUri(); return ResponseEntity.created(location).build(); }
@Override protected String determineTargetUrl(HttpServletRequest request, HttpServletResponse response) { UriComponentsBuilder builder = UriComponentsBuilder.fromUri(casLogout); addLogoutServiceParameter(builder, fromContextPath(request).build().toUriString()); return builder.build().toUriString(); } }
private static UriComponentsBuilder initBaseUrl() { UriComponentsBuilder builder = ServletUriComponentsBuilder.fromCurrentServletMapping(); return UriComponentsBuilder.fromPath(builder.build().getPath()); }
private static String getPath() { UriComponentsBuilder builder = ServletUriComponentsBuilder.fromCurrentServletMapping(); String path = builder.build().getPath(); return path != null ? path : ""; }
@Test public void fromServletMapping() { this.request.setRequestURI("/mvc-showcase/app/simple"); this.request.setServletPath("/app"); this.request.setQueryString("foo=123"); String result = ServletUriComponentsBuilder.fromServletMapping(this.request).build().toUriString(); assertEquals("http://localhost/mvc-showcase/app", result); }
@Test // SPR-10272 public void pathExtension() { this.request.setRequestURI("/rest/books/6.json"); ServletUriComponentsBuilder builder = ServletUriComponentsBuilder.fromRequestUri(this.request); String extension = builder.removePathExtension(); String result = builder.path("/pages/1.{ext}").buildAndExpand(extension).toUriString(); assertEquals("http://localhost/rest/books/6/pages/1.json", result); }
@RequestMapping(method = RequestMethod.POST, value = "/{user}/customers") ResponseEntity<Customer> addCustomer(@PathVariable Long user, @RequestBody Customer c) { Customer customer = crmService.addCustomer(user, c.getFirstName(), c.getLastName()); URI uriOfNewResource = ServletUriComponentsBuilder.fromCurrentContextPath() .path("/users/{user}/customers/{customer}") .buildAndExpand(user, customer.getId()) .toUri(); HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setLocation(uriOfNewResource); return new ResponseEntity<>(customer, httpHeaders, HttpStatus.CREATED); }
@RequestMapping(value = "/", method = RequestMethod.GET) String index(Model model, // @QuerydslPredicate(root = User.class) Predicate predicate, // @PageableDefault(sort = { "lastname", "firstname" }) Pageable pageable, // @RequestParam MultiValueMap<String, String> parameters) { ServletUriComponentsBuilder builder = ServletUriComponentsBuilder.fromCurrentRequest(); builder.replaceQueryParam("page", new Object[0]); model.addAttribute("baseUri", builder.build().toUri()); model.addAttribute("users", repository.findAll(predicate, pageable)); return "index"; } }