Java Code Examples for org.springframework.web.servlet.support.ServletUriComponentsBuilder#fromCurrentRequest()

The following examples show how to use org.springframework.web.servlet.support.ServletUriComponentsBuilder#fromCurrentRequest() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ResponseLinksMapper.java    From moserp with Apache License 2.0 6 votes vote down vote up
Object fixLink(Object value) {
    if (!(value instanceof String)) {
        return value;
    }
    String href = (String) value;
    Matcher urlWithServiceNameMatcher = urlWithServiceNamePattern.matcher(href);
    Matcher standardUrlMatcher = standardUrlPattern.matcher(href);
    if (!standardUrlMatcher.matches() && urlWithServiceNameMatcher.matches()) {
        String possibleServiceName = urlWithServiceNameMatcher.group(1);
        log.debug("Possible service name: " + possibleServiceName);
        if (services.contains(possibleServiceName)) {
            log.debug("Service found");
            String gatewayPath = serviceRouteMapper.apply(possibleServiceName);
            String originalRestPath = urlWithServiceNameMatcher.group(2);
            ServletUriComponentsBuilder servletUriComponentsBuilder = ServletUriComponentsBuilder.fromCurrentRequest();
            UriComponents uriComponents = servletUriComponentsBuilder.replacePath(gatewayPath).path(originalRestPath).build();
            log.debug("Mapping " + value + " to " +  uriComponents);
            return uriComponents.toString();
        }
    }
    return href;
}
 
Example 2
Source File: StyleServiceImpl.java    From molgenis with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * 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;
  }
}
 
Example 3
Source File: UserController.java    From spring-data-examples with Apache License 2.0 5 votes vote down vote up
@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";
}
 
Example 4
Source File: MolgenisServletUriComponentsBuilder.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Creates a ServletUriComponentsBuilder from the current request with decoded query params. This
 * is needed to prevent double encoding for the query parameters when using
 * ServletUriComponentsBuilder.build(encoded=false)
 * ServletUriComponentsBuilder.build(encoded=true) cannot be used for RSQL since it throws an
 * exception on the use of '='
 */
// removing cast results in 'unclear varargs or non-varargs' warning
@SuppressWarnings("RedundantCast")
public static ServletUriComponentsBuilder fromCurrentRequestDecodedQuery() {
  ServletUriComponentsBuilder builder = ServletUriComponentsBuilder.fromCurrentRequest();
  Map<String, String[]> params = getCurrentRequest().getParameterMap();
  for (Entry<String, String[]> param : params.entrySet()) {
    builder.replaceQueryParam(param.getKey(), (Object[]) param.getValue());
  }
  return builder;
}
 
Example 5
Source File: ServletUriComponentsBuilderFactory.java    From molgenis with GNU Lesser General Public License v3.0 4 votes vote down vote up
public ServletUriComponentsBuilder fromCurrentRequest() {
  return ServletUriComponentsBuilder.fromCurrentRequest();
}