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
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
public class HttpPrintStream extends PrintStream{
|
|
|
|
|
private HashMap<String, String> cookie;
|
|
|
|
|
private StringBuffer buffer;
|
|
|
|
|
private boolean buffer_enabled;
|
|
|
|
|
|
|
|
|
|
public HttpPrintStream(OutputStream out) {
|
|
|
|
|
super(out);
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
* is enabled until you close or flush the stream.
|
|
|
|
|
*
|
|
|
|
|
* @param b
|
|
|
|
|
*/
|
|
|
|
|
public void enableBuffering(boolean b){
|
|
|
|
|
buffer_enabled = b;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sends the given header directly to the client.
|
|
|
|
|
* No buffering involved.
|
|
|
|
|
*
|
2009-02-26 17:10:57 +00:00
|
|
|
* @param header is the header to send
|
2008-11-14 16:38:36 +00:00
|
|
|
* @throws Exception Throws exception if the header has already been sent
|
|
|
|
|
*/
|
|
|
|
|
public void sendHeader(String header) throws Exception{
|
|
|
|
|
if(cookie == null)
|
|
|
|
|
throw new Exception("Header already sent!!!");
|
2009-04-16 20:51:15 +00:00
|
|
|
super.print(header+"\n");
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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{
|
|
|
|
|
if(cookie != null){
|
|
|
|
|
for(String key : cookie.keySet()){
|
2009-04-16 20:51:15 +00:00
|
|
|
super.print("Set-Cookie: "+key+"="+cookie.get(key)+"; \n");
|
2008-11-14 16:38:36 +00:00
|
|
|
}
|
2009-04-16 20:51:15 +00:00
|
|
|
super.print(" \n");
|
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;
|
|
|
|
|
}
|
|
|
|
|
super.flush();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void close(){
|
|
|
|
|
flush();
|
|
|
|
|
super.close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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));}
|
2008-11-14 16:38:36 +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));}
|
2008-11-14 16:38:36 +00:00
|
|
|
}
|