/*******************************************************************************
* HelloNzb -- The Binary Usenet Tool
* Copyright (C) 2010-2013 Matthias F. Brandstetter
* https://sourceforge.net/projects/hellonzb/
*
* 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 3 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
package me.mabra.hellonzb.httpserver.nioengine;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.handler.codec.http.HttpMethod;
import org.jboss.netty.handler.codec.http.HttpRequest;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
public class HttpGetHandler
{
private HashMap<String, String> resourceFiles;
private final HttpJythonHandler jythonHandler;
public HttpGetHandler(HttpJythonHandler jython)
{
jythonHandler = jython;
resourceFiles = new HashMap<String, String>();
}
public void addResource(String uri, String path)
{
resourceFiles.put(uri, path);
}
public void messageReceived(HttpServerHandler parentHandler, MessageEvent e, HttpRequest request) throws IOException
{
// evaluate request method (GET, POST, etc.)
final String allowedMethod = "GET";
HttpMethod requestMethod = request.getMethod();
if(!requestMethod.toString().equalsIgnoreCase(allowedMethod))
{
parentHandler.invalidRequestMethod(e, allowedMethod);
return;
}
// get name of file to handle
String page = parentHandler.requestedPage();
if(page == null)
{
parentHandler.redirect(e, parentHandler.homeUrl + "/index.html");
return;
}
if(page.endsWith(".py"))
{
// call Jython/Python handler
jythonHandler.messageReceived(parentHandler, e, request);
return;
}
String path = resourceFiles.get(page);
if(path == null)
{
System.err.println("Can't locate resource file to page " + page);
parentHandler.pageNotFound(e);
return;
}
File resource = new File(path);
if(!resource.isFile() || !resource.canRead())
{
System.err.println("Can't access resource file " + path);
throw new IOException(); // TODO
}
// GET -- ok, set standard response header
String contentType = null;
if(page.endsWith(".html") || page.endsWith(".htm"))
contentType = "text/html";
else if(page.endsWith(".css"))
contentType = "text/css";
else
throw new RuntimeException("Unknown file type detected: " + page);
// prepare data (string) to be sent back to client
StringBuilder responseText = new StringBuilder();
for(String line : readWebResource(path))
responseText.append(line);
// write data back to client
parentHandler.writeResponse(e, contentType, responseText.toString());
}
private ArrayList<String> readWebResource(String filename) throws IOException
{
ArrayList<String> data = new ArrayList<String>();
data.addAll(readFile(filename));
return data;
}
private ArrayList<String> readFile(String filename) throws IOException
{
ArrayList<String> data = new ArrayList<String>();
String line = "";
File file = new File(filename);
if(!file.exists() || !file.canRead())
return null;
FileReader fr = new FileReader(file);
BufferedReader input = new BufferedReader(fr);
do
{
line = input.readLine();
if(line != null)
data.add(line + "\n");
}
while(line != null);
input.close();
fr.close();
return data;
}
}