If my absolute path in HttpServletRequest
is http://localhost:8080/my-app/sub/servlet/path
, I would want to extract only http://localhost:8080/
(means schema + domain + port). I don't know the exact naming for this part combination.
There is org.springframework.web.util.UrlPathHelper
for all sort of extracting parts of the url, but this is missing.
Maybe (to complete the helper methods), you could add something similar:
/**
* Example
* requestURL: http://localhost:8080/my-app/sub/servlet/path
* requestURI: /my-app/sub/servlet/path
* @return http://localhost:8080
*/
public static String getWebserverPath(HttpServletRequest req) {
return StringUtils.substringBefore(req.getRequestURL().toString(), req.getRequestURI());
}
Comment From: rstoyanchev
Maybe I'm missing something but wouldn't that be?
UrlPathHelper pathHelper = new UrlPathHelper();
pathHelper.setAlwaysUseFullPath(true);
pathHelper.getLookupPathForRequest(request);
Comment From: membersound
Well, if it' supposed to work like this, then it would be a bug:
@Test
public void testParseMainWebserverPath() {
MockHttpServletRequest req = new MockHttpServletRequest();
req.setServerName("www.example.com");
req.setServerPort(8080);
req.setScheme("https");
req.setServletPath("/my-app");
req.setRequestURI("/my-app/foo");
//OK
assertEquals("https://www.example.com:8080",
StringUtils.substringBefore(req.getRequestURL().toString(), req.getRequestURI()));
org.springframework.web.util.UrlPathHelper h = new org.springframework.web.util.UrlPathHelper();
h.setAlwaysUseFullPath(true);
//FAILS. result is: "/my-app/foo"
assertEquals("https://www.example.com:8080", h.getLookupPathForRequest(req));
}
Comment From: ganeshbch
/**
* Example
* requestURL: http://localhost:8080/my-app/sub/servlet/path
* requestURI: /my-app/sub/servlet/path
* @return http://localhost:8080
*/
public static String extractURL(String url){
// Split the url by '//' ( This can occur only at 1 place in any url)
String[] stringparts = url.split("//");
// The first element of stringparts is the schema (namely http:)
String schema = stringparts[0];
// The remaning URL with the second element
String remainingURL = stringparts[1];
// Append '//' to schema as the string has been split by this character
schema+="//";
// Only the part of the url before the first '/' is the domain and port
String[] urlparts = remainingURL.split("/");
// get the domain and port
String domainandport = urlparts[0];
// Append '/' to domainandport as string was split by '/'
domainandport+='/';
// Result url required is Schema+domain+port
String resultURL = schema.concat(domainandport);
//Return the result
return resultURL;
}
public static String getWebserverPath(HttpServletRequest req) {
//return StringUtils.substringBefore(req.getRequestURL().toString(), //req.getRequestURI());
return extractURL(req.getRequestURL().toString());
}
The extractURL function extracts the required schema+domain+port and returns the results as string.
`url = http://localhost:8080/my-app/sub/servlet/path`
It works by splitting the url by '//' as this can occur only once in any url.
the first part in this split is the schema (http in this case).
`schema = http://`
The second part is the remaining url.
`remaining url = localhost:8080/my-app/sub/servlet/path`
The part of the remaning url before the occurance of first '/' is the domain and port.
`first part of remaning url = localhost:8080/`
We can split the remaining url by '/' and extract only the first split.
In this way we can extract schema and domain+port in any URL and return the result as a string.
`Concatenation of schema and first part of remaning url = http://localhost:8080/
` Hope this solves the issue.
I tested the extractURL function for http://localhost:8080/my-app/sub/servlet/path and it returns the expected result.
Comment From: membersound
@ganeshbhargav Well the thing is not how to extract the desired part from the url. But rather that UrlPathHelper
should provide a method that can return this part (as all other parts can be already retrieved from UrlPathHelper
).
Comment From: rstoyanchev
Okay so I misunderstood. What you want is not the path but the scheme + host + port. That's not in scope for this class which deals mainly with the path as its name suggests. It is mainly for internal use by the framework to derive the path to use for request mapping.
You can consider UriComponentsBuilder#fromUri
along with replacePath(null)
and replaceQuery(null)
. In any case UrlPathHelper
isn't the place for it.
Comment From: membersound
I discovered that ServletUriComponentsBuilder
gives exactly what I was looking for:
ServletUriComponentsBuilder.fromCurrentContextPath()
.replacePath("/")
.toUriString();