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