1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
package com.jcabi.http.mock; |
31 | |
|
32 | |
import com.jcabi.log.Logger; |
33 | |
import com.sun.grizzly.tcp.http11.GrizzlyAdapter; |
34 | |
import com.sun.grizzly.tcp.http11.GrizzlyRequest; |
35 | |
import com.sun.grizzly.tcp.http11.GrizzlyResponse; |
36 | |
import java.io.OutputStreamWriter; |
37 | |
import java.io.PrintWriter; |
38 | |
import java.io.UnsupportedEncodingException; |
39 | |
import java.net.HttpURLConnection; |
40 | |
import java.util.Collection; |
41 | |
import java.util.Iterator; |
42 | |
import java.util.LinkedList; |
43 | |
import java.util.NoSuchElementException; |
44 | |
import java.util.Queue; |
45 | |
import java.util.concurrent.ConcurrentLinkedQueue; |
46 | |
import java.util.concurrent.atomic.AtomicInteger; |
47 | |
import lombok.EqualsAndHashCode; |
48 | |
import org.apache.http.HttpHeaders; |
49 | |
import org.hamcrest.Matcher; |
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
@SuppressWarnings("PMD.TooManyMethods") |
59 | 69 | final class MkGrizzlyAdapter extends GrizzlyAdapter { |
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
private static final String ENCODING = "UTF-8"; |
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | 69 | private final transient Queue<QueryWithAnswer> queue = |
70 | |
new ConcurrentLinkedQueue<QueryWithAnswer>(); |
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | 69 | private final transient Queue<Conditional> conditionals = |
76 | |
new ConcurrentLinkedQueue<Conditional>(); |
77 | |
|
78 | |
|
79 | |
@Override |
80 | |
@SuppressWarnings |
81 | |
( |
82 | |
{ |
83 | |
"PMD.AvoidCatchingThrowable", |
84 | |
"PMD.AvoidInstantiatingObjectsInLoops", |
85 | |
"rawtypes" |
86 | |
} |
87 | |
) |
88 | |
public void service(final GrizzlyRequest request, |
89 | |
final GrizzlyResponse response) { |
90 | |
try { |
91 | 107 | final MkQuery query = new GrizzlyQuery(request); |
92 | 109 | final Iterator<Conditional> iter = this.conditionals.iterator(); |
93 | 109 | boolean matched = false; |
94 | 112 | while (iter.hasNext()) { |
95 | 111 | final Conditional cond = iter.next(); |
96 | 111 | if (cond.matches(query)) { |
97 | 110 | matched = true; |
98 | 110 | final MkAnswer answer = cond.answer(); |
99 | 110 | this.queue.add(new QueryWithAnswer(query, answer)); |
100 | 110 | for (final String name : answer.headers().keySet()) { |
101 | |
|
102 | 25 | for (final String value : answer.headers().get(name)) { |
103 | 29 | response.addHeader(name, value); |
104 | 29 | } |
105 | 25 | } |
106 | 108 | response.addHeader( |
107 | |
HttpHeaders.SERVER, |
108 | |
String.format( |
109 | |
"%s query #%d, %d answer(s) left", |
110 | |
this.getClass().getName(), |
111 | |
this.queue.size(), this.conditionals.size() |
112 | |
) |
113 | |
); |
114 | 108 | response.setStatus(answer.status()); |
115 | 107 | final byte[] body = answer.bodyBytes(); |
116 | 106 | response.getStream().write(body); |
117 | 109 | response.setContentLength(body.length); |
118 | 107 | if (cond.decrement() == 0) { |
119 | 89 | iter.remove(); |
120 | |
} |
121 | |
break; |
122 | |
} |
123 | 2 | } |
124 | 108 | if (!matched) { |
125 | 1 | throw new NoSuchElementException("No matching answers found."); |
126 | |
} |
127 | |
|
128 | 1 | } catch (final Throwable ex) { |
129 | 1 | MkGrizzlyAdapter.fail(response, ex); |
130 | 107 | } |
131 | 108 | } |
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
public void next(final MkAnswer answer, final Matcher<MkQuery> query, |
142 | |
final int count) { |
143 | 98 | this.conditionals.add(new Conditional(answer, query, count)); |
144 | 99 | } |
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
public MkQuery take() { |
151 | 30 | return this.queue.remove().que; |
152 | |
} |
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
public MkQuery take(final Matcher<MkAnswer> matcher) { |
162 | 1 | MkQuery result = null; |
163 | 1 | final Iterator<QueryWithAnswer> iter = this.queue.iterator(); |
164 | 1 | while (iter.hasNext()) { |
165 | 1 | final QueryWithAnswer candidate = iter.next(); |
166 | 1 | if (matcher.matches(candidate.answer())) { |
167 | 1 | result = candidate.query(); |
168 | 1 | iter.remove(); |
169 | 1 | break; |
170 | |
} |
171 | 0 | } |
172 | 1 | if (result == null) { |
173 | |
|
174 | 0 | throw new NoSuchElementException("No matching results found"); |
175 | |
} |
176 | 1 | return result; |
177 | |
} |
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
public Collection<MkQuery> takeAll(final Matcher<MkAnswer> matcher) { |
188 | 3 | final Collection<MkQuery> results = new LinkedList<MkQuery>(); |
189 | 3 | final Iterator<QueryWithAnswer> iter = this.queue.iterator(); |
190 | 12 | while (iter.hasNext()) { |
191 | 9 | final QueryWithAnswer candidate = iter.next(); |
192 | 9 | if (matcher.matches(candidate.answer())) { |
193 | 8 | results.add(candidate.query()); |
194 | 8 | iter.remove(); |
195 | |
} |
196 | 9 | } |
197 | 3 | if (results.isEmpty()) { |
198 | 0 | throw new NoSuchElementException("No matching results found"); |
199 | |
} |
200 | 3 | return results; |
201 | |
} |
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | |
public int queries() { |
208 | 10 | return this.queue.size(); |
209 | |
} |
210 | |
|
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | |
|
216 | |
private static void fail(final GrizzlyResponse<?> response, |
217 | |
final Throwable failure) { |
218 | 1 | response.setStatus(HttpURLConnection.HTTP_INTERNAL_ERROR); |
219 | |
final PrintWriter writer; |
220 | |
try { |
221 | 1 | writer = new PrintWriter( |
222 | |
new OutputStreamWriter( |
223 | |
response.getStream(), |
224 | |
MkGrizzlyAdapter.ENCODING |
225 | |
) |
226 | |
); |
227 | 0 | } catch (final UnsupportedEncodingException ex) { |
228 | 0 | throw new IllegalStateException(ex); |
229 | 1 | } |
230 | |
try { |
231 | 1 | writer.print(Logger.format("%[exception]s", failure)); |
232 | |
} finally { |
233 | 1 | writer.close(); |
234 | 1 | } |
235 | 1 | } |
236 | |
|
237 | |
|
238 | |
|
239 | |
|
240 | 0 | @EqualsAndHashCode(of = { "answr", "condition" }) |
241 | |
private static final class Conditional { |
242 | |
|
243 | |
|
244 | |
|
245 | |
private final transient MkAnswer answr; |
246 | |
|
247 | |
|
248 | |
|
249 | |
private final transient Matcher<MkQuery> condition; |
250 | |
|
251 | |
|
252 | |
|
253 | |
private final transient AtomicInteger count; |
254 | |
|
255 | |
|
256 | |
|
257 | |
|
258 | |
|
259 | |
|
260 | |
Conditional(final MkAnswer ans, final Matcher<MkQuery> matcher, |
261 | 99 | final int times) { |
262 | 99 | this.answr = ans; |
263 | 99 | this.condition = matcher; |
264 | 99 | if (times < 1) { |
265 | 0 | throw new IllegalArgumentException( |
266 | |
"Answer must be returned at least once." |
267 | |
); |
268 | |
} else { |
269 | 99 | this.count = new AtomicInteger(times); |
270 | |
} |
271 | 99 | } |
272 | |
|
273 | |
|
274 | |
|
275 | |
|
276 | |
public MkAnswer answer() { |
277 | 110 | return this.answr; |
278 | |
} |
279 | |
|
280 | |
|
281 | |
|
282 | |
|
283 | |
|
284 | |
public boolean matches(final MkQuery query) { |
285 | 111 | return this.condition.matches(query); |
286 | |
} |
287 | |
|
288 | |
|
289 | |
|
290 | |
|
291 | |
public int decrement() { |
292 | 106 | return this.count.decrementAndGet(); |
293 | |
} |
294 | |
} |
295 | |
|
296 | |
|
297 | |
|
298 | |
|
299 | 69 | @EqualsAndHashCode(of = { "answr", "que" }) |
300 | 29 | private static final class QueryWithAnswer { |
301 | |
|
302 | |
|
303 | |
|
304 | |
private final transient MkAnswer answr; |
305 | |
|
306 | |
|
307 | |
|
308 | |
private final transient MkQuery que; |
309 | |
|
310 | |
|
311 | |
|
312 | |
|
313 | |
|
314 | 109 | QueryWithAnswer(final MkQuery qry, final MkAnswer ans) { |
315 | 109 | this.answr = ans; |
316 | 109 | this.que = qry; |
317 | 109 | } |
318 | |
|
319 | |
|
320 | |
|
321 | |
|
322 | |
public MkQuery query() { |
323 | 9 | return this.que; |
324 | |
} |
325 | |
|
326 | |
|
327 | |
|
328 | |
|
329 | |
public MkAnswer answer() { |
330 | 10 | return this.answr; |
331 | |
} |
332 | |
} |
333 | |
} |