* @param type ContentType
* @return Request information from a http exchange
* @throws IOException when the request information could not be read
*/
public static HttpRequestInfo getRequestInfo(HttpExchange request, ContentType type) throws IOException {
HttpRequestInfo requestInfo = new HttpRequestInfo();
if (request.getHttpContext().getAuthenticator() instanceof BasicAuthenticator) {
requestInfo.setAuthType(HttpServletRequest.BASIC_AUTH);
}
URI u = request.getRequestURI();
URI requestURI = null;
try {
requestURI = new URI(u.getScheme(), u.getUserInfo(), u.getHost(), u.getPort(), u.getPath(), null, null);
} catch (URISyntaxException e) {
// Strange that this could happen when copying from another URI.
LOGGER.debug(e);
}
requestInfo.setCharacterEncoding(type.getCharset());
requestInfo.setContentType(type.toString());
requestInfo.setContextPath(request.getHttpContext().getPath());
requestInfo.setLocalAddr(request.getLocalAddress().getAddress().getHostAddress());
requestInfo.setLocalName(request.getLocalAddress().getAddress().getHostName());
requestInfo.setMethod(request.getRequestMethod());
requestInfo.setProtocol(request.getProtocol());
requestInfo.setQueryString(u.getQuery());
requestInfo.setRemoteAddr(request.getRemoteAddress().getAddress().getHostAddress());
requestInfo.setRemoteHost(request.getRemoteAddress().getAddress().getHostName());
if (request.getHttpContext().getAuthenticator() instanceof BasicAuthenticator) {
requestInfo.setRemoteUser(request.getPrincipal().getUsername());
}
requestInfo.setContentLength(request.getRequestBody().available());
// requestInfo.setRequestSessionId(request.getRequestedSessionId());
if (requestURI != null) {
requestInfo.setRequestURI(requestURI.toString());
}
requestInfo.setScheme(u.getScheme());
requestInfo.setServerName(u.getHost());
requestInfo.setRequestPath(u.getPath());
// Http Query params...
if (requestInfo.getQueryString() != null) {
Charset charset = null;
if (type.getCharset() != null) {
try {
charset = Charset.forName(type.getCharset());
} catch (Exception exception) {
LOGGER.debug(exception);
}
}
for (NameValuePair nameValuePair : URLEncodedUtils.parse(requestInfo.getQueryString(), charset)) {
requestInfo.addQueryParam(nameValuePair.getName(), nameValuePair.getValue());
}
}
// Credentials...
requestInfo.getCredentials().addAll(new HttpExchangeCredentialExtractor().extract(request));
if (LOGGER.isTraceEnabled()) {
LOGGER.trace(requestInfo);
}