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.HashMap;
24  import java.util.List;
25  import java.util.Map;
26  import java.util.regex.Matcher;
27  import java.util.regex.Pattern;
28  
29  import org.apache.mina.http.api.HttpMethod;
30  import org.apache.mina.http.api.HttpRequest;
31  import org.apache.mina.http.api.HttpVersion;
32  
33  public class HttpRequestImpl implements HttpRequest {
34  	
35      private final HttpVersion version;
36  
37      private final HttpMethod method;
38  
39      private final String requestedPath;
40      
41      private final String queryString;
42  
43      private final Map<String, String> headers;
44  
45      public HttpRequestImpl(HttpVersion version, HttpMethod method, String requestedPath, String queryString, Map<String, String> headers) {
46          this.version = version;
47          this.method = method;
48          this.requestedPath = requestedPath;
49          this.queryString = queryString;
50          this.headers = headers;
51      }
52  
53      public HttpVersion getProtocolVersion() {
54          return version;
55      }
56  
57      public String getContentType() {
58          return headers.get("content-type");
59      }
60  
61      public boolean isKeepAlive() {
62          return false;
63      }
64  
65      public String getHeader(String name) {
66          return headers.get(name);
67      }
68  
69      public boolean containsHeader(String name) {
70          return headers.containsKey(name);
71      }
72  
73      public Map<String, String> getHeaders() {
74          return headers;
75      }
76  
77      public boolean containsParameter(String name) {
78      	Matcher m = parameterPattern(name);
79      	return m.find();
80      }
81  
82      public String getParameter(String name) {
83      	Matcher m = parameterPattern(name);
84      	if (m.find()) {
85      		return m.group(1);
86      	} else {
87      		return null;
88      	}
89      }
90      
91      protected Matcher parameterPattern(String name) {
92      	return Pattern.compile("[&]"+name+"=([^&]*)").matcher("&"+queryString);
93      }
94  
95      public Map<String, List<String>> getParameters() {
96      	Map<String, List<String>> parameters = new HashMap<String, List<String>>();
97          String[] params = queryString.split("&");
98          if (params.length == 1) {
99          	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 }