1 | /** |
2 | * Licensed to jclouds, Inc. (jclouds) under one or more |
3 | * contributor license agreements. See the NOTICE file |
4 | * distributed with this work for additional information |
5 | * regarding copyright ownership. jclouds 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 | package org.jclouds.http.options; |
20 | |
21 | import java.util.Collection; |
22 | |
23 | import com.google.common.collect.LinkedHashMultimap; |
24 | import com.google.common.collect.Multimap; |
25 | |
26 | /** |
27 | * |
28 | * @see HttpRequestOptions |
29 | * @author Adrian Cole |
30 | * |
31 | */ |
32 | public class BaseHttpRequestOptions implements HttpRequestOptions { |
33 | |
34 | protected final Multimap<String, String> matrixParameters = LinkedHashMultimap.create(); |
35 | protected final Multimap<String, String> formParameters = LinkedHashMultimap.create(); |
36 | protected final Multimap<String, String> queryParameters = LinkedHashMultimap.create(); |
37 | protected final Multimap<String, String> headers = LinkedHashMultimap.create(); |
38 | protected String payload; |
39 | protected String pathSuffix; |
40 | |
41 | public String buildStringPayload() { |
42 | return payload; |
43 | } |
44 | |
45 | protected String getFirstMatrixOrNull(String string) { |
46 | Collection<String> values = matrixParameters.get(string); |
47 | return (values != null && values.size() >= 1) ? values.iterator().next() : null; |
48 | } |
49 | |
50 | protected String getFirstQueryOrNull(String string) { |
51 | Collection<String> values = queryParameters.get(string); |
52 | return (values != null && values.size() >= 1) ? values.iterator().next() : null; |
53 | } |
54 | |
55 | protected String getFirstFormOrNull(String string) { |
56 | Collection<String> values = formParameters.get(string); |
57 | return (values != null && values.size() >= 1) ? values.iterator().next() : null; |
58 | } |
59 | |
60 | protected String getFirstHeaderOrNull(String string) { |
61 | Collection<String> values = headers.get(string); |
62 | return (values != null && values.size() >= 1) ? values.iterator().next() : null; |
63 | } |
64 | |
65 | protected void replaceHeader(String key, String value) { |
66 | headers.removeAll(key); |
67 | headers.put(key, value); |
68 | } |
69 | |
70 | /** |
71 | * {@inheritDoc} |
72 | */ |
73 | public Multimap<String, String> buildRequestHeaders() { |
74 | return headers; |
75 | } |
76 | |
77 | /** |
78 | * {@inheritDoc} |
79 | */ |
80 | public Multimap<String, String> buildQueryParameters() { |
81 | return queryParameters; |
82 | } |
83 | |
84 | /** |
85 | * {@inheritDoc} |
86 | */ |
87 | public Multimap<String, String> buildMatrixParameters() { |
88 | return matrixParameters; |
89 | } |
90 | |
91 | public String buildPathSuffix() { |
92 | return pathSuffix; |
93 | } |
94 | |
95 | public Multimap<String, String> buildFormParameters() { |
96 | return formParameters; |
97 | } |
98 | |
99 | @Override |
100 | public int hashCode() { |
101 | final int prime = 31; |
102 | int result = 1; |
103 | result = prime * result + ((formParameters == null) ? 0 : formParameters.hashCode()); |
104 | result = prime * result + ((headers == null) ? 0 : headers.hashCode()); |
105 | result = prime * result + ((matrixParameters == null) ? 0 : matrixParameters.hashCode()); |
106 | result = prime * result + ((pathSuffix == null) ? 0 : pathSuffix.hashCode()); |
107 | result = prime * result + ((payload == null) ? 0 : payload.hashCode()); |
108 | result = prime * result + ((queryParameters == null) ? 0 : queryParameters.hashCode()); |
109 | return result; |
110 | } |
111 | |
112 | @Override |
113 | public boolean equals(Object obj) { |
114 | if (this == obj) |
115 | return true; |
116 | if (obj == null) |
117 | return false; |
118 | if (getClass() != obj.getClass()) |
119 | return false; |
120 | BaseHttpRequestOptions other = (BaseHttpRequestOptions) obj; |
121 | if (formParameters == null) { |
122 | if (other.formParameters != null) |
123 | return false; |
124 | } else if (!formParameters.equals(other.formParameters)) |
125 | return false; |
126 | if (headers == null) { |
127 | if (other.headers != null) |
128 | return false; |
129 | } else if (!headers.equals(other.headers)) |
130 | return false; |
131 | if (matrixParameters == null) { |
132 | if (other.matrixParameters != null) |
133 | return false; |
134 | } else if (!matrixParameters.equals(other.matrixParameters)) |
135 | return false; |
136 | if (pathSuffix == null) { |
137 | if (other.pathSuffix != null) |
138 | return false; |
139 | } else if (!pathSuffix.equals(other.pathSuffix)) |
140 | return false; |
141 | if (payload == null) { |
142 | if (other.payload != null) |
143 | return false; |
144 | } else if (!payload.equals(other.payload)) |
145 | return false; |
146 | if (queryParameters == null) { |
147 | if (other.queryParameters != null) |
148 | return false; |
149 | } else if (!queryParameters.equals(other.queryParameters)) |
150 | return false; |
151 | return true; |
152 | } |
153 | |
154 | @Override |
155 | public String toString() { |
156 | return "[formParameters=" + formParameters + ", headers=" + headers + ", matrixParameters=" |
157 | + matrixParameters + ", pathSuffix=" + pathSuffix + ", payload=" + payload |
158 | + ", queryParameters=" + queryParameters + "]"; |
159 | } |
160 | |
161 | } |