Java Code Examples for org.apache.catalina.connector.Request#getServerName()
The following examples show how to use
org.apache.catalina.connector.Request#getServerName() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractAccessLogValve.java From Tomcat8-Source-Read with MIT License | 6 votes |
@Override public void addElement(CharArrayWriter buf, Date date, Request request, Response response, long time) { String value = null; if (requestAttributesEnabled) { Object serverName = request.getAttribute(SERVER_NAME_ATTRIBUTE); if (serverName != null) { value = serverName.toString(); } } if (value == null || value.length() == 0) { value = request.getServerName(); } if (value == null || value.length() == 0) { value = "-"; } if (ipv6Canonical) { value = IPv6Utils.canonize(value); } buf.append(value); }
Example 2
Source File: TestRemoteIpValve.java From Tomcat8-Source-Read with MIT License | 5 votes |
@Override public void invoke(Request request, Response response) throws IOException, ServletException { this.remoteHost = request.getRemoteHost(); this.remoteAddr = request.getRemoteAddr(); this.scheme = request.getScheme(); this.secure = request.isSecure(); this.serverName = request.getServerName(); this.serverPort = request.getServerPort(); this.forwardedFor = request.getHeader("x-forwarded-for"); this.forwardedBy = request.getHeader("x-forwarded-by"); }
Example 3
Source File: JDBCAccessLogValve.java From Tomcat8-Source-Read with MIT License | 4 votes |
@Override public void log(Request request, Response response, long time) { if (!getState().isAvailable()) { return; } final String EMPTY = "" ; String remoteHost; if(resolveHosts) { if (requestAttributesEnabled) { Object host = request.getAttribute(REMOTE_HOST_ATTRIBUTE); if (host == null) { remoteHost = request.getRemoteHost(); } else { remoteHost = (String) host; } } else { remoteHost = request.getRemoteHost(); } } else { if (requestAttributesEnabled) { Object addr = request.getAttribute(REMOTE_ADDR_ATTRIBUTE); if (addr == null) { remoteHost = request.getRemoteAddr(); } else { remoteHost = (String) addr; } } else { remoteHost = request.getRemoteAddr(); } } String user = request.getRemoteUser(); String query=request.getRequestURI(); long bytes = response.getBytesWritten(true); if(bytes < 0) { bytes = 0; } int status = response.getStatus(); String virtualHost = EMPTY; String method = EMPTY; String referer = EMPTY; String userAgent = EMPTY; String logPattern = pattern; if (logPattern.equals("combined")) { virtualHost = request.getServerName(); method = request.getMethod(); referer = request.getHeader("referer"); userAgent = request.getHeader("user-agent"); } synchronized (this) { int numberOfTries = 2; while (numberOfTries>0) { try { open(); ps.setString(1, remoteHost); ps.setString(2, user); ps.setTimestamp(3, new Timestamp(getCurrentTimeMillis())); ps.setString(4, query); ps.setInt(5, status); if(useLongContentLength) { ps.setLong(6, bytes); } else { if (bytes > Integer.MAX_VALUE) { bytes = -1 ; } ps.setInt(6, (int) bytes); } if (logPattern.equals("combined")) { ps.setString(7, virtualHost); ps.setString(8, method); ps.setString(9, referer); ps.setString(10, userAgent); } ps.executeUpdate(); return; } catch (SQLException e) { // Log the problem for posterity container.getLogger().error(sm.getString("jdbcAccessLogValve.exception"), e); // Close the connection so that it gets reopened next time if (conn != null) { close(); } } numberOfTries--; } } }
Example 4
Source File: RealmBase.java From Tomcat8-Source-Read with MIT License | 4 votes |
/** * Enforce any user data constraint required by the security constraint * guarding this request URI. Return <code>true</code> if this constraint * was not violated and processing should continue, or <code>false</code> * if we have created a response already. * * @param request Request we are processing * @param response Response we are creating * @param constraints Security constraint being checked * * @exception IOException if an input/output error occurs */ @Override public boolean hasUserDataPermission(Request request, Response response, SecurityConstraint []constraints) throws IOException { // Is there a relevant user data constraint? if (constraints == null || constraints.length == 0) { if (log.isDebugEnabled()) log.debug(" No applicable security constraint defined"); return true; } for(int i=0; i < constraints.length; i++) { SecurityConstraint constraint = constraints[i]; String userConstraint = constraint.getUserConstraint(); if (userConstraint == null) { if (log.isDebugEnabled()) log.debug(" No applicable user data constraint defined"); return true; } if (userConstraint.equals(TransportGuarantee.NONE.name())) { if (log.isDebugEnabled()) log.debug(" User data constraint has no restrictions"); return true; } } // Validate the request against the user data constraint if (request.getRequest().isSecure()) { if (log.isDebugEnabled()) log.debug(" User data constraint already satisfied"); return true; } // Initialize variables we need to determine the appropriate action int redirectPort = request.getConnector().getRedirectPort(); // Is redirecting disabled? if (redirectPort <= 0) { if (log.isDebugEnabled()) log.debug(" SSL redirect is disabled"); response.sendError (HttpServletResponse.SC_FORBIDDEN, request.getRequestURI()); return false; } // Redirect to the corresponding SSL port StringBuilder file = new StringBuilder(); String protocol = "https"; String host = request.getServerName(); // Protocol file.append(protocol).append("://").append(host); // Host with port if(redirectPort != 443) { file.append(":").append(redirectPort); } // URI file.append(request.getRequestURI()); String requestedSessionId = request.getRequestedSessionId(); if ((requestedSessionId != null) && request.isRequestedSessionIdFromURL()) { file.append(";"); file.append(SessionConfig.getSessionUriParamName( request.getContext())); file.append("="); file.append(requestedSessionId); } String queryString = request.getQueryString(); if (queryString != null) { file.append('?'); file.append(queryString); } if (log.isDebugEnabled()) log.debug(" Redirecting to " + file.toString()); response.sendRedirect(file.toString(), transportGuaranteeRedirectStatus); return false; }
Example 5
Source File: TestRemoteIpValve.java From Tomcat8-Source-Read with MIT License | 4 votes |
@Test public void testInvokeXforwardedHost() throws Exception { // PREPARE RemoteIpValve remoteIpValve = new RemoteIpValve(); remoteIpValve.setHostHeader("x-forwarded-host"); remoteIpValve.setPortHeader("x-forwarded-port"); remoteIpValve.setProtocolHeader("x-forwarded-proto"); RemoteAddrAndHostTrackerValve remoteAddrAndHostTrackerValve = new RemoteAddrAndHostTrackerValve(); remoteIpValve.setNext(remoteAddrAndHostTrackerValve); Request request = new MockRequest(); request.setCoyoteRequest(new org.apache.coyote.Request()); // client ip request.setRemoteAddr("192.168.0.10"); request.setRemoteHost("192.168.0.10"); // protocol request.setSecure(false); request.setServerPort(8080); request.getCoyoteRequest().scheme().setString("http"); // host and port request.getCoyoteRequest().serverName().setString("10.0.0.1"); request.getCoyoteRequest().getMimeHeaders().addValue("x-forwarded-host").setString("example.com:8443"); request.getCoyoteRequest().getMimeHeaders().addValue("x-forwarded-port").setString("8443"); request.getCoyoteRequest().getMimeHeaders().addValue("x-forwarded-proto").setString("https"); // TEST remoteIpValve.invoke(request, null); // VERIFY // protocol String actualServerName = remoteAddrAndHostTrackerValve.getServerName(); Assert.assertEquals("tracked serverName", "example.com", actualServerName); String actualScheme = remoteAddrAndHostTrackerValve.getScheme(); Assert.assertEquals("tracked scheme", "https", actualScheme); int actualServerPort = remoteAddrAndHostTrackerValve.getServerPort(); Assert.assertEquals("tracked serverPort", 8443, actualServerPort); boolean actualSecure = remoteAddrAndHostTrackerValve.isSecure(); Assert.assertTrue("tracked secure", actualSecure); String actualPostInvokeServerName = request.getServerName(); Assert.assertEquals("postInvoke serverName", "10.0.0.1", actualPostInvokeServerName); boolean actualPostInvokeSecure = request.isSecure(); Assert.assertFalse("postInvoke secure", actualPostInvokeSecure); int actualPostInvokeServerPort = request.getServerPort(); Assert.assertEquals("postInvoke serverPort", 8080, actualPostInvokeServerPort); String actualPostInvokeScheme = request.getScheme(); Assert.assertEquals("postInvoke scheme", "http", actualPostInvokeScheme); }
Example 6
Source File: TestRemoteIpValve.java From Tomcat8-Source-Read with MIT License | 4 votes |
@Test public void testInvokeXforwardedHostAndPort() throws Exception { // PREPARE RemoteIpValve remoteIpValve = new RemoteIpValve(); remoteIpValve.setHostHeader("x-forwarded-host"); remoteIpValve.setPortHeader("x-forwarded-port"); remoteIpValve.setProtocolHeader("x-forwarded-proto"); RemoteAddrAndHostTrackerValve remoteAddrAndHostTrackerValve = new RemoteAddrAndHostTrackerValve(); remoteIpValve.setNext(remoteAddrAndHostTrackerValve); Request request = new MockRequest(); request.setCoyoteRequest(new org.apache.coyote.Request()); // client ip request.setRemoteAddr("192.168.0.10"); request.setRemoteHost("192.168.0.10"); // protocol request.setSecure(false); request.setServerPort(8080); request.getCoyoteRequest().scheme().setString("http"); // host and port request.getCoyoteRequest().serverName().setString("10.0.0.1"); request.getCoyoteRequest().getMimeHeaders().addValue("x-forwarded-host").setString("example.com"); request.getCoyoteRequest().getMimeHeaders().addValue("x-forwarded-port").setString("8443"); request.getCoyoteRequest().getMimeHeaders().addValue("x-forwarded-proto").setString("https"); // TEST remoteIpValve.invoke(request, null); // VERIFY // protocol String actualServerName = remoteAddrAndHostTrackerValve.getServerName(); Assert.assertEquals("tracked serverName", "example.com", actualServerName); String actualScheme = remoteAddrAndHostTrackerValve.getScheme(); Assert.assertEquals("tracked scheme", "https", actualScheme); int actualServerPort = remoteAddrAndHostTrackerValve.getServerPort(); Assert.assertEquals("tracked serverPort", 8443, actualServerPort); boolean actualSecure = remoteAddrAndHostTrackerValve.isSecure(); Assert.assertTrue("tracked secure", actualSecure); String actualPostInvokeServerName = request.getServerName(); Assert.assertEquals("postInvoke serverName", "10.0.0.1", actualPostInvokeServerName); boolean actualPostInvokeSecure = request.isSecure(); Assert.assertFalse("postInvoke secure", actualPostInvokeSecure); int actualPostInvokeServerPort = request.getServerPort(); Assert.assertEquals("postInvoke serverPort", 8080, actualPostInvokeServerPort); String actualPostInvokeScheme = request.getScheme(); Assert.assertEquals("postInvoke scheme", "http", actualPostInvokeScheme); }
Example 7
Source File: JDBCAccessLogValve.java From Tomcat7.0.67 with Apache License 2.0 | 4 votes |
@Override public void log(Request request, Response response, long time) { if (!getState().isAvailable()) { return; } final String EMPTY = "" ; String remoteHost; if(resolveHosts) { if (requestAttributesEnabled) { Object host = request.getAttribute(REMOTE_HOST_ATTRIBUTE); if (host == null) { remoteHost = request.getRemoteHost(); } else { remoteHost = (String) host; } } else { remoteHost = request.getRemoteHost(); } } else { if (requestAttributesEnabled) { Object addr = request.getAttribute(REMOTE_ADDR_ATTRIBUTE); if (addr == null) { remoteHost = request.getRemoteAddr(); } else { remoteHost = (String) addr; } } else { remoteHost = request.getRemoteAddr(); } } String user = request.getRemoteUser(); String query=request.getRequestURI(); long bytes = response.getBytesWritten(true); if(bytes < 0) { bytes = 0; } int status = response.getStatus(); String virtualHost = EMPTY; String method = EMPTY; String referer = EMPTY; String userAgent = EMPTY; String logPattern = pattern; if (logPattern.equals("combined")) { virtualHost = request.getServerName(); method = request.getMethod(); referer = request.getHeader("referer"); userAgent = request.getHeader("user-agent"); } synchronized (this) { int numberOfTries = 2; while (numberOfTries>0) { try { open(); ps.setString(1, remoteHost); ps.setString(2, user); ps.setTimestamp(3, new Timestamp(getCurrentTimeMillis())); ps.setString(4, query); ps.setInt(5, status); if(useLongContentLength) { ps.setLong(6, bytes); } else { if (bytes > Integer.MAX_VALUE) { bytes = -1 ; } ps.setInt(6, (int) bytes); } if (logPattern.equals("combined")) { ps.setString(7, virtualHost); ps.setString(8, method); ps.setString(9, referer); ps.setString(10, userAgent); } ps.executeUpdate(); return; } catch (SQLException e) { // Log the problem for posterity container.getLogger().error(sm.getString("jdbcAccessLogValve.exception"), e); // Close the connection so that it gets reopened next time if (conn != null) { close(); } } numberOfTries--; } } }
Example 8
Source File: RealmBase.java From Tomcat7.0.67 with Apache License 2.0 | 4 votes |
/** * Enforce any user data constraint required by the security constraint * guarding this request URI. Return <code>true</code> if this constraint * was not violated and processing should continue, or <code>false</code> * if we have created a response already. * * @param request Request we are processing * @param response Response we are creating * @param constraints Security constraint being checked * * @exception IOException if an input/output error occurs */ @Override public boolean hasUserDataPermission(Request request, Response response, SecurityConstraint []constraints) throws IOException { // Is there a relevant user data constraint? if (constraints == null || constraints.length == 0) { if (log.isDebugEnabled()) log.debug(" No applicable security constraint defined"); return (true); } for(int i=0; i < constraints.length; i++) { SecurityConstraint constraint = constraints[i]; String userConstraint = constraint.getUserConstraint(); if (userConstraint == null) { if (log.isDebugEnabled()) log.debug(" No applicable user data constraint defined"); return (true); } if (userConstraint.equals(Constants.NONE_TRANSPORT)) { if (log.isDebugEnabled()) log.debug(" User data constraint has no restrictions"); return (true); } } // Validate the request against the user data constraint if (request.getRequest().isSecure()) { if (log.isDebugEnabled()) log.debug(" User data constraint already satisfied"); return (true); } // Initialize variables we need to determine the appropriate action int redirectPort = request.getConnector().getRedirectPort(); // Is redirecting disabled? if (redirectPort <= 0) { if (log.isDebugEnabled()) log.debug(" SSL redirect is disabled"); response.sendError (HttpServletResponse.SC_FORBIDDEN, request.getRequestURI()); return (false); } // Redirect to the corresponding SSL port StringBuilder file = new StringBuilder(); String protocol = "https"; String host = request.getServerName(); // Protocol file.append(protocol).append("://").append(host); // Host with port if(redirectPort != 443) { file.append(":").append(redirectPort); } // URI file.append(request.getRequestURI()); String requestedSessionId = request.getRequestedSessionId(); if ((requestedSessionId != null) && request.isRequestedSessionIdFromURL()) { file.append(";"); file.append(SessionConfig.getSessionUriParamName( request.getContext())); file.append("="); file.append(requestedSessionId); } String queryString = request.getQueryString(); if (queryString != null) { file.append('?'); file.append(queryString); } if (log.isDebugEnabled()) log.debug(" Redirecting to " + file.toString()); response.sendRedirect(file.toString()); return (false); }
Example 9
Source File: CrossSubdomainSessionValve.java From scipio-erp with Apache License 2.0 | 4 votes |
protected void replaceCookie(Request request, Response response, Cookie cookie) { Delegator delegator = (Delegator) request.getAttribute("delegator"); // copy the existing session cookie, but use a different domain (only if domain is valid) String cookieDomain = null; cookieDomain = EntityUtilProperties.getPropertyValue("url", "cookie.domain", "", delegator); if (UtilValidate.isEmpty(cookieDomain)) { String serverName = request.getServerName(); String[] domainArray = serverName.split("\\."); // check that the domain isn't an IP address if (domainArray.length == 4) { boolean isIpAddress = true; for (String domainSection : domainArray) { if (!UtilValidate.isIntegerInRange(domainSection, 0, 255)) { isIpAddress = false; break; } } if (isIpAddress) { return; } } if (domainArray.length > 2) { cookieDomain = "." + domainArray[domainArray.length - 2] + "." + domainArray[domainArray.length - 1]; } } if (UtilValidate.isNotEmpty(cookieDomain)) { Cookie newCookie = new Cookie(cookie.getName(), cookie.getValue()); if (cookie.getPath() != null) { newCookie.setPath(cookie.getPath()); } newCookie.setDomain(cookieDomain); newCookie.setMaxAge(cookie.getMaxAge()); newCookie.setVersion(cookie.getVersion()); if (cookie.getComment() != null) { newCookie.setComment(cookie.getComment()); } newCookie.setSecure(cookie.getSecure()); newCookie.setHttpOnly(cookie.isHttpOnly()); // if the response has already been committed, our replacement strategy will have no effect if (response.isCommitted()) { Debug.logError("CrossSubdomainSessionValve: response was already committed!", module); } // find the Set-Cookie header for the existing cookie and replace its value with new cookie MimeHeaders mimeHeaders = request.getCoyoteRequest().getMimeHeaders(); for (int i = 0, size = mimeHeaders.size(); i < size; i++) { if (mimeHeaders.getName(i).equals("Set-Cookie")) { MessageBytes value = mimeHeaders.getValue(i); if (value.indexOf(cookie.getName()) >= 0) { String newCookieValue = request.getContext().getCookieProcessor().generateHeader(newCookie); if (Debug.verboseOn()) Debug.logVerbose("CrossSubdomainSessionValve: old Set-Cookie value: " + value.toString(), module); if (Debug.verboseOn()) Debug.logVerbose("CrossSubdomainSessionValve: new Set-Cookie value: " + newCookieValue, module); value.setString(newCookieValue); } } } } }
Example 10
Source File: JDBCAccessLogValve.java From tomcatsrc with Apache License 2.0 | 4 votes |
@Override public void log(Request request, Response response, long time) { if (!getState().isAvailable()) { return; } final String EMPTY = "" ; String remoteHost; if(resolveHosts) { if (requestAttributesEnabled) { Object host = request.getAttribute(REMOTE_HOST_ATTRIBUTE); if (host == null) { remoteHost = request.getRemoteHost(); } else { remoteHost = (String) host; } } else { remoteHost = request.getRemoteHost(); } } else { if (requestAttributesEnabled) { Object addr = request.getAttribute(REMOTE_ADDR_ATTRIBUTE); if (addr == null) { remoteHost = request.getRemoteAddr(); } else { remoteHost = (String) addr; } } else { remoteHost = request.getRemoteAddr(); } } String user = request.getRemoteUser(); String query=request.getRequestURI(); long bytes = response.getBytesWritten(true); if(bytes < 0) { bytes = 0; } int status = response.getStatus(); String virtualHost = EMPTY; String method = EMPTY; String referer = EMPTY; String userAgent = EMPTY; String logPattern = pattern; if (logPattern.equals("combined")) { virtualHost = request.getServerName(); method = request.getMethod(); referer = request.getHeader("referer"); userAgent = request.getHeader("user-agent"); } synchronized (this) { int numberOfTries = 2; while (numberOfTries>0) { try { open(); ps.setString(1, remoteHost); ps.setString(2, user); ps.setTimestamp(3, new Timestamp(getCurrentTimeMillis())); ps.setString(4, query); ps.setInt(5, status); if(useLongContentLength) { ps.setLong(6, bytes); } else { if (bytes > Integer.MAX_VALUE) { bytes = -1 ; } ps.setInt(6, (int) bytes); } if (logPattern.equals("combined")) { ps.setString(7, virtualHost); ps.setString(8, method); ps.setString(9, referer); ps.setString(10, userAgent); } ps.executeUpdate(); return; } catch (SQLException e) { // Log the problem for posterity container.getLogger().error(sm.getString("jdbcAccessLogValve.exception"), e); // Close the connection so that it gets reopened next time if (conn != null) { close(); } } numberOfTries--; } } }
Example 11
Source File: RealmBase.java From tomcatsrc with Apache License 2.0 | 4 votes |
/** * Enforce any user data constraint required by the security constraint * guarding this request URI. Return <code>true</code> if this constraint * was not violated and processing should continue, or <code>false</code> * if we have created a response already. * * @param request Request we are processing * @param response Response we are creating * @param constraints Security constraint being checked * * @exception IOException if an input/output error occurs */ @Override public boolean hasUserDataPermission(Request request, Response response, SecurityConstraint []constraints) throws IOException { // Is there a relevant user data constraint? if (constraints == null || constraints.length == 0) { if (log.isDebugEnabled()) log.debug(" No applicable security constraint defined"); return (true); } for(int i=0; i < constraints.length; i++) { SecurityConstraint constraint = constraints[i]; String userConstraint = constraint.getUserConstraint(); if (userConstraint == null) { if (log.isDebugEnabled()) log.debug(" No applicable user data constraint defined"); return (true); } if (userConstraint.equals(Constants.NONE_TRANSPORT)) { if (log.isDebugEnabled()) log.debug(" User data constraint has no restrictions"); return (true); } } // Validate the request against the user data constraint if (request.getRequest().isSecure()) { if (log.isDebugEnabled()) log.debug(" User data constraint already satisfied"); return (true); } // Initialize variables we need to determine the appropriate action int redirectPort = request.getConnector().getRedirectPort(); // Is redirecting disabled? if (redirectPort <= 0) { if (log.isDebugEnabled()) log.debug(" SSL redirect is disabled"); response.sendError (HttpServletResponse.SC_FORBIDDEN, request.getRequestURI()); return (false); } // Redirect to the corresponding SSL port StringBuilder file = new StringBuilder(); String protocol = "https"; String host = request.getServerName(); // Protocol file.append(protocol).append("://").append(host); // Host with port if(redirectPort != 443) { file.append(":").append(redirectPort); } // URI file.append(request.getRequestURI()); String requestedSessionId = request.getRequestedSessionId(); if ((requestedSessionId != null) && request.isRequestedSessionIdFromURL()) { file.append(";"); file.append(SessionConfig.getSessionUriParamName( request.getContext())); file.append("="); file.append(requestedSessionId); } String queryString = request.getQueryString(); if (queryString != null) { file.append('?'); file.append(queryString); } if (log.isDebugEnabled()) log.debug(" Redirecting to " + file.toString()); response.sendRedirect(file.toString(), transportGuaranteeRedirectStatus); return (false); }