Changed HttpPrintStream to use a internal stream instead of extending one

This commit is contained in:
Ziver Koc 2015-04-12 22:19:59 +00:00
parent a199165756
commit 1684c86a60
13 changed files with 130 additions and 119 deletions

View file

@ -104,10 +104,9 @@ public class HttpClient {
request.setHeader("Content-Length", data);
request.println();
request.print( data );
}
else
request.println("");
request.println();
request.close();
// Response

View file

@ -22,6 +22,7 @@
package zutil.net.http;
import java.io.IOException;
import java.util.Map;
/**
@ -45,5 +46,5 @@ public interface HttpPage{
HttpHeaderParser client_info,
Map<String,Object> session,
Map<String,String> cookie,
Map<String,String> request);
Map<String,String> request) throws IOException;
}

View file

@ -22,8 +22,7 @@
package zutil.net.http;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.*;
import java.util.HashMap;
/**
@ -33,13 +32,15 @@ import java.util.HashMap;
* @author Ziver
*
*/
public class HttpPrintStream extends PrintStream{
public class HttpPrintStream extends OutputStream{
// Defines the type of message
public enum HttpMessageType{
REQUEST,
RESPONSE
}
// The actual output stream
private PrintStream out;
// This defines the type of message that will be generated
private HttpMessageType message_type;
// The status code of the message, ONLY for response
@ -74,14 +75,13 @@ public class HttpPrintStream extends PrintStream{
* @param type is the type of message
*/
public HttpPrintStream(OutputStream out, HttpMessageType type) {
super(out);
this.out = new PrintStream(out);
this.message_type = type;
res_status_code = 0;
headers = new HashMap<String, String>();
cookies = new HashMap<String, String>();
buffer = new StringBuffer();
buffer_enabled = false;
this.res_status_code = 0;
this.headers = new HashMap<String, String>();
this.cookies = new HashMap<String, String>();
this.buffer = new StringBuffer();
this.buffer_enabled = false;
}
/**
@ -93,7 +93,7 @@ public class HttpPrintStream extends PrintStream{
*
* @param b
*/
public void enableBuffering(boolean b){
public void enableBuffering(boolean b) throws IOException {
buffer_enabled = b;
if(!buffer_enabled) flush();
}
@ -103,11 +103,11 @@ public class HttpPrintStream extends PrintStream{
*
* @param key is the name of the cookie
* @param value is the value of the cookie
* @throws Exception Throws exception if the header has already been sent
* @throws IOException Throws exception if the header has already been sent
*/
public void setCookie(String key, String value) throws RuntimeException{
public void setCookie(String key, String value) throws IOException{
if(cookies == null)
throw new RuntimeException("Header already sent!!!");
throw new IOException("Header already sent!");
cookies.put(key, value);
}
@ -116,11 +116,11 @@ public class HttpPrintStream extends PrintStream{
*
* @param key is the header name
* @param value is the value of the header
* @throws Exception Throws exception if the header has already been sent
* @throws IOException Throws exception if the header has already been sent
*/
public void setHeader(String key, String value) throws RuntimeException{
public void setHeader(String key, String value) throws IOException{
if(headers == null)
throw new RuntimeException("Header already sent!!!");
throw new IOException("Header already sent!");
headers.put(key, value);
}
@ -128,13 +128,13 @@ public class HttpPrintStream extends PrintStream{
* Sets the status code of the message, ONLY available in HTTP RESPONSE
*
* @param code the code from 100 up to 599
* @throws RuntimeException if the header has already been sent or the message type is wrong
* @throws IOException if the header has already been sent or the message type is wrong
*/
public void setStatusCode(int code) throws RuntimeException{
public void setStatusCode(int code) throws IOException{
if( res_status_code == null )
throw new RuntimeException("Header already sent!!!");
throw new IOException("Header already sent!");
if( message_type != HttpMessageType.RESPONSE )
throw new RuntimeException("Status Code is only available in HTTP RESPONSE!!!");
throw new IOException("Status Code is only available in HTTP RESPONSE!");
res_status_code = code;
}
@ -142,26 +142,26 @@ public class HttpPrintStream extends PrintStream{
* 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
* @throws IOException if the header has already been sent or the message type is wrong
*/
public void setRequestType(String req_type) throws RuntimeException{
public void setRequestType(String req_type) throws IOException{
if( req_type == null )
throw new RuntimeException("Header already sent!!!");
throw new IOException("Header already sent!");
if( message_type != HttpMessageType.REQUEST )
throw new RuntimeException("Request Message Type is only available in HTTP REQUEST!!!");
throw new IOException("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
* @throws IOException if the header has already been sent or the message type is wrong
*/
public void setRequestURL(String req_url) throws RuntimeException{
public void setRequestURL(String req_url) throws IOException{
if( req_url == null )
throw new RuntimeException("Header already sent!!!");
throw new IOException("Header already sent!");
if( message_type != HttpMessageType.REQUEST )
throw new RuntimeException("Request URL is only available in HTTP REQUEST!!!");
throw new IOException("Request URL is only available in HTTP REQUEST!");
this.req_url = req_url;
}
@ -172,65 +172,70 @@ public class HttpPrintStream extends PrintStream{
cookies = map;
}
/**
* Prints a new line
*/
public void println() throws IOException {
printOrBuffer(System.lineSeparator());
}
/**
* Prints with a new line
*/
public void println(String s){
printOrBuffer(s+"\n");
public void println(String s) throws IOException {
printOrBuffer(s + System.lineSeparator());
}
/**
* Prints an string
*/
public void print(String s){
public void print(String s) throws IOException {
printOrBuffer(s);
}
/**
* prints to all
* Will buffer String or directly output headers if needed and then the String
*/
private void printOrBuffer(String s){
private void printOrBuffer(String s) throws IOException {
if(buffer_enabled){
buffer.append(s);
}
else{
if(res_status_code != null){
if( message_type==HttpMessageType.REQUEST )
super.print(req_type+" "+req_url+" HTTP/1.0");
out.print(req_type + " " + req_url + " HTTP/1.0");
else
super.print("HTTP/1.0 "+res_status_code+" "+getStatusString(res_status_code));
super.print(System.lineSeparator());
out.print("HTTP/1.0 " + res_status_code + " " + getStatusString(res_status_code));
out.println();
res_status_code = null;
req_type = null;
req_url = null;
}
if(headers != null){
for(String key : headers.keySet()){
super.print(key+": "+headers.get(key));
super.print(System.lineSeparator());
out.println(key + ": " + headers.get(key));
}
headers = null;
}
if(cookies != null){
if( !cookies.isEmpty() ){
if( message_type==HttpMessageType.REQUEST ){
super.print("Cookie: ");
out.print("Cookie: ");
for(String key : cookies.keySet()){
super.print(key+"="+cookies.get(key)+"; ");
out.print(key + "=" + cookies.get(key) + "; ");
}
super.print(System.lineSeparator());
out.println();
}
else{
for(String key : cookies.keySet()){
super.print("Set-Cookie: "+key+"="+cookies.get(key)+";");
super.print(System.lineSeparator());
out.print("Set-Cookie: " + key + "=" + cookies.get(key) + ";");
out.print(System.lineSeparator());
}
}
}
super.print(System.lineSeparator());
out.print(System.lineSeparator());
cookies = null;
}
super.print(s);
out.print(s);
}
}
@ -241,14 +246,23 @@ public class HttpPrintStream extends PrintStream{
return res_status_code == null && headers == null && cookies == null;
}
/**
* Sends out the buffer and clears it
*/
public void flush(){
@Override
public void flush() throws IOException {
flushBuffer();
super.flush();
out.flush();
}
protected void flushBuffer(){
@Override
public void close() throws IOException {
flush();
out.close();
}
protected void flushBuffer() throws IOException {
if(buffer_enabled){
buffer_enabled = false;
printOrBuffer(buffer.toString());
@ -260,40 +274,26 @@ public class HttpPrintStream extends PrintStream{
}
}
public void close(){
flush();
super.close();
/**
* Will flush all buffers and write binary data to stream
*/
@Override
public void write(int b) throws IOException {
flushBuffer();
out.write(b);
}
/**
* * Will flush all buffers and write binary data to stream
*/
@Override
public void write(byte[] buf, int off, int len) throws IOException {
flushBuffer();
out.write(buf, off, len);
}
public void print(boolean x){ printOrBuffer(String.valueOf(x));}
public void print(char x){ printOrBuffer(String.valueOf(x));}
public void print(char[] x){ printOrBuffer(new String(x));}
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));}
public void println(){ println("");}
public void println(boolean x){ println(String.valueOf(x));}
public void println(char x){ println(String.valueOf(x));}
public void println(char[] x){ println(new String(x));}
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));}
/* java.lang.StackOverflowError
public void write(int b) {
flushBuffer();
super.write(b);
private void rawWrite(String s) throws IOException {
out.write(s.getBytes());
}
public void write(byte buf[], int off, int len){
flushBuffer();
super.write(buf, off, len);
}*/
private String getStatusString(int type){
switch(type){

View file

@ -255,15 +255,18 @@ public class HttpServer extends ThreadedTCPNetworkServer{
//********************************************************************************
} catch (Exception e) {
logger.log(Level.WARNING, "500 Internal Server Error", e);
if(!out.isHeaderSent())
out.setStatusCode( 500 );
if(e.getMessage() != null)
out.println( "500 Internal Server Error: "+e.getMessage() );
else if(e.getCause() != null){
out.println( "500 Internal Server Error: "+e.getCause().getMessage() );
try {
if (!out.isHeaderSent())
out.setStatusCode(500);
if (e.getMessage() != null)
out.println("500 Internal Server Error: " + e.getMessage());
else if (e.getCause() != null) {
out.println("500 Internal Server Error: " + e.getCause().getMessage());
} else {
out.println("500 Internal Server Error: " + e);
}
else{
out.println( "500 Internal Server Error: "+e);
}catch(IOException ioe){
logger.log(Level.SEVERE, null, ioe);
}
}

View file

@ -61,7 +61,7 @@ public class HttpFilePage implements HttpPage{
HttpHeaderParser client_info,
Map<String, Object> session,
Map<String, String> cookie,
Map<String, String> request) {
Map<String, String> request) throws IOException{
try {
// Is the root only one file or a folder

View file

@ -91,7 +91,7 @@ public class SSDPServer extends ThreadedUDPNetwork implements ThreadedUDPNetwork
SSDPServer ssdp = new SSDPServer();
ssdp.addService(service);
ssdp.start();
MultiPrintStream.out.println("SSDP Server running");
logger.info("SSDP Server running");
}
public SSDPServer() throws IOException{
@ -224,7 +224,7 @@ public class SSDPServer extends ThreadedUDPNetwork implements ThreadedUDPNetwork
}
}
} catch (IOException e) {
e.printStackTrace();
logger.log(Level.SEVERE, null, e);
}
}
@ -288,7 +288,7 @@ public class SSDPServer extends ThreadedUDPNetwork implements ThreadedUDPNetwork
super.send( packet );
} catch (Exception e) {
e.printStackTrace();
logger.log(Level.SEVERE, null, e);
}
}
@ -337,7 +337,7 @@ public class SSDPServer extends ThreadedUDPNetwork implements ThreadedUDPNetwork
super.send( packet );
} catch (Exception e) {
e.printStackTrace();
logger.log(Level.SEVERE, null, e);
}
}
}

View file

@ -24,6 +24,7 @@ package zutil.net.ssdp;
import zutil.net.http.HttpPrintStream;
import java.io.IOException;
import java.net.InetAddress;
import java.util.Date;
import java.util.HashMap;
@ -135,11 +136,15 @@ public class StandardSSDPInfo implements SSDPServiceInfo, SSDPCustomInfo{
@Override
public void setHeaders(HttpPrintStream http) {
if(headers != null) {
try {
if (headers != null) {
for (String key : headers.keySet()) {
http.setHeader(key, headers.get(key));
}
}
}catch(IOException e){
}
}
public InetAddress getInetAddress(){

View file

@ -22,6 +22,7 @@
package zutil.net.upnp;
import java.io.IOException;
import java.util.Map;
import java.util.UUID;
@ -46,7 +47,7 @@ public class UPnPMediaServer extends UPnPRootDevice{
public void respond(HttpPrintStream out, HttpHeaderParser clientInfo,
Map<String, Object> session, Map<String, String> cookie,
Map<String, String> request) {
Map<String, String> request) throws IOException {
out.enableBuffering(true);
out.setHeader("Content-Type", "text/xml");

View file

@ -23,6 +23,7 @@
package zutil.net.upnp.services;
import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
@ -154,7 +155,7 @@ public class UPnPContentDirectory implements UPnPService, HttpPage, WSInterface
@WSDisabled
public void respond(HttpPrintStream out, HttpHeaderParser clientInfo,
Map<String, Object> session, Map<String, String> cookie,
Map<String, String> request) {
Map<String, String> request) throws IOException {
out.enableBuffering(true);
out.setHeader("Content-Type", "text/xml");

View file

@ -27,6 +27,7 @@ import zutil.net.http.HttpPage;
import zutil.net.http.HttpPrintStream;
import zutil.net.ws.WebServiceDef;
import java.io.IOException;
import java.util.Map;
/**
@ -44,7 +45,7 @@ public class WSDLHttpPage implements HttpPage {
HttpHeaderParser client_info,
Map<String, Object> session,
Map<String, String> cookie,
Map<String, String> request) {
Map<String, String> request) throws IOException{
out.setHeader("Content-Type", "text/xml");
wsdl.write( out );
}

View file

@ -34,10 +34,7 @@ import zutil.net.ws.WSReturnObject;
import zutil.net.ws.WSReturnObject.WSValueName;
import zutil.net.ws.WebServiceDef;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.io.*;
import java.lang.reflect.Field;
import java.util.ArrayList;
@ -62,10 +59,13 @@ public class WSDLWriter{
services.add(serv);
}
public void write( Writer out ) throws IOException {
out.write(generate());
}
public void write( PrintStream out ) {
out.print(generate());
}
public void write( OutputStream out ) throws IOException {
out.write(generate().getBytes() );
}

View file

@ -44,7 +44,7 @@ public class HTTPGuessTheNumber implements HttpPage{
HttpHeaderParser client_info,
Map<String, Object> session,
Map<String, String> cookie,
Map<String, String> request) {
Map<String, String> request) throws IOException {
out.enableBuffering(true);
out.println("<html>");

View file

@ -44,7 +44,7 @@ public class HTTPUploaderTest implements HttpPage{
HttpHeaderParser client_info,
Map<String, Object> session,
Map<String, String> cookie,
Map<String, String> request) {
Map<String, String> request) throws IOException {
if(!session.containsKey("file1")){
out.println("</html>" +