001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *  
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *  
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License. 
018 *  
019 */
020package org.apache.mina.http;
021
022import java.util.ArrayList;
023import java.util.HashMap;
024import java.util.List;
025import java.util.Map;
026import java.util.regex.Matcher;
027import java.util.regex.Pattern;
028
029import org.apache.mina.http.api.HttpMethod;
030import org.apache.mina.http.api.HttpRequest;
031import org.apache.mina.http.api.HttpVersion;
032
033public class HttpRequestImpl implements HttpRequest {
034        
035    private final HttpVersion version;
036
037    private final HttpMethod method;
038
039    private final String requestedPath;
040    
041    private final String queryString;
042
043    private final Map<String, String> headers;
044
045    public HttpRequestImpl(HttpVersion version, HttpMethod method, String requestedPath, String queryString, Map<String, String> headers) {
046        this.version = version;
047        this.method = method;
048        this.requestedPath = requestedPath;
049        this.queryString = queryString;
050        this.headers = headers;
051    }
052
053    public HttpVersion getProtocolVersion() {
054        return version;
055    }
056
057    public String getContentType() {
058        return headers.get("content-type");
059    }
060
061    public boolean isKeepAlive() {
062        return false;
063    }
064
065    public String getHeader(String name) {
066        return headers.get(name);
067    }
068
069    public boolean containsHeader(String name) {
070        return headers.containsKey(name);
071    }
072
073    public Map<String, String> getHeaders() {
074        return headers;
075    }
076
077    public boolean containsParameter(String name) {
078        Matcher m = parameterPattern(name);
079        return m.find();
080    }
081
082    public String getParameter(String name) {
083        Matcher m = parameterPattern(name);
084        if (m.find()) {
085                return m.group(1);
086        } else {
087                return null;
088        }
089    }
090    
091    protected Matcher parameterPattern(String name) {
092        return Pattern.compile("[&]"+name+"=([^&]*)").matcher("&"+queryString);
093    }
094
095    public Map<String, List<String>> getParameters() {
096        Map<String, List<String>> parameters = new HashMap<String, List<String>>();
097        String[] params = queryString.split("&");
098        if (params.length == 1) {
099                return parameters;
100        }
101        for (int i = 0; i < params.length; i++) {
102                        String[] param = params[i].split("=");
103                        String name = param[0];
104                        String value = param.length == 2 ? param[1] : "";
105                        if (!parameters.containsKey(name)) {
106                                parameters.put(name, new ArrayList<String>());
107                        }
108                        parameters.get(name).add(value);
109                }
110        return parameters;
111    }
112    
113    public String getQueryString() {
114        return queryString;
115    }
116
117    public HttpMethod getMethod() {
118        return method;
119    }
120    
121    public String getRequestPath() {
122        return requestedPath;
123    }
124
125    public String toString() {
126        StringBuilder sb = new StringBuilder();
127        sb.append("HTTP REQUEST METHOD: ").append(method).append('\n');
128        sb.append("VERSION: ").append(version).append('\n');
129        sb.append("PATH: ").append(requestedPath).append('\n');
130        sb.append("QUERY:").append(queryString).append('\n');
131
132        sb.append("--- HEADER --- \n");
133        
134        for (Map.Entry<String, String> entry : headers.entrySet()) {
135            sb.append(entry.getKey()).append(':').append(entry.getValue()).append('\n');
136        }
137
138        sb.append("--- PARAMETERS --- \n");
139        Map<String, List<String>> parameters = getParameters();
140
141        for (Map.Entry<String, List<String>> entry : parameters.entrySet()) {
142            String key = entry.getKey();
143            
144            for (String value : entry.getValue()) { 
145                sb.append(key).append(':').append(value).append('\n'); 
146            }
147        }
148        
149        return sb.toString();
150    }
151}