@@ -44,33 +44,154 @@ public final class Response implements Closeable {
4444 private final String reason ;
4545 private final Map <String , Collection <String >> headers ;
4646 private final Body body ;
47-
48- private Response (int status , String reason , Map <String , Collection <String >> headers , Body body ) {
49- checkState (status >= 200 , "Invalid status code: %s" , status );
50- this .status = status ;
51- this .reason = reason ; //nullable
52- this .headers = Collections .unmodifiableMap (caseInsensitiveCopyOf (headers ));
53- this .body = body ; //nullable
47+ private final Request request ;
48+
49+ private Response (Builder builder ) {
50+ checkState (builder .status >= 200 , "Invalid status code: %s" , builder .status );
51+ this .status = builder .status ;
52+ this .reason = builder .reason ; //nullable
53+ this .headers = Collections .unmodifiableMap (caseInsensitiveCopyOf (builder .headers ));
54+ this .body = builder .body ; //nullable
55+ this .request = builder .request ; //nullable
5456 }
5557
58+ /**
59+ * @deprecated To be removed in Feign 10
60+ */
61+ @ Deprecated
5662 public static Response create (int status , String reason , Map <String , Collection <String >> headers ,
5763 InputStream inputStream , Integer length ) {
58- return new Response (status , reason , headers , InputStreamBody .orNull (inputStream , length ));
64+ return Response .builder ()
65+ .status (status )
66+ .reason (reason )
67+ .headers (headers )
68+ .body (InputStreamBody .orNull (inputStream , length ))
69+ .build ();
5970 }
6071
72+ /**
73+ * @deprecated To be removed in Feign 10
74+ */
75+ @ Deprecated
6176 public static Response create (int status , String reason , Map <String , Collection <String >> headers ,
6277 byte [] data ) {
63- return new Response (status , reason , headers , ByteArrayBody .orNull (data ));
78+ return Response .builder ()
79+ .status (status )
80+ .reason (reason )
81+ .headers (headers )
82+ .body (ByteArrayBody .orNull (data ))
83+ .build ();
6484 }
6585
86+ /**
87+ * @deprecated To be removed in Feign 10
88+ */
89+ @ Deprecated
6690 public static Response create (int status , String reason , Map <String , Collection <String >> headers ,
6791 String text , Charset charset ) {
68- return new Response (status , reason , headers , ByteArrayBody .orNull (text , charset ));
92+ return Response .builder ()
93+ .status (status )
94+ .reason (reason )
95+ .headers (headers )
96+ .body (ByteArrayBody .orNull (text , charset ))
97+ .build ();
6998 }
7099
100+ /**
101+ * @deprecated To be removed in Feign 10
102+ */
103+ @ Deprecated
71104 public static Response create (int status , String reason , Map <String , Collection <String >> headers ,
72105 Body body ) {
73- return new Response (status , reason , headers , body );
106+ return Response .builder ()
107+ .status (status )
108+ .reason (reason )
109+ .headers (headers )
110+ .body (body )
111+ .build ();
112+ }
113+
114+ public Builder toBuilder (){
115+ return new Builder (this );
116+ }
117+
118+ public static Builder builder (){
119+ return new Builder ();
120+ }
121+
122+ public static final class Builder {
123+ int status ;
124+ String reason ;
125+ Map <String , Collection <String >> headers ;
126+ Body body ;
127+ Request request ;
128+
129+ Builder () {
130+ }
131+
132+ Builder (Response source ) {
133+ this .status = source .status ;
134+ this .reason = source .reason ;
135+ this .headers = source .headers ;
136+ this .body = source .body ;
137+ this .request = source .request ;
138+ }
139+
140+ /** @see Response#status*/
141+ public Builder status (int status ) {
142+ this .status = status ;
143+ return this ;
144+ }
145+
146+ /** @see Response#reason */
147+ public Builder reason (String reason ) {
148+ this .reason = reason ;
149+ return this ;
150+ }
151+
152+ /** @see Response#headers */
153+ public Builder headers (Map <String , Collection <String >> headers ) {
154+ this .headers = headers ;
155+ return this ;
156+ }
157+
158+ /** @see Response#body */
159+ public Builder body (Body body ) {
160+ this .body = body ;
161+ return this ;
162+ }
163+
164+ /** @see Response#body */
165+ public Builder body (InputStream inputStream , Integer length ) {
166+ this .body = InputStreamBody .orNull (inputStream , length );
167+ return this ;
168+ }
169+
170+ /** @see Response#body */
171+ public Builder body (byte [] data ) {
172+ this .body = ByteArrayBody .orNull (data );
173+ return this ;
174+ }
175+
176+ /** @see Response#body */
177+ public Builder body (String text , Charset charset ) {
178+ this .body = ByteArrayBody .orNull (text , charset );
179+ return this ;
180+ }
181+
182+ /** @see Response#request
183+ *
184+ * NOTE: will add null check in version 10 which may require changes
185+ * to custom feign.Client or loggers
186+ */
187+ public Builder request (Request request ) {
188+ this .request = request ;
189+ return this ;
190+ }
191+
192+ public Response build () {
193+ return new Response (this );
194+ }
74195 }
75196
76197 /**
@@ -105,6 +226,13 @@ public Body body() {
105226 return body ;
106227 }
107228
229+ /**
230+ * if present, the request that generated this response
231+ */
232+ public Request request () {
233+ return request ;
234+ }
235+
108236 @ Override
109237 public String toString () {
110238 StringBuilder builder = new StringBuilder ("HTTP/1.1 " ).append (status );
0 commit comments