Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow use of MvcUriComponentsBuilder independent of Servlet request lifecycle [SPR-12800] #17397

Closed
spring-issuemaster opened this issue Mar 10, 2015 · 6 comments

Comments

Projects
None yet
2 participants
@spring-issuemaster
Copy link
Collaborator

commented Mar 10, 2015

Simon opened SPR-12800 and commented

It is a common requirement to generate links to controllers outside of an HttpServletRequest when generating E-Mails etc. Therefore it would be nice to have a way to create links which uses a configurable baseURI for the link generation instead of the request context path like this:

private String fromStatelessMethodCall(Object invocationInfo) {
  MvcUriComponentsBuilder.MethodInvocationInfo info = (MvcUriComponentsBuilder.MethodInvocationInfo) invocationInfo;
  Method method = info.getControllerMethod();
  String typePath = getTypeRequestMapping(method.getDeclaringClass());
  String methodPath = getMethodRequestMapping(method);
  String path = pathMatcher.combine(typePath, methodPath);
  UriComponentsBuilder builder = UriComponentsBuilder.fromUri(baseURI).path(path);
  UriComponents uriComponents = applyContributors(builder, method, info.getArgumentValues());
  return uriComponents.toUriString();
}

Affects: 4.1.5

Issue Links:

  • #17218 Allow customizing "base" path in MvcUriComponentsBuilder (e.g. path prefix for locale)

Referenced from: commits febcd0c

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Mar 13, 2015

Rossen Stoyanchev commented

This is very closely related to #17218 which comes from a different angle (i.e. inserting a custom path prefix for the locale of a request) but ultimately comes down to customizing the use of ServletUriComponentsBuilder.fromCurrentServletMapping() within MvcUriComponentsBuilder.

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Mar 16, 2015

Rossen Stoyanchev commented

I'm thinking about adding static methods like setCurrentRequest(UriComponentsBuilder) and resetCurrentRequest() that would set up a ThreadLocal variable within MvcUriComponentsBuilder.

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Mar 16, 2015

Simon commented

Since the creation of absolute urls seems not to be an edge case wouldnt it be better to provide a simple method that allows a base url parameter. This would avoid thread locals.

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Mar 16, 2015

Rossen Stoyanchev commented

Yes we can do that also. It really means adding overloaded methods for all fromXxx methods.

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Mar 16, 2015

Rossen Stoyanchev commented

I've added overloaded methods.

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Mar 17, 2015

Simon commented

Nice, thank you for looking into it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.