001    /******************************************************************************
002     * Copyright (C) MActor Developers. 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     ******************************************************************************/
020    package org.mactor.brokers.http;
021    
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    
029    import org.apache.log4j.Logger;
030    
031    /**
032     * Represents a HTTP Request
033     * 
034     * @author Lars Ivar Almli
035     */
036    public class HttpRequest {
037            protected static Logger log = Logger.getLogger(HttpRequest.class);
038            String method;
039            String requestedUrl;
040            String data;
041            Map<String, String> headers = new HashMap<String, String>();
042            public HttpRequest(InputStream is) throws IOException {
043                    PushbackInputStream pis = new PushbackInputStream(is);
044                    String first = readLine(pis);
045                    String[] parts = first.split(" ");
046                    this.method = parts[0];
047                    this.requestedUrl = parts[1];
048                    while (true) {
049                            String header = readLine(pis);
050                            if (header == null || header.trim().length() == 0)
051                                    break;
052                            int sep = header.indexOf(':');
053                            if (sep >= 0 && sep + 1 < header.length()) {
054                                    this.headers.put(header.substring(0, sep).toLowerCase().trim(), header.substring(sep + 1, header.length()).toLowerCase().trim());
055                            }
056                    }
057                    if (log.isDebugEnabled())
058                            log.debug("Received request on url: '" + requestedUrl + "'. Headers:" + this.headers);
059                    if ("POST".equalsIgnoreCase(method)) {
060                            String str = headers.get("content-length");
061                            int len = 0;
062                            if (str == null)
063                                    throw new IOException("Invalid request. Content-Length not specified in POST");
064                            try {
065                                    len = Integer.parseInt(str.trim());
066                            } catch (NumberFormatException nof) {
067                                    throw new IOException("Invalid request. Unparseable Content-Length header in POST");
068                            }
069                            if (log.isDebugEnabled()) {
070                                    log.debug("Expecting content of length:" + len);
071                            }
072                            data = readContent(pis, len);
073                    }
074                    if (log.isDebugEnabled())
075                            log.debug("Content:" + data);
076            }
077            private String readLine(PushbackInputStream pis) throws IOException {
078                    ByteArrayOutputStream bao = new ByteArrayOutputStream();
079                    byte[] buffer = new byte[1];
080                    while (pis.read(buffer) >= 0) {
081                            if (isNewLine(buffer[0])) {
082                                    if (pis.read(buffer) >= 0) {
083                                            if (!isNewLine(buffer[0])) {
084                                                    pis.unread(buffer);
085                                            }
086                                    }
087                                    break;
088                            }
089                            bao.write(buffer);
090                    }
091                    return bao.toString();
092            }
093            private String readContent(InputStream is, int len) throws IOException {
094                    ByteArrayOutputStream bao = new ByteArrayOutputStream();
095                    int tot = 0;
096                    byte[] buffer = new byte[len];
097                    while (true) {
098                            int read = is.read(buffer);
099                            if (read <= 0)
100                                    break;
101                            bao.write(buffer, 0, read);
102                            tot = tot + read;
103                            if (tot >= len)
104                                    break;
105                    }
106                    return bao.toString();
107            }
108            boolean isNewLine(byte c) {
109                    return c == '\n' || c == '\r';
110            }
111            public String getData() {
112                    return data;
113            }
114            public String getHeader(String headerName) {
115                    return headers.get(headerName);
116            }
117            public Map<String, String> getHeaders() {
118                    return headers;
119            }
120            public String getMethod() {
121                    return method;
122            }
123            public String getRequestedUrl() {
124                    return requestedUrl;
125            }
126    }