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.annotation;
18  
19  import static java.util.Collections.*;
20  import static org.apache.juneau.common.utils.Utils.*;
21  
22  import java.util.*;
23  
24  import org.apache.juneau.*;
25  import org.apache.juneau.http.annotation.*;
26  import org.apache.juneau.parser.*;
27  import org.apache.juneau.rest.*;
28  import org.apache.juneau.rest.httppart.*;
29  import org.apache.juneau.rest.mock.*;
30  import org.apache.juneau.serializer.*;
31  import org.apache.juneau.utest.utils.*;
32  import org.junit.jupiter.api.*;
33  
34  class Rest_DefaultRequestAttributes_Test extends TestBase {
35  
36  	//------------------------------------------------------------------------------------------------------------------
37  	// @RestPreCall
38  	//------------------------------------------------------------------------------------------------------------------
39  
40  	@Rest(
41  		parsers=A1.class,
42  		defaultRequestAttributes={
43  			"p1:sp1", // Unchanged servlet-level property.
44  			"p2:sp2", // Servlet-level property overridden by onPreCall.
45  			"p3:sp3", // Servlet-level property overridded by method.
46  			"p4:sp4"  // Servlet-level property overridden by method then onPreCall.
47  		}
48  	)
49  	public static class A {
50  
51  		@RestPreCall
52  		public void onPreCall(RestRequest req) {
53  			var attrs = req.getAttributes();
54  			attrs.set("p2", "xp2");
55  			attrs.set("p4", "xp4");
56  			attrs.set("p5", "xp5"); // New property
57  			var overrideContentType = req.getHeaderParam("Override-Content-Type").orElse(null);
58  			if (overrideContentType != null)
59  				req.getHeaders().set("Content-Type", overrideContentType);
60  		}
61  
62  		@RestPut(
63  			defaultRequestAttributes={
64  				"p3:mp3",
65  				"p4:mp4"
66  			}
67  		)
68  		public String a(@Content String in) {
69  			return in;
70  		}
71  
72  		@RestPut
73  		public String b(RestRequest req, RequestAttributes attrs) throws Exception {
74  			attrs.set("p3", "pp3");
75  			attrs.set("p4", "pp4");
76  			return req.getContent().as(String.class);
77  		}
78  	}
79  
80  	public static class A1 extends FakeReaderParser {
81  		public A1(FakeReaderParser.Builder b) {
82  			super(b.consumes("text/a1,text/a2,text/a3").function((session,in,type)->in(session)));
83  		}
84  
85  		private static Object in(ReaderParserSession session) {
86  			var sp = session.getSessionProperties();
87  			return "p1="+sp.get("p1",null)+",p2="+sp.get("p2",null)+",p3="+sp.get("p3",null)+",p4="+sp.get("p4",null)+",p5="+sp.get("p5",null);
88  
89  		}
90  	}
91  
92  	@Test void a01_preCall() throws Exception {
93  		var a = MockRestClient.build(A.class);
94  		a.put("/a", null).contentType("text/a1").run().assertContent("p1=sp1,p2=xp2,p3=mp3,p4=xp4,p5=xp5");
95  		a.put("/a", null).contentType("text/a1").header("Override-Content-Type", "text/a2").run().assertContent("p1=sp1,p2=xp2,p3=mp3,p4=xp4,p5=xp5");
96  		a.put("/b", null).contentType("text/a1").run().assertContent("p1=sp1,p2=xp2,p3=pp3,p4=pp4,p5=xp5");
97  		a.put("/b", null).contentType("text/a1").header("Override-Content-Type", "text/a2").run().assertContent("p1=sp1,p2=xp2,p3=pp3,p4=pp4,p5=xp5");
98  	}
99  
100 	//------------------------------------------------------------------------------------------------------------------
101 	// @RestPostCall
102 	//------------------------------------------------------------------------------------------------------------------
103 
104 	@Rest(
105 		serializers=B1.class,
106 		defaultRequestAttributes={
107 			"p1:sp1", // Unchanged servlet-level property.
108 			"p2:sp2", // Servlet-level property overridden by onPostCall.
109 			"p3:sp3", // Servlet-level property overridded by method.
110 			"p4:sp4"  // Servlet-level property overridden by method then onPostCall.
111 		}
112 	)
113 	public static class B {
114 
115 		@RestPostCall
116 		public void onPostCall(RestRequest req, RestResponse res) {
117 			var attrs = req.getAttributes();
118 			attrs.set("p2", "xp2");
119 			attrs.set("p4", "xp4");
120 			attrs.set("p5", "xp5"); // New property
121 			var overrideAccept = req.getHeaderParam("Override-Accept").orElse(null);
122 			if (overrideAccept != null)
123 				req.getHeaders().set("Accept", overrideAccept);
124 			var overrideContentType = req.getHeaderParam("Override-Content-Type").orElse(null);
125 			if (overrideContentType != null)
126 				attrs.set("Override-Content-Type", overrideContentType);
127 		}
128 
129 		@RestPut(
130 			defaultRequestAttributes={
131 				"p3:mp3",
132 				"p4:mp4"
133 			},
134 			defaultRequestHeaders="Accept: text/s2"
135 		)
136 		public String a() {
137 			return null;
138 		}
139 
140 		@RestPut
141 		public String b(RestRequest req, RequestAttributes attrs) {
142 			attrs.set("p3", "pp3");
143 			attrs.set("p4", "pp4");
144 			var accept = req.getHeaderParam("Accept").orElse(null);
145 			if (accept == null || accept.isEmpty())
146 				req.getHeaders().set("Accept", "text/s2");
147 			return null;
148 		}
149 	}
150 
151 	public static class B1 extends FakeWriterSerializer {
152 		public B1(FakeWriterSerializer.Builder b) {
153 			super(b.produces("test/s1").accept("text/s1,text/s2,text/s3").function((s,o) -> out(s)).headers(B1::headers));
154 		}
155 		public static String out(SerializerSession s) {
156 			var sp = s.getSessionProperties();
157 			return "p1="+sp.get("p1",null)+",p2="+sp.get("p2",null)+",p3="+sp.get("p3",null)+",p4="+sp.get("p4",null)+",p5="+sp.get("p5",null);
158 		}
159 		public static Map<String,String> headers(SerializerSession s) {
160 			var sp = s.getSessionProperties();
161 			if (sp.containsKey("Override-Content-Type"))
162 				return map("Content-Type",sp.getString("Override-Content-Type",null));
163 			return emptyMap();
164 		}
165 	}
166 
167 	@Test void b01_postCall() throws Exception {
168 		var b = MockRestClient.build(B.class);
169 		b.put("/a", null).accept("text/s1").run().assertContent("p1=sp1,p2=xp2,p3=mp3,p4=xp4,p5=xp5");
170 		b.put("/a", null).accept("text/s1").header("Override-Accept", "text/s2").run().assertContent("p1=sp1,p2=xp2,p3=mp3,p4=xp4,p5=xp5");
171 		b.put("/a", null).accept("text/s1").header("Override-Content-Type", "text/s3").run().assertContent("p1=sp1,p2=xp2,p3=mp3,p4=xp4,p5=xp5");
172 		b.put("/a", null).run().assertContent("p1=sp1,p2=xp2,p3=mp3,p4=xp4,p5=xp5");
173 		b.put("/a", null).header("Override-Accept", "text/s3").run().assertContent("p1=sp1,p2=xp2,p3=mp3,p4=xp4,p5=xp5");
174 		b.put("/a", null).header("Override-Content-Type", "text/s3").run().assertContent("p1=sp1,p2=xp2,p3=mp3,p4=xp4,p5=xp5");
175 		b.put("/b", null).accept("text/s1").run().assertContent("p1=sp1,p2=xp2,p3=pp3,p4=xp4,p5=xp5");
176 		b.put("/b", null).accept("text/s1").header("Override-Accept", "text/s2").run().assertContent("p1=sp1,p2=xp2,p3=pp3,p4=xp4,p5=xp5");
177 		b.put("/b", null).accept("text/s1").header("Override-Content-Type", "text/s3").run().assertContent("p1=sp1,p2=xp2,p3=pp3,p4=xp4,p5=xp5");
178 		b.put("/b", null).run().assertContent("p1=sp1,p2=xp2,p3=pp3,p4=xp4,p5=xp5");
179 		b.put("/b", null).header("Override-Accept", "text/s3").run().assertContent("p1=sp1,p2=xp2,p3=pp3,p4=xp4,p5=xp5");
180 		b.put("/b", null).header("Override-Content-Type", "text/s3").run().assertContent("p1=sp1,p2=xp2,p3=pp3,p4=xp4,p5=xp5");
181 	}
182 }