Source code: jac/aspects/gui/web/AbstractJacRequest.java
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 jac.aspects.gui.web;
19
20 import jac.util.Semaphore;
21 import java.util.Enumeration;
22 import java.util.Hashtable;
23 import java.util.Map;
24 import javax.servlet.http.HttpServletRequest;
25 import jac.util.Log;
26
27 /**
28 * This class represents a multi-part HttpRequest.
29 */
30 public abstract class AbstractJacRequest implements JacRequest {
31 Map headers = new Hashtable();
32 public AbstractJacRequest(HttpServletRequest servletRequest) {
33 Enumeration headerNames = servletRequest.getHeaderNames();
34 while (headerNames.hasMoreElements()) {
35 String name = (String)headerNames.nextElement();
36 headers.put(name,servletRequest.getHeader(name));
37 }
38 }
39 public abstract Object getParameter(String name);
40 public boolean isIEUserAgent() {
41 String userAgent = getUserAgent();
42 if (userAgent!=null && userAgent.indexOf("MSIE")!=-1) {
43 return true;
44 } else {
45 return false;
46 }
47 }
48 public String getUserAgent() {
49 return getHeader("User-Agent");
50 }
51 public String getHeader(String name) {
52 return (String)headers.get(name);
53 }
54
55 JacRequest parent;
56 public void setParent(JacRequest parent) {
57 this.parent = parent;
58 }
59
60 /** The semaphore that blocks the requesting thread until the
61 response is available. */
62 transient protected Semaphore semaphore = new Semaphore();
63
64 protected static final long DEFAULT_REQUEST_TIMEOUT = 1000*60*30; // 30 minutes
65
66 public boolean waitForResponse() {
67 Log.trace("web.session",
68 "wait for response " + this + " (" + semaphore.getCount()+")");
69 boolean result = semaphore.acquire(DEFAULT_REQUEST_TIMEOUT);
70 if (result) {
71 Log.trace("web.session",
72 "got response " + this + " (" + semaphore.getCount()+")");
73 if (semaphore.getCount()>0) {
74 Log.warning("Session "+this+": semaphore > 0 ("+semaphore.getCount()+")");
75 }
76 } else {
77 Log.trace("web.session","timeout "+this);
78 }
79 return result;
80 }
81
82 public void setResponse() {
83 Log.trace("web.session",
84 "set response " + this + " (" + semaphore.getCount()+")");
85 semaphore.release();
86 }
87
88 }