File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\util\QueryParameterMap.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2011, CloudBees, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package hudson.util;
25
26 import javax.servlet.http.HttpServletRequest;
27 import java.io.UnsupportedEncodingException;
28 import java.net.URLDecoder;
29 import java.util.ArrayList;
30 import java.util.Collections;
31 import java.util.HashMap;
32 import java.util.List;
33 import java.util.Map;
34
35 /**
36 * Parses the query string of the URL into a key/value pair.
37 *
38 * <p>
39 * This class is even useful on the server side, as {@link HttpServletRequest#getParameter(String)}
40 * can try to parse into the payload (and that can cause an exception if the payload is already consumed.
41 * See HUDSON-8056.)
42 *
43 * <p>
44 * So if you are handling the payload yourself and only want to access the query parameters,
45 * use this class.
46 *
47 * @author Kohsuke Kawaguchi
48 * @since 1.394
49 */
50 public class QueryParameterMap {
51 private final Map<String,List<String>> store = new HashMap<String, List<String>>();
52
53 /**
54 * @param queryString
55 * String that looks like "abc=def&ghi=jkl"
56 */
57 public QueryParameterMap(String queryString) {
58 if (queryString==null || queryString.length()==0) return;
59 try {
60 for (String param : queryString.split("&")) {
61 String[] kv = param.split("=");
62 String key = URLDecoder.decode(kv[0], "UTF-8");
63 String value = URLDecoder.decode(kv[1], "UTF-8");
64 List<String> values = store.get(key);
65 if (values == null)
66 store.put(key, values = new ArrayList<String>());
67 values.add(value);
68 }
69 } catch (UnsupportedEncodingException e) {
70 throw new AssertionError(e);
71 }
72 }
73
74 public QueryParameterMap(HttpServletRequest req) {
75 this(req.getQueryString());
76 }
77
78 public String get(String name) {
79 List<String> v = store.get(name);
80 return v!=null?v.get(0):null;
81 }
82
83 public List<String> getAll(String name) {
84 List<String> v = store.get(name);
85 return v!=null? Collections.unmodifiableList(v) : Collections.<String>emptyList();
86 }
87 }
88