001: /*
002: * This file is part of the QuickServer library
003: * Copyright (C) 2003-2005 QuickServer.org
004: *
005: * Use, modification, copying and distribution of this software is subject to
006: * the terms and conditions of the GNU Lesser General Public License.
007: * You should have received a copy of the GNU LGP License along with this
008: * library; if not, you can download a copy from <http://www.quickserver.org/>.
009: *
010: * For questions, suggestions, bug-reports, enhancement-requests etc.
011: * visit http://www.quickserver.org
012: *
013: */
014:
015: package filesrv;
016:
017: import java.net.*;
018: import java.io.*;
019: import java.util.*;
020: import org.quickserver.net.server.*;
021: import java.util.logging.*;
022: import org.quickserver.util.MyString;
023:
024: /**
025: * FileServer Example
026: * @author Akshathkumar Shetty
027: */
028: public class CommandHandler implements ClientEventHandler,
029: ClientCommandHandler, ClientWriteHandler {
030: private static Logger logger = Logger
031: .getLogger(CommandHandler.class.getName());
032:
033: public void gotConnected(ClientHandler handler)
034: throws SocketTimeoutException, IOException {
035: logger.fine("Connection opened: "
036: + handler.getSocket().getInetAddress());
037: //handler.setDataMode(DataMode.BYTE, DataType.OUT); //pick from xml
038: }
039:
040: public void lostConnection(ClientHandler handler)
041: throws IOException {
042: cleanByteBuffer(handler);
043: logger.fine("Connection lost: "
044: + handler.getSocket().getInetAddress());
045: }
046:
047: public void closingConnection(ClientHandler handler)
048: throws IOException {
049: cleanByteBuffer(handler);
050: logger.fine("Connection closed: "
051: + handler.getSocket().getInetAddress());
052: }
053:
054: private void cleanByteBuffer(ClientHandler handler) {
055: Data data = (Data) handler.getClientData();
056: data.cleanPooledByteBuffer(handler.getServer());
057: }
058:
059: public void handleCommand(ClientHandler handler, String command)
060: throws SocketTimeoutException, IOException {
061:
062: Data data = (Data) handler.getClientData();
063: try {
064: if (data.isHeaderReady() == false) {
065: if (command.startsWith("GET /")) {
066: data.initHeader(command);
067: } else {
068: serveBadRequest(handler, BAD_REQUEST);
069: handler.closeConnection();
070: }
071: return;
072: }
073:
074: if (data.addHeader(command) == false) {
075: return;
076: }
077:
078: if (data.isDirList()) {
079: listDir(handler, data);
080: } else {
081: if (handler.getServer().getConfig().getServerMode()
082: .getBlocking()) {
083: data.sendFileBlocking(handler);
084: } else {
085: data.sendFileNonBlocking(handler);
086: }
087: }
088: } catch (BadRequestException e) {
089: logger.fine("BadRequestException : " + e);
090: serveBadRequest(handler, ERROR_HEADER + "Bad Req: "
091: + e.getMessage() + ERROR_FOOTER, "400 Bad Request");
092: handler.closeConnection();
093: } catch (Exception e) {
094: logger.info("Error processing : "
095: + MyString.getStackTrace(e));
096: try {
097: serveBadRequest(handler, ERROR_HEADER + "Bad Req: "
098: + e.getMessage() + ERROR_FOOTER);
099: } catch (Exception err) {
100: logger.warning("Error processing error: " + err);
101: }
102: handler.closeConnection();
103: }
104: }
105:
106: private void listDir(ClientHandler handler, Data data)
107: throws IOException {
108: String content = data.getDirList();
109:
110: StringBuffer sb = new StringBuffer();
111: sb.append("HTTP/1.1 200 OK\r\n");
112: sb.append("Server: ").append(handler.getServer().getName())
113: .append("\r\n");
114: sb.append("Content-Type: text/html").append("\r\n");
115: ;
116: sb.append("Content-Length: " + content.length()).append("\r\n");
117: ;
118: sb.append("\r\n");
119: sb.append(content);
120:
121: if (handler.getServer().getConfig().getServerMode()
122: .getBlocking()) {
123: handler.sendClientBytes(sb.toString());
124: handler.closeConnection();
125: } else {
126: logger.fine("Will Send: \n" + sb.toString());
127: data.makeNonBlockingWrite(handler,
128: sb.toString().getBytes(), false,
129: "Sending HTTP header with dir list.", true);
130: }
131: }
132:
133: private void serveBadRequest(ClientHandler handler, String error)
134: throws IOException {
135: serveBadRequest(handler, error, "500 Internal Server Error");
136: }
137:
138: private void serveBadRequest(ClientHandler handler, String error,
139: String msg) throws IOException {
140: StringBuffer sb = new StringBuffer();
141:
142: sb.append("HTTP/1.1 ").append(msg).append("\r\n");
143: sb.append("Server: ").append(handler.getServer().getName())
144: .append("\r\n");
145: sb.append("Content-Type: text/html").append("\r\n");
146: ;
147: sb.append("Content-Length: " + error.length()).append("\r\n");
148: ;
149: sb.append("\r\n");
150: sb.append(error);
151: handler.sendClientBytes(sb.toString());
152: }
153:
154: public void handleWrite(ClientHandler handler) throws IOException {
155: handler.isConnected();
156: Data data = (Data) handler.getClientData();
157: try {
158: data.writeData(handler);
159: } catch (Exception e) {
160: logger.fine("Error processing: " + e);
161: if (data.getWroteFileHttpHeader() == false) {
162: try {
163: serveBadRequest(handler, ERROR_HEADER
164: + "Error processing: " + e.getMessage()
165: + ERROR_FOOTER);
166: } catch (Exception err) {
167: logger.warning("Error processing error: " + err);
168: }
169: }
170: handler.closeConnection();
171: }
172:
173: }
174:
175: //-- ERRORS--
176: private static final String ERROR_HEADER = "<html>\r\n<head>\r\n<title>HFtp Server - Error</title>\r\n</head>\r\n"
177: + "<body>"
178: + "<H3>HFtp Server - Error</H3><hr/><font color=\"red\">";
179: private static final String ERROR_FOOTER = "</font><hr/>\r\n</body>\r\n</html>";
180: private static final String BAD_REQUEST = ERROR_HEADER
181: + "HTTP GET requests are only supported!" + ERROR_FOOTER;
182:
183: }
|