List of usage examples for javax.servlet ServletRequest getRemoteHost
public String getRemoteHost();
From source file:org.jsecurity.web.WebUtils.java
/** * Returns the <code>InetAddress</code> associated with the current request, or <code>null</code> if the * address cannot be resolved/determined. * <p/>/* ww w .j a v a 2s . com*/ * This implementation returns the InetAddress resolved from the request's * {@link javax.servlet.ServletRequest#getRemoteHost() remoteHost} value. The returned <code>String</code> * is resolved to an InetAddress by calling * {@link InetAddress#getByName(String) InetAddress.getByName(remoteHost)}. If the remote host is <code>null</code> * or <code>getByName(remoteHost)</code> throws an exception, <code>null</code> is returned. * * @param request the incoming ServletRequest * @return the <code>InetAddress</code> associated with the current request, or <code>null</code> if the * address cannot be resolved/determined. */ public static InetAddress getInetAddress(ServletRequest request) { InetAddress clientAddress = null; //get the Host/IP the client is coming from: String addrString = request.getRemoteHost(); try { clientAddress = InetAddress.getByName(addrString); } catch (UnknownHostException e) { if (log.isInfoEnabled()) { log.info("Unable to acquire InetAddress from ServletRequest", e); } } return clientAddress; }
From source file:ro.cs.cm.web.filter.GeneralFilter.java
private void log(ServletRequest req) { logger.debug("-------------------------------------------------------------"); logger.debug(req.getRemoteHost() + "(" + req.getRemoteAddr() + "):" + req.getRemotePort()); }
From source file:org.everit.jetty.server.ecm.tests.EchoRemoteInfoServlet.java
@Override public void service(final ServletRequest req, final ServletResponse res) throws ServletException, IOException { PrintWriter writer = res.getWriter(); JSONObject jsonObject = new JSONObject(); jsonObject.put("remoteAddr", req.getRemoteAddr()); jsonObject.put("remoteHost", req.getRemoteHost()); jsonObject.put("remotePort", req.getRemotePort()); jsonObject.put("serverName", req.getServerName()); jsonObject.put("serverPort", req.getServerPort()); jsonObject.put("protocol", req.getProtocol()); jsonObject.put("secure", req.isSecure()); writer.write(jsonObject.toString()); }
From source file:LogFilter.java
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) { ServletContext context = getFilterConfig().getServletContext(); long bef = System.currentTimeMillis(); try {/*from ww w.ja v a2s. c o m*/ chain.doFilter(req, res); } catch (Exception e) { } long aft = System.currentTimeMillis(); context.log("Request to " + req.getRemoteHost() + ": " + (aft - bef)); }
From source file:org.sakaiproject.hybrid.util.TrustedLoginFilter.java
/** * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, * javax.servlet.ServletResponse, javax.servlet.FilterChain) *//*from ww w.j av a 2 s . c om*/ @SuppressWarnings({ "PMD.CyclomaticComplexity", "PMD.OnlyOneReturn", "PMD.AvoidDeeplyNestedIfStmts", "PMD.DataflowAnomalyAnalysis" }) public void doFilter(final ServletRequest req, final ServletResponse resp, final FilterChain chain) throws IOException, ServletException { if (enabled && req instanceof HttpServletRequest) { HttpServletRequest hreq = (HttpServletRequest) req; final String host = req.getRemoteHost(); if (safeHosts.indexOf(host) < 0) { LOG.warn("Ignoring Trusted Token request from: " + host); chain.doFilter(req, resp); return; } else { Session currentSession = null; Session requestSession = null; final String trustedUserName = xSakaiToken.getValidatedEid(hreq, sharedSecret); if (trustedUserName != null) { currentSession = sessionManager.getCurrentSession(); if (!trustedUserName.equals(currentSession.getUserEid())) { User user = null; try { user = userDirectoryService.getUserByEid(trustedUserName); } catch (UserNotDefinedException e) { LOG.warn(trustedUserName + " not found!"); } if (user != null) { requestSession = sessionManager.startSession(); requestSession.setUserEid(user.getEid()); requestSession.setUserId(user.getId()); requestSession.setActive(); sessionManager.setCurrentSession(requestSession); // wrap the request so that we can get the user // via getRemoteUser() in other places. if (!(hreq instanceof ToolRequestWrapper)) { hreq = new ToolRequestWrapper(hreq, trustedUserName); } } } } try { chain.doFilter(hreq, resp); } finally { if (requestSession != null) { requestSession.invalidate(); } if (currentSession != null) { sessionManager.setCurrentSession(currentSession); } } } } else { chain.doFilter(req, resp); return; } }
From source file:com.doculibre.constellio.filters.LocalRequestFilter.java
@Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { Set<String> acceptedAndDynamicHosts = new HashSet<String>(); acceptedAndDynamicHosts.addAll(Arrays.asList(ACCEPTED_HOSTS)); // Solution adapted from http://www.exampledepot.com/egs/java.net/Local.html String hostName = InetAddress.getLocalHost().getHostName(); InetAddress addrs[] = InetAddress.getAllByName(hostName); for (InetAddress addr : addrs) { String hostAddress = addr.getHostAddress(); acceptedAndDynamicHosts.add(hostAddress); }//w w w . j av a2 s .co m boolean valid; if (isIgnoredRequest(request) || isFileRequest(request)) { valid = true; } else { valid = false; String remoteHost = request.getRemoteHost(); for (String acceptedHost : acceptedAndDynamicHosts) { if (remoteHost.equals(acceptedHost)) { valid = true; } } } if (valid) { // Pass control on to the next filter chain.doFilter(request, response); } else { HttpServletRequest httpRequest = (HttpServletRequest) request; throw new ServletException("Cannot send request to a servlet from outside the Web application : " + httpRequest.getRequestURL()); } }
From source file:org.geoserver.filters.LoggingFilter.java
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { String message = ""; String body = null;/*w ww. j ava 2s . c o m*/ String path = ""; if (enabled) { if (req instanceof HttpServletRequest) { HttpServletRequest hreq = (HttpServletRequest) req; path = hreq.getRemoteHost() + " \"" + hreq.getMethod() + " " + hreq.getRequestURI(); if (hreq.getQueryString() != null) { path += "?" + hreq.getQueryString(); } path += "\""; message = "" + path; message += " \"" + noNull(hreq.getHeader("User-Agent")); message += "\" \"" + noNull(hreq.getHeader("Referer")) + "\" "; message += "\" \"" + noNull(hreq.getHeader("Content-type")) + "\" "; if (logBodies && (hreq.getMethod().equals("PUT") || hreq.getMethod().equals("POST"))) { message += " request-size: " + hreq.getContentLength(); message += " body: "; String encoding = hreq.getCharacterEncoding(); if (encoding == null) { // the default encoding for HTTP 1.1 encoding = "ISO-8859-1"; } ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] bytes; try (InputStream is = hreq.getInputStream()) { IOUtils.copy(is, bos); bytes = bos.toByteArray(); body = new String(bytes, encoding); } req = new BufferedRequestWrapper(hreq, encoding, bytes); } } else { message = "" + req.getRemoteHost() + " made a non-HTTP request"; } logger.info(message + (body == null ? "" : "\n" + body + "\n")); long startTime = System.currentTimeMillis(); chain.doFilter(req, res); long requestTime = System.currentTimeMillis() - startTime; logger.info(path + " took " + requestTime + "ms"); } else { chain.doFilter(req, res); } }
From source file:org.soaplab.clients.spinet.filters.RequestDumperFilter.java
/** * Time the processing that is performed by all subsequent filters in the * current filter stack, including the ultimately invoked servlet. * * @param request The servlet request we are processing * @param result The servlet response we are creating * @param chain The filter chain we are processing * * @exception IOException if an input/output error occurs * @exception ServletException if a servlet error occurs *//*from w w w. ja v a 2s . c o m*/ public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { if (filterConfig == null) return; // Render the generic servlet request properties StringWriter sw = new StringWriter(); PrintWriter writer = new PrintWriter(sw); writer.println("Request Received at " + (new Timestamp(System.currentTimeMillis()))); writer.println(" characterEncoding=" + request.getCharacterEncoding()); writer.println(" contentLength=" + request.getContentLength()); writer.println(" contentType=" + request.getContentType()); writer.println(" locale=" + request.getLocale()); writer.print(" locales="); Enumeration locales = request.getLocales(); boolean first = true; while (locales.hasMoreElements()) { Locale locale = (Locale) locales.nextElement(); if (first) first = false; else writer.print(", "); writer.print(locale.toString()); } writer.println(); Enumeration names = request.getParameterNames(); while (names.hasMoreElements()) { String name = (String) names.nextElement(); writer.print(" parameter=" + name + "="); String values[] = request.getParameterValues(name); for (int i = 0; i < values.length; i++) { if (i > 0) writer.print(", "); writer.print(values[i]); } writer.println(); } writer.println(" protocol=" + request.getProtocol()); writer.println(" remoteAddr=" + request.getRemoteAddr()); writer.println(" remoteHost=" + request.getRemoteHost()); writer.println(" scheme=" + request.getScheme()); writer.println(" serverName=" + request.getServerName()); writer.println(" serverPort=" + request.getServerPort()); writer.println(" isSecure=" + request.isSecure()); // Render the HTTP servlet request properties if (request instanceof HttpServletRequest) { writer.println("---------------------------------------------"); HttpServletRequest hrequest = (HttpServletRequest) request; writer.println(" contextPath=" + hrequest.getContextPath()); Cookie cookies[] = hrequest.getCookies(); if (cookies == null) cookies = new Cookie[0]; for (int i = 0; i < cookies.length; i++) { writer.println(" cookie=" + cookies[i].getName() + "=" + cookies[i].getValue()); } names = hrequest.getHeaderNames(); while (names.hasMoreElements()) { String name = (String) names.nextElement(); String value = hrequest.getHeader(name); writer.println(" header=" + name + "=" + value); } writer.println(" method=" + hrequest.getMethod()); writer.println(" pathInfo=" + hrequest.getPathInfo()); writer.println(" queryString=" + hrequest.getQueryString()); writer.println(" remoteUser=" + hrequest.getRemoteUser()); writer.println("requestedSessionId=" + hrequest.getRequestedSessionId()); writer.println(" requestURI=" + hrequest.getRequestURI()); writer.println(" servletPath=" + hrequest.getServletPath()); } writer.println("============================================="); // Log the resulting string writer.flush(); filterConfig.getServletContext().log(sw.getBuffer().toString()); log.info(sw.getBuffer().toString()); // Pass control on to the next filter chain.doFilter(request, response); }
From source file:org.zilverline.web.RequestDumperFilter.java
/** * Time the processing that is performed by all subsequent filters in the current filter stack, including the ultimately invoked * servlet./*from w ww . j a v a 2s. c o m*/ * * @param request The servlet request we are processing * @param response The servlet response we are creating * @param chain The filter chain we are processing * * @exception IOException if an input/output error occurs * @exception ServletException if a servlet error occurs */ public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException { if (filterConfig == null) { return; } log.debug("Request Received at " + (new Timestamp(System.currentTimeMillis()))); log.debug(" characterEncoding=" + request.getCharacterEncoding()); log.debug(" contentLength=" + request.getContentLength()); log.debug(" contentType=" + request.getContentType()); log.debug(" locale=" + request.getLocale()); Enumeration locales = request.getLocales(); StringBuffer localesBuffer = new StringBuffer(" locales="); boolean first = true; while (locales.hasMoreElements()) { Locale locale = (Locale) locales.nextElement(); if (first) { first = false; } else { localesBuffer.append(", "); } localesBuffer.append(locale.toString()); } log.debug(localesBuffer); Enumeration names = request.getParameterNames(); while (names.hasMoreElements()) { String name = (String) names.nextElement(); StringBuffer paramsBuffer = new StringBuffer(); paramsBuffer.append(" parameter=" + name + "="); String[] values = request.getParameterValues(name); for (int i = 0; i < values.length; i++) { if (i > 0) { paramsBuffer.append(", "); } paramsBuffer.append(values[i]); } log.debug(paramsBuffer); } log.debug(" protocol=" + request.getProtocol()); log.debug(" remoteAddr=" + request.getRemoteAddr()); log.debug(" remoteHost=" + request.getRemoteHost()); log.debug(" scheme=" + request.getScheme()); log.debug(" serverName=" + request.getServerName()); log.debug(" serverPort=" + request.getServerPort()); log.debug(" isSecure=" + request.isSecure()); // Render the HTTP servlet request properties if (request instanceof HttpServletRequest) { log.debug("---------------------------------------------"); HttpServletRequest hrequest = (HttpServletRequest) request; log.debug(" contextPath=" + hrequest.getContextPath()); Cookie[] cookies = hrequest.getCookies(); if (cookies == null) { cookies = new Cookie[0]; } for (int i = 0; i < cookies.length; i++) { log.debug(" cookie=" + cookies[i].getName() + "=" + cookies[i].getValue()); } names = hrequest.getHeaderNames(); while (names.hasMoreElements()) { String name = (String) names.nextElement(); String value = hrequest.getHeader(name); log.debug(" header=" + name + "=" + value); } log.debug(" method=" + hrequest.getMethod()); log.debug(" pathInfo=" + hrequest.getPathInfo()); log.debug(" queryString=" + hrequest.getQueryString()); log.debug(" remoteUser=" + hrequest.getRemoteUser()); log.debug("requestedSessionId=" + hrequest.getRequestedSessionId()); log.debug(" requestURI=" + hrequest.getRequestURI()); log.debug(" servletPath=" + hrequest.getServletPath()); } log.debug("============================================="); // Pass control on to the next filter chain.doFilter(request, response); }
From source file:edu.vt.middleware.servlet.filter.RequestDumperFilter.java
/** {@inheritDoc} */ @SuppressWarnings(value = "unchecked") public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException { if (this.config == null) { return;// w w w . j a va2 s .c om } // Just pass through to next filter if we're not at TRACE level if (!logger.isTraceEnabled()) { chain.doFilter(request, response); return; } // Create a variable to hold the (possibly different) request // passed to downstream filters ServletRequest downstreamRequest = request; // Render the generic servlet request properties final StringWriter sw = new StringWriter(); final PrintWriter writer = new PrintWriter(sw); writer.println("Dumping request..."); writer.println("-----------------------------------------------------"); writer.println("REQUEST received " + Calendar.getInstance().getTime()); writer.println(" characterEncoding=" + request.getCharacterEncoding()); writer.println(" contentLength=" + request.getContentLength()); writer.println(" contentType=" + request.getContentType()); writer.println(" locale=" + request.getLocale()); writer.print(" locales="); final Enumeration<Locale> locales = request.getLocales(); for (int i = 0; locales.hasMoreElements(); i++) { if (i > 0) { writer.print(", "); } writer.print(locales.nextElement()); } writer.println(); final Enumeration<String> paramNames = request.getParameterNames(); while (paramNames.hasMoreElements()) { final String name = paramNames.nextElement(); writer.print(" parameter=" + name + "="); final String[] values = request.getParameterValues(name); for (int i = 0; i < values.length; i++) { if (i > 0) { writer.print(", "); } writer.print(values[i]); } writer.println(); } writer.println(" protocol=" + request.getProtocol()); writer.println(" remoteAddr=" + request.getRemoteAddr()); writer.println(" remoteHost=" + request.getRemoteHost()); writer.println(" scheme=" + request.getScheme()); writer.println(" serverName=" + request.getServerName()); writer.println(" serverPort=" + request.getServerPort()); writer.println(" isSecure=" + request.isSecure()); // Render the HTTP servlet request properties if (request instanceof HttpServletRequest) { final HttpServletRequest hrequest = (HttpServletRequest) request; writer.println(" contextPath=" + hrequest.getContextPath()); Cookie[] cookies = hrequest.getCookies(); if (cookies == null) { cookies = new Cookie[0]; } for (int i = 0; i < cookies.length; i++) { writer.println(" cookie=" + cookies[i].getName() + "=" + cookies[i].getValue()); } final Enumeration<String> headerNames = hrequest.getHeaderNames(); while (headerNames.hasMoreElements()) { final String name = headerNames.nextElement(); final String value = hrequest.getHeader(name); writer.println(" header=" + name + "=" + value); } writer.println(" method=" + hrequest.getMethod()); writer.println(" pathInfo=" + hrequest.getPathInfo()); writer.println(" queryString=" + hrequest.getQueryString()); writer.println(" remoteUser=" + hrequest.getRemoteUser()); writer.println("requestedSessionId=" + hrequest.getRequestedSessionId()); writer.println(" requestURI=" + hrequest.getRequestURI()); writer.println(" servletPath=" + hrequest.getServletPath()); // Create a wrapped request that contains the request body // and that we will pass to downstream filters final ByteArrayRequestWrapper wrappedRequest = new ByteArrayRequestWrapper(hrequest); downstreamRequest = wrappedRequest; writer.println(wrappedRequest.getRequestBodyAsString()); } writer.println("-----------------------------------------------------"); // Log the resulting string writer.flush(); logger.trace(sw.getBuffer().toString()); // Pass control on to the next filter chain.doFilter(downstreamRequest, response); }