alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Spring Framework example source code file (AbstractRequestLoggingFilter.java)

This example Spring Framework source code file (AbstractRequestLoggingFilter.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Spring Framework tags/keywords

default_after_message_prefix, default_after_message_suffix, default_before_message_prefix, default_before_message_prefix, default_before_message_suffix, default_before_message_suffix, filterchain, http, io, ioexception, onceperrequestfilter, request, response, servlet, servletexception, string, string, stringbuffer, stringbuffer

The Spring Framework AbstractRequestLoggingFilter.java source code

/*
 * Copyright 2002-2007 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.web.filter;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.util.StringUtils;

/**
 * Base class for <code>Filters that perform logging operations before and after a
 * request is processed.
 *
 * <p>Subclasses should override the beforeRequest(HttpServletRequest, String)
 * and <code>afterRequest(HttpServletRequest, String) methods to perform the actual
 * logging around the request.
 *
 * <p>Subclasses are passed the message to write to the log in the beforeRequest
 * and <code>afterRequest methods. By default, only the URI of the request is logged.
 * However, setting the <code>includeQueryString property to true will
 * cause the query string of the request to be included also.
 *
 * <p>Prefixes and suffixes for the before and after messages can be configured
 * using the <code>beforeMessagePrefix, afterMessagePrefix,
 * <code>beforeMessageSuffix and afterMessageSuffix properties,
 *
 * @author Rob Harrop
 * @author Juergen Hoeller
 * @since 1.2.5
 * @see #beforeRequest
 * @see #afterRequest
 */
public abstract class AbstractRequestLoggingFilter extends OncePerRequestFilter {

	public static final String DEFAULT_BEFORE_MESSAGE_PREFIX = "Before request [";

	public static final String DEFAULT_BEFORE_MESSAGE_SUFFIX = "]";

	public static final String DEFAULT_AFTER_MESSAGE_PREFIX = "After request [";

	public static final String DEFAULT_AFTER_MESSAGE_SUFFIX = "]";


	private boolean includeQueryString = false;

	private boolean includeClientInfo = false;

	private String beforeMessagePrefix = DEFAULT_BEFORE_MESSAGE_PREFIX;

	private String beforeMessageSuffix = DEFAULT_BEFORE_MESSAGE_SUFFIX;

	private String afterMessagePrefix = DEFAULT_AFTER_MESSAGE_PREFIX;

	private String afterMessageSuffix = DEFAULT_AFTER_MESSAGE_SUFFIX;


	/**
	 * Set whether or not the query string should be included in the log message.
	 * <p>Should be configured using an <init-param> for parameter
	 * name "includeQueryString" in the filter definition in <code>web.xml.
	 */
	public void setIncludeQueryString(boolean includeQueryString) {
		this.includeQueryString = includeQueryString;
	}

	/**
	 * Return whether or not the query string should be included in the log message.
	 */
	protected boolean isIncludeQueryString() {
		return this.includeQueryString;
	}

	/**
	 * Set whether or not the client address and session id should be included
	 * in the log message.
	 * <p>Should be configured using an <init-param> for parameter
	 * name "includeClientInfo" in the filter definition in <code>web.xml.
	 */
	public void setIncludeClientInfo(boolean includeClientInfo) {
		this.includeClientInfo = includeClientInfo;
	}

	/**
	 * Return whether or not the client address and session id should be included
	 * in the log message.
	 */
	protected boolean isIncludeClientInfo() {
		return this.includeClientInfo;
	}

	/**
	 * Set the value that should be prepended to the log message written
	 * <i>before a request is processed.
	 */
	public void setBeforeMessagePrefix(String beforeMessagePrefix) {
		this.beforeMessagePrefix = beforeMessagePrefix;
	}

	/**
	 * Set the value that should be apppended to the log message written
	 * <i>before a request is processed.
	 */
	public void setBeforeMessageSuffix(String beforeMessageSuffix) {
		this.beforeMessageSuffix = beforeMessageSuffix;
	}

	/**
	 * Set the value that should be prepended to the log message written
	 * <i>after a request is processed.
	 */
	public void setAfterMessagePrefix(String afterMessagePrefix) {
		this.afterMessagePrefix = afterMessagePrefix;
	}

	/**
	 * Set the value that should be appended to the log message written
	 * <i>after a request is processed.
	 */
	public void setAfterMessageSuffix(String afterMessageSuffix) {
		this.afterMessageSuffix = afterMessageSuffix;
	}


	/**
	 * Forwards the request to the next filter in the chain and delegates
	 * down to the subclasses to perform the actual request logging both
	 * before and after the request is processed.
	 * @see #beforeRequest
	 * @see #afterRequest
	 */
	protected void doFilterInternal(
			HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
			throws ServletException, IOException {

		beforeRequest(request, getBeforeMessage(request));
		try {
			filterChain.doFilter(request, response);
		}
		finally {
			afterRequest(request, getAfterMessage(request));
		}
	}


	/**
	 * Get the message to write to the log before the request.
	 * @see #createMessage
	 */
	private String getBeforeMessage(HttpServletRequest request) {
		return createMessage(request, this.beforeMessagePrefix, this.beforeMessageSuffix);
	}

	/**
	 * Get the message to write to the log after the request.
	 * @see #createMessage
	 */
	private String getAfterMessage(HttpServletRequest request) {
		return createMessage(request, this.afterMessagePrefix, this.afterMessageSuffix);
	}

	/**
	 * Create a log message for the given request, prefix and suffix.
	 * <p>If includeQueryString is true then
	 * the inner part of the log message will take the form
	 * <code>request_uri?query_string otherwise the message will
	 * simply be of the form <code>request_uri.
	 * <p>The final message is composed of the inner part as described
	 * and the supplied prefix and suffix.
	 */
	protected String createMessage(HttpServletRequest request, String prefix, String suffix) {
		StringBuffer buffer = new StringBuffer();
		buffer.append(prefix);
		buffer.append("uri=").append(request.getRequestURI());
		if (isIncludeQueryString()) {
			buffer.append('?').append(request.getQueryString());
		}
		if (isIncludeClientInfo()) {
			String client = request.getRemoteAddr();
			if (StringUtils.hasLength(client)) {
				buffer.append(";client=").append(client);
			}
			HttpSession session = request.getSession(false);
			if (session != null) {
				buffer.append(";session=").append(session.getId());
			}
			String user = request.getRemoteUser();
			if (user != null) {
				buffer.append(";user=").append(user);
			}
		}
		buffer.append(suffix);
		return buffer.toString();
	}


	/**
	 * Concrete subclasses should implement this method to write a log message
	 * <i>before the request is processed.
	 * @param request current HTTP request
	 * @param message the message to log
	 */
	protected abstract void beforeRequest(HttpServletRequest request, String message);

	/**
	 * Concrete subclasses should implement this method to write a log message
	 * <i>after the request is processed.
	 * @param request current HTTP request
	 * @param message the message to log
	 */
	protected abstract void afterRequest(HttpServletRequest request, String message);

}

Other Spring Framework examples (source code examples)

Here is a short list of links related to this Spring Framework AbstractRequestLoggingFilter.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.