Archivos por Etiqueta: tomcat

Java: ResponseHeaders Servlet Filter

Un simple filtro de Tomcat para incluir Headers en las cabeceras de respuesta HTTP (util cuando tienes un Tomcat standalone y quieres incluir un Cache-Control o similar):

import java.io.IOException;
import java.util.Collections;
import java.util.Enumeration;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;

public final class ResponseHeadersFilter implements Filter {
	private Map<String, String> headers = null;

	@Override
	public void init(final FilterConfig filterConfig) throws ServletException {
		final LinkedHashMap<String, String> headers = 
				new LinkedHashMap<String, String>();
		final Enumeration<String> e = filterConfig.getInitParameterNames();
		while (e.hasMoreElements()) {
			final String name = e.nextElement();
			final String value = filterConfig.getInitParameter(name);
			headers.put(name, value);
		}
		this.headers = Collections.unmodifiableMap(headers);
	}

	@Override
	public void doFilter(final ServletRequest request, 
			final ServletResponse response, final FilterChain chain)
			throws IOException, ServletException {
		if (response instanceof HttpServletResponse) {
			final HttpServletResponse res = ((HttpServletResponse) response);
			for (final Entry<String, String> e : headers.entrySet()) {
				res.setHeader(e.getKey(), e.getValue());
			}
		}
		chain.doFilter(request, response);
	}

	@Override
	public void destroy() {
	}
}

Y la configuracion del web.xml:

    <filter>
        <filter-name>ResponseHeadersFilter</filter-name>
        <filter-class>com.package.ResponseHeadersFilter</filter-class>
        <init-param>
           <param-name>Cache-Control</param-name>
           <param-value>must-revalidate, max-age=0, no-cache, no-store</param-value>
        </init-param>
        <init-param>
           <param-name>Expires</param-name>
           <param-value>0</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>ResponseHeadersFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

La salida sera algo asi:

HTTP/1.1 200 OK
Content-Type: text/html;charset=UTF-8
Cache-Control: must-revalidate, max-age=0, no-cache, no-store
Expires: 0
Content-Length: 3
Date: Sat, 04 Jan 2014 05:40:23 GMT

...

Source code: ResponseHeadersFilter.java

Java: Async vs Sync Servlet and BIO / NIO Tomcat Connector

Un pequeño micro benchmark para comparar un Servlet Sincrono “clasico” versus su version Asincrona (de la nueva especificacion de Servlet 3.0) y la diferencia de rendimiento del conector BIO vs NIO.

## Connector BIO: org.apache.coyote.http11.Http11Protocol ##
SyncServlet  time=52837ms threads=100 req/thread=2000 request=200000 req/ms=3 
AsyncServlet time=55006ms threads=100 req/thread=2000 request=200000 req/ms=3 
SyncServlet  time=50124ms threads=100 req/thread=2000 request=200000 req/ms=3 
AsyncServlet time=54444ms threads=100 req/thread=2000 request=200000 req/ms=3 
SyncServlet  time=49937ms threads=100 req/thread=2000 request=200000 req/ms=4 
AsyncServlet time=54180ms threads=100 req/thread=2000 request=200000 req/ms=3 

## Connector NIO: org.apache.coyote.http11.Http11NioProtocol ##
SyncServlet  time=64741ms threads=100 req/thread=2000 request=200000 req/ms=3 
AsyncServlet time=72572ms threads=100 req/thread=2000 request=200000 req/ms=2 
SyncServlet  time=64163ms threads=100 req/thread=2000 request=200000 req/ms=3 
AsyncServlet time=72353ms threads=100 req/thread=2000 request=200000 req/ms=2 
SyncServlet  time=66192ms threads=100 req/thread=2000 request=200000 req/ms=3 
AsyncServlet time=71574ms threads=100 req/thread=2000 request=200000 req/ms=2 

* Menor tiempo mejor

Leer más de esta entrada

Java: Dump Servlet Filter

Un simple filtro de Tomcat para hacer Dumps de los parametros:

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public final class DumpFilter implements Filter {
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,SSS");
	private volatile int id = 0;

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, 
			final FilterChain chain) throws IOException, ServletException {
		final String myId = Integer.toHexString(++id | 0x80000000);
		final String ts = formatLocalDateTime(System.currentTimeMillis());
		final StringBuilder sb = new StringBuilder(1024);
		final Enumeration<String> e = request.getParameterNames();

		while (e.hasMoreElements()) {
			final String key = e.nextElement();
			final String[] values = request.getParameterValues(key);
			sb.setLength(0);
			for (final String value : values) {
				sb.append(ts).append(" DUMP(").append(myId).append(':');
				sb.append(request.getRemoteAddr()).append(':');
				sb.append(request.getRemotePort()).append("): ");
				sb.append(key).append('=').append(value).append('\n');
			}
			System.out.print(sb.toString());
		}
		chain.doFilter(request, response);
	}

	private String formatLocalDateTime(final long millis) {
		synchronized (sdf) {
			return sdf.format(new Date(millis));
		}
	}

	@Override
	public void destroy() {
	}
}

Y la configuracion del web.xml:

    <filter>
        <filter-name>DumpFilter</filter-name>
        <filter-class>com.package.DumpFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>DumpFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

La salida sera algo asi:

2013-07-30 21:00:35,577 DUMP(80000002:127.0.0.1:5203): param1=value1
2013-07-30 21:00:35,577 DUMP(80000002:127.0.0.1:5203): param2=value2

Source code: DumpFilter.java

A %d blogueros les gusta esto: