001: package com.teamkonzept.web.servlet;
002:
003: import java.util.*;
004: import java.io.*;
005: import javax.servlet.http.*;
006:
007: import com.teamkonzept.lib.*;
008: import com.teamkonzept.web.*;
009:
010: import org.apache.log4j.Category;
011:
012: public class ServletInterface implements TKHttpInterface {
013:
014: protected TKHashtable env = null;
015: protected TKHashtable params = null;
016: protected TKHashtable headers = null;
017: protected String gateway = null;
018:
019: protected HttpServletRequest req;
020: protected HttpServletResponse resp;
021: protected HttpServlet serv;
022:
023: // wir sind ja jetzt Tomcat, wie ist das zu aendern ?
024: static protected String attrPrefJServ = "org.apache.jserv.";
025:
026: private static Category cat = Category
027: .getInstance(ServletInterface.class);
028:
029: public ServletInterface(HttpServletRequest req,
030: HttpServletResponse resp, HttpServlet serv) {
031: this .req = req;
032: this .resp = resp;
033: this .serv = serv;
034: }
035:
036: public HttpServletRequest getRequest() {
037: return req;
038: }
039:
040: /** returns the http session */
041: public HttpSession getSession(boolean create) {
042: return req.getSession(create);
043: }
044:
045: /** returns the htp session */
046: public HttpSession getSession() {
047: return req.getSession();
048: }
049:
050: protected String getGateway() {
051: if (gateway != null)
052: return gateway;
053: gateway = (String) req.getAttribute(attrPrefJServ
054: + "GATEWAY_INTERFACE");
055: if (gateway != null)
056: return gateway;
057:
058: return (gateway = "unknown");
059: }
060:
061: public TKHashtable getParams() {
062: if (params != null)
063: return params;
064:
065: Hashtable tmpHash = null;
066: if (req.getMethod().equals("POST")) {
067: String contentType = req.getContentType();
068: if (contentType.equals("application/x-www-form-urlencoded")) {
069: try {
070: tmpHash = HttpUtils.parsePostData(req
071: .getContentLength(), req.getInputStream());
072: } catch (IOException e) {
073: e.printStackTrace(new PrintStream(getLogStream()));
074: }
075: } else if (contentType.startsWith("multipart/form-data")) {
076: int boudaryStart = contentType.indexOf("boundary=") + 9;
077: String boundary = (boudaryStart <= 9 ? null
078: : contentType.substring(boudaryStart));
079: try {
080: TKHttpMultipartBuffer buf = new TKHttpMultipartBuffer(
081: req.getInputStream(), req
082: .getContentLength(), boundary);
083: tmpHash = buf.getParams();
084: } catch (IOException e) {
085: cat.error("getParams", e);
086: }
087:
088: }
089: } else {
090: String queryString = req.getQueryString();
091: if (queryString != null && queryString.length() > 0) {
092: try {
093: tmpHash = HttpUtils.parseQueryString(req
094: .getQueryString());
095: } catch (Throwable t) {
096: tmpHash = new Hashtable(1);
097: tmpHash.put("QUERY_STRING", queryString);
098: }
099: }
100: }
101:
102: if (tmpHash != null && tmpHash.size() > 0) {
103: params = new TKHashtable(tmpHash.size());
104: Enumeration keys = tmpHash.keys();
105:
106: while (keys.hasMoreElements()) {
107: Object key = keys.nextElement();
108: Object val = tmpHash.get(key);
109: if (val instanceof Object[]) {
110: Object[] valVec = (Object[]) val;
111: if (valVec.length > 1) {
112: params.put(key, new TKVector(valVec));
113: } else {
114: params.put(key, valVec[0]);
115: }
116: } else {
117: params.put(key, val);
118: }
119: }
120: } else {
121: params = new TKHashtable();
122: }
123:
124: return params;
125: }
126:
127: public TKHashtable getEnvironment() {
128: if (env != null)
129: return env;
130:
131: // Unterschiedlich je nach Gateway
132: env = new TKHashtable();
133:
134: Object attrsObj = req.getAttribute(attrPrefJServ
135: + "attribute_names");
136: if (attrsObj != null && attrsObj instanceof Enumeration) {
137: Enumeration attrs = (Enumeration) attrsObj;
138: while (attrs.hasMoreElements()) {
139: String attr = attrs.nextElement().toString();
140: if (req.getAttribute(attrPrefJServ + attr) != null) {
141: env.put(attr, req
142: .getAttribute(attrPrefJServ + attr)
143: .toString());
144: } else {
145: env.put(attr, "");
146: }
147: }
148: }
149:
150: // das auf jeden Fall
151: String s;
152: int i;
153: i = req.getContentLength();
154: if (i >= 0)
155: env.put("CONTENT_LENGTH", String.valueOf(i));
156: s = req.getContentType();
157: if (s != null)
158: env.put("CONTENT_TYPE", s);
159: s = req.getScheme();
160: if (s != null)
161: env.put("SCHEME", s);
162: s = req.getProtocol();
163: if (s != null)
164: env.put("SERVER_PROTOCOL", s);
165: i = req.getServerPort();
166: if (i >= 0)
167: env.put("SERVER_PORT", String.valueOf(i));
168: s = req.getRemoteAddr();
169: if (s != null)
170: env.put("REMOTE_ADDR", s);
171: s = req.getRemoteHost();
172: if (s != null)
173: env.put("REMOTE_HOST", s);
174: s = req.getAuthType();
175: if (s != null)
176: env.put("AUTH_TYPE", s);
177: s = req.getPathInfo();
178: if (s != null)
179: env.put("PATH_INFO", s);
180: s = req.getPathTranslated();
181: if (s != null)
182: env.put("PATH_TRANSLATED", s);
183: s = req.getMethod();
184: if (s != null)
185: env.put("REQUEST_METHOD", s);
186: s = req.getRemoteUser();
187: if (s != null)
188: env.put("REMOTE_USER", s);
189:
190: return env;
191: }
192:
193: public HttpServletResponse getResponse() {
194: return resp;
195: }
196:
197: public javax.servlet.ServletContext getServletContext() {
198: return serv.getServletContext();
199: }
200:
201: public OutputStream getOutputStream() {
202: try {
203: return resp.getOutputStream();
204: } catch (IOException e) {
205: e.printStackTrace(new PrintStream(getLogStream()));
206: }
207: return System.out;
208: }
209:
210: public OutputStream getLogStream() {
211: return new ServletLog(serv.getServletContext());
212: }
213:
214: public String getOwnName() {
215: String path = req.getServletPath();
216: int l = path.lastIndexOf("/");
217: if (l != -1)
218: return path.substring(l + 1);
219: return path;
220: }
221:
222: public String getOwnURL() {
223: String s = req.getRequestURI();
224: int i = s.indexOf('?');
225: return (i >= 0 ? s.substring(0, i) : s);
226: }
227:
228: public String getOwnPath() {
229: String path = req.getServletPath();
230: int l = path.lastIndexOf("/");
231: if (l != -1)
232: return path.substring(0, l + 1);
233: return "";
234: }
235:
236: public String getContextPath() {
237: return req.getContextPath();
238: }
239:
240: public String getDocumentRoot() {
241: return req.getRealPath("/");
242: }
243:
244: public String getServerName() {
245: return req.getServerName();
246: }
247:
248: public String getRemoteUser() {
249: return req.getRemoteUser();
250: }
251:
252: public TKHashtable getHeaders() {
253: if (headers != null)
254: return headers;
255:
256: TKHashtable headers = new TKHashtable();
257: Enumeration e = req.getHeaderNames();
258: while (e.hasMoreElements()) {
259: Object k = e.nextElement();
260: Object v = req.getHeader((String) k);
261: headers.put(k, (v != null ? v : ""));
262: }
263: return headers;
264: }
265:
266: public void setStatus(int code, String msg) {
267: resp.setStatus(code, msg);
268: }
269:
270: public void addHeader(String name, String value) {
271: resp.setHeader(name, value);
272: }
273:
274: public String getPathInfo() {
275: return req.getPathInfo();
276: }
277:
278: public String getPathTranslated() {
279: return req.getPathTranslated();
280: }
281:
282: public Cookie[] getCookies() {
283: return req.getCookies();
284: }
285:
286: /** liefert absolute URL - inkl. Servername + Port */
287: public String getAbsoluteURL() {
288: return (HttpUtils.getRequestURL(req)).toString();
289: }
290:
291: public void addCookie(final Cookie cookie) {
292: resp.addCookie(cookie);
293: }
294:
295: public void addDateHeader(final String name, final long value) {
296: resp.setDateHeader(name, value);
297: }
298: }
|