Skip to content
This repository was archived by the owner on Mar 20, 2021. It is now read-only.

Commit 70520b2

Browse files
committed
[2.3.x] Initial improvement pass on PushResource.
- Add the ability to set both 'request' and 'response' headers for the resource. - Expose add/set header methods in the builder. - Replace Map<String,String> for header storage with MimeHeaders instances to support add/set header methods.
1 parent 85776ff commit 70520b2

File tree

4 files changed

+147
-44
lines changed

4 files changed

+147
-44
lines changed

modules/http/src/main/java/org/glassfish/grizzly/http/util/MimeHeaders.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
33
*
4-
* Copyright (c) 2010-2015 Oracle and/or its affiliates. All rights reserved.
4+
* Copyright (c) 2010-2017 Oracle and/or its affiliates. All rights reserved.
55
*
66
* The contents of this file are subject to the terms of either the GNU
77
* General Public License Version 2 only ("GPL") or the Common Development
@@ -201,7 +201,7 @@ public String toString() {
201201
}
202202

203203
public void copyFrom(final MimeHeaders source) {
204-
if (source.size() == 0) {
204+
if (source == null || source.size() == 0) {
205205
return;
206206
}
207207
this.maxNumHeaders = source.maxNumHeaders;

modules/http2/src/main/java/org/glassfish/grizzly/http2/Http2ServerFilter.java

+6-10
Original file line numberDiff line numberDiff line change
@@ -822,7 +822,7 @@ protected void processOutgoingHttpHeader(final FilterChainContext ctx,
822822

823823
if (!response.isCommitted()) {
824824
prepareOutgoingResponse(response);
825-
pushAssociatedResourses(ctx, stream);
825+
pushAssociatedResources(ctx, stream);
826826
}
827827

828828
final FilterChainContext.TransportContext transportContext = ctx.getTransportContext();
@@ -833,7 +833,7 @@ protected void processOutgoingHttpHeader(final FilterChainContext ctx,
833833
transportContext.getMessageCloner());
834834
}
835835

836-
private void pushAssociatedResourses(final FilterChainContext ctx,
836+
private void pushAssociatedResources(final FilterChainContext ctx,
837837
final Http2Stream stream) throws IOException {
838838
final Map<String, PushResource> pushResourceMap =
839839
stream.getAssociatedResourcesToPush();
@@ -899,14 +899,10 @@ private void pushAssociatedResourses(final FilterChainContext ctx,
899899
response.setContentLengthLong(source.remaining());
900900
}
901901

902-
// Add extra headers if any
903-
final Map<String, String> extraHeaders = pushResource.getHeaders();
904-
if (extraHeaders != null) {
905-
for (Map.Entry<String, String> headerEntry : extraHeaders.entrySet()) {
906-
response.addHeader(headerEntry.getKey(), headerEntry.getValue());
907-
}
908-
}
909-
902+
// Add any extra push promise and push response headers
903+
request.getHeaders().copyFrom(pushResource.getRequestHeaders());
904+
response.getHeaders().copyFrom(pushResource.getResponseHeaders());
905+
910906
prepareOutgoingRequest(request);
911907
prepareOutgoingResponse(response);
912908

modules/http2/src/main/java/org/glassfish/grizzly/http2/PushResource.java

+126-27
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
33
*
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.
55
*
66
* The contents of this file are subject to the terms of either the GNU
77
* General Public License Version 2 only ("GPL") or the Common Development
@@ -40,10 +40,9 @@
4040

4141
package org.glassfish.grizzly.http2;
4242

43-
import java.util.HashMap;
44-
import java.util.Map;
4543
import org.glassfish.grizzly.http.util.Header;
4644
import org.glassfish.grizzly.http.util.HttpStatus;
45+
import org.glassfish.grizzly.http.util.MimeHeaders;
4746

4847
/**
4948
* The class represents the data to be pushed from server to client.
@@ -55,16 +54,14 @@
5554
* @author Alexey Stashok.
5655
*/
5756
public final class PushResource {
57+
5858
private Source resource;
59-
6059
private int priority;
61-
6260
private HttpStatus statusCode = HttpStatus.OK_200;
63-
6461
private String contentType;
62+
private MimeHeaders pushPromise;
63+
private MimeHeaders pushResponse;
6564

66-
private Map<String, String> headers;
67-
6865
public static PushResourceBuilder builder() {
6966
return new PushResourceBuilder();
7067
}
@@ -101,13 +98,25 @@ public String getContentType() {
10198
}
10299

103100
/**
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.
106103
*/
107-
public Map<String, String> getHeaders() {
108-
return headers;
104+
public MimeHeaders getRequestHeaders() {
105+
return pushPromise;
109106
}
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+
111120
/**
112121
* PushResource builder to be used to create {@link PushResource} instance.
113122
*/
@@ -184,34 +193,108 @@ public PushResourceBuilder contentType(final String contentType) {
184193
}
185194

186195
/**
187-
* Adds additional header to be pushed.
196+
* Adds an additional header to be included in the PUSH_PROMISE sent to the client.
188197
* @param name the header name.
189198
* @param value the header value.
190199
*
191200
* @return {@link PushResourceBuilder}.
192201
*/
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);
199206
return this;
200207
}
201208

202209
/**
203-
* Adds additional header to be pushed.
210+
* Adds an additional header to be included in the PUSH_PROMISE sent to the client.
204211
* @param name the header name.
205212
* @param value the header value.
206213
*
207214
* @return {@link PushResourceBuilder}.
208215
*/
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);
215298
return this;
216299
}
217300

@@ -221,5 +304,21 @@ public PushResourceBuilder header(final Header name, final String value) {
221304
public PushResource build() {
222305
return pushResource;
223306
}
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+
}
224323
}
225324
}

modules/http2/src/test/java/org/glassfish/grizzly/http2/ServerPushTest.java

+13-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
33
*
4-
* Copyright (c) 2013-2015 Oracle and/or its affiliates. All rights reserved.
4+
* Copyright (c) 2013-2017 Oracle and/or its affiliates. All rights reserved.
55
*
66
* The contents of this file are subject to the terms of either the GNU
77
* General Public License Version 2 only ("GPL") or the Common Development
@@ -104,7 +104,8 @@ public void testFilePush() throws Exception {
104104

105105
final InputStream fis = new FileInputStream(tmpFile);
106106
byte[] data = new byte[(int) tmpFile.length()];
107-
fis.read(data);
107+
int len = fis.read(data);
108+
assertEquals(len, tmpFile.length());
108109
fis.close();
109110

110111
doTestPushResource(new TestResourceFactory() {
@@ -189,7 +190,8 @@ public void service(final Request request, final Response response) throws Excep
189190
.source(resourceFactory.create(http2Stream));
190191

191192
if (hasExtraHeader) {
192-
pushResourceBuilder.header(extraHeaderName, extraHeaderValue);
193+
pushResourceBuilder.addRequestHeader(extraHeaderName, extraHeaderValue);
194+
pushResourceBuilder.addResponseHeader(extraHeaderName, extraHeaderValue);
193195
}
194196

195197
http2Stream.addPushResource(
@@ -231,15 +233,20 @@ public void service(final Request request, final Response response) throws Excep
231233
final HttpHeader header2 = content2.getHttpHeader();
232234

233235
final HttpResponsePacket pushedResponse;
236+
final HttpRequestPacket pushedRequest;
234237
final HttpContent pushedContent;
235238
final HttpResponsePacket mainResponse;
236-
237-
if (Http2Stream.getStreamFor(header1).isPushStream()) {
239+
240+
final Http2Stream stream = Http2Stream.getStreamFor(header1);
241+
assertNotNull(stream);
242+
if (stream.isPushStream()) {
238243
pushedResponse = (HttpResponsePacket) header1;
244+
pushedRequest = pushedResponse.getRequest();
239245
pushedContent = content1;
240246
mainResponse = (HttpResponsePacket) header2;
241247
} else {
242248
pushedResponse = (HttpResponsePacket) header2;
249+
pushedRequest = pushedResponse.getRequest();
243250
pushedContent = content2;
244251
mainResponse = (HttpResponsePacket) header1;
245252
}
@@ -250,6 +257,7 @@ public void service(final Request request, final Response response) throws Excep
250257
assertEquals(resourceAsciiPayloadToCheck.length, pushedContent.getContent().remaining());
251258
if (hasExtraHeader) {
252259
assertEquals(extraHeaderValue, pushedResponse.getHeader(extraHeaderName));
260+
assertEquals(extraHeaderValue, pushedRequest.getHeader(extraHeaderName));
253261
}
254262

255263
assertEquals(200, mainResponse.getStatus());

0 commit comments

Comments
 (0)