Initial implementation of RestHttpPage class

This commit is contained in:
Ziver Koc 2014-10-07 15:26:51 +00:00
parent 6e271f56d9
commit a7e6324a10
11 changed files with 81 additions and 35 deletions

View file

@ -456,12 +456,13 @@ public abstract class DBBean {
* This is a workaround if the field is not visible to other classes * This is a workaround if the field is not visible to other classes
* *
* @param field is the field * @param field is the field
* @return the value of the field
*/ */
protected void setFieldValue(Field field, Object o){ protected void setFieldValue(Field field, Object o){
try { try {
if( !Modifier.isPublic( field.getModifiers())) if( !Modifier.isPublic( field.getModifiers()))
field.setAccessible(true); field.setAccessible(true);
// Set basic datatype // Set basic datatype
if( o == null && !Object.class.isAssignableFrom( field.getType() ) ){ if( o == null && !Object.class.isAssignableFrom( field.getType() ) ){
logger.fine("Trying to set primitive data type to null!"); logger.fine("Trying to set primitive data type to null!");

View file

@ -70,7 +70,7 @@ public class HttpHeaderParser {
/** /**
* Parses the HTTP header information from an String * Parses the HTTP header information from an String
* *
* @param in is the string * @param in is the string
*/ */
public HttpHeaderParser(String in){ public HttpHeaderParser(String in){
url_attr = new HashMap<String, String>(); url_attr = new HashMap<String, String>();
@ -94,8 +94,7 @@ public class HttpHeaderParser {
* Parses the first header line and ads the values to * Parses the first header line and ads the values to
* the map and returns the file name and path * the map and returns the file name and path
* *
* @param header The header String * @param line The header String
* @param map The HashMap to put the variables to
* @return The path and file name as a String * @return The path and file name as a String
*/ */
protected void parseStatusLine(String line){ protected void parseStatusLine(String line){

View file

@ -35,15 +35,14 @@ public interface HttpPage{
* This method has to be implemented for every page. * This method has to be implemented for every page.
* This method is called when a client wants a response * This method is called when a client wants a response
* from this specific page. * from this specific page.
* * @param out is the PrintStream to the client
* @param out is the PrintStream to the client * @param client_info is information about the client
* @param client_info is information about the client * @param session is session values for the client
* @param session is session values for the client * @param cookie is cookie information from the client
* @param cookie is cookie information from the client * @param request is POST and GET requests from the client
* @param request is POST and GET requests from the client */
*/
public abstract void respond(HttpPrintStream out, public abstract void respond(HttpPrintStream out,
Map<String,String> client_info, HttpHeaderParser client_info,
Map<String,Object> session, Map<String,Object> session,
Map<String,String> cookie, Map<String,String> cookie,
Map<String,String> request); Map<String,String> request);

View file

@ -64,8 +64,8 @@ public class HttpServer extends ThreadedTCPNetworkServer{
/** /**
* Creates a new instance of the sever * Creates a new instance of the sever
* *
* @param url The address to the server * @param url The address to the server
* @param port The port that the server should listen to * @param port The port that the server should listen to
*/ */
public HttpServer(String url, int port){ public HttpServer(String url, int port){
this(url, port, null, null); this(url, port, null, null);
@ -75,10 +75,10 @@ public class HttpServer extends ThreadedTCPNetworkServer{
/** /**
* Creates a new instance of the sever * Creates a new instance of the sever
* *
* @param url The address to the server * @param url The address to the server
* @param port The port that the server should listen to * @param port The port that the server should listen to
* @param sslCert If this is not null then the server will use SSL connection with this keyStore file path * @param keyStore If this is not null then the server will use SSL connection with this keyStore file path
* @param sslCert If this is not null then the server will use a SSL connection with the given certificate * @param keyStorePass If this is not null then the server will use a SSL connection with the given certificate
*/ */
public HttpServer(String url, int port, File keyStore, String keyStorePass){ public HttpServer(String url, int port, File keyStore, String keyStorePass){
super( port, keyStore, keyStorePass ); super( port, keyStore, keyStorePass );
@ -119,8 +119,8 @@ public class HttpServer extends ThreadedTCPNetworkServer{
/** /**
* Add a HttpPage to a specific URL * Add a HttpPage to a specific URL
* *
* @param name The URL or name of the page * @param name The URL or name of the page
* @param page The page itself * @param page The page itself
*/ */
public void setPage(String name, HttpPage page){ public void setPage(String name, HttpPage page){
if(name.charAt(0) != '/') if(name.charAt(0) != '/')
@ -132,7 +132,7 @@ public class HttpServer extends ThreadedTCPNetworkServer{
* This is a default page that will be shown * This is a default page that will be shown
* if there is no other matching page, * if there is no other matching page,
* *
* @param page The HttpPage that will be shown * @param page The HttpPage that will be shown
*/ */
public void setDefaultPage(HttpPage page){ public void setDefaultPage(HttpPage page){
defaultPage = page; defaultPage = page;
@ -168,7 +168,6 @@ public class HttpServer extends ThreadedTCPNetworkServer{
public void run(){ public void run(){
String tmp = null; String tmp = null;
HashMap<String,String> client_info = new HashMap<String,String>();
HashMap<String,String> cookie = new HashMap<String,String>(); HashMap<String,String> cookie = new HashMap<String,String>();
HashMap<String,String> request = new HashMap<String,String>(); HashMap<String,String> request = new HashMap<String,String>();
@ -178,7 +177,6 @@ public class HttpServer extends ThreadedTCPNetworkServer{
HttpHeaderParser parser = new HttpHeaderParser(in); HttpHeaderParser parser = new HttpHeaderParser(in);
logger.finest(parser.toString()); logger.finest(parser.toString());
client_info = parser.getHeaders();
request = parser.getURLAttributes(); request = parser.getURLAttributes();
cookie = parser.getCookies(); cookie = parser.getCookies();
@ -238,9 +236,8 @@ public class HttpServer extends ThreadedTCPNetworkServer{
// Debug // Debug
if(logger.isLoggable(Level.FINE)){ if(logger.isLoggable(Level.FINE)){
logger.finest( "# page_url: "+parser.getRequestURL() ); logger.finest( "# page_url: "+parser.getRequestURL() );
logger.finest( "# client_session: "+client_session );
logger.finest( "# cookie: "+cookie ); logger.finest( "# cookie: "+cookie );
logger.finest( "# client_session: "+client_session );
logger.finest( "# client_info: "+client_info );
logger.finest( "# request: "+request ); logger.finest( "# request: "+request );
} }
//**************************** RESPONSE ************************************ //**************************** RESPONSE ************************************
@ -251,10 +248,10 @@ public class HttpServer extends ThreadedTCPNetworkServer{
out.setCookie( "session_id", ""+client_session.get("session_id") ); out.setCookie( "session_id", ""+client_session.get("session_id") );
if( !parser.getRequestURL().isEmpty() && pages.containsKey(parser.getRequestURL()) ){ if( !parser.getRequestURL().isEmpty() && pages.containsKey(parser.getRequestURL()) ){
pages.get(parser.getRequestURL()).respond(out, client_info, client_session, cookie, request); pages.get(parser.getRequestURL()).respond(out, parser, client_session, cookie, request);
} }
else if( defaultPage != null ){ else if( defaultPage != null ){
defaultPage.respond(out, client_info, client_session, cookie, request); defaultPage.respond(out, parser, client_session, cookie, request);
} }
else{ else{
out.setStatusCode( 404 ); out.setStatusCode( 404 );
@ -279,7 +276,7 @@ public class HttpServer extends ThreadedTCPNetworkServer{
} }
try{ try{
logger.fine("Conection Closed!!!"); logger.fine("Connection Closed!!!");
out.close(); out.close();
in.close(); in.close();
socket.close(); socket.close();

View file

@ -25,6 +25,7 @@ package zutil.net.upnp;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.UUID;
import zutil.net.http.HttpHeaderParser;
import zutil.net.http.HttpPrintStream; import zutil.net.http.HttpPrintStream;
/** /**
@ -43,7 +44,7 @@ public class UPnPMediaServer extends UPnPRootDevice{
url = location; url = location;
} }
public void respond(HttpPrintStream out, Map<String, String> clientInfo, public void respond(HttpPrintStream out, HttpHeaderParser clientInfo,
Map<String, Object> session, Map<String, String> cookie, Map<String, Object> session, Map<String, String> cookie,
Map<String, String> request) { Map<String, String> request) {

View file

@ -30,6 +30,7 @@ import java.util.Map;
import org.dom4j.DocumentException; import org.dom4j.DocumentException;
import zutil.io.file.FileUtil; import zutil.io.file.FileUtil;
import zutil.net.http.HttpHeaderParser;
import zutil.net.http.HttpPage; import zutil.net.http.HttpPage;
import zutil.net.http.HttpPrintStream; import zutil.net.http.HttpPrintStream;
import zutil.net.upnp.UPnPService; import zutil.net.upnp.UPnPService;
@ -151,7 +152,7 @@ public class UPnPContentDirectory implements UPnPService, HttpPage, WSInterface
@WSDisabled @WSDisabled
public void respond(HttpPrintStream out, Map<String, String> clientInfo, public void respond(HttpPrintStream out, HttpHeaderParser clientInfo,
Map<String, Object> session, Map<String, String> cookie, Map<String, Object> session, Map<String, String> cookie,
Map<String, String> request) { Map<String, String> request) {

View file

@ -22,8 +22,15 @@
package zutil.net.ws.rest; package zutil.net.ws.rest;
import zutil.converters.Converter;
import zutil.net.http.HttpHeaderParser;
import zutil.net.http.HttpPage; import zutil.net.http.HttpPage;
import zutil.net.http.HttpPrintStream; import zutil.net.http.HttpPrintStream;
import zutil.net.http.HttpURL;
import zutil.net.ws.WSInterface;
import zutil.net.ws.WSMethodDef;
import zutil.net.ws.WSParameterDef;
import zutil.net.ws.WebServiceDef;
import java.util.Map; import java.util.Map;
@ -31,12 +38,49 @@ import java.util.Map;
* User: Ziver * User: Ziver
*/ */
public class RestHttpPage implements HttpPage { public class RestHttpPage implements HttpPage {
/** The object that the functions will be invoked from **/
private WebServiceDef wsDef;
/** This instance of the web service class is used if session is disabled **/
private WSInterface ws;
public RestHttpPage( WSInterface wsObject ){
this.ws = wsObject;
this.wsDef = new WebServiceDef(ws.getClass());
}
@Override @Override
public void respond(HttpPrintStream out, public void respond(HttpPrintStream out,
Map<String, String> client_info, HttpHeaderParser client_info,
Map<String, Object> session, Map<String, Object> session,
Map<String, String> cookie, Map<String, String> cookie,
Map<String, String> request) { Map<String, String> request) {
execute(request);
}
private void execute(Map<String, String> input){
}
private Object[] prepareInputParams(WSMethodDef method, Map<String, String> input){
Object[] inputParams = new Object[method.getInputCount()];
// Get the parameter values
for(int i=0; i<method.getInputCount() ;i++){
WSParameterDef param = method.getInput( i );
if( input.containsKey(param.getName()) ){
inputParams[i] = Converter.fromString(
input.get(param.getName()),
param.getParamClass());
}
}
return inputParams;
}
private void generateResponse(){
} }
} }

View file

@ -31,6 +31,7 @@ import org.dom4j.io.XMLWriter;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;
import zutil.converters.Converter; import zutil.converters.Converter;
import zutil.log.LogUtil; import zutil.log.LogUtil;
import zutil.net.http.HttpHeaderParser;
import zutil.net.http.HttpPage; import zutil.net.http.HttpPage;
import zutil.net.http.HttpPrintStream; import zutil.net.http.HttpPrintStream;
import zutil.net.ws.*; import zutil.net.ws.*;
@ -110,7 +111,7 @@ public class SOAPHttpPage implements HttpPage{
public void respond(HttpPrintStream out, public void respond(HttpPrintStream out,
Map<String, String> client_info, HttpHeaderParser client_info,
Map<String, Object> session, Map<String, Object> session,
Map<String, String> cookie, Map<String, String> cookie,
Map<String, String> request) { Map<String, String> request) {

View file

@ -22,6 +22,7 @@
package zutil.parser.wsdl; package zutil.parser.wsdl;
import zutil.net.http.HttpHeaderParser;
import zutil.net.http.HttpPage; import zutil.net.http.HttpPage;
import zutil.net.http.HttpPrintStream; import zutil.net.http.HttpPrintStream;
import zutil.net.ws.WebServiceDef; import zutil.net.ws.WebServiceDef;
@ -40,7 +41,7 @@ public class WSDLHttpPage implements HttpPage {
} }
public void respond(HttpPrintStream out, public void respond(HttpPrintStream out,
Map<String, String> client_info, HttpHeaderParser client_info,
Map<String, Object> session, Map<String, Object> session,
Map<String, String> cookie, Map<String, String> cookie,
Map<String, String> request) { Map<String, String> request) {

View file

@ -25,6 +25,7 @@ package zutil.test;
import java.io.IOException; import java.io.IOException;
import java.util.Map; import java.util.Map;
import zutil.net.http.HttpHeaderParser;
import zutil.net.http.HttpPage; import zutil.net.http.HttpPage;
import zutil.net.http.HttpPrintStream; import zutil.net.http.HttpPrintStream;
import zutil.net.http.HttpServer; import zutil.net.http.HttpServer;
@ -40,7 +41,7 @@ public class HTTPGuessTheNumber implements HttpPage{
} }
public void respond(HttpPrintStream out, public void respond(HttpPrintStream out,
Map<String, String> client_info, HttpHeaderParser client_info,
Map<String, Object> session, Map<String, Object> session,
Map<String, String> cookie, Map<String, String> cookie,
Map<String, String> request) { Map<String, String> request) {

View file

@ -25,6 +25,7 @@ package zutil.test;
import java.io.IOException; import java.io.IOException;
import java.util.Map; import java.util.Map;
import zutil.net.http.HttpHeaderParser;
import zutil.net.http.HttpPage; import zutil.net.http.HttpPage;
import zutil.net.http.HttpPrintStream; import zutil.net.http.HttpPrintStream;
import zutil.net.http.HttpServer; import zutil.net.http.HttpServer;
@ -40,7 +41,7 @@ public class HTTPUploaderTest implements HttpPage{
} }
public void respond(HttpPrintStream out, public void respond(HttpPrintStream out,
Map<String, String> client_info, HttpHeaderParser client_info,
Map<String, Object> session, Map<String, Object> session,
Map<String, String> cookie, Map<String, String> cookie,
Map<String, String> request) { Map<String, String> request) {