-
Notifications
You must be signed in to change notification settings - Fork 409
Expand file tree
/
Copy pathServletUtils.java
More file actions
236 lines (213 loc) · 7.48 KB
/
ServletUtils.java
File metadata and controls
236 lines (213 loc) · 7.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package com.onelogin.saml2.servlet;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import com.onelogin.saml2.http.HttpRequest;
import com.onelogin.saml2.util.Util;
/**
* ServletUtils class of OneLogin's Java Toolkit.
*
* A class that contains several auxiliary methods related to HttpServletRequest and HttpServletResponse
*/
public class ServletUtils {
private ServletUtils() {
//not called
}
/**
* Creates an HttpRequest from an HttpServletRequest.
*
* @param req the incoming HttpServletRequest
* @return a HttpRequest
*/
public static HttpRequest makeHttpRequest(HttpServletRequest req) {
@SuppressWarnings("unchecked")
final Map<String, String[]> paramsAsArray = (Map<String, String[]>) req.getParameterMap();
final Map<String, List<String>> paramsAsList = new HashMap<>();
for (Map.Entry<String, String[]> param : paramsAsArray.entrySet()) {
paramsAsList.put(param.getKey(), Arrays.asList(param.getValue()));
}
return new HttpRequest(req.getRequestURL().toString(), paramsAsList, req.getQueryString());
}
/**
* Returns the protocol + the current host + the port (if different than
* common ports).
*
* @param request
* HttpServletRequest object to be processed
*
* @return the HOST URL
*/
public static String getSelfURLhost(HttpServletRequest request) {
String hostUrl = StringUtils.EMPTY;
final int serverPort = request.getServerPort();
if ((serverPort == 80) || (serverPort == 443) || serverPort == 0) {
hostUrl = String.format("%s://%s", request.getScheme(), request.getServerName());
} else {
hostUrl = String.format("%s://%s:%s", request.getScheme(), request.getServerName(), serverPort);
}
return hostUrl;
}
/**
* @param request
* HttpServletRequest object to be processed
*
* @return the server name
*/
public static String getSelfHost(HttpServletRequest request) {
return request.getServerName();
}
/**
* Check if under https or http protocol
*
* @param request
* HttpServletRequest object to be processed
*
* @return false if https is not active
*/
public static boolean isHTTPS(HttpServletRequest request) {
return request.isSecure();
}
/**
* Returns the URL of the current context + current view + query
*
* @param request
* HttpServletRequest object to be processed
*
* @return current context + current view + query
*/
public static String getSelfURL(HttpServletRequest request) {
String url = getSelfURLhost(request);
String requestUri = request.getRequestURI();
String queryString = request.getQueryString();
if (null != requestUri && !requestUri.isEmpty()) {
url += requestUri;
}
if (null != queryString && !queryString.isEmpty()) {
url += '?' + queryString;
}
return url;
}
/**
* Returns the URL of the current host + current view.
*
* @param request
* HttpServletRequest object to be processed
*
* @return current host + current view
*/
public static String getSelfURLNoQuery(HttpServletRequest request) {
return request.getRequestURL().toString();
}
/**
* Returns the routed URL of the current host + current view.
*
* @param request
* HttpServletRequest object to be processed
*
* @return the current routed url
*/
public static String getSelfRoutedURLNoQuery(HttpServletRequest request) {
String url = getSelfURLhost(request);
String requestUri = request.getRequestURI();
if (null != requestUri && !requestUri.isEmpty()) {
url += requestUri;
}
return url;
}
/**
* Redirect to location url
*
* @param response
* HttpServletResponse object to be used
* @param location
* target location url
* @param parameters
* GET parameters to be added
* @param stay
* True if we want to stay (returns the url string) False to execute redirection
*
* @return string the target URL
* @throws IOException
*
* @see javax.servlet.http.HttpServletResponse#sendRedirect(String)
*/
public static String sendRedirect(HttpServletResponse response, String location, Map<String, String> parameters, Boolean stay) throws IOException {
String target = location;
if (!parameters.isEmpty()) {
boolean first = !location.contains("?");
for (Map.Entry<String, String> parameter : parameters.entrySet())
{
if (first) {
target += "?";
first = false;
} else {
target += "&";
}
target += parameter.getKey();
if (!parameter.getValue().isEmpty()) {
target += "=" + Util.urlEncoder(parameter.getValue());
}
}
}
if (!stay) {
response.sendRedirect(target);
}
return target;
}
public static String sendPost(HttpServletResponse response, String location, Map<String, String> parameters, Boolean stay) throws IOException {
StringBuilder html = new StringBuilder();
html.append("<html>\n<head>\n<title>SAML Post</title>\n</head>\n")
.append("<body onload=\"document.getElementById('theform').submit();\">\n")
.append("<form id=\"theform\" action=\"").append(location).append("\" method=\"post\">\n");
for (String name : parameters.keySet()) {
String value = parameters.get(name);
html.append("<input type=\"hidden\" name=\"").append(name).append("\" value=\"").append(StringEscapeUtils.escapeHtml4(value)).append("\"/>\n");
}
html.append("</form>\n</body>\n</html>");
if (!stay) {
response.setContentType("text/html;charset=UTF-8");
response.setContentLength(html.toString().getBytes("UTF-8").length);
response.getWriter().write(html.toString());
}
return html.toString();
}
/**
* Redirect to location url
*
* @param response
* HttpServletResponse object to be used
* @param location
* target location url
* @param parameters
* GET parameters to be added
*
* @throws IOException
*
* @see javax.servlet.http.HttpServletResponse#sendRedirect(String)
*/
public static void sendRedirect(HttpServletResponse response, String location, Map<String, String> parameters) throws IOException {
sendRedirect(response, location, parameters, false);
}
/**
* Redirect to location url
*
* @param response
* HttpServletResponse object to be used
* @param location
* target location url
*
* @throws IOException
*
* @see HttpServletResponse#sendRedirect(String)
*/
public static void sendRedirect(HttpServletResponse response, String location) throws IOException {
Map<String, String> parameters =new HashMap<String, String>();
sendRedirect(response, location, parameters);
}
}