KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > RequestServicer


1 /*
2  * @(#)RequestServicer.java 1.3 05/11/17
3  *
4  * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * -Redistribution of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  *
12  * -Redistribution in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * Neither the name of Sun Microsystems, Inc. or the names of contributors may
17  * be used to endorse or promote products derived from this software without
18  * specific prior written permission.
19  *
20  * This software is provided "AS IS," without a warranty of any kind. ALL
21  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
22  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
23  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
24  * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
25  * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
26  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
27  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
28  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
29  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
30  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
31  *
32  * You acknowledge that this software is not designed, licensed or intended
33  * for use in the design, construction, operation or maintenance of any
34  * nuclear facility.
35  */

36
37 import java.io.*;
38 import java.nio.*;
39
40 /**
41  * Primary driver class used by blocking Servers to receive,
42  * prepare, send, and shutdown requests.
43  *
44  * @author Mark Reinhold
45  * @author Brad R. Wetmore
46  * @version 1.3, 05/11/17
47  */

48 class RequestServicer implements Runnable JavaDoc {
49
50     private ChannelIO cio;
51
52     private static int created = 0;
53
54     RequestServicer(ChannelIO cio) {
55     this.cio = cio;
56
57     // Simple heartbeat to let user know we're alive.
58
synchronized (RequestServicer.class) {
59         created++;
60         if ((created % 50) == 0) {
61         System.out.println(".");
62         created = 0;
63         } else {
64         System.out.print(".");
65         }
66     }
67     }
68
69     private void service() throws IOException {
70     Reply rp = null;
71     try {
72         ByteBuffer rbb = receive(); // Receive
73
Request rq = null;
74         try { // Parse
75
rq = Request.parse(rbb);
76         } catch (MalformedRequestException x) {
77         rp = new Reply(Reply.Code.BAD_REQUEST,
78                    new StringContent(x));
79         }
80         if (rp == null) rp = build(rq); // Build
81
do {} while (rp.send(cio)); // Send
82
do {} while (!cio.shutdown());
83         cio.close();
84         rp.release();
85     } catch (IOException x) {
86         String JavaDoc m = x.getMessage();
87         if (!m.equals("Broken pipe") &&
88             !m.equals("Connection reset by peer")) {
89         System.err.println("RequestHandler: " + x.toString());
90         }
91
92         try {
93         /*
94          * We had a failure here, so we'll try to be nice
95          * before closing down and send off a close_notify,
96          * but if we can't get the message off with one try,
97          * we'll just shutdown.
98          */

99         cio.shutdown();
100         } catch (IOException e) {
101         // ignore
102
}
103
104         cio.close();
105         if (rp != null) {
106         rp.release();
107         }
108     }
109     }
110
111     public void run() {
112     try {
113         service();
114     } catch (IOException x) {
115         x.printStackTrace();
116     }
117     }
118
119     ByteBuffer receive() throws IOException {
120
121     do {} while (!cio.doHandshake());
122
123     for (;;) {
124         int read = cio.read();
125         ByteBuffer bb = cio.getReadBuf();
126         if ((read < 0) || (Request.isComplete(bb))) {
127         bb.flip();
128         return bb;
129         }
130     }
131     }
132
133     Reply build(Request rq) throws IOException {
134
135     Reply rp = null;
136     Request.Action action = rq.action();
137     if ((action != Request.Action.GET) &&
138         (action != Request.Action.HEAD))
139         rp = new Reply(Reply.Code.METHOD_NOT_ALLOWED,
140                new StringContent(rq.toString()));
141     else
142         rp = new Reply(Reply.Code.OK,
143                new FileContent(rq.uri()), action);
144     try {
145         rp.prepare();
146     } catch (IOException x) {
147         rp.release();
148         rp = new Reply(Reply.Code.NOT_FOUND,
149                new StringContent(x));
150         rp.prepare();
151     }
152     return rp;
153     }
154 }
155
Popular Tags