001: /*
002: * ServletHTTPRequest.java
003: *
004: * Created on July 11, 2003, 12:43 PM
005: */
006:
007: package com.sun.portal.rproxy.connectionhandler;
008:
009: import java.net.URLDecoder;
010: import java.util.ArrayList;
011: import java.util.Enumeration;
012: import java.util.HashMap;
013: import java.util.Iterator;
014: import java.util.List;
015: import java.util.Map;
016: import java.util.NoSuchElementException;
017: import java.util.StringTokenizer;
018: import java.util.Vector;
019:
020: import javax.servlet.RequestDispatcher;
021: import javax.servlet.ServletInputStream;
022: import javax.servlet.http.Cookie;
023: import javax.servlet.http.HttpServletRequest;
024: import javax.servlet.http.HttpSession;
025:
026: /**
027: *
028: * @author Rajesh T
029: * @version 1.0 @ date 11-07-2003
030: */
031: public class ServletHTTPRequest implements HttpServletRequest {
032: private HTTPRequest req = null;
033:
034: private String _crlf = new String(new byte[] { 13, 10 });
035:
036: private char lf = '\n';
037:
038: // private Hastable _headerLines = null;
039:
040: private boolean _firstLine = true;
041:
042: private boolean _headerComplete = false;
043:
044: private List _headerLines = new ArrayList();
045:
046: private String _httpVersion = "";
047:
048: private String _method = null;
049:
050: private String _path = "";
051:
052: private HashMap headerMap = new HashMap();
053:
054: public ServletHTTPRequest(HTTPRequest req) {
055: this .req = req;
056: parseHeaders(new String(req.getRequestHeaderBytes()));
057: }
058:
059: /*
060: * Req
061: */
062: public void removeAttribute(String name) {
063: return;
064: }
065:
066: /*
067: * Req
068: */
069:
070: public void setAttribute(String name, Object value) {
071: return;
072: }
073:
074: /*
075: * Req
076: */
077: public String getAuthType() {
078: return null;
079: }
080:
081: /*
082: * Req
083: */
084: public String getContextPath() {
085: throw new UnsupportedOperationException(
086: "getContextPath() not supported");
087: }
088:
089: /*
090: * Req
091: */
092: public Cookie[] getCookies() {
093: return null;
094: // throw new UnsupportedOperationException("getCookies() not
095: // supported");
096: }
097:
098: /*
099: * Req
100: */
101: public long getDateHeader(String name) {
102: // return null;
103: throw new UnsupportedOperationException(
104: "getDateHeader() not supported");
105: }
106:
107: /*
108: * Req
109: */
110: public String getHeader(String header) {
111: return req.getRequestHeader(header);
112: }
113:
114: /*
115: * Req
116: */
117: public Enumeration getHeaderNames() {
118: String s;
119: Vector headers = new Vector();
120: int headerOffset;
121: int numHeaders = _headerLines.size();
122: for (int i = 0; i < numHeaders; ++i) {
123: s = (String) _headerLines.get(i);
124: headerOffset = s.indexOf(':');
125: headers.add(headerOffset == -1 ? s : s.substring(0,
126: headerOffset).trim());
127: }
128: return headers.elements();
129: }
130:
131: /*
132: * Req
133: */
134: public Enumeration getHeaders(String header) {
135: String s;
136: Vector headers = new Vector();
137: int headerLen = header.length();
138: int headerOffset;
139: int numHeaders = _headerLines.size();
140: for (int i = 0; i < numHeaders; ++i) {
141: s = (String) _headerLines.get(i);
142: if (s.regionMatches(true, 0, header, 0, headerLen)) {
143: headerOffset = s.indexOf(':');
144: headers.add(headerOffset == -1 ? s : s.substring(0,
145: headerOffset).trim());
146: }
147: }
148: return headers.elements();
149: }
150:
151: /*
152: * Req
153: */
154: public int getIntHeader(String name) {
155: String value = headerMap.get(name).toString();
156: try {
157: return Integer.valueOf(value).intValue();
158: } catch (NumberFormatException nfe) {
159: return -1;
160: }
161: }
162:
163: /*
164: * Req
165: */
166: public String getMethod() {
167: return _method;
168: }
169:
170: /*
171: * Req
172: */
173: public String getPathInfo() {
174: // return null;
175: throw new UnsupportedOperationException(
176: "getPathInfo() not supported");
177: }
178:
179: /*
180: * Req
181: */
182: public String getPathTranslated() {
183: // return null;
184: throw new UnsupportedOperationException(
185: "getPathTranslated() not supported");
186: }
187:
188: /*
189: * Req
190: */
191: public String getQueryString() {
192: String URI = getRequestURI();
193: int queryIndex = URI.indexOf("?");
194: if (queryIndex == -1) {
195: return null;
196: }
197: return URI.substring(queryIndex + 1);
198: }
199:
200: /*
201: * Req
202: */
203: public String getRemoteUser() {
204: // return null;
205: throw new UnsupportedOperationException(
206: "getRemoteUser() not supported");
207: }
208:
209: /*
210: * Req
211: */
212: public String getRequestedSessionId() {
213: // return null;
214: throw new UnsupportedOperationException(
215: "getRequestedSessionId() not supported");
216: }
217:
218: /*
219: * Req URL ??
220: */
221: public String getRequestURI() {
222: String decodedPath = URLDecoder.decode(_path);
223: return decodedPath;
224: }
225:
226: /*
227: * Req
228: */
229: public String getServletPath() {
230: // return null;
231: throw new UnsupportedOperationException(
232: "getServletPath() not supported");
233: }
234:
235: /*
236: * Req
237: */
238: public HttpSession getSession() {
239: // return null;
240: throw new UnsupportedOperationException(
241: "getSession() not supported");
242: }
243:
244: /*
245: * Req
246: */
247: public HttpSession getSession(boolean create) {
248: // return null;
249: throw new UnsupportedOperationException(
250: "getSession() not supported");
251: }
252:
253: /*
254: * Req
255: */
256: public java.security.Principal getUserPrincipal() {
257: // return null;
258: throw new UnsupportedOperationException(
259: "getUserPrincipal() not supported");
260: }
261:
262: /*
263: * Req
264: */
265: public boolean isRequestedSessionIdFromCookie() {
266: // return null;
267: throw new UnsupportedOperationException(
268: "isRequestedSessionIdFromCookie() not supported");
269: }
270:
271: /*
272: * Req
273: */
274: public boolean isRequestedSessionIdFromUrl() {
275: // return null;
276: throw new UnsupportedOperationException(
277: "isRequestedSessionIdFromUrl() not supported");
278: }
279:
280: /*
281: * Req
282: */
283: public boolean isRequestedSessionIdFromURL() {
284: // return null;
285: throw new UnsupportedOperationException(
286: "isRequestedSessionIdFromURL() not supported");
287: }
288:
289: /*
290: * Req
291: */
292: public boolean isRequestedSessionIdValid() {
293: // return null;
294: throw new UnsupportedOperationException(
295: "isRequestedSessionIdValid() not supported");
296: }
297:
298: /*
299: * Req
300: */
301: public boolean isUserInRole(String role) {
302: // return null;
303: throw new UnsupportedOperationException(
304: "isUserInRole() not supported");
305: }
306:
307: /*
308: * Req
309: */
310: public Object getAttribute(String name) {
311: return null;
312: }
313:
314: /*
315: * Req
316: */
317: public Enumeration getAttributeNames() {
318: return null;
319: }
320:
321: /*
322: * Req
323: */
324: public String getCharacterEncoding() {
325: return null;
326: }
327:
328: /*
329: * Req
330: */
331: public int getContentLength() {
332: return 0;
333: }
334:
335: /*
336: * Req
337: */
338: public ServletInputStream getInputStream() {
339: // return null;
340: throw new UnsupportedOperationException(
341: "getInputStream() not supported");
342: }
343:
344: /*
345: * Req
346: */
347: public java.util.Locale getLocale() {
348: return null;
349: }
350:
351: /*
352: * Req
353: */
354: public Enumeration getLocales() {
355: return new Vector().elements();
356: }
357:
358: /*
359: * Req Paramerter
360: */
361: public String getParameter(String name) {
362: // return null;
363: QueryStringParser qP = new QueryStringParser(getQueryString());
364: Map map = qP.getParamMap();
365: Object param = map.get(name);
366:
367: if (param == null) {
368: return null;
369: }
370: return param.toString();
371: }
372:
373: /*
374: * Req
375: */
376: public Enumeration getParameterNames() {
377: return new Vector().elements();
378: }
379:
380: /*
381: * Req
382: */
383: public String[] getParameterValues(String name) {
384: return null;
385: }
386:
387: /*
388: * Req
389: */
390: public String getProtocol() {
391: return _httpVersion;
392: }
393:
394: /*
395: * Req
396: */
397: public java.io.BufferedReader getReader() {
398: return null;
399: }
400:
401: /*
402: * Req
403: */
404: public String getRealPath(String path) {
405: return null;
406: }
407:
408: /*
409: * Req
410: */
411: public String getRemoteAddr() {
412: return null;
413: }
414:
415: /*
416: * Req
417: */
418: public String getRemoteHost() {
419: return null;
420: }
421:
422: /*
423: * Req
424: */
425: public RequestDispatcher getRequestDispatcher(String path) {
426: return null;
427: }
428:
429: /*
430: * Req
431: */
432: public String getScheme() {
433: return null;
434: }
435:
436: /*
437: * Req
438: */
439: public String getServerName() {
440: return null;
441: }
442:
443: /*
444: * Req
445: */
446: public int getServerPort() {
447: return -1;
448: }
449:
450: /*
451: * Req
452: */
453: public boolean isSecure() {
454: throw new UnsupportedOperationException(
455: "isSecure() not supported");
456: }
457:
458: /*
459: * Req
460: */
461: public String getContentType() {
462: return null;
463: }
464:
465: private void addHeaderLine(String s) {
466:
467: if (_firstLine) {
468: // System.out.println("First Line :"+s);
469: if (s.equals(_crlf)) {
470: return;
471: }
472:
473: int strLength = s.length();
474: // if (strLength > 0 && s.charAt(strLength -1) == lf){
475: if (strLength > 0) {
476: if (!s.endsWith(_crlf)) {
477: s = s.substring(0, strLength - 1) + _crlf;
478: }
479: if (s.startsWith(_crlf)) {
480: s = s.substring(2);
481: }
482:
483: else if (s.charAt(0) == lf) {
484:
485: s = s.substring(1);
486: }
487: if (s.equals(_crlf)) {
488: return;
489: }
490: _firstLine = false;
491: StringTokenizer st = new StringTokenizer(s);
492: _method = st.nextToken();
493: _path = st.nextToken();
494:
495: try {
496: _httpVersion = st.nextToken();
497: } catch (NoSuchElementException ex) {
498: _httpVersion = "HTTP/0.9";
499: _headerComplete = true;
500: }
501: }
502: } else {
503:
504: int strLength = s.length();
505: if (!s.endsWith(_crlf) && strLength > 0) {
506: s = s.substring(0, strLength - 1) + _crlf;
507: int colIndex = s.indexOf(":");
508: if (colIndex != -1) {
509: String name = s.substring(0, colIndex);
510: String value = s
511: .substring(colIndex + 1, s.length());
512: headerMap.put(name.trim(), value.trim());
513: } else {
514: // System.out.println (" Name No value ::: "+ s);
515: headerMap.put(s.trim(), null);
516: }
517:
518: }
519:
520: if (s.equals(_crlf)) {
521: _headerComplete = true;
522: } else {
523: _headerLines.add(s);
524: }
525: }
526: }
527:
528: public String toString() {
529: StringBuffer sb = new StringBuffer();
530: Iterator iter = _headerLines.iterator();
531:
532: sb.append(_method).append(" ").append(_path).append(" ")
533: .append(_httpVersion).append("\r\n");
534: while (iter.hasNext()) {
535: sb.append(iter.next().toString());
536: }
537: return sb.toString();
538: }
539:
540: private void parseHeaders(String headers) {
541: StringTokenizer stz = new StringTokenizer(headers, "\n");
542: while (stz.hasMoreElements()) {
543: addHeaderLine(stz.nextToken());
544: }
545: }
546: }
|