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;//Collections.unmodifiableMap(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          // TODO Auto-generated method stub
64          return false;
65      }
66  
67      public String getHeader(String name) {
68          return headers.get(name);
69      }
70  
71      public boolean containsHeader(String name) {
72          return headers.containsKey(name);
73      }
74  
75      public Map<String, String> getHeaders() {
76          return headers;
77      }
78  
79      public boolean containsParameter(String name) {
80      	Matcher m = parameterPattern(name);
81      	return m.find();
82      }
83  
84      public String getParameter(String name) {
85      	Matcher m = parameterPattern(name);
86      	if (m.find()) {
87      		return m.group(1);
88      	} else {
89      		return null;
90      	}
91      }
92      
93      protected Matcher parameterPattern(String name) {
94      	return Pattern.compile("[&]"+name+"=([^&]*)").matcher("&"+queryString);
95      }
96  
97      public Map<String, List<String>> getParameters() {
98      	Map<String, List<String>> parameters = new HashMap<String, List<String>>();
99          String[] params = queryString.split("&");
100         if (params.length == 1) {
101         	return parameters;
102         }
103         for (int i = 0; i < params.length; i++) {
104 			String[] param = params[i].split("=");
105 			String name = param[0];
106 			String value = param.length == 2 ? param[1] : "";
107 			if (!parameters.containsKey(name)) {
108 				parameters.put(name, new ArrayList<String>());
109 			}
110 			parameters.get(name).add(value);
111 		}
112         return parameters;
113     }
114     
115     public String getQueryString() {
116     	return queryString;
117     }
118 
119     public HttpMethod getMethod() {
120         return method;
121     }
122     
123     public String getRequestPath() {
124     	return requestedPath;
125     }
126 
127     public String toString() {
128         String result = "HTTP REQUEST METHOD: " + method + "\n";
129         result += "VERSION: " + version + "\n";
130         result += "PATH: " + requestedPath + "\n";
131         result += "QUERY:" + queryString + "\n";
132 
133         result += "--- HEADER --- \n";
134         for (String key : headers.keySet()) {
135             String value = headers.get(key);
136             result += key + ":" + value + "\n";
137         }
138 
139         result += "--- PARAMETERS --- \n";
140         Map<String, List<String>> parameters = getParameters();
141         for (String key : parameters.keySet()) {
142         	Collection<String> values = parameters.get(key);
143         	for (String value : values) { result += key + ":" + value + "\n"; }
144         }
145         
146         return result;
147     }
148 }