1 /* 2 * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 import java.io.*; 25 import java.net.*; 26 import java.net.http.HttpHeaders; 27 import java.nio.file.*; 28 import java.util.List; 29 import java.util.Map; 30 import java.util.function.BiPredicate; 31 32 public class PushHandler implements Http2Handler { 33 34 static final BiPredicate<String,String> ACCEPT_ALL = (x, y) -> true; 35 36 final Path tempFile; 37 final int loops; 38 final long file_size; 39 PushHandler(Path file, int loops)40 public PushHandler(Path file, int loops) throws Exception { 41 tempFile = file; 42 this.loops = loops; 43 this.file_size = Files.size(file); 44 } 45 46 int invocation = 0; 47 handle(Http2TestExchange ee)48 public void handle(Http2TestExchange ee) { 49 try { 50 System.err.println ("Server: handle " + ee); 51 invocation++; 52 53 if (ee.serverPushAllowed()) { 54 URI requestURI = ee.getRequestURI(); 55 for (int i=0; i<loops; i++) { 56 InputStream is = new FileInputStream(tempFile.toFile()); 57 URI u = requestURI.resolve("/x/y/z/" + Integer.toString(i)); 58 HttpHeaders h = HttpHeaders.of(Map.of("X-foo", List.of("bar")), 59 ACCEPT_ALL); 60 ee.serverPush(u, h, is); 61 } 62 System.err.println ("Server: sent all pushes"); 63 } 64 ee.sendResponseHeaders(200, file_size); 65 OutputStream os = ee.getResponseBody(); 66 InputStream iis = new FileInputStream(tempFile.toFile()); 67 iis.transferTo(os); 68 os.close(); 69 iis.close(); 70 } catch (Exception ex) { 71 System.err.println ("Server: exception " + ex); 72 } 73 } 74 } 75