KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > gui > web > AbstractJacRequest


1 /*
2   Copyright (C) 2002 Laurent Martelli <laurent@aopsys.com>
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */

17
18 package org.objectweb.jac.aspects.gui.web;
19
20 import java.util.Enumeration JavaDoc;
21 import java.util.Hashtable JavaDoc;
22 import java.util.Map JavaDoc;
23 import javax.servlet.http.HttpServletRequest JavaDoc;
24 import org.apache.log4j.Logger;
25 import org.objectweb.jac.util.Semaphore;
26
27 /**
28  * This class represents a multi-part HttpRequest.
29  */

30 public abstract class AbstractJacRequest implements JacRequest {
31     static Logger logger = Logger.getLogger("web.session");
32
33     Map JavaDoc headers = new Hashtable JavaDoc();
34     public AbstractJacRequest(HttpServletRequest JavaDoc servletRequest) {
35         Enumeration JavaDoc headerNames = servletRequest.getHeaderNames();
36         while (headerNames.hasMoreElements()) {
37             String JavaDoc name = (String JavaDoc)headerNames.nextElement();
38             headers.put(name,servletRequest.getHeader(name));
39         }
40     }
41     public abstract Object JavaDoc getParameter(String JavaDoc name);
42     public boolean isIEUserAgent() {
43         String JavaDoc userAgent = getUserAgent();
44         if (userAgent!=null && userAgent.indexOf("MSIE")!=-1) {
45             return true;
46         } else {
47             return false;
48         }
49     }
50     public String JavaDoc getUserAgent() {
51         return getHeader("User-Agent");
52     }
53     public boolean userAgentMatch(String JavaDoc s) {
54         String JavaDoc userAgent = getHeader("User-Agent");
55         if (userAgent!=null && userAgent.indexOf(s)!=-1) {
56             return true;
57         } else {
58             return false;
59         }
60     }
61     public String JavaDoc getHeader(String JavaDoc name) {
62         return (String JavaDoc)headers.get(name);
63     }
64
65     JacRequest parent;
66     public void setParent(JacRequest parent) {
67         this.parent = parent;
68     }
69
70     /** The semaphore that blocks the requesting thread until the
71         response is available. */

72     transient protected Semaphore semaphore = new Semaphore();
73
74     protected static final long DEFAULT_REQUEST_TIMEOUT = 1000*60*30; // 30 minutes
75

76     public boolean waitForResponse() {
77         logger.debug("wait for response " + this + " (" + semaphore.getCount()+")");
78         boolean result = semaphore.acquire(DEFAULT_REQUEST_TIMEOUT);
79         if (result) {
80             logger.debug("got response " + this + " (" + semaphore.getCount()+")");
81             if (semaphore.getCount()>0) {
82                 logger.warn("Session "+this+": semaphore > 0 ("+semaphore.getCount()+")");
83             }
84         } else {
85             logger.debug("timeout "+this);
86         }
87         return result;
88     }
89
90     public void setResponse() {
91         logger.debug("set response " + this + " (" + semaphore.getCount()+")");
92         semaphore.release();
93     }
94
95 }
96
Popular Tags