View Javadoc
1   package com.atlassian.refapp.sal.search.parameter;
2   
3   import com.atlassian.sal.api.search.parameter.SearchParameter;
4   import org.apache.commons.lang.StringUtils;
5   
6   import java.io.UnsupportedEncodingException;
7   import java.net.URLDecoder;
8   import java.net.URLEncoder;
9   
10  /**
11   * Basic name value pair search parameter.
12   *
13   * Copied from sal-core so we can remove the dep on Apache HTTP Client
14   */
15  public class BasicSearchParameter implements SearchParameter {
16      private String name;
17      private String value;
18  
19      public BasicSearchParameter(String queryString) {
20          initFromQueryString(queryString);
21      }
22  
23      public BasicSearchParameter(String name, String value) {
24          this.name = name;
25          this.value = value;
26      }
27  
28      public String getName() {
29          return name;
30      }
31  
32      public String getValue() {
33          return value;
34      }
35  
36      public String buildQueryString() {
37          try {
38              return URLEncoder.encode(name, "UTF-8") + "=" + URLEncoder.encode(value, "UTF-8");
39          } catch (UnsupportedEncodingException e) {
40              throw new RuntimeException("You're JVM doesn't support UTF-8", e);
41          }
42      }
43  
44      private void initFromQueryString(String queryString) {
45          if (StringUtils.isEmpty(queryString) || queryString.indexOf("=") == -1) {
46              throw new IllegalArgumentException("QueryString '" + queryString + "' does not appear to be a valid query string");
47          }
48  
49          final String[] strings;
50          try {
51              strings = URLDecoder.decode(queryString, "UTF-8").split("=");
52          } catch (UnsupportedEncodingException e) {
53              throw new RuntimeException("You're JVM doesn't support UTF-8", e);
54          }
55          this.name = strings[0];
56          this.value = strings[1];
57      }
58  
59      public boolean equals(Object o) {
60          if (this == o) {
61              return true;
62          }
63          if (o == null || getClass() != o.getClass()) {
64              return false;
65          }
66  
67          BasicSearchParameter that = (BasicSearchParameter) o;
68  
69          if (!name.equals(that.name)) {
70              return false;
71          }
72          if (!value.equals(that.value)) {
73              return false;
74          }
75  
76          return true;
77      }
78  
79      public int hashCode() {
80          int result;
81          result = name.hashCode();
82          result = 31 * result + value.hashCode();
83          return result;
84      }
85  }