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