Abstracted the HTTP server with TCP Network classes and added an SSDP service

This commit is contained in:
Ziver Koc 2010-01-31 18:10:00 +00:00
parent b3ad292ff9
commit 45f514fc27
25 changed files with 1645 additions and 688 deletions

View file

@ -11,17 +11,51 @@ import java.util.HashMap;
* @author Ziver
*
*/
public class HttpPrintStream extends PrintStream{
private Integer status_code;
private HashMap<String, String> header;
private HashMap<String, String> cookie;
private StringBuffer buffer;
private boolean buffer_enabled;
public class HttpPrintStream extends PrintStream{
// Defines the type of message
public enum HTTPMessageType{
REQUEST,
RESPONSE
}
// This defines the type of message that will be generated
private HTTPMessageType message_type;
// The status code of the message, ONLY for response
private Integer res_status_code;
// The request type of the message ONLY for request
private String req_type;
// The requesting url ONLY for request
private String req_url;
// An Map of all the header values
private HashMap<String, String> header;
// An Map of all the cookies
private HashMap<String, String> cookie;
// The buffered header
private StringBuffer buffer;
// If the header buffering is enabled
private boolean buffer_enabled;
/**
* Creates an new instance of HttpPrintStream with
* message type of RESPONSE and buffering disabled.
*
* @param out is the OutputStream to send the message
*/
public HttpPrintStream(OutputStream out) {
this( out, HTTPMessageType.RESPONSE );
}
/**
* Creates an new instance of HttpPrintStream with
* message type buffering disabled.
*
* @param out is the OutputStream to send the message
* @param type is the type of message
*/
public HttpPrintStream(OutputStream out, HTTPMessageType type) {
super(out);
status_code = 0;
this.message_type = type;
res_status_code = 0;
header = new HashMap<String, String>();
cookie = new HashMap<String, String>();
buffer = new StringBuffer();
@ -49,9 +83,9 @@ public class HttpPrintStream extends PrintStream{
* @param value is the value of the cookie
* @throws Exception Throws exception if the header has already been sent
*/
public void setCookie(String key, String value) throws Exception{
public void setCookie(String key, String value) throws RuntimeException{
if(cookie == null)
throw new Exception("Header already sent!!!");
throw new RuntimeException("Header already sent!!!");
cookie.put(key, value);
}
@ -62,22 +96,51 @@ public class HttpPrintStream extends PrintStream{
* @param value is the value of the header
* @throws Exception Throws exception if the header has already been sent
*/
public void setHeader(String key, String value) throws Exception{
public void setHeader(String key, String value) throws RuntimeException{
if(header == null)
throw new Exception("Header already sent!!!");
throw new RuntimeException("Header already sent!!!");
header.put(key, value);
}
/**
* Sets the return status code
* Sets the status code of the message, ONLY available in HTTP RESPONSE
*
* @param code the code from 100 up to 599
* @throws Exception Throws exception if the header has already been sent
* @throws RuntimeException if the header has already been sent or the message type is wrong
*/
public void setStatusCode(int code) throws Exception{
if(status_code == null)
throw new Exception("Header already sent!!!");
status_code = code;
public void setStatusCode(int code) throws RuntimeException{
if( res_status_code == null )
throw new RuntimeException("Header already sent!!!");
if( message_type != HTTPMessageType.RESPONSE )
throw new RuntimeException("Status Code is only available in HTTP RESPONSE!!!");
res_status_code = code;
}
/**
* Sets the request type of the message, ONLY available in HTTP REQUEST
*
* @param req_type is the type of the message, e.g. GET, POST...
* @throws RuntimeException if the header has already been sent or the message type is wrong
*/
public void setRequestType(String req_type) throws RuntimeException{
if( req_type == null )
throw new RuntimeException("Header already sent!!!");
if( message_type != HTTPMessageType.REQUEST )
throw new RuntimeException("Request Message Type is only available in HTTP REQUEST!!!");
this.req_type = req_type;
}
/**
* Sets the requesting URL of the message, ONLY available in HTTP REQUEST
*
* @param req_url is the URL
* @throws RuntimeException if the header has already been sent or the message type is wrong
*/
public void setRequestURL(String req_url) throws RuntimeException{
if( req_url == null )
throw new RuntimeException("Header already sent!!!");
if( message_type != HTTPMessageType.REQUEST )
throw new RuntimeException("Request URL is only available in HTTP REQUEST!!!");
this.req_url = req_url;
}
/**
@ -102,10 +165,15 @@ public class HttpPrintStream extends PrintStream{
buffer.append(s);
}
else{
if(status_code != null){
super.print("HTTP/1.0 "+status_code+" "+getStatusString(status_code));
if(res_status_code != null){
if( message_type==HTTPMessageType.REQUEST )
super.print(req_type+" "+req_url+" HTTP/1.1");
else
super.print("HTTP/1.1 "+res_status_code+" "+getStatusString(res_status_code));
super.println();
status_code = null;
res_status_code = null;
req_type = null;
req_url = null;
}
if(header != null){
for(String key : header.keySet()){
@ -115,9 +183,20 @@ public class HttpPrintStream extends PrintStream{
header = null;
}
if(cookie != null){
for(String key : cookie.keySet()){
super.print("Set-Cookie: "+key+"="+cookie.get(key)+";");
super.println();
if( !cookie.isEmpty() ){
if( message_type==HTTPMessageType.REQUEST ){
super.print("Cookie: ");
for(String key : cookie.keySet()){
super.print(key+"="+cookie.get(key)+"; ");
}
super.println();
}
else{
for(String key : cookie.keySet()){
super.print("Set-Cookie: "+key+"="+cookie.get(key)+";");
super.println();
}
}
}
super.println();
cookie = null;
@ -136,7 +215,7 @@ public class HttpPrintStream extends PrintStream{
buffer.delete(0, buffer.length());
buffer_enabled = true;
}
else if(status_code != null || header != null || cookie != null){
else if(res_status_code != null || header != null || cookie != null){
printOrBuffer("");
}
super.flush();
@ -165,7 +244,7 @@ public class HttpPrintStream extends PrintStream{
public void print(int x){ printOrBuffer(String.valueOf(x));}
public void print(long x){ printOrBuffer(String.valueOf(x));}
public void print(Object x){ printOrBuffer(String.valueOf(x));}
/*
public void write(int b) { print((char)b);}
public void write(byte buf[], int off, int len){