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

What this is

This file 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.

Other links

The source code

/*
 *                 Sun Public License Notice
 * 
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 * 
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

/**
 * MonitorRequestWrapper.java
 *
 *
 * Created: Tue Feb 27 18:32:32 2001
 *
 * @author Ana von Klopp
 * @author Simran Gleason
 * @version
 */

package org.netbeans.modules.web.monitor.server;

import java.util.Collection;
import java.util.Date;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Stack;
import java.util.Vector;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpUtils;
import org.netbeans.modules.web.monitor.data.RequestData;
import org.netbeans.modules.web.monitor.data.Param;

/**
 * The MonitorRequestWrapper class is used by the MonitorFilter to
 * wrap the request. It's main function is to ensure that the
 * application receives the data from a replay request.
 */
public class MonitorRequestWrapper extends HttpServletRequestWrapper {
    
    private boolean replay = false;

    // These fields hold local variables during replays. 
    private String localMethod = null;
    private String localProtocol = null;
    private String localScheme = null;
    private String localRemoteAddr = null;
    private String localQueryString = null;
    private Param[] localHeaders = null;
    private Vector localCookies = null;
    private Map oldParams = null; 
    private Map localParams = null;
    private Stack extraParamStack = null;

    // These fields are used to manage session replacement during
    // replay, if the server supports it
    public final static String JSESSIONID = "JSESSIONID"; // NOI18N
    public final static String REPLACED =
	"netbeans.replay.session-replaced"; //NOI18N    

    private static final boolean debug = false;
    
    MonitorRequestWrapper(HttpServletRequest req) {
	super(req);
    }

    /**
     * The filter will only wrap the request if it was a
     * HttpServletRequest. This is a convenience method for 
     * accessing the request variable as such, for those methods that
     * aren't available on the regular servlet request.
     */
    private HttpServletRequest getHRequest() { 
	return (HttpServletRequest)getRequest(); 
    }

    // GETTERS FROM THE HttpServletRequest
    //
    // The getters implement the decorator pattern, except for replays
    // where we use local data. 


    /**
     * During a replay, returns the local value. If not a replay,
     * simply returns the value of invoking the method on the wrapped 
     * request. 
     */
    public String getMethod() {
	if (replay) {
	    return localMethod;
	}
	return getHRequest().getMethod();
    }

    // ********************** PARAMETERS *************************
    // All the getParameter methods must refer to getParameterMap for
    // locally set parameters. See pushExtraParameters and
    // popExtraParameters for additional detail on local parameters. 
    //

    /** getParameterMap returns
* a) the parameter map from the request, if this is not a replay
* b) the local parameter map, if no extra parameters have been * set ("extra parameters" are parameters which are set through * the tag inside .
* c) the local parameter map augmented with parameters so set.
* * @see pushExtraParameters * @see popExtraParameters */ public java.util.Map getParameterMap() { if(debug) log("getParameterMap()"); //NOI18N if (!replay) return getRequest().getParameterMap(); // Could cache the results of processing the parameters, but // it is relatively unusual that this gets expensive. It will // only get repeated on a replay where the request processing // involves a dispatch made from a JSP using the param tag AND // the file that got dispatched to accesses the parameter more // than once. if(extraParamStack == null || extraParamStack.empty()) return (java.util.Map)localParams; Map map = (Map)extraParamStack.peek(); if(map.size() == 0) return (java.util.Map)localParams; Hashtable ht = new Hashtable(); Iterator keys = localParams.keySet().iterator(); while(keys.hasNext()) { Object o = keys.next(); if(map.containsKey(o)) { String[] vals0 = (String[])localParams.get(o); String[] vals1 = (String[])map.get(o); String[] vals2 = new String[vals0.length + vals1.length]; System.arraycopy(vals0, 0, vals2, 0, vals0.length); System.arraycopy(vals1, 0, vals2, vals0.length, vals1.length); ht.put(o, vals2); } else ht.put(o, localParams.get(o)); } keys = map.keySet().iterator(); while(keys.hasNext()) { Object o = keys.next(); if(localParams.containsKey(o)) continue; ht.put(o, map.get(o)); } return (Map)ht; } /** * If this is not a replay, getParameter(key) returns the value of * invoking the method on the original request. If it is a replay, * it returns the first string of the String array for the key, if * such an array exists. Otherwise it returns null. */ public String getParameter(String key) { if(debug) log("getParameters()"); //NOI18N if (!replay) return getRequest().getParameter(key); String [] values = (String[])getParameterMap().get(key); if (values != null && values.length > 0) { return values[0]; } return null; } /** * If this is not a replay, getParameterNames returns the value of * invoking the method on the original request. If it is a replay, * it returns an Enumeration derived from the keyset of the * parameter map. */ public Enumeration getParameterNames() { if(debug) log("getParameterNames"); //NOI18N if (!replay) return getRequest().getParameterNames(); if(debug) { Enumeration e = new Vector(getParameterMap().keySet()).elements(); while(e.hasMoreElements()) log("\t" + String.valueOf(e.nextElement())); //NOI18N } return new Vector(getParameterMap().keySet()).elements(); } /** * If this is not a replay, getParameterValues returns the value of * invoking the method on the original request. If it is a replay, * it returns a String array matching the key in the local parameter * map. */ public String [] getParameterValues(String name) { if(debug) log("getParameterValues"); //NOI18N if (!replay) return getRequest().getParameterValues(name); return (String[])getParameterMap().get(name); } /** * During a replay, returns the local value. If not a replay, * simply returns the value of invoking the method on the wrapped * request. */ public String getQueryString() { if (!replay) return getHRequest().getQueryString(); return localQueryString; } /** * During a replay, returns the local value. If not a replay, * simply returns the value of invoking the method on the wrapped * request. */ public String getProtocol() { if (!replay) return getRequest().getProtocol(); return localProtocol; } /** * During a replay, returns the local value. If not a replay, * simply returns the value of invoking the method on the wrapped * request. */ public String getScheme() { if (replay) return localScheme; return getRequest().getScheme(); } /** * During a replay, returns the local value. If not a replay, * simply returns the value of invoking the method on the wrapped * request. * * According to the Servlet specification, this method must return * null if there is no header of the specified name. */ public String getHeader(String key) { if (replay) { int len = localHeaders.length; for(int i=0; i 0) { if(debug) log("Now adding cookies"); //NOI18N String ckname = null, ckvalue=null; for(int i=0; i 0) cookieBuf.append("; "); //NOI18N cookieBuf.append(JSESSIONID); cookieBuf.append("="); //NOI18N cookieBuf.append(idFromRequest); } // In the latter case we do nothing. } else { // The session ID was not replaced, and to adjust for this // we add the session cookie from the incoming request, if // there is one. // PENDING! // If the user wanted to replace the ID and it failed, // this should be flagged here. if(debug) log("Old request is " + getHRequest().toString()); //NOI18N Cookie[] ck = getHRequest().getCookies(); if(debug) log("Got the incoming cookies"); //NOI18N if(ck != null && ck.length > 0) { for(int i=0; i 0) cookieBuf.append("; "); //NOI18N cookieBuf.append(JSESSIONID); cookieBuf.append("="); //NOI18N cookieBuf.append(ck[i].getValue()); } } } // the entity that sent the request did not send any // cookies, do nothing } String cookieStr = cookieBuf.toString(); if(cookieStr.equals("")) { //NOI18N if(debug) log("No cookies, deleting cookie header"); //NOI18N removeHeader("cookie"); //NOI18N } else { if(debug) log("Setting cookie header to " + //NOI18N cookieBuf.toString()); setHeader("cookie", cookieBuf.toString()); //NOI18N } } // UTILITY METHODS // These methods set local data. They are used by // populate to set data during replays. /** * This method MUST be invoked by the monitor filter whenever it * receives a request to process a dispatched request, BEFORE it * collects any data. * We need to do this because the specs allow web components to * add extra parameters on dispatch, e.g. * * * * and these are only available to the dispatched-to resources * (at least some servers remove them after the request dispatcher * has terminated). So we must have a mechanism which guarantees * that the resources in the web app (and the monitor itself) only * sees the parameters when they would have been visible without * the monitor in place. * To deal with such parameters on a replay (where the request's * "real" parameters have been replaced with the ones from the * original request) I have to add them to the ones that the * wrapper already knows about. */ void pushExtraParameters() { if(!replay) return; if(debug) log("pushExtraParameters"); //NOI18N // If this is a replay and the request was dispatched using // the following type of syntax // // // // then the server will add a parameter to the request in the // background (there are no API methods to do this). So we // have to check if the parameters that the original request // is aware of have grown. If so, we create an additional map // with the extra parameters. Map extraParams = new Hashtable(); Map currentMap = getRequest().getParameterMap(); Iterator keys = currentMap.keySet().iterator(); while(keys.hasNext()) { Object o = keys.next(); if(debug) { //log("Key: " + (String)o); //NOI18N String[] value = (String[])currentMap.get(o); StringBuffer buf = new StringBuffer(); for(int k=0; k
... 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.