use of org.springframework.web.context.request.RequestAttributes in project midpoint by Evolveum.
the class SecurityUtil method getCurrentConnectionInformation.
/**
* Returns current connection information, as derived from HTTP request stored in current thread.
* May be null if the thread is not associated with any HTTP request (e.g. task threads, operations invoked from GUI but executing in background).
*/
public static HttpConnectionInformation getCurrentConnectionInformation() {
RequestAttributes attr = RequestContextHolder.getRequestAttributes();
if (!(attr instanceof ServletRequestAttributes)) {
return null;
}
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) attr;
HttpServletRequest request = servletRequestAttributes.getRequest();
HttpConnectionInformation rv = new HttpConnectionInformation();
HttpSession session = request.getSession(false);
if (session != null) {
rv.setSessionId(session.getId());
}
long start = System.currentTimeMillis();
rv.setLocalHostName(request.getLocalName());
long delta = System.currentTimeMillis() - start;
if (delta > GET_LOCAL_NAME_THRESHOLD) {
LOGGER.warn("getLocalName() on HTTP request took {} milliseconds that is too long; " + "please check your DNS configuration. Local name = {}, local address = {}", delta, request.getLocalName(), request.getLocalAddr());
}
rv.setRemoteHostAddress(getRemoteHostAddress(request));
rv.setServerName(request.getServerName());
return rv;
}
use of org.springframework.web.context.request.RequestAttributes in project grails-core by grails.
the class WebUtils method clearGrailsWebRequest.
/**
* Removes any GrailsWebRequest instance from the current request.
*/
public static void clearGrailsWebRequest() {
RequestAttributes reqAttrs = RequestContextHolder.getRequestAttributes();
if (reqAttrs != null) {
// First remove the web request from the HTTP request attributes.
GrailsWebRequest webRequest = (GrailsWebRequest) reqAttrs;
webRequest.getRequest().removeAttribute(GrailsApplicationAttributes.WEB_REQUEST);
// Now remove it from RequestContextHolder.
RequestContextHolder.resetRequestAttributes();
}
}
use of org.springframework.web.context.request.RequestAttributes in project grails-core by grails.
the class GrailsWebRequest method lookup.
/**
* Looks up the current Grails WebRequest instance
* @return The GrailsWebRequest instance
*/
@Nullable
public static GrailsWebRequest lookup() {
GrailsWebRequest webRequest = null;
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
if (requestAttributes instanceof GrailsWebRequest) {
webRequest = (GrailsWebRequest) requestAttributes;
}
return webRequest;
}
use of org.springframework.web.context.request.RequestAttributes in project grails-core by grails.
the class AbstractGrailsView method renderWithinGrailsWebRequest.
private void renderWithinGrailsWebRequest(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
boolean attributesChanged = false;
try {
GrailsWebRequest webRequest;
if (!(requestAttributes instanceof GrailsWebRequest)) {
webRequest = createGrailsWebRequest(request, response, request.getServletContext());
attributesChanged = true;
WebUtils.storeGrailsWebRequest(webRequest);
} else {
webRequest = (GrailsWebRequest) requestAttributes;
}
renderTemplate(model, webRequest, request, response);
} finally {
if (attributesChanged) {
request.removeAttribute(GrailsApplicationAttributes.WEB_REQUEST);
RequestContextHolder.setRequestAttributes(requestAttributes);
}
}
}
use of org.springframework.web.context.request.RequestAttributes in project ontrack by nemerosa.
the class DefaultURIBuilder method build.
@Override
public URI build(Object methodInvocation) {
// Default builder
UriComponentsBuilder builder = MvcUriComponentsBuilder.fromMethodCall(methodInvocation);
// Default URI
UriComponents uriComponents = builder.build();
// TODO #251 Workaround for SPR-12771
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
HttpRequest httpRequest = new ServletServerHttpRequest(request);
String portHeader = httpRequest.getHeaders().getFirst("X-Forwarded-Port");
if (StringUtils.hasText(portHeader)) {
int port = Integer.parseInt(portHeader);
String scheme = uriComponents.getScheme();
if (("https".equals(scheme) && port == 443) || ("http".equals(scheme) && port == 80)) {
port = -1;
}
builder.port(port);
}
// OK
return builder.build().toUri();
}
Aggregations