0N/A/*
2362N/A * Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.
0N/A *
0N/A * Redistribution and use in source and binary forms, with or without
0N/A * modification, are permitted provided that the following conditions
0N/A * are met:
0N/A *
0N/A * - Redistributions of source code must retain the above copyright
0N/A * notice, this list of conditions and the following disclaimer.
0N/A *
0N/A * - Redistributions in binary form must reproduce the above copyright
0N/A * notice, this list of conditions and the following disclaimer in the
0N/A * documentation and/or other materials provided with the distribution.
0N/A *
2362N/A * - Neither the name of Oracle nor the names of its
0N/A * contributors may be used to endorse or promote products derived
0N/A * from this software without specific prior written permission.
0N/A *
0N/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
0N/A * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
0N/A * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
0N/A * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
0N/A * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
0N/A * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
0N/A * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
0N/A * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
0N/A * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
0N/A * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
0N/A * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0N/A */
0N/A
4378N/A/*
4378N/A * This source code is provided to illustrate the usage of a given feature
4378N/A * or technique and has been deliberately simplified. Additional steps
4378N/A * required for a production-quality application, such as security checks,
4378N/A * input validation and proper error handling, might not be present in
4378N/A * this sample code.
4378N/A */
4378N/A
4378N/A
0N/Aimport java.io.*;
0N/Aimport java.nio.*;
0N/A
0N/A/**
0N/A * Primary driver class used by blocking Servers to receive,
0N/A * prepare, send, and shutdown requests.
0N/A *
0N/A * @author Mark Reinhold
0N/A * @author Brad R. Wetmore
0N/A */
0N/Aclass RequestServicer implements Runnable {
0N/A
0N/A private ChannelIO cio;
0N/A
0N/A private static int created = 0;
0N/A
0N/A RequestServicer(ChannelIO cio) {
0N/A this.cio = cio;
0N/A
0N/A // Simple heartbeat to let user know we're alive.
0N/A synchronized (RequestServicer.class) {
0N/A created++;
0N/A if ((created % 50) == 0) {
0N/A System.out.println(".");
0N/A created = 0;
0N/A } else {
0N/A System.out.print(".");
0N/A }
0N/A }
0N/A }
0N/A
0N/A private void service() throws IOException {
0N/A Reply rp = null;
0N/A try {
0N/A ByteBuffer rbb = receive(); // Receive
0N/A Request rq = null;
0N/A try { // Parse
0N/A rq = Request.parse(rbb);
0N/A } catch (MalformedRequestException x) {
0N/A rp = new Reply(Reply.Code.BAD_REQUEST,
0N/A new StringContent(x));
0N/A }
0N/A if (rp == null) rp = build(rq); // Build
0N/A do {} while (rp.send(cio)); // Send
0N/A do {} while (!cio.shutdown());
0N/A cio.close();
0N/A rp.release();
0N/A } catch (IOException x) {
0N/A String m = x.getMessage();
0N/A if (!m.equals("Broken pipe") &&
0N/A !m.equals("Connection reset by peer")) {
0N/A System.err.println("RequestHandler: " + x.toString());
0N/A }
0N/A
0N/A try {
0N/A /*
0N/A * We had a failure here, so we'll try to be nice
0N/A * before closing down and send off a close_notify,
0N/A * but if we can't get the message off with one try,
0N/A * we'll just shutdown.
0N/A */
0N/A cio.shutdown();
0N/A } catch (IOException e) {
0N/A // ignore
0N/A }
0N/A
0N/A cio.close();
0N/A if (rp != null) {
0N/A rp.release();
0N/A }
0N/A }
0N/A }
0N/A
0N/A public void run() {
0N/A try {
0N/A service();
0N/A } catch (IOException x) {
0N/A x.printStackTrace();
0N/A }
0N/A }
0N/A
0N/A ByteBuffer receive() throws IOException {
0N/A
0N/A do {} while (!cio.doHandshake());
0N/A
0N/A for (;;) {
0N/A int read = cio.read();
0N/A ByteBuffer bb = cio.getReadBuf();
0N/A if ((read < 0) || (Request.isComplete(bb))) {
0N/A bb.flip();
0N/A return bb;
0N/A }
0N/A }
0N/A }
0N/A
0N/A Reply build(Request rq) throws IOException {
0N/A
0N/A Reply rp = null;
0N/A Request.Action action = rq.action();
0N/A if ((action != Request.Action.GET) &&
0N/A (action != Request.Action.HEAD))
0N/A rp = new Reply(Reply.Code.METHOD_NOT_ALLOWED,
0N/A new StringContent(rq.toString()));
0N/A else
0N/A rp = new Reply(Reply.Code.OK,
0N/A new FileContent(rq.uri()), action);
0N/A try {
0N/A rp.prepare();
0N/A } catch (IOException x) {
0N/A rp.release();
0N/A rp = new Reply(Reply.Code.NOT_FOUND,
0N/A new StringContent(x));
0N/A rp.prepare();
0N/A }
0N/A return rp;
0N/A }
0N/A}