1
1
/*
2
2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
3
*
4
- * Copyright (c) 2013-2016 Oracle and/or its affiliates. All rights reserved.
4
+ * Copyright (c) 2013-2017 Oracle and/or its affiliates. All rights reserved.
5
5
*
6
6
* The contents of this file are subject to the terms of either the GNU
7
7
* General Public License Version 2 only ("GPL") or the Common Development
40
40
41
41
package org .glassfish .grizzly .http2 ;
42
42
43
- import java .util .HashMap ;
44
- import java .util .Map ;
45
43
import org .glassfish .grizzly .http .util .Header ;
46
44
import org .glassfish .grizzly .http .util .HttpStatus ;
45
+ import org .glassfish .grizzly .http .util .MimeHeaders ;
47
46
48
47
/**
49
48
* The class represents the data to be pushed from server to client.
55
54
* @author Alexey Stashok.
56
55
*/
57
56
public final class PushResource {
57
+
58
58
private Source resource ;
59
-
60
59
private int priority ;
61
-
62
60
private HttpStatus statusCode = HttpStatus .OK_200 ;
63
-
64
61
private String contentType ;
62
+ private MimeHeaders pushPromise ;
63
+ private MimeHeaders pushResponse ;
65
64
66
- private Map <String , String > headers ;
67
-
68
65
public static PushResourceBuilder builder () {
69
66
return new PushResourceBuilder ();
70
67
}
@@ -101,13 +98,25 @@ public String getContentType() {
101
98
}
102
99
103
100
/**
104
- * Returns additional headers to be pushed.
105
- * <tt>null</tt> value means no additional headers to push .
101
+ * Returns additional headers to be included with the PUSH_PROMISE or <code>null</code>
102
+ * if not headers have been added or set .
106
103
*/
107
- public Map < String , String > getHeaders () {
108
- return headers ;
104
+ public MimeHeaders getRequestHeaders () {
105
+ return pushPromise ;
109
106
}
110
-
107
+
108
+ /**
109
+ * Returns additional headers to be included with the push response or <code>null</code>
110
+ * if not headers have been added or set.
111
+ */
112
+ public MimeHeaders getResponseHeaders () {
113
+ return pushResponse ;
114
+ }
115
+
116
+
117
+ // --------------------------------------------------------- Nested Classes
118
+
119
+
111
120
/**
112
121
* PushResource builder to be used to create {@link PushResource} instance.
113
122
*/
@@ -184,34 +193,108 @@ public PushResourceBuilder contentType(final String contentType) {
184
193
}
185
194
186
195
/**
187
- * Adds additional header to be pushed .
196
+ * Adds an additional header to be included in the PUSH_PROMISE sent to the client .
188
197
* @param name the header name.
189
198
* @param value the header value.
190
199
*
191
200
* @return {@link PushResourceBuilder}.
192
201
*/
193
- public PushResourceBuilder header (final String name , final String value ) {
194
- if (pushResource .headers == null ) {
195
- pushResource .headers = new HashMap <>(4 );
196
- }
197
-
198
- pushResource .headers .put (name , value );
202
+ @ SuppressWarnings ("UnusedReturnValue" )
203
+ public PushResourceBuilder addRequestHeader (final String name , final String value ) {
204
+ initializePushPromiseHeaders ();
205
+ pushResource .pushPromise .addValue (name ).setString (value );
199
206
return this ;
200
207
}
201
208
202
209
/**
203
- * Adds additional header to be pushed .
210
+ * Adds an additional header to be included in the PUSH_PROMISE sent to the client .
204
211
* @param name the header name.
205
212
* @param value the header value.
206
213
*
207
214
* @return {@link PushResourceBuilder}.
208
215
*/
209
- public PushResourceBuilder header (final Header name , final String value ) {
210
- if (pushResource .headers == null ) {
211
- pushResource .headers = new HashMap <>(4 );
212
- }
213
-
214
- pushResource .headers .put (name .toString (), value );
216
+ public PushResourceBuilder addRequestHeader (final Header name , final String value ) {
217
+ initializePushPromiseHeaders ();
218
+ pushResource .pushPromise .addValue (name ).setString (value );
219
+ return this ;
220
+ }
221
+
222
+ /**
223
+ * Sets an additional header to be included in the PUSH_PROMISE sent to the client.
224
+ * @param name the header name.
225
+ * @param value the header value.
226
+ *
227
+ * @return {@link PushResourceBuilder}.
228
+ */
229
+ public PushResourceBuilder setRequestHeader (final String name , final String value ) {
230
+ initializePushPromiseHeaders ();
231
+ pushResource .pushPromise .setValue (name ).setString (value );
232
+ return this ;
233
+ }
234
+
235
+ /**
236
+ * Sets an additional header to be included in the PUSH_PROMISE sent to the client.
237
+ * @param name the header name.
238
+ * @param value the header value.
239
+ *
240
+ * @return {@link PushResourceBuilder}.
241
+ */
242
+ public PushResourceBuilder setRequestHeader (final Header name , final String value ) {
243
+ initializePushPromiseHeaders ();
244
+ pushResource .pushPromise .setValue (name ).setString (value );
245
+ return this ;
246
+ }
247
+
248
+ /**
249
+ * Adds an additional header to be included in the response sent to the client.
250
+ * @param name the header name.
251
+ * @param value the header value.
252
+ *
253
+ * @return {@link PushResourceBuilder}.
254
+ */
255
+ @ SuppressWarnings ("UnusedReturnValue" )
256
+ public PushResourceBuilder addResponseHeader (final String name , final String value ) {
257
+ initializePushResponseHeaders ();
258
+ pushResource .pushResponse .addValue (name ).setString (value );
259
+ return this ;
260
+ }
261
+
262
+ /**
263
+ * Adds an additional header to be included in the response sent to the client.
264
+ * @param name the header name.
265
+ * @param value the header value.
266
+ *
267
+ * @return {@link PushResourceBuilder}.
268
+ */
269
+ public PushResourceBuilder addResponseHeader (final Header name , final String value ) {
270
+ initializePushResponseHeaders ();
271
+ pushResource .pushResponse .addValue (name ).setString (value );
272
+ return this ;
273
+ }
274
+
275
+ /**
276
+ * Sets an additional header to be included in the response sent to the client.
277
+ * @param name the header name.
278
+ * @param value the header value.
279
+ *
280
+ * @return {@link PushResourceBuilder}.
281
+ */
282
+ public PushResourceBuilder setResponseHeader (final String name , final String value ) {
283
+ initializePushResponseHeaders ();
284
+ pushResource .pushResponse .setValue (name ).setString (value );
285
+ return this ;
286
+ }
287
+
288
+ /**
289
+ * Sets an additional header to be included in the response sent to the client.
290
+ * @param name the header name.
291
+ * @param value the header value.
292
+ *
293
+ * @return {@link PushResourceBuilder}.
294
+ */
295
+ public PushResourceBuilder setResponseHeader (final Header name , final String value ) {
296
+ initializePushResponseHeaders ();
297
+ pushResource .pushResponse .setValue (name ).setString (value );
215
298
return this ;
216
299
}
217
300
@@ -221,5 +304,21 @@ public PushResourceBuilder header(final Header name, final String value) {
221
304
public PushResource build () {
222
305
return pushResource ;
223
306
}
307
+
308
+
309
+ // ---------------------------------------------------- Private Methods
310
+
311
+
312
+ private void initializePushPromiseHeaders () {
313
+ if (pushResource .pushPromise == null ) {
314
+ pushResource .pushPromise = new MimeHeaders ();
315
+ }
316
+ }
317
+
318
+ private void initializePushResponseHeaders () {
319
+ if (pushResource .pushResponse == null ) {
320
+ pushResource .pushResponse = new MimeHeaders ();
321
+ }
322
+ }
224
323
}
225
324
}
0 commit comments