2008-11-14 16:38:36 +00:00
|
|
|
package zutil.network.http;
|
|
|
|
|
|
|
|
|
|
import java.io.OutputStream;
|
|
|
|
|
import java.io.PrintStream;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This PrintStream is written for HTTP use
|
|
|
|
|
* It has buffer capabilities and cookie management.
|
|
|
|
|
*
|
|
|
|
|
* @author Ziver
|
|
|
|
|
*
|
|
|
|
|
*/
|
2009-05-17 18:46:05 +00:00
|
|
|
public class HttpPrintStream extends PrintStream{
|
|
|
|
|
private Integer status_code;
|
|
|
|
|
private HashMap<String, String> header;
|
2008-11-14 16:38:36 +00:00
|
|
|
private HashMap<String, String> cookie;
|
|
|
|
|
private StringBuffer buffer;
|
|
|
|
|
private boolean buffer_enabled;
|
|
|
|
|
|
|
|
|
|
public HttpPrintStream(OutputStream out) {
|
|
|
|
|
super(out);
|
|
|
|
|
|
2009-05-17 18:46:05 +00:00
|
|
|
status_code = 0;
|
|
|
|
|
header = new HashMap<String, String>();
|
2008-11-14 16:38:36 +00:00
|
|
|
cookie = new HashMap<String, String>();
|
|
|
|
|
buffer = new StringBuffer();
|
|
|
|
|
buffer_enabled = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Enable the buffering capability of the PrintStream.
|
|
|
|
|
* Nothing will be sent to the client when buffering
|
2009-05-17 18:46:05 +00:00
|
|
|
* is enabled until you close or flush the stream.
|
|
|
|
|
* This function will flush the stream if buffering is
|
|
|
|
|
* disabled.
|
2008-11-14 16:38:36 +00:00
|
|
|
*
|
|
|
|
|
* @param b
|
|
|
|
|
*/
|
|
|
|
|
public void enableBuffering(boolean b){
|
|
|
|
|
buffer_enabled = b;
|
2009-05-17 18:46:05 +00:00
|
|
|
if(!buffer_enabled) flush();
|
2008-11-14 16:38:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Adds a cookie that will be sent to the client
|
|
|
|
|
*
|
2009-02-26 17:10:57 +00:00
|
|
|
* @param key is the name of the cookie
|
|
|
|
|
* @param value is the value of the cookie
|
2008-11-14 16:38:36 +00:00
|
|
|
* @throws Exception Throws exception if the header has already been sent
|
|
|
|
|
*/
|
|
|
|
|
public void setCookie(String key, String value) throws Exception{
|
|
|
|
|
if(cookie == null)
|
|
|
|
|
throw new Exception("Header already sent!!!");
|
|
|
|
|
cookie.put(key, value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2009-05-17 18:46:05 +00:00
|
|
|
* Adds an header value
|
2008-11-14 16:38:36 +00:00
|
|
|
*
|
2009-05-17 18:46:05 +00:00
|
|
|
* @param key is the header name
|
|
|
|
|
* @param value is the value of the header
|
2008-11-14 16:38:36 +00:00
|
|
|
* @throws Exception Throws exception if the header has already been sent
|
|
|
|
|
*/
|
2009-05-17 18:46:05 +00:00
|
|
|
public void setHeader(String key, String value) throws Exception{
|
|
|
|
|
if(header == null)
|
2008-11-14 16:38:36 +00:00
|
|
|
throw new Exception("Header already sent!!!");
|
2009-05-17 18:46:05 +00:00
|
|
|
header.put(key, value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sets the return status code
|
|
|
|
|
*
|
|
|
|
|
* @param code the code from 100 up to 599
|
|
|
|
|
* @throws Exception Throws exception if the header has already been sent
|
|
|
|
|
*/
|
|
|
|
|
public void setStatusCode(int code) throws Exception{
|
|
|
|
|
if(status_code == null)
|
|
|
|
|
throw new Exception("Header already sent!!!");
|
|
|
|
|
status_code = code;
|
2008-11-14 16:38:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2009-02-26 17:10:57 +00:00
|
|
|
* Prints with a new line
|
2008-11-14 16:38:36 +00:00
|
|
|
*/
|
|
|
|
|
public void println(String s){
|
|
|
|
|
printOrBuffer(s+"\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2009-04-16 20:51:15 +00:00
|
|
|
* Prints an string
|
2008-11-14 16:38:36 +00:00
|
|
|
*/
|
|
|
|
|
public void print(String s){
|
2009-04-16 20:51:15 +00:00
|
|
|
printOrBuffer(s);
|
2008-11-14 16:38:36 +00:00
|
|
|
}
|
2009-05-17 18:46:05 +00:00
|
|
|
|
2008-11-14 16:38:36 +00:00
|
|
|
/**
|
|
|
|
|
* prints to all
|
|
|
|
|
*/
|
2009-04-16 20:51:15 +00:00
|
|
|
private void printOrBuffer(String s){
|
2008-11-14 16:38:36 +00:00
|
|
|
if(buffer_enabled){
|
|
|
|
|
buffer.append(s);
|
|
|
|
|
}
|
|
|
|
|
else{
|
2009-05-17 18:46:05 +00:00
|
|
|
if(status_code != null){
|
|
|
|
|
super.print("HTTP/1.0 "+status_code+" "+getStatusString(status_code));
|
|
|
|
|
super.println();
|
|
|
|
|
status_code = null;
|
|
|
|
|
}
|
|
|
|
|
if(header != null){
|
|
|
|
|
for(String key : header.keySet()){
|
|
|
|
|
super.print(key+": "+header.get(key));
|
|
|
|
|
super.println();
|
|
|
|
|
}
|
|
|
|
|
header = null;
|
|
|
|
|
}
|
2008-11-14 16:38:36 +00:00
|
|
|
if(cookie != null){
|
|
|
|
|
for(String key : cookie.keySet()){
|
2009-05-17 18:46:05 +00:00
|
|
|
super.print("Set-Cookie: "+key+"="+cookie.get(key)+";");
|
|
|
|
|
super.println();
|
2008-11-14 16:38:36 +00:00
|
|
|
}
|
2009-05-17 18:46:05 +00:00
|
|
|
super.println();
|
2008-11-14 16:38:36 +00:00
|
|
|
cookie = null;
|
|
|
|
|
}
|
|
|
|
|
super.print(s);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2009-02-26 17:10:57 +00:00
|
|
|
* Sends out the buffer and clears it
|
2008-11-14 16:38:36 +00:00
|
|
|
*/
|
|
|
|
|
public void flush(){
|
|
|
|
|
if(buffer_enabled){
|
|
|
|
|
buffer_enabled = false;
|
|
|
|
|
printOrBuffer(buffer.toString());
|
|
|
|
|
buffer.delete(0, buffer.length());
|
|
|
|
|
buffer_enabled = true;
|
2009-05-17 18:46:05 +00:00
|
|
|
}
|
|
|
|
|
else if(status_code != null || header != null || cookie != null){
|
|
|
|
|
printOrBuffer("");
|
|
|
|
|
}
|
2008-11-14 16:38:36 +00:00
|
|
|
super.flush();
|
|
|
|
|
}
|
2009-05-17 18:46:05 +00:00
|
|
|
|
2008-11-14 16:38:36 +00:00
|
|
|
public void close(){
|
|
|
|
|
flush();
|
|
|
|
|
super.close();
|
|
|
|
|
}
|
2009-05-17 18:46:05 +00:00
|
|
|
|
2008-11-14 16:38:36 +00:00
|
|
|
public void println(){ println("");}
|
2009-04-16 20:51:15 +00:00
|
|
|
public void println(boolean x){ println(String.valueOf(x));}
|
|
|
|
|
public void println(char x){ println(String.valueOf(x));}
|
2008-11-14 16:38:36 +00:00
|
|
|
public void println(char[] x){ println(new String(x));}
|
2009-04-16 20:51:15 +00:00
|
|
|
public void println(double x){ println(String.valueOf(x));}
|
|
|
|
|
public void println(float x){ println(String.valueOf(x));}
|
|
|
|
|
public void println(int x){ println(String.valueOf(x));}
|
|
|
|
|
public void println(long x){ println(String.valueOf(x));}
|
|
|
|
|
public void println(Object x){ println(String.valueOf(x));}
|
2009-05-17 18:46:05 +00:00
|
|
|
|
2009-04-16 20:51:15 +00:00
|
|
|
public void print(boolean x){ printOrBuffer(String.valueOf(x));}
|
|
|
|
|
public void print(char x){ printOrBuffer(String.valueOf(x));}
|
2008-11-14 16:38:36 +00:00
|
|
|
public void print(char[] x){ printOrBuffer(new String(x));}
|
2009-04-16 20:51:15 +00:00
|
|
|
public void print(double x){ printOrBuffer(String.valueOf(x));}
|
|
|
|
|
public void print(float x){ printOrBuffer(String.valueOf(x));}
|
|
|
|
|
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));}
|
2009-05-17 18:46:05 +00:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
public void write(int b) { print((char)b);}
|
|
|
|
|
public void write(byte buf[], int off, int len){
|
|
|
|
|
print(new String(buf, off, len));}
|
|
|
|
|
*/
|
|
|
|
|
private String getStatusString(int type){
|
|
|
|
|
switch(type){
|
|
|
|
|
case 100: return "Continue";
|
|
|
|
|
case 200: return "OK";
|
|
|
|
|
case 301: return "Moved Permanently";
|
|
|
|
|
case 307: return "Temporary Redirect";
|
|
|
|
|
case 400: return "Bad Request";
|
|
|
|
|
case 401: return "Unauthorized";
|
|
|
|
|
case 403: return "Forbidden";
|
|
|
|
|
case 404: return "Not Found";
|
|
|
|
|
case 500: return "Internal Server Error";
|
|
|
|
|
case 501: return "Not Implemented";
|
|
|
|
|
default: return "";
|
|
|
|
|
}
|
|
|
|
|
}
|
2008-11-14 16:38:36 +00:00
|
|
|
}
|