HelpConnection.java
| Index Score | ||
|---|---|---|
![]() |
![]() |
org.mrbook.mrpostman.help |
![]() |
![]() |
MrPostman |
View: Reasons, Metrics, Source Code
These are the metrics that contribute to the Enerjy Score for this file, ranked by impact. So the metrics listed at the top influence the score to a greater extent that the metrics listed at the bottom.
/*
* -*- mode: java; c-basic-indent: 4; indent-tabs-mode: nil -*-
* :indentSize=4:noTabs=true:tabSize=4:indentOnTab=true:indentOnEnter=true:mode=java:
* ex: set tabstop=4 expandtab:
*
* MrPostman - webmail <-> email gateway
* Copyright (C) 2002-2003 MrPostman Development Group
* Projectpage: http://mrbook.org/mrpostman/
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* In particular, this implies that users are responsible for
* using MrPostman after reading the terms and conditions given
* by their web-mail provider.
*
* You should have received a copy of the GNU General Public License
* Named LICENSE in the base directory of this distribution,
* if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package org.mrbook.mrpostman.help;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class HelpConnection implements Runnable {
public static final String CVSID = "$Id: HelpConnection.java,v 1.7 2005/07/30 10:29:43 mvlcek Exp $";
private static Logger logger = Logger.getLogger("org.mrbook.mrpostman.help.HelpConnection");
private static Pattern getPat = Pattern.compile("\\AGET\\s(\\S+)\\sHTTP");
private static final int BUFFER_SIZE = 1024;
private static final String ROOT_DOC = "/index.html";
protected Socket client;
protected BufferedReader in;
public HelpConnection(Socket clientSocket) {
client = clientSocket;
try {
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
} catch (IOException e) {
logger.log(Level.SEVERE, "IOException in HelpConnection - ", e);
}
}
public void run() {
try {
String cmd = in.readLine();
if (!cmd.startsWith("GET")) {
try {
client.close(); //we won't be very gracious about this!
} catch (IOException e) {
}
} else {
//Fetch the resource URL and return the data...
logger.log(Level.FINEST, cmd);
Matcher getMatcher = getPat.matcher(cmd);
if (getMatcher.find()) {
String resourceName = getMatcher.group(1);
logger.log(Level.FINEST, resourceName);
writeResource(resourceName, client.getOutputStream());
}
}
client.close();
} catch (IOException ioe) {
}
}
private String createErrorResponse() {
StringBuffer buff = new StringBuffer();
buff.append("HTTP/1.1 404 Resource not found\r\n");
buff.append("Content-Type: text/html; charset=iso-8859-1\r\n\r\n");
buff.append("<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\r\n");
buff.append("<HTML><HEAD><TITLE>404 Resource not found</TITLE>\r\n");
buff.append("</HEAD><BODY><H1>Resource not found</H1>\r\n");
buff.append("</BODY></HTML>\r\n");
return buff.toString();
}
private void writeResource(String resourceName, OutputStream os) {
//Firstly check to see if the resourceName is '/' - we'll substitute this for /index.html
if (resourceName.equals("/")) {
resourceName = ROOT_DOC;
}
//Block the resource types we don't want to return...
int extStart = resourceName.lastIndexOf(".");
if (extStart > -1) {
//check that the index is not of a type we are blocking...
String ext = resourceName.substring(extStart);
logger.log(Level.FINEST, ext);
if (isResourceTypeAllowed(ext)) {
//check if resource exists, if so send header then data
//otherwise send error..
InputStream is = this.getClass().getResourceAsStream(resourceName);
if (is == null) {
sendError(os); //couldn't find the resource
} else {
// locate resource and return...
logger.log(Level.FINEST, "HelpConnection serving " + resourceName);
String header = createSuccessfulResponse(getContentTypeString(ext));
try {
OutputStreamWriter osw = new OutputStreamWriter(os);
osw.write(header, 0, header.length());
osw.flush();
int read = 0;
byte[] tmpBuffer = new byte[BUFFER_SIZE];
while (true) {
read = is.read(tmpBuffer);
if (read > 0) {
os.write(tmpBuffer, 0, read);
} else {
break;
}
}
} catch (IOException oie) {
logger.log(Level.SEVERE, "HelpConnection IOException whilst serving " + resourceName);
}
}
} else {
logger.log(Level.INFO, "Illegal resource type requested - not returning " + resourceName);
sendError(os);
}
} else {
//the resource must have an extension!
logger.log(Level.INFO, "Illegal resource type requested - not returning " + resourceName);
sendError(os);
}
}
private void sendError(OutputStream os) {
try {
PrintWriter out = new PrintWriter(new PrintStream(client.getOutputStream()));
out.print(createErrorResponse());
out.flush();
} catch (Exception e) {
try {
client.close();
} catch (IOException e2) {
}
}
}
private String createSuccessfulResponse(String contentType) {
StringBuffer buff = new StringBuffer();
buff.append("HTTP/1.1 200 OK\r\n");
buff.append("Content-Type: ").append(contentType).append("\r\n\r\n");
return buff.toString();
}
private String getContentTypeString(String extension) {
if (extension.equalsIgnoreCase(".html")) {
return "text/html";
}
if (extension.equalsIgnoreCase(".properties")) {
return "text/text";
}
if (extension.equalsIgnoreCase("jpeg") || extension.equalsIgnoreCase("jpg")) {
return "image/jpeg";
}
if (extension.equalsIgnoreCase("png")) {
return "image/png";
}
if (extension.equalsIgnoreCase("gif")) {
return "image/gif";
}
return "application/octet-stream"; //we'll use this as the unknown
}
private boolean isResourceTypeAllowed(String ext) {
return ext.equalsIgnoreCase(".html") || ext.equalsIgnoreCase(".htm") || ext.equalsIgnoreCase(".jpg")
|| ext.equalsIgnoreCase(".jpeg") || ext.equalsIgnoreCase(".gif") || ext.equalsIgnoreCase(".png")
|| ext.equalsIgnoreCase(".properties");
}
}
The table below shows all metrics for HelpConnection.java.




