Skip to content

Commit

Permalink
get client address of user moved into separate method and log accordi…
Browse files Browse the repository at this point in the history
…ngly
  • Loading branch information
michaelwechner committed Jun 26, 2013
1 parent a0054e6 commit 2fed842
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions src/webapp/src/java/org/wyona/yanel/servlet/YanelServlet.java
Expand Up @@ -2271,6 +2271,7 @@ private HttpServletResponse generateResponse(View view, Resource res, HttpServle
log.warn("Returned content size of request '" + request.getRequestURI() + "' is 0");
}
} catch(Exception e) {
log.error("Writing into response failed for request '" + request.getRequestURL() + "' (Client: " + getClientAddressOfUser(request) + ")"); // INFO: For example in the case of ClientAbortException
log.error(e, e);
throw new ServletException(e);
} finally {
Expand Down Expand Up @@ -2779,18 +2780,7 @@ Differentiate between hits, pageviews (only html or also PDF, etc.?) and visits
accessLogMessage = accessLogMessage + AccessLog.encodeLogField("http-status", "" + HttpServletResponse.SC_OK);
}

String remoteIPAddr = request.getHeader("X-FORWARDED-FOR");
if (remoteIPAddr != null) { // INFO: We do not need to check realm.isProxySet() additionally, because some deployments are using a proxy without having set the Yanel proxy configuration, hence it is sufficient to just check whether an X-FORWARDED-FOR header is set
if (remoteIPAddr.indexOf("unknown") >= 0) {
log.warn("TODO: Clean remote IP address: " + remoteIPAddr);
}
accessLogMessage = accessLogMessage + AccessLog.encodeLogField("ip", remoteIPAddr);
} else {
if (log.isDebugEnabled()) {
log.debug("No such request header: X-FORWARDED-FOR (hence fallback to request.getRemoteAddr())"); // INFO: For example in the case of AJP or if no proxy is used
}
accessLogMessage = accessLogMessage + AccessLog.encodeLogField("ip", request.getRemoteAddr()); // INFO: For performance reasons we do not use getRemoteHost(), but rather just log the IP address.
}
accessLogMessage = accessLogMessage + AccessLog.encodeLogField("ip", getClientAddressOfUser(request));

logAccess.info(accessLogMessage);

Expand Down Expand Up @@ -3179,4 +3169,23 @@ private String[] getTagsFromAnnotatableResource(Resource resource, String servle
}
return tags;
}

/**
* Get client address of user
* @param request Client request
*/
private String getClientAddressOfUser(HttpServletRequest request) {
String remoteIPAddr = request.getHeader("X-FORWARDED-FOR");
if (remoteIPAddr != null) { // INFO: We do not need to check realm.isProxySet() additionally, because some deployments are using a proxy without having set the Yanel proxy configuration, hence it is sufficient to just check whether an X-FORWARDED-FOR header is set
if (remoteIPAddr.indexOf("unknown") >= 0) {
log.warn("TODO: Clean remote IP address: " + remoteIPAddr);
}
return remoteIPAddr;
} else {
if (log.isDebugEnabled()) {
log.debug("No such request header: X-FORWARDED-FOR (hence fallback to request.getRemoteAddr())"); // INFO: For example in the case of AJP or if no proxy is used
}
return request.getRemoteAddr(); // INFO: For performance reasons we do not use getRemoteHost(), but rather just use the IP address.
}
}
}

0 comments on commit 2fed842

Please sign in to comment.