001    /*
002      Copyright (C) 2002 Laurent Martelli <laurent@aopsys.com>
003    
004      This program is free software; you can redistribute it and/or modify
005      it under the terms of the GNU Lesser General Public License as
006      published by the Free Software Foundation; either version 2 of the
007      License, or (at your option) any later version.
008    
009      This program is distributed in the hope that it will be useful,
010      but WITHOUT ANY WARRANTY; without even the implied warranty of
011      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012      GNU Lesser General Public License for more details.
013    
014      You should have received a copy of the GNU Lesser General Public License
015      along with this program; if not, write to the Free Software
016      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
017    
018    package org.objectweb.jac.aspects.gui.web;
019    
020    import java.util.Enumeration;
021    import java.util.Hashtable;
022    import java.util.Map;
023    import javax.servlet.http.HttpServletRequest;
024    import org.apache.log4j.Logger;
025    import org.objectweb.jac.util.Semaphore;
026    
027    /**
028     * This class represents a multi-part HttpRequest.
029     */
030    public abstract class AbstractJacRequest implements JacRequest {
031        static Logger logger = Logger.getLogger("web.session");
032    
033        Map headers = new Hashtable();
034        public AbstractJacRequest(HttpServletRequest servletRequest) {
035            Enumeration headerNames = servletRequest.getHeaderNames();
036            while (headerNames.hasMoreElements()) {
037                String name = (String)headerNames.nextElement();
038                headers.put(name,servletRequest.getHeader(name));
039            }
040        }
041        public abstract Object getParameter(String name);
042        public boolean isIEUserAgent() {
043            String userAgent = getUserAgent();
044            if (userAgent!=null && userAgent.indexOf("MSIE")!=-1) {
045                return true;
046            } else {
047                return false;
048            }
049        }
050        public String getUserAgent() {
051            return getHeader("User-Agent");
052        }
053        public boolean userAgentMatch(String s) {
054            String userAgent = getHeader("User-Agent");
055            if (userAgent!=null && userAgent.indexOf(s)!=-1) {
056                return true;
057            } else {
058                return false;
059            }        
060        }
061        public String getHeader(String name) {
062            return (String)headers.get(name);
063        }
064    
065        JacRequest parent;
066        public void setParent(JacRequest parent) {
067            this.parent = parent;
068        }
069    
070        /** The semaphore that blocks the requesting thread until the
071            response is available. */
072        transient protected Semaphore semaphore = new Semaphore();
073    
074        protected static final long DEFAULT_REQUEST_TIMEOUT = 1000*60*30; // 30 minutes
075       
076        public boolean waitForResponse() {
077            logger.debug("wait for response " + this + " (" + semaphore.getCount()+")");
078            boolean result = semaphore.acquire(DEFAULT_REQUEST_TIMEOUT);
079            if (result) {
080                logger.debug("got response " + this + " (" + semaphore.getCount()+")");
081                if (semaphore.getCount()>0) {
082                    logger.warn("Session "+this+": semaphore > 0 ("+semaphore.getCount()+")");
083                }
084            } else {
085                logger.debug("timeout "+this);         
086            }
087            return result;
088        }
089    
090        public void setResponse() {
091            logger.debug("set response " + this + " (" + semaphore.getCount()+")");
092            semaphore.release();
093        }
094    
095    }