KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > psibt > framework > net > RootRequestHandler


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

16 package com.psibt.framework.net;
17
18 import java.io.*;
19 import java.net.*;
20 import java.util.*;
21
22 /**
23  * This class implements a RequestHandler for the root path "/" in the PluggableHTTPServer.
24  * A simple HTML message will be replied to the client.
25  *
26  * @author <a HREF="mailto:V.Mentzner@psi-bt.de">Volker Mentzner</a>
27  */

28 public class RootRequestHandler implements HTTPRequestHandler {
29
30   private String JavaDoc title;
31   private String JavaDoc description;
32   private String JavaDoc handledPath;
33   private String JavaDoc ReplyType = "Content-type: text/html\r\n\r\n";
34   private String JavaDoc ReplyHTML = "<HTML><HEAD><TITLE>Root</TITLE></HEAD>\r\n"
35                            + "<BODY><H1>Root</H1>\r\n"
36                            + "</BODY></HTML>\r\n";
37
38  /**
39    * Creates a new RootRequestHandler object
40    */

41   public RootRequestHandler() {
42     this.setTitle("root page");
43     this.setDescription("root page");
44     this.setHandledPath("/");
45   }
46
47   /**
48    * Gets the content type of the reply HTTP message
49    *
50    * @return content type as String
51    */

52   public String JavaDoc getReplyType() {
53     return this.ReplyType;
54   }
55
56   /**
57    * Sets the content type of the reply HTTP message
58    *
59    * @param ReplyType - content type as String
60    */

61   public void setReplyType(String JavaDoc ReplyType) {
62     this.ReplyType = ReplyType;
63   }
64
65   /**
66    * Gets the HTML data of the reply HTTP message
67    *
68    * @return HTML message as String
69    */

70   public String JavaDoc getReplyHTML() {
71     return this.ReplyHTML;
72   }
73
74   /**
75    * Sets the HTML data of the reply HTTP message
76    *
77    * @param ReplyHTML - HTML message as String
78    */

79   public void setReplyHTML(String JavaDoc ReplyHTML) {
80     this.ReplyHTML = ReplyHTML;
81   }
82
83  /**
84    * Gets the title for html page
85    */

86   public String JavaDoc getTitle() {
87     return this.title;
88   }
89
90  /**
91    * Sets the title for html page
92    */

93   public void setTitle(String JavaDoc title) {
94     this.title = title;
95   }
96
97  /**
98    * Gets the description for html page
99    */

100   public String JavaDoc getDescription() {
101     return this.description;
102   }
103
104  /**
105    * Sets the description for html page
106    */

107   public void setDescription(String JavaDoc description) {
108     this.description = description;
109   }
110
111   /**
112    * Gets the server path
113    *
114    * @return the server path
115    */

116   public String JavaDoc getHandledPath() {
117     return this.handledPath;
118   }
119
120   /**
121    * Sets the server path
122    *
123    * @param path - the server path
124    */

125   public void setHandledPath(String JavaDoc path) {
126     this.handledPath = path;
127   }
128
129  /**
130    * Handles the given request and writes the reply to the given out-stream.
131    *
132    * @param request - client browser request
133    * @param out - Out stream for sending data to client browser
134    * @return if the request was handled by this handler : true, else : false
135    */

136   public boolean handleRequest(String JavaDoc request, Writer out) {
137     String JavaDoc path = "";
138     String JavaDoc query = null;
139     try {
140       URL url = new URL("http://localhost"+request);
141       path = url.getPath();
142       query = url.getPath();
143       if (path.equals(handledPath) == false) {
144         return false;
145       }
146
147       out.write("HTTP/1.0 200 OK\r\n");
148       if (ReplyType != null)
149         out.write(ReplyType);
150       if (ReplyHTML != null)
151         out.write(ReplyHTML);
152       out.flush();
153       return true;
154     } catch (Exception JavaDoc ex) {
155       return false;
156     }
157   }
158 }
Popular Tags