1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package com.psibt.framework.net;
18
19 import java.io;
20 import java.net;
21 import java.util;
22
23 /**
24 * This class implements a RequestHandler for the root path "/" in the PluggableHTTPServer.
25 * A simple HTML message will be replied to the client.
26 *
27 * @author <a HREF="mailto:V.Mentzner@psi-bt.de">Volker Mentzner</a>
28 */
29 public class RootRequestHandler implements HTTPRequestHandler {
30
31 private String title;
32 private String description;
33 private String handledPath;
34 private String ReplyType = "Content-type: text/html\r\n\r\n";
35 private String ReplyHTML = "<HTML><HEAD><TITLE>Root</TITLE></HEAD>\r\n"
36 + "<BODY><H1>Root</H1>\r\n"
37 + "</BODY></HTML>\r\n";
38
39 /**
40 * Creates a new RootRequestHandler object
41 */
42 public RootRequestHandler() {
43 this.setTitle("root page");
44 this.setDescription("root page");
45 this.setHandledPath("/");
46 }
47
48 /**
49 * Gets the content type of the reply HTTP message
50 *
51 * @return content type as String
52 */
53 public String getReplyType() {
54 return this.ReplyType;
55 }
56
57 /**
58 * Sets the content type of the reply HTTP message
59 *
60 * @param ReplyType - content type as String
61 */
62 public void setReplyType(String ReplyType) {
63 this.ReplyType = ReplyType;
64 }
65
66 /**
67 * Gets the HTML data of the reply HTTP message
68 *
69 * @return HTML message as String
70 */
71 public String getReplyHTML() {
72 return this.ReplyHTML;
73 }
74
75 /**
76 * Sets the HTML data of the reply HTTP message
77 *
78 * @param ReplyHTML - HTML message as String
79 */
80 public void setReplyHTML(String ReplyHTML) {
81 this.ReplyHTML = ReplyHTML;
82 }
83
84 /**
85 * Gets the title for html page
86 */
87 public String getTitle() {
88 return this.title;
89 }
90
91 /**
92 * Sets the title for html page
93 */
94 public void setTitle(String title) {
95 this.title = title;
96 }
97
98 /**
99 * Gets the description for html page
100 */
101 public String getDescription() {
102 return this.description;
103 }
104
105 /**
106 * Sets the description for html page
107 */
108 public void setDescription(String description) {
109 this.description = description;
110 }
111
112 /**
113 * Gets the server path
114 *
115 * @return the server path
116 */
117 public String getHandledPath() {
118 return this.handledPath;
119 }
120
121 /**
122 * Sets the server path
123 *
124 * @param path - the server path
125 */
126 public void setHandledPath(String path) {
127 this.handledPath = path;
128 }
129
130 /**
131 * Handles the given request and writes the reply to the given out-stream.
132 *
133 * @param request - client browser request
134 * @param out - Out stream for sending data to client browser
135 * @return if the request was handled by this handler : true, else : false
136 */
137 public boolean handleRequest(String request, Writer out) {
138 String path = "";
139 String query = null;
140 try {
141 URL url = new URL("http://localhost"+request);
142 path = url.getPath();
143 query = url.getPath();
144 if (path.equals(handledPath) == false) {
145 return false;
146 }
147
148 out.write("HTTP/1.0 200 OK\r\n");
149 if (ReplyType != null)
150 out.write(ReplyType);
151 if (ReplyHTML != null)
152 out.write(ReplyHTML);
153 out.flush();
154 return true;
155 } catch (Exception ex) {
156 return false;
157 }
158 }
159 }