1
|
|
|
package org.apereo.cas.util; |
2
|
|
|
|
3
|
|
|
import org.apache.commons.lang3.StringUtils; |
4
|
|
|
import org.apereo.cas.authentication.adaptive.geo.GeoLocationRequest; |
5
|
|
|
import org.apereo.cas.authentication.principal.WebApplicationService; |
6
|
|
|
import org.apereo.cas.web.support.ArgumentExtractor; |
7
|
|
|
import org.slf4j.Logger; |
8
|
|
|
import org.slf4j.LoggerFactory; |
9
|
|
|
import org.springframework.web.context.request.RequestContextHolder; |
10
|
|
|
import org.springframework.web.context.request.ServletRequestAttributes; |
11
|
|
|
|
12
|
|
|
import javax.servlet.http.HttpServletRequest; |
13
|
|
|
import javax.servlet.http.HttpServletResponse; |
14
|
|
|
import java.util.Enumeration; |
15
|
|
|
import java.util.LinkedHashMap; |
16
|
|
|
import java.util.List; |
17
|
|
|
import java.util.Map; |
18
|
|
|
import java.util.Objects; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* This is {@link HttpRequestUtils}. |
22
|
|
|
* |
23
|
|
|
* @author Misagh Moayyed |
24
|
|
|
* @since 5.2.0 |
25
|
|
|
*/ |
26
|
|
|
public final class HttpRequestUtils { |
27
|
|
|
/** |
28
|
|
|
* Constant representing the request header for user agent. |
29
|
|
|
*/ |
30
|
|
|
public static final String USER_AGENT_HEADER = "user-agent"; |
31
|
|
|
|
32
|
|
|
private static final Logger LOGGER = LoggerFactory.getLogger(HttpRequestUtils.class); |
33
|
|
|
|
34
|
|
|
private HttpRequestUtils() {} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Gets http servlet request from request attributes. |
38
|
|
|
* |
39
|
|
|
* @return the http servlet request from request attributes |
40
|
|
|
*/ |
41
|
|
|
public static HttpServletRequest getHttpServletRequestFromRequestAttributes() { |
42
|
|
|
try { |
43
|
|
|
return ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest(); |
44
|
|
|
} catch (final Exception e) { |
45
|
|
|
LOGGER.trace(e.getMessage(), e); |
46
|
|
|
} |
47
|
|
|
return null; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Gets http servlet response from request attributes. |
52
|
|
|
* |
53
|
|
|
* @return the http servlet response from request attributes |
54
|
|
|
*/ |
55
|
|
|
public static HttpServletResponse getHttpServletResponseFromRequestAttributes() { |
56
|
|
|
return ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getResponse(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Gets http servlet request geo location. |
61
|
|
|
* |
62
|
|
|
* @param request the request |
63
|
|
|
* @return the http servlet request geo location |
64
|
|
|
*/ |
65
|
|
|
public static GeoLocationRequest getHttpServletRequestGeoLocation(final HttpServletRequest request) { |
66
|
|
|
final int latIndex = 0; |
67
|
|
|
final int longIndex = 1; |
68
|
|
|
final int accuracyIndex = 2; |
69
|
|
|
final int timeIndex = 3; |
70
|
|
|
final GeoLocationRequest loc = new GeoLocationRequest(); |
71
|
|
|
if (request != null) { |
72
|
|
|
final String geoLocationParam = request.getParameter("geolocation"); |
73
|
|
|
if (StringUtils.isNotBlank(geoLocationParam)) { |
74
|
|
|
final String[] geoLocation = geoLocationParam.split(","); |
75
|
|
|
loc.setLatitude(geoLocation[latIndex]); |
76
|
|
|
loc.setLongitude(geoLocation[longIndex]); |
77
|
|
|
loc.setAccuracy(geoLocation[accuracyIndex]); |
78
|
|
|
loc.setTimestamp(geoLocation[timeIndex]); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
return loc; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Gets request headers. |
86
|
|
|
* |
87
|
|
|
* @param request the request |
88
|
|
|
* @return the request headers |
89
|
|
|
*/ |
90
|
|
|
public static Map<String, String> getRequestHeaders(final HttpServletRequest request) { |
91
|
|
|
final Map<String, String> headers = new LinkedHashMap<>(); |
92
|
|
|
final Enumeration<String> headerNames = request.getHeaderNames(); |
93
|
|
|
if (headerNames != null) { |
94
|
|
|
while (headerNames.hasMoreElements()) { |
95
|
|
|
final String headerName = headerNames.nextElement(); |
96
|
|
|
final String headerValue = StringUtils.stripToEmpty(request.getHeader(headerName)); |
97
|
|
|
headers.put(headerName, headerValue); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
return headers; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Gets http servlet request user agent. |
105
|
|
|
* |
106
|
|
|
* @param request the request |
107
|
|
|
* @return the http servlet request user agent |
108
|
|
|
*/ |
109
|
|
|
public static String getHttpServletRequestUserAgent(final HttpServletRequest request) { |
110
|
|
|
if (request != null) { |
111
|
|
|
return request.getHeader(USER_AGENT_HEADER); |
112
|
|
|
} |
113
|
|
|
return null; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Gets the service from the request based on given extractors. |
118
|
|
|
* |
119
|
|
|
* @param argumentExtractors the argument extractors |
120
|
|
|
* @param request the request |
121
|
|
|
* @return the service, or null. |
122
|
|
|
*/ |
123
|
|
|
public static WebApplicationService getService(final List<ArgumentExtractor> argumentExtractors, final HttpServletRequest request) { |
124
|
|
|
return argumentExtractors.stream().map(argumentExtractor -> argumentExtractor.extractService(request)) |
125
|
|
|
.filter(Objects::nonNull).findFirst().orElse(null); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
|
129
|
|
|
} |
130
|
|
|
|