1/*
2 * Copyright (c) 2001, 2003, 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/*
25 *
26 * This is a HTTP test server.
27 */
28
29import java.io.*;
30import java.net.*;
31import javax.net.*;
32
33/*
34 * OriginServer.java -- a simple server that can serve
35 * http requests in both clear and secure channel.
36 */
37
38public abstract class OriginServer implements Runnable {
39
40    private ServerSocket server = null;
41    Exception serverException = null;
42
43    /**
44     * Constructs a OriginServer based on ss and
45     * obtains a response data's bytecodes using the method
46     * getBytes.
47     */
48    protected OriginServer(ServerSocket ss) throws Exception
49    {
50        server = ss;
51        newListener();
52        if (serverException != null)
53            throw serverException;
54    }
55
56    /**
57     * Returns an array of bytes containing the bytes for
58     * data sent in the response.
59     *
60     * @return the bytes for the information that is being sent
61     */
62    public abstract byte[] getBytes();
63
64    /**
65     * The "listen" thread that accepts a connection to the
66     * server, parses header and sends back the response
67     */
68    public void run()
69    {
70        Socket socket;
71
72        // accept a connection
73        try {
74            socket = server.accept();
75        } catch (IOException e) {
76            System.out.println("Class Server died: " + e.getMessage());
77            serverException = e;
78            return;
79        }
80        try {
81            DataOutputStream out =
82                new DataOutputStream(socket.getOutputStream());
83            try {
84                BufferedReader in =
85                    new BufferedReader(new InputStreamReader(
86                                socket.getInputStream()));
87                // read the request
88                readRequest(in);
89                // retrieve bytecodes
90                byte[] bytecodes = getBytes();
91                // send bytecodes in response (assumes HTTP/1.0 or later)
92                try {
93                    out.writeBytes("HTTP/1.0 200 OK\r\n");
94                    out.writeBytes("Content-Length: " + bytecodes.length +
95                                   "\r\n");
96                    out.writeBytes("Content-Type: text/html\r\n\r\n");
97                    out.write(bytecodes);
98                    out.flush();
99                } catch (IOException ie) {
100                    serverException = ie;
101                    return;
102                }
103
104            } catch (Exception e) {
105                // write out error response
106                out.writeBytes("HTTP/1.0 400 " + e.getMessage() + "\r\n");
107                out.writeBytes("Content-Type: text/html\r\n\r\n");
108                out.flush();
109            }
110
111        } catch (IOException ex) {
112            System.out.println("Server: Error writing response: "
113                                 + ex.getMessage());
114            serverException = ex;
115
116        } finally {
117            try {
118                socket.close();
119            } catch (IOException e) {
120                serverException = e;
121            }
122        }
123    }
124
125    /**
126     * Create a new thread to listen.
127     */
128    private void newListener()
129    {
130        (new Thread(this)).start();
131    }
132
133    /**
134     * read the response, don't care for the syntax of the request-line
135     */
136    private static void readRequest(BufferedReader in)
137        throws IOException
138    {
139        String line = null;
140        do {
141            line = in.readLine();
142            System.out.println("Server recieved: " + line);
143        } while ((line.length() != 0) &&
144                (line.charAt(0) != '\r') && (line.charAt(0) != '\n'));
145
146        // read the last line to clear the POST input buffer
147        // would be best to make sure all bytes are read, maybe later.
148        line = in.readLine();
149        System.out.println("Command received: " + line);
150
151        System.out.println("");
152    }
153}
154