001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 2005 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: JFormAuthenticator.java 7591 2005-10-24 16:08:12Z moghrabi $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas.security.realm.web.catalina55;
025:
026: import java.io.IOException;
027: import java.io.InputStream;
028: import java.util.Enumeration;
029: import java.util.Locale;
030:
031: import javax.servlet.http.Cookie;
032:
033: import org.apache.catalina.Session;
034: import org.apache.catalina.authenticator.Constants;
035: import org.apache.catalina.authenticator.FormAuthenticator;
036: import org.apache.catalina.authenticator.SavedRequest;
037: import org.apache.catalina.connector.Request;
038: import org.apache.tomcat.util.buf.ByteChunk;
039:
040: /**
041: * Form authenticator which don't save the body if there is no body
042: * Extends FormAuthenticator of Tomcat.
043: * @author Florent Benoit
044: */
045: public class JFormAuthenticator extends FormAuthenticator {
046:
047: /**
048: * Buffer size
049: */
050: private static final int BUFFER_SIZE = 4096;
051:
052: /**
053: * Save the original request information into our session.
054: * @param request The request to be saved
055: * @param session The session to contain the saved information
056: * @throws IOException if the save of the request fails
057: */
058: protected void saveRequest(Request request, Session session)
059: throws IOException {
060:
061: // Create and populate a SavedRequest object for this request
062: SavedRequest saved = new SavedRequest();
063: Cookie[] cookies = request.getCookies();
064: if (cookies != null) {
065: for (int i = 0; i < cookies.length; i++) {
066: saved.addCookie(cookies[i]);
067: }
068: }
069: Enumeration names = request.getHeaderNames();
070: while (names.hasMoreElements()) {
071: String name = (String) names.nextElement();
072: Enumeration values = request.getHeaders(name);
073: while (values.hasMoreElements()) {
074: String value = (String) values.nextElement();
075: saved.addHeader(name, value);
076: }
077: }
078: Enumeration locales = request.getLocales();
079: while (locales.hasMoreElements()) {
080: Locale locale = (Locale) locales.nextElement();
081: saved.addLocale(locale);
082: }
083:
084: if ("POST".equalsIgnoreCase(request.getMethod())) {
085: ByteChunk body = new ByteChunk();
086: body.setLimit(request.getConnector().getMaxSavePostSize());
087:
088: byte[] buffer = new byte[BUFFER_SIZE];
089: int bytesRead;
090: InputStream is = request.getInputStream();
091: if (is.available() > 0) {
092: while ((bytesRead = is.read(buffer)) >= 0) {
093: body.append(buffer, 0, bytesRead);
094: }
095: saved.setBody(body);
096: }
097: }
098: saved.setMethod(request.getMethod());
099: saved.setQueryString(request.getQueryString());
100: saved.setRequestURI(request.getRequestURI());
101:
102: // Stash the SavedRequest in our session for later use
103: session.setNote(Constants.FORM_REQUEST_NOTE, saved);
104: }
105: }
|