• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.xtremelabs.robolectric.tester.org.apache.http;
2 
3 import org.apache.http.HttpRequest;
4 import org.apache.http.client.methods.HttpPost;
5 import org.apache.http.params.BasicHttpParams;
6 import org.apache.http.params.HttpParams;
7 import org.apache.http.util.EntityUtils;
8 
9 import java.io.IOException;
10 import java.io.UnsupportedEncodingException;
11 import java.lang.reflect.Field;
12 import java.net.URI;
13 import java.net.URLDecoder;
14 import java.util.LinkedHashMap;
15 import java.util.Map;
16 import java.util.StringTokenizer;
17 
18 public class ParamsParser {
parseParams(HttpRequest request)19     public static Map<String, String> parseParams(HttpRequest request) {
20         URI uri = URI.create(request.getRequestLine().getUri());
21         String rawQuery = uri.getRawQuery();
22         Map<String, String> params;
23         if (rawQuery != null) {
24             params = parseParamsFromQuery(rawQuery);
25         } else if (request instanceof HttpPost && ((HttpPost) request).getEntity() != null) {
26             try {
27                 params = parseParamsFromQuery(EntityUtils.toString(((HttpPost) request).getEntity()));
28             } catch (IOException e) {
29                 throw new RuntimeException(e);
30             }
31         } else {
32             HttpParams httpParams = request.getParams();
33             if (httpParams instanceof BasicHttpParams) {
34                 Map<String, String> parameters = getPrivateMember(httpParams, "parameters");
35                 params = new LinkedHashMap<String, String>(parameters);
36             }
37             else {
38                 throw new RuntimeException("Was expecting a "+BasicHttpParams.class.getName());
39             }
40             return params;
41         }
42         return params;
43     }
44 
parseParamsFromQuery(String rawQuery)45     private static Map<String, String> parseParamsFromQuery(String rawQuery) {
46         Map<String, String> params = new LinkedHashMap<String, String>();
47         StringTokenizer tok = new StringTokenizer(rawQuery, "&", false);
48         while (tok.hasMoreTokens()) {
49             String name, value;
50             String nextParam = tok.nextToken();
51             if (nextParam.contains("=")) {
52                 String[] nameAndValue = nextParam.split("=");
53                 name = nameAndValue[0];
54                 try {
55                     value = URLDecoder.decode(nameAndValue[1], "UTF-8");
56                 } catch (UnsupportedEncodingException e) {
57                     throw new RuntimeException(e);
58                 }
59             } else {
60                 name = nextParam;
61                 value = "";
62             }
63             params.put(name, value);
64         }
65         return params;
66     }
67 
getPrivateMember(Object obj, String name)68     private static <T> T getPrivateMember(Object obj, String name) {
69         try {
70             Field f = obj.getClass().getDeclaredField(name);
71             f.setAccessible(true);
72             //noinspection unchecked
73             return (T) f.get(obj);
74         } catch (NoSuchFieldException e) {
75             throw new RuntimeException(e);
76         } catch (IllegalAccessException e) {
77             throw new RuntimeException(e);
78         }
79     }
80 }
81