KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > tutorial > Reply


1 /*
2  * Copyright 2004 Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package tutorial;
19
20 import java.io.DataOutputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.OutputStream JavaDoc;
23 import java.util.Date JavaDoc;
24
25 /**
26  * Static-only class to send a simple HTTP reply.
27  *
28  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
29  */

30 public class Reply {
31   /**
32    * Definition of a carriage-return/line-feed entity.
33    */

34   public final static String JavaDoc CRLF = "\r\n";
35   /**
36    * The HTTP RFC specification version supported by this web server.
37    */

38   public final static String JavaDoc HTTP_VERSION = "HTTP/1.0";
39   /**
40    * The HTTP RFC specification version supported by this web server.
41    */

42   public final static String JavaDoc WEB_SERVER_NAME = "Merlin Simple Server/1.0";
43   /**
44    * HTTP 200 OK
45    */

46   public final static String JavaDoc HTTP_200 = HTTP_VERSION + " 200 OK" + CRLF;
47   /**
48    * HTTP 400 Bad Request
49    */

50   public final static String JavaDoc HTTP_400 = HTTP_VERSION + " 400 Bad Request" +
51       CRLF;
52   /**
53    * HTTP 403 Forbidden
54    */

55   public final static String JavaDoc HTTP_403 = HTTP_VERSION + " 404 Forbidden" + CRLF;
56   /**
57    * HTTP 404 Not Found
58    */

59   public final static String JavaDoc HTTP_404 = HTTP_VERSION + " 404 Not Found" + CRLF;
60   /**
61    * HTTP 405 Method Not Allowed
62    */

63   public final static String JavaDoc HTTP_405 = HTTP_VERSION +
64       " 405 Method Not Allowed" + CRLF;
65
66   /**
67    * Private constructor to enforce static-only class.
68    */

69   private Reply() {
70   }
71
72   /**
73    *
74    * @param os
75    * @param type
76    * @param msg
77    * @throws IOException
78    */

79   public static void sendHttpReply(OutputStream JavaDoc os, String JavaDoc type, String JavaDoc msg) throws
80       IOException JavaDoc {
81
82     DataOutputStream JavaDoc dos = new DataOutputStream JavaDoc(os);
83     dos.writeBytes(type);
84     dos.writeBytes("Connection: close" + CRLF);
85     dos.writeBytes("Server: " + WEB_SERVER_NAME + CRLF);
86     dos.writeBytes("Date: " + RfcDateFormat.format(new Date JavaDoc()) + CRLF);
87     dos.writeBytes("Content-Type: text/html; charset=iso-8859-1" + CRLF);
88     //dos.writeBytes("Content-Type: text/html" + CRLF);
89
dos.writeBytes("Content-Length: " + msg.length() + CRLF);
90     if (type.equals(HTTP_405)) {
91       dos.writeBytes("Allow: GET" + CRLF);
92     }
93     dos.writeBytes(CRLF);
94
95     String JavaDoc s = new String JavaDoc("<html><body><h1>" + msg + "</h1></body></html>");
96     //dos.writeBytes(s);
97
dos.writeUTF(s);
98     dos.flush();
99     dos.close();
100   }
101 }
102
Popular Tags