View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.juneau.rest;
18  
19  import static org.apache.juneau.commons.utils.CollectionUtils.*;
20  import static org.apache.juneau.commons.utils.IoUtils.*;
21  import static org.apache.juneau.http.HttpMethod.*;
22  
23  import java.io.*;
24  import java.util.*;
25  
26  import org.apache.juneau.*;
27  import org.apache.juneau.commons.reflect.*;
28  import org.apache.juneau.config.*;
29  import org.apache.juneau.cp.*;
30  import org.apache.juneau.encoders.*;
31  import org.apache.juneau.http.header.*;
32  import org.apache.juneau.json.*;
33  import org.apache.juneau.parser.*;
34  import org.apache.juneau.plaintext.*;
35  import org.apache.juneau.rest.annotation.*;
36  import org.apache.juneau.rest.arg.*;
37  import org.apache.juneau.rest.httppart.*;
38  import org.apache.juneau.rest.mock.*;
39  import org.junit.jupiter.api.*;
40  
41  import jakarta.servlet.*;
42  
43  class RestOp_Params_Test extends TestBase {
44  
45  	//------------------------------------------------------------------------------------------------------------------
46  	// Various parameters
47  	//------------------------------------------------------------------------------------------------------------------
48  
49  	@Rest(messages="RestParamsTest")
50  	public static class A {
51  		@RestGet
52  		public String a(ResourceBundle t) {
53  			return t == null ? null : t.getString("foo");
54  		}
55  		@RestGet
56  		public String b(Messages t) {
57  			return t == null ? null : t.getString("foo");
58  		}
59  		@RestPost
60  		public String c(InputStream t) throws IOException {
61  			return read(t);
62  		}
63  		@RestPost
64  		public String d(ServletInputStream t) throws IOException {
65  			return read(t);
66  		}
67  		@RestPost
68  		public String e(Reader t) throws IOException {
69  			return read(t);
70  		}
71  		@RestGet
72  		public void f(OutputStream t) throws IOException {
73  			t.write("OK".getBytes());
74  		}
75  		@RestGet
76  		public void g(ServletOutputStream t) throws IOException {
77  			t.write("OK".getBytes());
78  		}
79  		@RestGet
80  		public void h(Writer t) throws IOException {
81  			t.write("OK");
82  		}
83  		@RestGet
84  		public boolean i(RequestHeaders t) {
85  			return t != null;
86  		}
87  		@RestGet
88  		public boolean j(RequestQueryParams t) {
89  			return t != null;
90  		}
91  		@RestGet
92  		public boolean k(RequestFormParams t) {
93  			return t != null;
94  		}
95  		@RestGet
96  		public String l(@Method String t) {
97  			return t;
98  		}
99  		@RestGet
100 		public boolean n(RestContext t) {
101 			return t != null;
102 		}
103 		@RestOp(method=GET,parsers={JsonParser.class})
104 		public String o(Parser t) {
105 			return t.getClass().getName();
106 		}
107 		@RestGet
108 		public String p(Locale t) {
109 			return t.toString();
110 		}
111 		@RestGet
112 		public boolean q(org.apache.juneau.bean.swagger.Swagger t) {
113 			return t != null;
114 		}
115 		@RestGet
116 		public boolean r(RequestPathParams t) {
117 			return t != null;
118 		}
119 		@RestGet
120 		public boolean s(RequestContent t) {
121 			return t != null;
122 		}
123 		@RestGet
124 		public boolean t(Config t) {
125 			return t != null;
126 		}
127 	}
128 
129 	@Test void a01_params() throws Exception {
130 		var a = MockRestClient.build(A.class);
131 		a.post("/c", "foo").run().assertContent("foo");
132 		a.post("/d", "foo").run().assertContent("foo");
133 		a.post("/e", "foo").run().assertContent("foo");
134 		a.get("/f").run().assertContent("OK");
135 		a.get("/g").run().assertContent("OK");
136 		a.get("/h").run().assertContent("OK");
137 		a.get("/i").run().assertContent("true");
138 		a.get("/j").run().assertContent("true");
139 		a.get("/k").run().assertContent("true");
140 		a.get("/l").run().assertContent("GET");
141 		a.get("/n").run().assertContent("true");
142 		a.get("/o").contentType("application/json").run().assertContent("org.apache.juneau.json.JsonParser");
143 		a.get("/q").run().assertContent("true");
144 		a.get("/r").run().assertContent("true");
145 		a.get("/s").run().assertContent("true");
146 		a.get("/t").run().assertContent("true");
147 	}
148 
149 	//------------------------------------------------------------------------------------------------------------------
150 	// Headers
151 	//------------------------------------------------------------------------------------------------------------------
152 
153 	@Rest(
154 		serializers=B1a.class,
155 		parsers=B1b.class,
156 		encoders=B1c.class,
157 		allowedHeaderParams="*"
158 	)
159 	public static class B1 {
160 
161 		@RestGet
162 		public String accept(Accept accept) {
163 			return accept.getValue();
164 		}
165 		@RestGet
166 		public String acceptCharset(AcceptCharset acceptCharset) {
167 			return acceptCharset.getValue();
168 		}
169 		@RestGet
170 		public String acceptEncoding(AcceptEncoding acceptEncoding) {
171 			return acceptEncoding.getValue();
172 		}
173 		@RestGet
174 		public String acceptLanguage(AcceptLanguage acceptLanguage) {
175 			return acceptLanguage.getValue();
176 		}
177 		@RestGet
178 		public String authorization(Authorization authorization) {
179 			return authorization.getValue();
180 		}
181 		@RestGet
182 		public String cacheControl(CacheControl cacheControl) {
183 			return cacheControl.getValue();
184 		}
185 		@RestGet
186 		public String connection(Connection connection) {
187 			return connection.getValue();
188 		}
189 		@RestGet
190 		public String contentLength(ContentLength contentLength) {
191 			return contentLength.getValue();
192 		}
193 		@RestGet
194 		public String contentType(ContentType contentType) {
195 			return contentType.getValue();
196 		}
197 		@RestGet
198 		public String date(org.apache.juneau.http.header.Date date) {
199 			return date.getValue();
200 		}
201 		@RestGet
202 		public String expect(Expect expect) {
203 			return expect.getValue();
204 		}
205 		@RestGet
206 		public String from(From from) {
207 			return from.getValue();
208 		}
209 		@RestGet
210 		public String host(Host host) {
211 			return host.getValue();
212 		}
213 		@RestGet
214 		public String ifMatch(IfMatch ifMatch) {
215 			return ifMatch.getValue();
216 		}
217 		@RestGet
218 		public String ifModifiedSince(IfModifiedSince ifModifiedSince) {
219 			return ifModifiedSince.getValue();
220 		}
221 		@RestGet
222 		public String ifNoneMatch(IfNoneMatch ifNoneMatch) {
223 			return ifNoneMatch.getValue();
224 		}
225 		@RestGet
226 		public String ifRange(IfRange ifRange) {
227 			return ifRange.getValue();
228 		}
229 		@RestGet
230 		public String ifUnmodifiedSince(IfUnmodifiedSince ifUnmodifiedSince) {
231 			return ifUnmodifiedSince.getValue();
232 		}
233 		@RestGet
234 		public String maxForwards(MaxForwards maxForwards) {
235 			return maxForwards.getValue();
236 		}
237 		@RestGet
238 		public String pragma(Pragma pragma) {
239 			return pragma.getValue();
240 		}
241 		@RestGet
242 		public String proxyAuthorization(ProxyAuthorization proxyAuthorization) {
243 			return proxyAuthorization.getValue();
244 		}
245 		@RestGet
246 		public String range(Range range) {
247 			return range.getValue();
248 		}
249 		@RestGet
250 		public String referer(Referer referer) {
251 			return referer.getValue();
252 		}
253 		@RestGet
254 		public String te(TE te) {
255 			return te.getValue();
256 		}
257 		@RestGet
258 		public String upgrade(Upgrade upgrade) {
259 			return upgrade.getValue();
260 		}
261 		@RestGet
262 		public String userAgent(UserAgent userAgent) {
263 			return userAgent.getValue();
264 		}
265 		@RestGet
266 		public String warning(Warning warning) {
267 			return warning.getValue();
268 		}
269 	}
270 
271 	public static class B1a extends PlainTextSerializer {
272 		public B1a(PlainTextSerializer.Builder b) {
273 			super(b.accept("*/*"));
274 		}
275 	}
276 
277 	public static class B1b extends PlainTextParser {
278 		public B1b(PlainTextParser.Builder b) {
279 			super(b.consumes("*/*"));
280 		}
281 	}
282 
283 	public static class B1c extends IdentityEncoder {
284 		@Override /* ConfigEncoder */
285 		public String[] getCodings() {
286 			return a("*");
287 		}
288 	}
289 
290 	@Test void b01_headers() throws Exception {
291 		var b = MockRestClient.build(B1.class);
292 
293 		b.get("/accept").accept("text/foo").run().assertContent("text/foo");
294 		b.get("/accept").accept("text/foo+bar").run().assertContent("text/foo+bar");
295 		b.get("/accept").accept("text/*").run().assertContent("text/*");
296 		b.get("/accept").accept("*/foo").run().assertContent("*/foo");
297 		b.get("/accept").accept("text/foo;q=1.0").run().assertContent("text/foo;q=1.0");
298 		b.get("/accept").accept("text/foo;q=0.9").run().assertContent("text/foo;q=0.9");
299 		b.get("/accept").accept("text/foo;x=X;q=0.9;y=Y").run().assertContent("text/foo;x=X;q=0.9;y=Y");
300 		b.get("/accept?Accept=text/foo").run().assertContent("text/foo");
301 		b.get("/acceptCharset").acceptCharset("UTF-8").run().assertContent("UTF-8");
302 		b.get("/acceptCharset?Accept-Charset=UTF-8").run().assertContent("UTF-8");
303 		b.get("/acceptEncoding?Accept-Encoding=*").run().assertContent("*");
304 		b.get("/authorization?Authorization=foo").run().assertContent("foo");
305 		b.get("/cacheControl?Cache-Control=foo").run().assertContent("foo");
306 		b.get("/connection?Connection=foo").run().assertContent("foo");
307 		b.get("/contentLength?Content-Length=0").run().assertContent("0");
308 		b.get("/contentType").contentType("text/foo").run().assertContent("text/foo");
309 		b.get("/contentType?Content-Type=text/foo").run().assertContent("text/foo");
310 		b.get("/date?Date=Mon, 3 Dec 2007 10:15:30 GMT").run().assertContent("Mon, 3 Dec 2007 10:15:30 GMT");
311 		b.get("/expect?Expect=100-continue").run().assertContent("100-continue");
312 		b.get("/from?From=foo").run().assertContent("foo");
313 		b.get("/host").uriHost("localhost").run().assertContent("localhost");
314 		b.get("/host?Host=localhost").run().assertContent("localhost");
315 		b.get("/ifMatch?If-Match=\"foo\"").run().assertContent("\"foo\"");
316 		b.get("/ifModifiedSince?If-Modified-Since=Mon, 3 Dec 2007 10:15:30 GMT").run().assertContent("Mon, 3 Dec 2007 10:15:30 GMT");
317 		b.get("/ifNoneMatch?If-None-Match=\"foo\"").run().assertContent("\"foo\"");
318 		b.get("/ifRange?If-Range=\"foo\"").run().assertContent("\"foo\"");
319 		b.get("/ifUnmodifiedSince?If-Unmodified-Since=Mon, 3 Dec 2007 10:15:30 GMT").run().assertContent("Mon, 3 Dec 2007 10:15:30 GMT");
320 		b.get("/maxForwards?Max-Forwards=123").run().assertContent("123");
321 		b.get("/pragma?Pragma=foo").run().assertContent("foo");
322 		b.get("/proxyAuthorization?Proxy-Authorization=foo").run().assertContent("foo");
323 		b.get("/range?Range=foo").run().assertContent("foo");
324 		b.get("/referer?Referer=foo").run().assertContent("foo");
325 		b.get("/te?TE=foo").run().assertContent("foo");
326 		b.get("/upgrade?Upgrade=foo").run().assertContent("foo");
327 		b.get("/userAgent?User-Agent=foo").run().assertContent("foo");
328 		b.get("/warning?Warning=foo").run().assertContent("foo");
329 	}
330 
331 	//------------------------------------------------------------------------------------------------------------------
332 	// Custom header.
333 	//------------------------------------------------------------------------------------------------------------------
334 
335 	@Rest(
336 		restOpArgs=B2a.class,
337 		allowedHeaderParams="Custom"
338 	)
339 	public static class B2 {
340 		@RestGet
341 		public String a(B2b customHeader) {
342 			return customHeader.toString();
343 		}
344 	}
345 
346 	public static class B2a implements RestOpArg {
347 
348 		public static B2a create(ParameterInfo pi) {
349 			if (pi.isType(B2b.class))
350 				return new B2a();
351 			return null;
352 		}
353 
354 		@Override
355 		public Object resolve(RestOpSession opSession) throws Exception {
356 			return new B2b(opSession.getRequest().getHeaderParam("Custom").orElse(null));
357 		}
358 	}
359 
360 	public static class B2b {
361 		public String value;
362 		public B2b(String value) {
363 			this.value = value;
364 		}
365 		@Override
366 		public String toString() {
367 			return value;
368 		}
369 	}
370 
371 	@Test void b02_customHeader() throws Exception {
372 		var b = MockRestClient.build(B2.class);
373 		b.get("/a").header("Custom", "foo").run().assertContent("foo");
374 		b.get("/a?Custom=foo").run().assertContent("foo");
375 	}
376 }