diff --git a/src/zutil/net/http/HttpClient.java b/src/zutil/net/http/HttpClient.java index 3625294..d0bb204 100644 --- a/src/zutil/net/http/HttpClient.java +++ b/src/zutil/net/http/HttpClient.java @@ -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 diff --git a/src/zutil/net/http/HttpPage.java b/src/zutil/net/http/HttpPage.java index d728251..44e886d 100644 --- a/src/zutil/net/http/HttpPage.java +++ b/src/zutil/net/http/HttpPage.java @@ -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 session, Map cookie, - Map request); + Map request) throws IOException; } diff --git a/src/zutil/net/http/HttpPrintStream.java b/src/zutil/net/http/HttpPrintStream.java index ec5733b..034072b 100644 --- a/src/zutil/net/http/HttpPrintStream.java +++ b/src/zutil/net/http/HttpPrintStream.java @@ -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(); - cookies = new HashMap(); - buffer = new StringBuffer(); - buffer_enabled = false; + this.res_status_code = 0; + this.headers = new HashMap(); + this.cookies = new HashMap(); + 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; } @@ -171,66 +171,71 @@ public class HttpPrintStream extends PrintStream{ protected void setCookies( HashMap map ){ 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"); - else - super.print("HTTP/1.0 "+res_status_code+" "+getStatusString(res_status_code)); - super.print(System.lineSeparator()); + out.print(req_type + " " + req_url + " HTTP/1.0"); + else + 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()); + for(String key : headers.keySet()){ + out.println(key + ": " + headers.get(key)); } headers = null; } if(cookies != null){ if( !cookies.isEmpty() ){ if( message_type==HttpMessageType.REQUEST ){ - super.print("Cookie: "); - for(String key : cookies.keySet()){ - super.print(key+"="+cookies.get(key)+"; "); + out.print("Cookie: "); + for(String key : cookies.keySet()){ + 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()); + for(String key : cookies.keySet()){ + 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){ diff --git a/src/zutil/net/http/HttpServer.java b/src/zutil/net/http/HttpServer.java index ef1345f..e01372e 100644 --- a/src/zutil/net/http/HttpServer.java +++ b/src/zutil/net/http/HttpServer.java @@ -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() ); - } - else{ - out.println( "500 Internal Server Error: "+e); + 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); + } + }catch(IOException ioe){ + logger.log(Level.SEVERE, null, ioe); } } diff --git a/src/zutil/net/http/pages/HttpFilePage.java b/src/zutil/net/http/pages/HttpFilePage.java index c6cbf3a..e4022c3 100644 --- a/src/zutil/net/http/pages/HttpFilePage.java +++ b/src/zutil/net/http/pages/HttpFilePage.java @@ -61,7 +61,7 @@ public class HttpFilePage implements HttpPage{ HttpHeaderParser client_info, Map session, Map cookie, - Map request) { + Map request) throws IOException{ try { // Is the root only one file or a folder diff --git a/src/zutil/net/ssdp/SSDPServer.java b/src/zutil/net/ssdp/SSDPServer.java index 20d5802..ce8eec0 100644 --- a/src/zutil/net/ssdp/SSDPServer.java +++ b/src/zutil/net/ssdp/SSDPServer.java @@ -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); } } } diff --git a/src/zutil/net/ssdp/StandardSSDPInfo.java b/src/zutil/net/ssdp/StandardSSDPInfo.java index abf00cd..21cb717 100644 --- a/src/zutil/net/ssdp/StandardSSDPInfo.java +++ b/src/zutil/net/ssdp/StandardSSDPInfo.java @@ -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,10 +136,14 @@ public class StandardSSDPInfo implements SSDPServiceInfo, SSDPCustomInfo{ @Override public void setHeaders(HttpPrintStream http) { - if(headers != null) { - for (String key : headers.keySet()) { - http.setHeader(key, headers.get(key)); + try { + if (headers != null) { + for (String key : headers.keySet()) { + http.setHeader(key, headers.get(key)); + } } + }catch(IOException e){ + } } diff --git a/src/zutil/net/upnp/UPnPMediaServer.java b/src/zutil/net/upnp/UPnPMediaServer.java index b140bcd..aaa852b 100644 --- a/src/zutil/net/upnp/UPnPMediaServer.java +++ b/src/zutil/net/upnp/UPnPMediaServer.java @@ -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 session, Map cookie, - Map request) { + Map request) throws IOException { out.enableBuffering(true); out.setHeader("Content-Type", "text/xml"); diff --git a/src/zutil/net/upnp/services/UPnPContentDirectory.java b/src/zutil/net/upnp/services/UPnPContentDirectory.java index bf12188..5b27b23 100644 --- a/src/zutil/net/upnp/services/UPnPContentDirectory.java +++ b/src/zutil/net/upnp/services/UPnPContentDirectory.java @@ -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 session, Map cookie, - Map request) { + Map request) throws IOException { out.enableBuffering(true); out.setHeader("Content-Type", "text/xml"); diff --git a/src/zutil/parser/wsdl/WSDLHttpPage.java b/src/zutil/parser/wsdl/WSDLHttpPage.java index 314a8c4..537bbd4 100644 --- a/src/zutil/parser/wsdl/WSDLHttpPage.java +++ b/src/zutil/parser/wsdl/WSDLHttpPage.java @@ -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 session, Map cookie, - Map request) { + Map request) throws IOException{ out.setHeader("Content-Type", "text/xml"); wsdl.write( out ); } diff --git a/src/zutil/parser/wsdl/WSDLWriter.java b/src/zutil/parser/wsdl/WSDLWriter.java index 8e2e65b..2ba59b7 100644 --- a/src/zutil/parser/wsdl/WSDLWriter.java +++ b/src/zutil/parser/wsdl/WSDLWriter.java @@ -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() ); } diff --git a/src/zutil/test/HTTPGuessTheNumber.java b/src/zutil/test/HTTPGuessTheNumber.java index 73e8b11..253a94e 100644 --- a/src/zutil/test/HTTPGuessTheNumber.java +++ b/src/zutil/test/HTTPGuessTheNumber.java @@ -44,7 +44,7 @@ public class HTTPGuessTheNumber implements HttpPage{ HttpHeaderParser client_info, Map session, Map cookie, - Map request) { + Map request) throws IOException { out.enableBuffering(true); out.println(""); diff --git a/src/zutil/test/HTTPUploaderTest.java b/src/zutil/test/HTTPUploaderTest.java index b7a71f0..a9ccea3 100644 --- a/src/zutil/test/HTTPUploaderTest.java +++ b/src/zutil/test/HTTPUploaderTest.java @@ -41,10 +41,10 @@ public class HTTPUploaderTest implements HttpPage{ } public void respond(HttpPrintStream out, - HttpHeaderParser client_info, - Map session, - Map cookie, - Map request) { + HttpHeaderParser client_info, + Map session, + Map cookie, + Map request) throws IOException { if(!session.containsKey("file1")){ out.println("" +