KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Reply


1 /*
2  * @(#)Reply.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 import java.nio.charset.*;
40
41 /**
42  * An object used for sending Content to the requestor.
43  *
44  * @author Mark Reinhold
45  * @author Brad R. Wetmore
46  * @version 1.3, 05/11/17
47  */

48 class Reply implements Sendable {
49
50     /**
51      * A helper class which define the HTTP response codes
52      */

53     static class Code {
54
55     private int number;
56     private String JavaDoc reason;
57     private Code(int i, String JavaDoc r) { number = i; reason = r; }
58     public String JavaDoc toString() { return number + " " + reason; }
59
60     static Code OK = new Code(200, "OK");
61     static Code BAD_REQUEST = new Code(400, "Bad Request");
62     static Code NOT_FOUND = new Code(404, "Not Found");
63     static Code METHOD_NOT_ALLOWED = new Code(405, "Method Not Allowed");
64
65     }
66
67     private Code code;
68     private Content content;
69     private boolean headersOnly;
70
71     Reply(Code rc, Content c) {
72     this(rc, c, null);
73     }
74
75     Reply(Code rc, Content c, Request.Action head) {
76     code = rc;
77     content = c;
78     headersOnly = (head == Request.Action.HEAD);
79     }
80
81     private static String JavaDoc CRLF = "\r\n";
82     private static Charset ascii = Charset.forName("US-ASCII");
83
84     private ByteBuffer hbb = null;
85
86     private ByteBuffer headers() {
87     CharBuffer cb = CharBuffer.allocate(1024);
88     for (;;) {
89         try {
90         cb.put("HTTP/1.0 ").put(code.toString()).put(CRLF);
91         cb.put("Server: niossl/0.1").put(CRLF);
92         cb.put("Content-type: ").put(content.type()).put(CRLF);
93         cb.put("Content-length: ")
94             .put(Long.toString(content.length())).put(CRLF);
95         cb.put(CRLF);
96         break;
97         } catch (BufferOverflowException x) {
98         assert(cb.capacity() < (1 << 16));
99         cb = CharBuffer.allocate(cb.capacity() * 2);
100         continue;
101         }
102     }
103     cb.flip();
104     return ascii.encode(cb);
105     }
106
107     public void prepare() throws IOException {
108     content.prepare();
109     hbb = headers();
110     }
111
112     public boolean send(ChannelIO cio) throws IOException {
113
114     if (hbb == null)
115         throw new IllegalStateException JavaDoc();
116
117     if (hbb.hasRemaining()) {
118         if (cio.write(hbb) <= 0)
119         return true;
120     }
121
122     if (!headersOnly) {
123         if (content.send(cio))
124         return true;
125     }
126
127     if (!cio.dataFlush())
128         return true;
129
130     return false;
131     }
132
133     public void release() throws IOException {
134     content.release();
135     }
136 }
137
Popular Tags