View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.mina.http;
21  
22  import java.util.ArrayList;
23  import java.util.Collection;
24  import java.util.HashMap;
25  import java.util.List;
26  import java.util.Map;
27  import java.util.regex.Matcher;
28  import java.util.regex.Pattern;
29  
30  import org.apache.mina.http.api.HttpMethod;
31  import org.apache.mina.http.api.HttpRequest;
32  import org.apache.mina.http.api.HttpVersion;
33  
34  public class HttpRequestImpl implements HttpRequest {
35  	
36      private final HttpVersion version;
37  
38      private final HttpMethod method;
39  
40      private final String requestedPath;
41      
42      private final String queryString;
43  
44      private final Map<String, String> headers;
45  
46      public HttpRequestImpl(HttpVersion version, HttpMethod method, String requestedPath, String queryString, Map<String, String> headers) {
47          this.version = version;
48          this.method = method;
49          this.requestedPath = requestedPath;
50          this.queryString = queryString;
51          this.headers = headers;
52      }
53  
54      public HttpVersion getProtocolVersion() {
55          return version;
56      }
57  
58      public String getContentType() {
59          return headers.get("content-type");
60      }
61  
62      public boolean isKeepAlive() {
63          return false;
64      }
65  
66      public String getHeader(String name) {
67          return headers.get(name);
68      }
69  
70      public boolean containsHeader(String name) {
71          return headers.containsKey(name);
72      }
73  
74      public Map<String, String> getHeaders() {
75          return headers;
76      }
77  
78      public boolean containsParameter(String name) {
79      	Matcher m = parameterPattern(name);
80      	return m.find();
81      }
82  
83      public String getParameter(String name) {
84      	Matcher m = parameterPattern(name);
85      	if (m.find()) {
86      		return m.group(1);
87      	} else {
88      		return null;
89      	}
90      }
91      
92      protected Matcher parameterPattern(String name) {
93      	return Pattern.compile("[&]"+name+"=([^&]*)").matcher("&"+queryString);
94      }
95  
96      public Map<String, List<String>> getParameters() {
97      	Map<String, List<String>> parameters = new HashMap<String, List<String>>();
98          String[] params = queryString.split("&");
99          if (params.length == 1) {
100         	return parameters;
101         }
102         for (int i = 0; i < params.length; i++) {
103 			String[] param = params[i].split("=");
104 			String name = param[0];
105 			String value = param.length == 2 ? param[1] : "";
106 			if (!parameters.containsKey(name)) {
107 				parameters.put(name, new ArrayList<String>());
108 			}
109 			parameters.get(name).add(value);
110 		}
111         return parameters;
112     }
113     
114     public String getQueryString() {
115     	return queryString;
116     }
117 
118     public HttpMethod getMethod() {
119         return method;
120     }
121     
122     public String getRequestPath() {
123     	return requestedPath;
124     }
125 
126     public String toString() {
127         String result = "HTTP REQUEST METHOD: " + method + "\n";
128         result += "VERSION: " + version + "\n";
129         result += "PATH: " + requestedPath + "\n";
130         result += "QUERY:" + queryString + "\n";
131 
132         result += "--- HEADER --- \n";
133         for (String key : headers.keySet()) {
134             String value = headers.get(key);
135             result += key + ":" + value + "\n";
136         }
137 
138         result += "--- PARAMETERS --- \n";
139         Map<String, List<String>> parameters = getParameters();
140         for (String key : parameters.keySet()) {
141         	Collection<String> values = parameters.get(key);
142         	for (String value : values) { result += key + ":" + value + "\n"; }
143         }
144         
145         return result;
146     }
147 }