1/*
2 * Copyright (c) 2015, 2016, 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
24import java.io.*;
25import java.net.*;
26import java.nio.file.*;
27import jdk.incubator.http.internal.common.HttpHeadersImpl;
28
29public class PushHandler implements Http2Handler {
30
31    final Path tempFile;
32    final int loops;
33    final int file_size;
34
35    public PushHandler(int file_size, int loops) throws Exception {
36        tempFile = TestUtil.getAFile(file_size);
37        this.loops = loops;
38        this.file_size = file_size;
39    }
40
41    int invocation = 0;
42
43    public void handle(Http2TestExchange ee) {
44        try {
45            System.err.println ("Server: handle " + ee);
46            invocation++;
47
48            if (ee.serverPushAllowed()) {
49                for (int i=0; i<loops; i++) {
50                    InputStream is = new FileInputStream(tempFile.toFile());
51                    URI u = new URI ("http://www.foo.com/x/y/z/" + Integer.toString(i));
52                    HttpHeadersImpl h = new HttpHeadersImpl();
53                    h.addHeader("X-foo", "bar");
54                    ee.serverPush(u, h, is);
55                }
56                System.err.println ("Server: sent all pushes");
57            }
58            ee.sendResponseHeaders(200, file_size);
59            OutputStream os = ee.getResponseBody();
60            InputStream iis = new FileInputStream(tempFile.toFile());
61            iis.transferTo(os);
62            os.close();
63            iis.close();
64        } catch (Exception ex) {
65            System.err.println ("Server: exception " + ex);
66        }
67    }
68}
69