@Override
@SuppressWarnings("unchecked")
protected void service(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
try {
parameters.set(new QueryDictionary());
requestHeaders.set(new QueryDictionary());
responseHeaders.set(new QueryDictionary());
boolean proceed = true;
if (authenticationRequired) {
String authorization = request.getHeader("Authorization");
if (authorization == null) {
proceed = false;
doUnauthorized(request, response);
} else {
String encodedCredentials = authorization.substring
(BASIC_AUTHENTICATION_TAG.length() + 1);
String decodedCredentials = new String(Base64.decode(encodedCredentials));
String[] credentialsPair = decodedCredentials.split(":");
String username = credentialsPair.length > 0 ? credentialsPair[0] : "";
String password = credentialsPair.length > 1 ? credentialsPair[1] : "";
credentials.set(new Credentials(username, password));
}
}
if (proceed) {
// Extract our location context
try {
URL url = new URL(request.getRequestURL().toString());
String requestURI = request.getRequestURI();
String requestContext = request.getContextPath();
hostname.set(url.getHost());
contextPath.set(requestContext);
queryPath.set(requestURI);
port.set(request.getLocalPort());
secure.set(url.getProtocol().equalsIgnoreCase(HTTPS_PROTOCOL));
method.set(Method.valueOf(request.getMethod().toUpperCase()));
if (requestURI.startsWith(requestContext)) {
queryPath.set(requestURI.substring(requestContext.length()));
}
} catch (MalformedURLException exception) {
throw new ServletException(exception);
}
// Copy the query string into our arguments dictionary
String queryString = request.getQueryString();
if (queryString != null) {
QueryDictionary parametersDictionary = parameters.get();
String[] pairs = queryString.split("&");
for (int i = 0, n = pairs.length; i < n; i++) {
String[] pair = pairs[i].split("=");
String key = URLDecoder.decode(pair[0], URL_ENCODING);
String value = URLDecoder.decode((pair.length > 1) ? pair[1] : "", URL_ENCODING);
parametersDictionary.add(key, value);
}
}
// Copy the request headers into our request properties dictionary
QueryDictionary requestHeaderDictionary = requestHeaders.get();
Enumeration<String> headerNames = request.getHeaderNames();
while (headerNames.hasMoreElements()) {
String headerName = headerNames.nextElement();
String headerValue = request.getHeader(headerName);
requestHeaderDictionary.add(headerName, headerValue);
}
if (authenticationRequired) {
try {
authorize();