001: /******************************************************************************
002: * Copyright (C) Lars Ivar Almli. All rights reserved. *
003: * ---------------------------------------------------------------------------*
004: * This file is part of MActor. *
005: * *
006: * MActor is free software; you can redistribute it and/or modify *
007: * it under the terms of the GNU General Public License as published by *
008: * the Free Software Foundation; either version 2 of the License, or *
009: * (at your option) any later version. *
010: * *
011: * MActor is distributed in the hope that it will be useful, *
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
014: * GNU General Public License for more details. *
015: * *
016: * You should have received a copy of the GNU General Public License *
017: * along with MActor; if not, write to the Free Software *
018: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
019: ******************************************************************************/package org.mactor.brokers.http;
020:
021: import java.io.BufferedReader;
022: import java.io.ByteArrayOutputStream;
023: import java.io.IOException;
024: import java.io.InputStream;
025: import java.io.PushbackInputStream;
026: import java.util.HashMap;
027: import java.util.Map;
028: import org.apache.log4j.Logger;
029:
030: /**
031: * Represents a HTTP Request
032: *
033: * @author Lars Ivar Almli
034: */
035: public class HttpRequest {
036: protected static Logger log = Logger.getLogger(HttpRequest.class);
037:
038: String method;
039: String requestedUrl;
040: String data;
041: Map<String, String> headers = new HashMap<String, String>();
042:
043: public HttpRequest(InputStream is) throws IOException {
044: PushbackInputStream pis = new PushbackInputStream(is);
045: String first = readLine(pis);
046: String[] parts = first.split(" ");
047: this .method = parts[0];
048: this .requestedUrl = parts[1];
049:
050: while (true) {
051: String header = readLine(pis);
052: if (header == null || header.trim().length() == 0)
053: break;
054: int sep = header.indexOf(':');
055: if (sep >= 0 && sep + 1 < header.length()) {
056: this .headers.put(header.substring(0, sep).toLowerCase()
057: .trim(), header.substring(sep + 1,
058: header.length()).toLowerCase().trim());
059: }
060: }
061: if (log.isDebugEnabled())
062: log.debug("Received request on url: '" + requestedUrl
063: + "'. Headers:" + this .headers);
064:
065: if ("POST".equalsIgnoreCase(method)) {
066:
067: String str = headers.get("content-length");
068: int len = 0;
069: if (str == null)
070: throw new IOException(
071: "Invalid request. Content-Length not specified in POST");
072: try {
073: len = Integer.parseInt(str.trim());
074: } catch (NumberFormatException nof) {
075: throw new IOException(
076: "Invalid request. Unparseable Content-Length header in POST");
077: }
078: if (log.isDebugEnabled()) {
079: log.debug("Expecting content of length:" + len);
080: }
081: data = readContent(pis, len);
082: }
083:
084: if (log.isDebugEnabled())
085: log.debug("Content:" + data);
086: }
087:
088: private String readLine(PushbackInputStream pis) throws IOException {
089: ByteArrayOutputStream bao = new ByteArrayOutputStream();
090: byte[] buffer = new byte[1];
091: while (pis.read(buffer) != 0) {
092: if (isNewLine(buffer[0])) {
093: if (pis.read(buffer) != 0) {
094: if (!isNewLine(buffer[0])) {
095: pis.unread(buffer);
096: }
097: }
098: break;
099: }
100: bao.write(buffer);
101: }
102: return bao.toString();
103: }
104:
105: private String readContent(InputStream is, int len)
106: throws IOException {
107: ByteArrayOutputStream bao = new ByteArrayOutputStream();
108: int tot = 0;
109: byte[] buffer = new byte[len];
110: while (true) {
111: int read = is.read(buffer);
112: if (read <= 0)
113: break;
114: bao.write(buffer, 0, read);
115: tot = tot + read;
116: if (tot >= len)
117: break;
118: }
119: return bao.toString();
120: }
121:
122: boolean isNewLine(byte c) {
123: return c == '\n' || c == '\r';
124: }
125:
126: public String getData() {
127: return data;
128: }
129:
130: public String getHeader(String headerName) {
131: return headers.get(headerName);
132: }
133:
134: public Map<String, String> getHeaders() {
135: return headers;
136: }
137:
138: public String getMethod() {
139: return method;
140: }
141:
142: public String getRequestedUrl() {
143: return requestedUrl;
144: }
145: }
|