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/Aimport java.nio.channels.*;
0N/A
0N/A/**
0N/A * Primary driver class used by non-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 RequestHandler implements Handler {
0N/A
0N/A private ChannelIO cio;
0N/A private ByteBuffer rbb = null;
0N/A
0N/A private boolean requestReceived = false;
0N/A private Request request = null;
0N/A private Reply reply = null;
0N/A
0N/A private static int created = 0;
0N/A
0N/A RequestHandler(ChannelIO cio) {
0N/A this.cio = cio;
0N/A
0N/A // Simple heartbeat to let user know we're alive.
0N/A synchronized (RequestHandler.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 // Returns true when request is complete
0N/A // May expand rbb if more room required
0N/A //
0N/A private boolean receive(SelectionKey sk) throws IOException {
0N/A ByteBuffer tmp = null;
0N/A
0N/A if (requestReceived) {
0N/A return true;
0N/A }
0N/A
0N/A if (!cio.doHandshake(sk)) {
0N/A return false;
0N/A }
0N/A
0N/A if ((cio.read() < 0) || Request.isComplete(cio.getReadBuf())) {
0N/A rbb = cio.getReadBuf();
0N/A return (requestReceived = true);
0N/A }
0N/A return false;
0N/A }
0N/A
0N/A // When parse is successfull, saves request and returns true
0N/A //
0N/A private boolean parse() throws IOException {
0N/A try {
0N/A request = Request.parse(rbb);
0N/A return true;
0N/A } catch (MalformedRequestException x) {
0N/A reply = new Reply(Reply.Code.BAD_REQUEST,
0N/A new StringContent(x));
0N/A }
0N/A return false;
0N/A }
0N/A
0N/A // Ensures that reply field is non-null
0N/A //
0N/A private void build() throws IOException {
0N/A Request.Action action = request.action();
0N/A if ((action != Request.Action.GET) &&
0N/A (action != Request.Action.HEAD)) {
0N/A reply = new Reply(Reply.Code.METHOD_NOT_ALLOWED,
0N/A new StringContent(request.toString()));
0N/A }
0N/A reply = new Reply(Reply.Code.OK,
0N/A new FileContent(request.uri()), action);
0N/A }
0N/A
0N/A public void handle(SelectionKey sk) throws IOException {
0N/A try {
0N/A
0N/A if (request == null) {
0N/A if (!receive(sk))
0N/A return;
0N/A rbb.flip();
0N/A if (parse())
0N/A build();
0N/A try {
0N/A reply.prepare();
0N/A } catch (IOException x) {
0N/A reply.release();
0N/A reply = new Reply(Reply.Code.NOT_FOUND,
0N/A new StringContent(x));
0N/A reply.prepare();
0N/A }
0N/A if (send()) {
0N/A // More bytes remain to be written
0N/A sk.interestOps(SelectionKey.OP_WRITE);
0N/A } else {
0N/A // Reply completely written; we're done
0N/A if (cio.shutdown()) {
0N/A cio.close();
0N/A reply.release();
0N/A }
0N/A }
0N/A } else {
0N/A if (!send()) { // Should be rp.send()
0N/A if (cio.shutdown()) {
0N/A cio.close();
0N/A reply.release();
0N/A }
0N/A }
0N/A }
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 (reply != null) {
0N/A reply.release();
0N/A }
0N/A }
0N/A
0N/A }
0N/A
0N/A private boolean send() throws IOException {
0N/A try {
0N/A return reply.send(cio);
0N/A } catch (IOException x) {
0N/A if (x.getMessage().startsWith("Resource temporarily")) {
0N/A System.err.println("## RTA");
0N/A return true;
0N/A }
0N/A throw x;
0N/A }
0N/A }
0N/A}