changed folder name
This commit is contained in:
parent
4503531ec9
commit
80c6a52c69
73 changed files with 0 additions and 0 deletions
266
src/zutil/net/http/HTTPHeaderParser.java
Normal file
266
src/zutil/net/http/HTTPHeaderParser.java
Normal file
|
|
@ -0,0 +1,266 @@
|
|||
package zutil.net.http;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Scanner;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class HTTPHeaderParser {
|
||||
// Some Cached regex's
|
||||
private static final Pattern colonPattern = Pattern.compile(":");
|
||||
private static final Pattern equalPattern = Pattern.compile("=");
|
||||
private static final Pattern andPattern = Pattern.compile("&");
|
||||
private static final Pattern semiColonPattern = Pattern.compile(";");
|
||||
|
||||
// HTTP info
|
||||
private String type;
|
||||
private String url;
|
||||
private HashMap<String, String> url_attr;
|
||||
private float version;
|
||||
private int httpCode;
|
||||
|
||||
// Parameters
|
||||
private HashMap<String, String> headers;
|
||||
private HashMap<String, String> cookies;
|
||||
|
||||
/**
|
||||
* Parses the HTTP header information from the stream
|
||||
*
|
||||
* @param in is the stream
|
||||
* @throws IOException
|
||||
*/
|
||||
public HTTPHeaderParser(BufferedReader in) throws IOException{
|
||||
url_attr = new HashMap<String, String>();
|
||||
headers = new HashMap<String, String>();
|
||||
cookies = new HashMap<String, String>();
|
||||
|
||||
String tmp = null;
|
||||
if( (tmp=in.readLine()) != null && !tmp.isEmpty() ){
|
||||
parseStatusLine( tmp );
|
||||
while( (tmp=in.readLine()) != null && !tmp.isEmpty() ){
|
||||
parseLine( tmp );
|
||||
}
|
||||
}
|
||||
parseCookies();
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses the HTTP header information from an String
|
||||
*
|
||||
* @param in is the string
|
||||
*/
|
||||
public HTTPHeaderParser(String in){
|
||||
url_attr = new HashMap<String, String>();
|
||||
headers = new HashMap<String, String>();
|
||||
cookies = new HashMap<String, String>();
|
||||
|
||||
Scanner sc = new Scanner(in);
|
||||
sc.useDelimiter("\n");
|
||||
String tmp = null;
|
||||
if( sc.hasNext() && !(tmp=sc.next()).isEmpty() ){
|
||||
parseStatusLine( tmp );
|
||||
while( sc.hasNext() && !(tmp=sc.next()).isEmpty() ){
|
||||
parseLine( tmp );
|
||||
}
|
||||
}
|
||||
parseCookies();
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses the first header line and ads the values to
|
||||
* the map and returns the file name and path
|
||||
*
|
||||
* @param header The header String
|
||||
* @param map The HashMap to put the variables to
|
||||
* @return The path and file name as a String
|
||||
*/
|
||||
protected void parseStatusLine(String line){
|
||||
// Server Response
|
||||
if( line.startsWith("HTTP/") ){
|
||||
version = Float.parseFloat( line.substring( 5 , 8) );
|
||||
httpCode = Integer.parseInt( line.substring( 9, 12 ));
|
||||
}
|
||||
// Client Request
|
||||
else{
|
||||
type = (line.substring(0, line.indexOf(" "))).trim();
|
||||
version = Float.parseFloat( line.substring(line.lastIndexOf("HTTP/")+5 , line.length()).trim() );
|
||||
line = (line.substring(type.length()+1, line.lastIndexOf("HTTP/"))).trim();
|
||||
|
||||
// parse URL and attributes
|
||||
int index = line.indexOf('?');
|
||||
if(index > -1){
|
||||
url = line.substring(0, index );
|
||||
line = line.substring( index+1, line.length());
|
||||
parseUrlAttributes(line, url_attr);
|
||||
}
|
||||
else{
|
||||
url = line;
|
||||
}
|
||||
|
||||
url = url.replaceAll("//", "/");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a String with variables from a get or post
|
||||
* that was sent from a client and puts the data into a HashMap
|
||||
*
|
||||
* @param attributes is the String containing all the attributes
|
||||
*/
|
||||
public static HashMap<String, String> parseUrlAttributes( String attributes ){
|
||||
HashMap<String, String> map = new HashMap<String, String>();
|
||||
parseUrlAttributes(attributes, map);
|
||||
return map;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a String with variables from a get or post
|
||||
* that was sent from a client and puts the data into a HashMap
|
||||
*
|
||||
* @param attributes is the String containing all the attributes
|
||||
* @param map is the HashMap to put all the values into
|
||||
*/
|
||||
public static void parseUrlAttributes(String attributes, HashMap<String, String> map){
|
||||
String[] tmp;
|
||||
// get the variables
|
||||
String[] data = andPattern.split( attributes );
|
||||
for(String element : data){
|
||||
tmp = equalPattern.split(element, 2);
|
||||
map.put(
|
||||
tmp[0].trim(), // Key
|
||||
(tmp.length>1 ? tmp[1] : "").trim()); //Value
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses the rest of the header
|
||||
*
|
||||
* @param line is the next line in the header
|
||||
*/
|
||||
protected void parseLine(String line){
|
||||
String[] data = colonPattern.split( line, 2 );
|
||||
headers.put(
|
||||
data[0].trim().toUpperCase(), // Key
|
||||
(data.length>1 ? data[1] : "").trim()); //Value
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses the attribute "Cookie" and returns a HashMap
|
||||
* with the values
|
||||
*
|
||||
* @return a HashMap with cookie values
|
||||
*/
|
||||
protected void parseCookies(){
|
||||
if( headers.containsKey("COOKIE") ){
|
||||
String[] tmp = semiColonPattern.split( headers.get("COOKIE") );
|
||||
String[] tmp2;
|
||||
for(String cookie : tmp){
|
||||
tmp2 = equalPattern.split(cookie, 2);
|
||||
cookies.put(
|
||||
tmp2[0].trim(), // Key
|
||||
(tmp2.length>1 ? tmp2[1] : "").trim()); //Value
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the HTTP message type( ex. GET,POST...)
|
||||
*/
|
||||
public String getRequestType(){
|
||||
return type;
|
||||
}
|
||||
/**
|
||||
* @return the HTTP version of this header
|
||||
*/
|
||||
public float getHTTPVersion(){
|
||||
return version;
|
||||
}
|
||||
/**
|
||||
* @return the HTTP Return Code from a Server
|
||||
*/
|
||||
public float getHTTPCode(){
|
||||
return httpCode;
|
||||
}
|
||||
/**
|
||||
* @return the URL that the client sent the server
|
||||
*/
|
||||
public String getRequestURL(){
|
||||
return url;
|
||||
}
|
||||
/**
|
||||
* Returns the URL attribute value of the given name.
|
||||
*
|
||||
* returns null if there is no such attribute
|
||||
*/
|
||||
public String getURLAttribute(String name){
|
||||
return url_attr.get( name );
|
||||
}
|
||||
/**
|
||||
* Returns the HTTP attribute value of the given name.
|
||||
*
|
||||
* returns null if there is no such attribute
|
||||
*/
|
||||
public String getHeader(String name){
|
||||
return headers.get( name.toUpperCase() );
|
||||
}
|
||||
/**
|
||||
* Returns the cookie value of the given name.
|
||||
*
|
||||
* returns null if there is no such attribute
|
||||
*/
|
||||
public String getCookie(String name){
|
||||
return cookies.get( name );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return a map of the parsed cookies
|
||||
*/
|
||||
public HashMap<String, String> getCookies(){
|
||||
return cookies;
|
||||
}
|
||||
/**
|
||||
* @return a map of the parsed URL attributes
|
||||
*/
|
||||
public HashMap<String, String> getURLAttributes(){
|
||||
return url_attr;
|
||||
}
|
||||
/**
|
||||
* @return a map of the parsed headers
|
||||
*/
|
||||
public HashMap<String, String> getHeaders(){
|
||||
return headers;
|
||||
}
|
||||
|
||||
|
||||
public String toString(){
|
||||
StringBuffer tmp = new StringBuffer();
|
||||
tmp.append("Type: ").append(type);
|
||||
tmp.append("\nHTTP Version: HTTP/").append(version);
|
||||
tmp.append("\nURL: ").append(url);
|
||||
|
||||
for( String key : url_attr.keySet() ){
|
||||
tmp.append("\nURL Attr: ");
|
||||
tmp.append(key);
|
||||
tmp.append("=");
|
||||
tmp.append( url_attr.get(key) );
|
||||
}
|
||||
|
||||
for( String key : headers.keySet() ){
|
||||
tmp.append("\nHeader: ");
|
||||
tmp.append(key);
|
||||
tmp.append("=");
|
||||
tmp.append( headers.get(key) );
|
||||
}
|
||||
|
||||
for( String key : cookies.keySet() ){
|
||||
tmp.append("\nCookie: ");
|
||||
tmp.append(key);
|
||||
tmp.append("=");
|
||||
tmp.append( cookies.get(key) );
|
||||
}
|
||||
|
||||
return tmp.toString();
|
||||
}
|
||||
}
|
||||
28
src/zutil/net/http/HttpPage.java
Normal file
28
src/zutil/net/http/HttpPage.java
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
package zutil.net.http;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* This is a interface for a ordinary page for the HttpServer
|
||||
*
|
||||
* @author Ziver
|
||||
*
|
||||
*/
|
||||
public interface HttpPage{
|
||||
/**
|
||||
* This method has to be implemented for every page.
|
||||
* This method is called when a client wants a response
|
||||
* from this specific page.
|
||||
*
|
||||
* @param out is the PrintStream to the client
|
||||
* @param client_info is information about the client
|
||||
* @param session is session values for the client
|
||||
* @param cookie is cookie information from the client
|
||||
* @param request is POST and GET requests from the client
|
||||
*/
|
||||
public abstract void respond(HttpPrintStream out,
|
||||
Map<String,String> client_info,
|
||||
Map<String,Object> session,
|
||||
Map<String,String> cookie,
|
||||
Map<String,String> request);
|
||||
}
|
||||
268
src/zutil/net/http/HttpPrintStream.java
Normal file
268
src/zutil/net/http/HttpPrintStream.java
Normal file
|
|
@ -0,0 +1,268 @@
|
|||
package zutil.net.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{
|
||||
// Defines the type of message
|
||||
public enum HTTPMessageType{
|
||||
REQUEST,
|
||||
RESPONSE
|
||||
}
|
||||
|
||||
// This defines the type of message that will be generated
|
||||
private HTTPMessageType message_type;
|
||||
// The status code of the message, ONLY for response
|
||||
private Integer res_status_code;
|
||||
// The request type of the message ONLY for request
|
||||
private String req_type;
|
||||
// The requesting url ONLY for request
|
||||
private String req_url;
|
||||
// An Map of all the header values
|
||||
private HashMap<String, String> header;
|
||||
// An Map of all the cookies
|
||||
private HashMap<String, String> cookie;
|
||||
// The buffered header
|
||||
private StringBuffer buffer;
|
||||
// If the header buffering is enabled
|
||||
private boolean buffer_enabled;
|
||||
|
||||
/**
|
||||
* Creates an new instance of HttpPrintStream with
|
||||
* message type of RESPONSE and buffering disabled.
|
||||
*
|
||||
* @param out is the OutputStream to send the message
|
||||
*/
|
||||
public HttpPrintStream(OutputStream out) {
|
||||
this( out, HTTPMessageType.RESPONSE );
|
||||
}
|
||||
/**
|
||||
* Creates an new instance of HttpPrintStream with
|
||||
* message type buffering disabled.
|
||||
*
|
||||
* @param out is the OutputStream to send the message
|
||||
* @param type is the type of message
|
||||
*/
|
||||
public HttpPrintStream(OutputStream out, HTTPMessageType type) {
|
||||
super(out);
|
||||
|
||||
this.message_type = type;
|
||||
res_status_code = 0;
|
||||
header = new HashMap<String, String>();
|
||||
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.
|
||||
* This function will flush the stream if buffering is
|
||||
* disabled.
|
||||
*
|
||||
* @param b
|
||||
*/
|
||||
public void enableBuffering(boolean b){
|
||||
buffer_enabled = b;
|
||||
if(!buffer_enabled) flush();
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a cookie that will be sent to the client
|
||||
*
|
||||
* @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
|
||||
*/
|
||||
public void setCookie(String key, String value) throws RuntimeException{
|
||||
if(cookie == null)
|
||||
throw new RuntimeException("Header already sent!!!");
|
||||
cookie.put(key, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds an header value
|
||||
*
|
||||
* @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
|
||||
*/
|
||||
public void setHeader(String key, String value) throws RuntimeException{
|
||||
if(header == null)
|
||||
throw new RuntimeException("Header already sent!!!");
|
||||
header.put(key, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
public void setStatusCode(int code) throws RuntimeException{
|
||||
if( res_status_code == null )
|
||||
throw new RuntimeException("Header already sent!!!");
|
||||
if( message_type != HTTPMessageType.RESPONSE )
|
||||
throw new RuntimeException("Status Code is only available in HTTP RESPONSE!!!");
|
||||
res_status_code = code;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
public void setRequestType(String req_type) throws RuntimeException{
|
||||
if( req_type == null )
|
||||
throw new RuntimeException("Header already sent!!!");
|
||||
if( message_type != HTTPMessageType.REQUEST )
|
||||
throw new RuntimeException("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
|
||||
*/
|
||||
public void setRequestURL(String req_url) throws RuntimeException{
|
||||
if( req_url == null )
|
||||
throw new RuntimeException("Header already sent!!!");
|
||||
if( message_type != HTTPMessageType.REQUEST )
|
||||
throw new RuntimeException("Request URL is only available in HTTP REQUEST!!!");
|
||||
this.req_url = req_url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prints with a new line
|
||||
*/
|
||||
public void println(String s){
|
||||
printOrBuffer(s+"\n");
|
||||
}
|
||||
|
||||
/**
|
||||
* Prints an string
|
||||
*/
|
||||
public void print(String s){
|
||||
printOrBuffer(s);
|
||||
}
|
||||
|
||||
/**
|
||||
* prints to all
|
||||
*/
|
||||
private void printOrBuffer(String s){
|
||||
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.1");
|
||||
else
|
||||
super.print("HTTP/1.1 "+res_status_code+" "+getStatusString(res_status_code));
|
||||
super.println();
|
||||
res_status_code = null;
|
||||
req_type = null;
|
||||
req_url = null;
|
||||
}
|
||||
if(header != null){
|
||||
for(String key : header.keySet()){
|
||||
super.print(key+": "+header.get(key));
|
||||
super.println();
|
||||
}
|
||||
header = null;
|
||||
}
|
||||
if(cookie != null){
|
||||
if( !cookie.isEmpty() ){
|
||||
if( message_type==HTTPMessageType.REQUEST ){
|
||||
super.print("Cookie: ");
|
||||
for(String key : cookie.keySet()){
|
||||
super.print(key+"="+cookie.get(key)+"; ");
|
||||
}
|
||||
super.println();
|
||||
}
|
||||
else{
|
||||
for(String key : cookie.keySet()){
|
||||
super.print("Set-Cookie: "+key+"="+cookie.get(key)+";");
|
||||
super.println();
|
||||
}
|
||||
}
|
||||
}
|
||||
super.println();
|
||||
cookie = null;
|
||||
}
|
||||
super.print(s);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends out the buffer and clears it
|
||||
*/
|
||||
public void flush(){
|
||||
if(buffer_enabled){
|
||||
buffer_enabled = false;
|
||||
printOrBuffer(buffer.toString());
|
||||
buffer.delete(0, buffer.length());
|
||||
buffer_enabled = true;
|
||||
}
|
||||
else if(res_status_code != null || header != null || cookie != null){
|
||||
printOrBuffer("");
|
||||
}
|
||||
super.flush();
|
||||
}
|
||||
|
||||
public void close(){
|
||||
flush();
|
||||
super.close();
|
||||
}
|
||||
|
||||
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));}
|
||||
|
||||
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 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 "";
|
||||
}
|
||||
}
|
||||
}
|
||||
263
src/zutil/net/http/HttpServer.java
Normal file
263
src/zutil/net/http/HttpServer.java
Normal file
|
|
@ -0,0 +1,263 @@
|
|||
package zutil.net.http;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.Socket;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import zutil.log.LogUtil;
|
||||
import zutil.net.threaded.ThreadedTCPNetworkServer;
|
||||
import zutil.net.threaded.ThreadedTCPNetworkServerThread;
|
||||
|
||||
|
||||
/**
|
||||
* A simple web server that handles both cookies and
|
||||
* sessions for all the clients
|
||||
*
|
||||
* @author Ziver
|
||||
*/
|
||||
public class HttpServer extends ThreadedTCPNetworkServer{
|
||||
public static final Logger logger = LogUtil.getLogger();
|
||||
public static final String SERVER_VERSION = "Ziver HttpServer 1.0";
|
||||
public static final int COOKIE_TTL = 200;
|
||||
public static final int SESSION_TTL = 10*60*1000; // in milliseconds
|
||||
|
||||
public final String server_url;
|
||||
public final int server_port;
|
||||
|
||||
private HashMap<String,HttpPage> pages;
|
||||
private HttpPage defaultPage;
|
||||
private Map<String,Map<String,Object>> sessions;
|
||||
private int nextSessionId;
|
||||
|
||||
/**
|
||||
* Creates a new instance of the sever
|
||||
*
|
||||
* @param url The address to the server
|
||||
* @param port The port that the server should listen to
|
||||
*/
|
||||
public HttpServer(String url, int port){
|
||||
this(url, port, null, null);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates a new instance of the sever
|
||||
*
|
||||
* @param url The address to the server
|
||||
* @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 sslCert 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){
|
||||
super( port, keyStore, keyStorePass );
|
||||
this.server_url = url;
|
||||
this.server_port = port;
|
||||
|
||||
pages = new HashMap<String,HttpPage>();
|
||||
sessions = Collections.synchronizedMap(new HashMap<String,Map<String,Object>>());
|
||||
nextSessionId = 0;
|
||||
|
||||
Timer timer = new Timer();
|
||||
timer.schedule(new GarbageCollector(), 0, SESSION_TTL / 2);
|
||||
|
||||
logger.info("HTTP"+(keyStore==null?"":"S")+" Server ready!");
|
||||
}
|
||||
|
||||
/**
|
||||
* This class acts as an garbage collector that
|
||||
* removes old sessions from the session HashMap
|
||||
*
|
||||
* @author Ziver
|
||||
*/
|
||||
private class GarbageCollector extends TimerTask {
|
||||
public void run(){
|
||||
Object[] keys = sessions.keySet().toArray();
|
||||
for(Object key : keys){
|
||||
Map<String,Object> client_session = sessions.get(key);
|
||||
|
||||
// Check if session is still valid
|
||||
if((Long)client_session.get("ttl") < System.currentTimeMillis()){
|
||||
sessions.remove(key);
|
||||
logger.fine("Removing Session: "+key);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a HttpPage to a specific URL
|
||||
*
|
||||
* @param name The URL or name of the page
|
||||
* @param page The page itself
|
||||
*/
|
||||
public void setPage(String name, HttpPage page){
|
||||
pages.put(name, page);
|
||||
}
|
||||
|
||||
/**
|
||||
* This is a default page that will be shown
|
||||
* if there is no other matching page,
|
||||
*
|
||||
* @param page The HttpPage that will be shown
|
||||
*/
|
||||
public void setDefaultPage(HttpPage page){
|
||||
defaultPage = page;
|
||||
}
|
||||
|
||||
protected ThreadedTCPNetworkServerThread getThreadInstance( Socket s ){
|
||||
try {
|
||||
return new HttpServerThread( s );
|
||||
} catch (IOException e) {
|
||||
logger.log(Level.SEVERE, "Could not start new Thread", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Internal class that handles all the requests
|
||||
*
|
||||
* @author Ziver
|
||||
*
|
||||
*/
|
||||
protected class HttpServerThread implements ThreadedTCPNetworkServerThread{
|
||||
private HttpPrintStream out;
|
||||
private BufferedReader in;
|
||||
private Socket socket;
|
||||
|
||||
public HttpServerThread(Socket socket) throws IOException{
|
||||
out = new HttpPrintStream(socket.getOutputStream());
|
||||
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
|
||||
this.socket = socket;
|
||||
logger.fine("New Connection!!! "+socket.getInetAddress().getHostName());
|
||||
}
|
||||
|
||||
public void run(){
|
||||
String tmp = null;
|
||||
|
||||
HashMap<String,String> client_info = new HashMap<String,String>();
|
||||
HashMap<String,String> cookie = new HashMap<String,String>();
|
||||
HashMap<String,String> request = new HashMap<String,String>();
|
||||
|
||||
//**************************** REQUEST *********************************
|
||||
try {
|
||||
logger.finer("Reciving Http Request!!!");
|
||||
|
||||
HTTPHeaderParser parser = new HTTPHeaderParser(in);
|
||||
logger.finest(parser.toString());
|
||||
client_info = parser.getHeaders();
|
||||
request = parser.getURLAttributes();
|
||||
cookie = parser.getCookies();
|
||||
|
||||
|
||||
//******* Read in the post data if available
|
||||
if( parser.getHeader("Content-Length")!=null ){
|
||||
// Reads the post data size
|
||||
tmp = parser.getHeader("Content-Length");
|
||||
int post_data_length = Integer.parseInt( tmp );
|
||||
// read the data
|
||||
StringBuffer tmpb = new StringBuffer();
|
||||
// read the data
|
||||
for(int i=0; i<post_data_length ;i++){
|
||||
tmpb.append((char)in.read());
|
||||
}
|
||||
|
||||
tmp = parser.getHeader("Content-Type");
|
||||
if( tmp.contains("application/x-www-form-urlencoded") ){
|
||||
// get the variables
|
||||
HTTPHeaderParser.parseUrlAttributes( tmpb.toString(), request );
|
||||
}
|
||||
else if( tmp.contains("application/soap+xml" ) ||
|
||||
tmp.contains("text/xml") ||
|
||||
tmp.contains("text/plain") ){
|
||||
// save the variables
|
||||
request.put( "" , tmpb.toString() );
|
||||
}
|
||||
else if( tmp.contains("multipart/form-data") ){
|
||||
// TODO: File upload
|
||||
throw new Exception( "\"multipart-form-data\" Not implemented!!!" );
|
||||
}
|
||||
}
|
||||
|
||||
//**************************** HANDLE REQUEST *********************************
|
||||
// Get the client session or create one
|
||||
Map<String, Object> client_session;
|
||||
long ttl_time = System.currentTimeMillis()+SESSION_TTL;
|
||||
if( cookie.containsKey("session_id") && sessions.containsKey(cookie.get("session_id")) ){
|
||||
client_session = sessions.get( cookie.get("session_id") );
|
||||
// Check if session is still valid
|
||||
if( (Long)client_session.get("ttl") < System.currentTimeMillis() ){
|
||||
int session_id = (Integer)client_session.get("session_id");
|
||||
client_session = Collections.synchronizedMap(new HashMap<String, Object>());
|
||||
client_session.put( "session_id", session_id);
|
||||
sessions.put( ""+session_id, client_session);
|
||||
}
|
||||
// renew the session TTL
|
||||
client_session.put( "ttl", ttl_time );
|
||||
}
|
||||
else{
|
||||
client_session = Collections.synchronizedMap(new HashMap<String, Object>());
|
||||
client_session.put( "session_id", nextSessionId );
|
||||
client_session.put( "ttl", ttl_time );
|
||||
sessions.put( ""+nextSessionId, client_session );
|
||||
nextSessionId++;
|
||||
}
|
||||
// Debug
|
||||
if(logger.isLoggable(Level.FINE)){
|
||||
logger.finest( "# page_url: "+parser.getRequestURL() );
|
||||
logger.finest( "# cookie: "+cookie );
|
||||
logger.finest( "# client_session: "+client_session );
|
||||
logger.finest( "# client_info: "+client_info );
|
||||
logger.finest( "# request: "+request );
|
||||
}
|
||||
//**************************** RESPONSE ************************************
|
||||
logger.finer("Sending Http Response!!!");
|
||||
out.setStatusCode(200);
|
||||
out.setHeader( "Server", SERVER_VERSION );
|
||||
out.setHeader( "Content-Type", "text/html" );
|
||||
out.setCookie( "session_id", ""+client_session.get("session_id") );
|
||||
|
||||
if( !parser.getRequestURL().isEmpty() && pages.containsKey(parser.getRequestURL()) ){
|
||||
pages.get(parser.getRequestURL()).respond(out, client_info, client_session, cookie, request);
|
||||
}
|
||||
else if( defaultPage != null ){
|
||||
defaultPage.respond(out, client_info, client_session, cookie, request);
|
||||
}
|
||||
else{
|
||||
out.setStatusCode( 404 );
|
||||
out.println( "404 Page Not Found: "+parser.getRequestURL() );
|
||||
logger.fine( "404 Page Not Found: "+parser.getRequestURL() );
|
||||
}
|
||||
|
||||
//********************************************************************************
|
||||
} catch (Exception e) {
|
||||
logger.log(Level.WARNING, "500 Internal Server Error", e);
|
||||
try {
|
||||
out.setStatusCode( 500 );
|
||||
} catch (Exception e1) {}
|
||||
if(e.getMessage() != null)
|
||||
out.println( "500 Internal Server Error: "+e.getMessage() );
|
||||
else{
|
||||
out.println( "500 Internal Server Error: "+e.getCause().getMessage() );
|
||||
}
|
||||
}
|
||||
|
||||
try{
|
||||
logger.fine("Conection Closed!!!");
|
||||
out.close();
|
||||
in.close();
|
||||
socket.close();
|
||||
} catch( Exception e ) {
|
||||
logger.log(Level.WARNING, "Could not close connection", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
51
src/zutil/net/http/multipart/MultipartField.java
Normal file
51
src/zutil/net/http/multipart/MultipartField.java
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
package zutil.net.http.multipart;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
|
||||
import zutil.ProgressListener;
|
||||
|
||||
|
||||
/**
|
||||
* A class for handling multipart field
|
||||
*
|
||||
* @author Ziver
|
||||
*/
|
||||
public class MultipartField{
|
||||
protected long received;
|
||||
protected long length;
|
||||
protected String contentType;
|
||||
|
||||
protected String fieldname;
|
||||
protected String value;
|
||||
|
||||
|
||||
protected MultipartField(){
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the amount of data received for this field
|
||||
*/
|
||||
public long getReceivedBytes(){
|
||||
return received;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the fieldname
|
||||
*/
|
||||
public String getFieldname(){
|
||||
return fieldname;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the value of the field
|
||||
*/
|
||||
public String getValue(){
|
||||
return value;
|
||||
}
|
||||
|
||||
protected void parse(){
|
||||
|
||||
}
|
||||
}
|
||||
63
src/zutil/net/http/multipart/MultipartFile.java
Normal file
63
src/zutil/net/http/multipart/MultipartFile.java
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
package zutil.net.http.multipart;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
|
||||
import zutil.ProgressListener;
|
||||
|
||||
|
||||
/**
|
||||
* A class for handling multipart files
|
||||
*
|
||||
* @author Ziver
|
||||
*/
|
||||
public class MultipartFile extends MultipartField{
|
||||
protected String filename;
|
||||
protected File file;
|
||||
|
||||
|
||||
protected MultipartFile(File tempFile){
|
||||
this.file = tempFile;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the amount of data received for this field
|
||||
*/
|
||||
public long getReceivedBytes(){
|
||||
return received;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the value of the field
|
||||
*/
|
||||
public String getValue(){
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the filename
|
||||
*/
|
||||
public String getFilename(){
|
||||
return filename;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the File class that points to the received file
|
||||
*/
|
||||
public File getFile(){
|
||||
return file;
|
||||
}
|
||||
|
||||
/**
|
||||
* Moves this file
|
||||
*
|
||||
* @param new_file is the new location to move the file to
|
||||
* @return if the move was successful
|
||||
*/
|
||||
public boolean moveFile(File new_file){
|
||||
boolean success = file.renameTo(new_file);
|
||||
if(success)
|
||||
file = new_file;
|
||||
return success;
|
||||
}
|
||||
}
|
||||
194
src/zutil/net/http/multipart/MultipartParser.java
Normal file
194
src/zutil/net/http/multipart/MultipartParser.java
Normal file
|
|
@ -0,0 +1,194 @@
|
|||
package zutil.net.http.multipart;
|
||||
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import zutil.ProgressListener;
|
||||
import zutil.net.http.HTTPHeaderParser;
|
||||
|
||||
/**
|
||||
* Parses a multipart/form-data http request,
|
||||
* saves files to temporary location.
|
||||
*
|
||||
* http://www.ietf.org/rfc/rfc1867.txt
|
||||
*
|
||||
* @author Ziver
|
||||
*
|
||||
*/
|
||||
public class MultipartParser {
|
||||
/** This is the temporary directory for the received files */
|
||||
private File tempDir;
|
||||
/** This is the delimiter that will separate the fields */
|
||||
private String delimiter;
|
||||
/** The length of the HTTP Body */
|
||||
private long contentLength;
|
||||
/** This is the input stream */
|
||||
private BufferedReader in;
|
||||
|
||||
/** This is the listener that will listen on the progress */
|
||||
private ProgressListener<MultipartField> listener;
|
||||
|
||||
|
||||
|
||||
public MultipartParser(BufferedReader in, HTTPHeaderParser header){
|
||||
this.in = in;
|
||||
|
||||
String cotype = header.getHeader("Content-type");
|
||||
cotype = cotype.split(" *; *")[1];
|
||||
delimiter = cotype.split(" *= *")[1];
|
||||
|
||||
contentLength = Long.parseLong( header.getHeader("Content-Length") );
|
||||
}
|
||||
|
||||
public MultipartParser(BufferedReader in, HttpServletRequest req){
|
||||
this.in = in;
|
||||
|
||||
String cotype = req.getHeader("Content-type");
|
||||
cotype = cotype.split(" *; *")[1];
|
||||
delimiter = cotype.split(" *= *")[1];
|
||||
|
||||
contentLength = req.getContentLength();
|
||||
}
|
||||
|
||||
public MultipartParser(BufferedReader in, String delimiter, long length){
|
||||
this.in = in;
|
||||
this.delimiter = delimiter;
|
||||
this.contentLength = length;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param listener is the listener that will be called for progress
|
||||
*/
|
||||
public void setListener(ProgressListener<MultipartField> listener){
|
||||
this.listener = listener;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses the HTTP Body and returns a list of fields
|
||||
*
|
||||
* @return A list of FormField
|
||||
*/
|
||||
public List<MultipartField> parse() throws IOException{
|
||||
ArrayList<MultipartField> list = new ArrayList<MultipartField>();
|
||||
// TODO: parse(list, delimiter);
|
||||
return list;
|
||||
}
|
||||
|
||||
// TODO:
|
||||
/*
|
||||
private void parse(List<MultipartField> list, String delimiter) throws IOException{
|
||||
String line = "";
|
||||
MultipartField field = null;
|
||||
delimiter = "--"+delimiter;
|
||||
String endDelimiter = delimiter+"--";
|
||||
BufferedWriter out = null;
|
||||
// Parsing the stream
|
||||
while(line != null){
|
||||
line = in.readLine();
|
||||
// Skip empty lines
|
||||
if(line == null || line.trim().isEmpty())
|
||||
continue;
|
||||
// End of field
|
||||
else if(line.equals( endDelimiter )){
|
||||
list.add(field);
|
||||
if(out != null) out.close();
|
||||
field.length = field.file.length();
|
||||
out = null;
|
||||
field = null;
|
||||
continue;
|
||||
}
|
||||
// New field
|
||||
else if(line.equals( delimiter )){
|
||||
if(field != null){
|
||||
list.add(field);
|
||||
if(out != null) out.close();
|
||||
field.length = field.file.length();
|
||||
out = null;
|
||||
field = null;
|
||||
}
|
||||
// Read the content-disposition
|
||||
line = in.readLine();
|
||||
if(line.toLowerCase().startsWith("content-disposition")){
|
||||
line = line.split(":", 2)[1];
|
||||
String[] fieldData = line.split(" *; *");
|
||||
//String type = fieldData[0].toLowerCase();
|
||||
field = new MultipartField();
|
||||
field.type = MultipartField.FieldType.Field;
|
||||
|
||||
// Parse content-disposition parameters
|
||||
for(String param : fieldData){
|
||||
String[] temp = param.split(" *= *");
|
||||
if(temp[0].equalsIgnoreCase("name"))
|
||||
field.fieldname = temp[1];
|
||||
else if(temp[0].equalsIgnoreCase("filename")){
|
||||
field.filename = temp[1];
|
||||
field.file = createTempFile();
|
||||
out = new BufferedWriter(new FileWriter(field.file));
|
||||
field.type = MultipartField.FieldType.File;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
throw new IOException("MultipartForm parse error unrecognized line: "+line);
|
||||
}
|
||||
// Read field data
|
||||
else if(field != null){
|
||||
if(field.type == MultipartField.FieldType.File){
|
||||
out.append(line);
|
||||
}
|
||||
else{
|
||||
field.value += line;
|
||||
}
|
||||
field.received += line.length();
|
||||
}
|
||||
}
|
||||
|
||||
if(field != null)
|
||||
throw new IOException("MultipartForm parse error stream ended prematurely");
|
||||
}
|
||||
*/
|
||||
|
||||
/**
|
||||
* Creates a temporary file in either the system
|
||||
* temporary folder or by the setTempDir() function
|
||||
*
|
||||
* @return the temporary file
|
||||
*/
|
||||
protected File createTempFile() throws IOException{
|
||||
if(tempDir != null)
|
||||
return File.createTempFile("upload", ".part", tempDir.getAbsoluteFile());
|
||||
else
|
||||
return File.createTempFile("upload", ".part");
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the initial delimiter
|
||||
*
|
||||
* @param delimiter is the new delimiter
|
||||
*/
|
||||
public void setDelimiter(String delimiter){
|
||||
this.delimiter = delimiter;
|
||||
}
|
||||
|
||||
public void setTempDir(File dir){
|
||||
if(!dir.isDirectory())
|
||||
throw new RuntimeException("\""+dir.getAbsolutePath()+"\" is not a directory!");
|
||||
if(!dir.canWrite())
|
||||
throw new RuntimeException("\""+dir.getAbsolutePath()+"\" is not writable!");
|
||||
tempDir = dir;
|
||||
}
|
||||
|
||||
public long getContentLength(){
|
||||
return contentLength;
|
||||
}
|
||||
}
|
||||
79
src/zutil/net/http/soap/SOAPClientFactory.java
Normal file
79
src/zutil/net/http/soap/SOAPClientFactory.java
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
package zutil.net.http.soap;
|
||||
|
||||
import javax.wsdl.WSDLException;
|
||||
|
||||
import javassist.CannotCompileException;
|
||||
import javassist.ClassPool;
|
||||
import javassist.CtClass;
|
||||
import javassist.CtMethod;
|
||||
import javassist.CtNewMethod;
|
||||
import javassist.NotFoundException;
|
||||
import zutil.net.ws.WSInterface;
|
||||
import zutil.net.ws.WSMethodDef;
|
||||
import zutil.net.ws.WebServiceDef;
|
||||
|
||||
/**
|
||||
* This is an factory that generates clients for web services
|
||||
*
|
||||
* @author Ziver
|
||||
*/
|
||||
public class SOAPClientFactory {
|
||||
|
||||
/**
|
||||
* Generates a Client Object for the web service.
|
||||
*
|
||||
* @param <T> is the class of the web service definition
|
||||
* @param intf is the class of the web service definition
|
||||
* @return a client Object
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> T getClient(Class<T> intf) throws InstantiationException, IllegalAccessException, CannotCompileException, NotFoundException, WSDLException{
|
||||
if( !WSInterface.class.isAssignableFrom( intf )){
|
||||
throw new ClassCastException("The Web Service class is not a subclass of WSInterface!");
|
||||
}
|
||||
return getClient( intf, new WebServiceDef((Class<? extends WSInterface>)intf) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a Client Object for the web service.
|
||||
*
|
||||
* @param <T> is the class of the web service definition
|
||||
* @param intf is the class of the web service definition
|
||||
* @param wsDef is the web service definition of the intf parameter
|
||||
* @return a client Object
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> T getClient(Class<T> intf, WebServiceDef wsDef) throws InstantiationException, IllegalAccessException, CannotCompileException, NotFoundException{
|
||||
if( !WSInterface.class.isAssignableFrom( intf )){
|
||||
throw new ClassCastException("The Web Service class is not a subclass of WSInterface!");
|
||||
}
|
||||
|
||||
// Generate the class
|
||||
ClassPool pool = ClassPool.getDefault();
|
||||
CtClass cc = pool.makeClass(intf.getName()+"Impl_"+Math.random());
|
||||
|
||||
CtClass intfClass = pool.get( intf.getName() );
|
||||
|
||||
// Is intf an interface
|
||||
if( intf.isInterface() ){
|
||||
cc.addInterface( intfClass );
|
||||
}
|
||||
// or a class
|
||||
else{
|
||||
cc.setSuperclass( intfClass );
|
||||
}
|
||||
|
||||
// Generate the methods
|
||||
// TODO:
|
||||
for(WSMethodDef methodDef : wsDef.getMethods()){
|
||||
CtMethod method = CtNewMethod.make("public int m(int i){}", cc);
|
||||
method.insertBefore("System.out.println(\"Hello.say():\");");
|
||||
}
|
||||
|
||||
// Initiate the class
|
||||
Class<T> c = cc.toClass();
|
||||
T obj = c.newInstance();
|
||||
|
||||
return obj;
|
||||
}
|
||||
}
|
||||
15
src/zutil/net/http/soap/SOAPException.java
Normal file
15
src/zutil/net/http/soap/SOAPException.java
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
package zutil.net.http.soap;
|
||||
|
||||
/**
|
||||
* This generates an client fault message
|
||||
* when used with SOAPHttpPage
|
||||
*
|
||||
* @author Ziver
|
||||
*/
|
||||
public class SOAPException extends Exception{
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public SOAPException(String string) {
|
||||
super(string);
|
||||
}
|
||||
}
|
||||
860
src/zutil/net/http/soap/SOAPHttpPage.java
Normal file
860
src/zutil/net/http/soap/SOAPHttpPage.java
Normal file
|
|
@ -0,0 +1,860 @@
|
|||
package zutil.net.http.soap;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Array;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.wsdl.Binding;
|
||||
import javax.wsdl.BindingInput;
|
||||
import javax.wsdl.BindingOperation;
|
||||
import javax.wsdl.BindingOutput;
|
||||
import javax.wsdl.Definition;
|
||||
import javax.wsdl.Fault;
|
||||
import javax.wsdl.Import;
|
||||
import javax.wsdl.Input;
|
||||
import javax.wsdl.Message;
|
||||
import javax.wsdl.Operation;
|
||||
import javax.wsdl.Output;
|
||||
import javax.wsdl.Part;
|
||||
import javax.wsdl.Port;
|
||||
import javax.wsdl.PortType;
|
||||
import javax.wsdl.Service;
|
||||
import javax.wsdl.WSDLException;
|
||||
import javax.wsdl.extensions.soap.SOAPAddress;
|
||||
import javax.wsdl.extensions.soap.SOAPBinding;
|
||||
import javax.wsdl.extensions.soap.SOAPBody;
|
||||
import javax.wsdl.extensions.soap.SOAPHeader;
|
||||
import javax.wsdl.extensions.soap.SOAPOperation;
|
||||
import javax.wsdl.factory.WSDLFactory;
|
||||
import javax.wsdl.xml.WSDLWriter;
|
||||
import javax.xml.namespace.QName;
|
||||
|
||||
import org.dom4j.Document;
|
||||
import org.dom4j.DocumentException;
|
||||
import org.dom4j.DocumentHelper;
|
||||
import org.dom4j.Element;
|
||||
import org.dom4j.io.OutputFormat;
|
||||
import org.dom4j.io.XMLWriter;
|
||||
import org.xml.sax.SAXException;
|
||||
|
||||
import zutil.converters.Converter;
|
||||
import zutil.io.StringOutputStream;
|
||||
import zutil.log.LogUtil;
|
||||
import zutil.net.http.HttpPage;
|
||||
import zutil.net.http.HttpPrintStream;
|
||||
import zutil.net.ws.WSInterface;
|
||||
import zutil.net.ws.WSObject;
|
||||
import zutil.net.ws.WSReturnValueList;
|
||||
import zutil.net.ws.WSInterface.WSDocumentation;
|
||||
import zutil.net.ws.WSInterface.WSParamDocumentation;
|
||||
import zutil.net.ws.WSObject.WSFieldName;
|
||||
|
||||
import com.ibm.wsdl.extensions.PopulatedExtensionRegistry;
|
||||
import com.ibm.wsdl.extensions.soap.SOAPConstants;
|
||||
import com.sun.org.apache.xerces.internal.dom.DocumentImpl;
|
||||
|
||||
/**
|
||||
* This is an HTTPPage for the HTTPServer that
|
||||
* handles soap messages.
|
||||
*
|
||||
* TODO: Header should be variables not methods
|
||||
* TODO: Read SOAPObjects as input parameter
|
||||
* TODO: Ability to have multiple arrays of same SOAPObject
|
||||
*
|
||||
* Features:
|
||||
* Input:
|
||||
* <br>-int
|
||||
* <br>-double
|
||||
* <br>-float
|
||||
* <br>-char
|
||||
* <br>-String
|
||||
* <br>-byte[]
|
||||
* <br>-And the Wrappers except byte
|
||||
*
|
||||
* Output:
|
||||
* <br>-SOAPObjects
|
||||
* <br>-SOAPReturnObjectList
|
||||
* <br>-byte[]
|
||||
* <br>-int
|
||||
* <br>-double
|
||||
* <br>-float
|
||||
* <br>-char
|
||||
* <br>-String
|
||||
* <br>-Arrays of Output
|
||||
* <br>-And the Wrappers except byte
|
||||
*
|
||||
* @author Ziver
|
||||
*/
|
||||
public class SOAPHttpPage implements HttpPage{
|
||||
public static final Logger logger = LogUtil.getLogger();
|
||||
|
||||
// valid methods for this soap page
|
||||
private HashMap<String, MethodCache> methods;
|
||||
// contains an method and the names for the parameters
|
||||
private class MethodCache{
|
||||
String[] paramName;
|
||||
boolean[] paramOptional;
|
||||
String[] returnName;
|
||||
Class<?>[] returnClass;
|
||||
Method method;
|
||||
boolean header;
|
||||
|
||||
MethodCache(Method m){
|
||||
method = m;
|
||||
paramName = new String[method.getParameterTypes().length];
|
||||
paramOptional = new boolean[method.getParameterTypes().length];
|
||||
header = false;
|
||||
|
||||
Class<?> tmp = m.getReturnType();
|
||||
if( WSReturnValueList.class.isAssignableFrom( tmp )){
|
||||
returnName = new String[ tmp.getFields().length ];
|
||||
returnClass = new Class<?>[ tmp.getFields().length ];
|
||||
}
|
||||
else if( !tmp.isAssignableFrom( void.class )){
|
||||
returnName = new String[1];
|
||||
returnClass = new Class<?>[1];
|
||||
}
|
||||
else{
|
||||
returnName = new String[0];
|
||||
returnClass = new Class<?>[0];
|
||||
}
|
||||
}
|
||||
}
|
||||
// The object that the functions will be invoked from
|
||||
private WSInterface interf;
|
||||
// The WSDL document
|
||||
private Definition wsdl;
|
||||
// The WSDL Type part
|
||||
private Document wsdlType;
|
||||
// the URL to this soap page
|
||||
private String url;
|
||||
// Session enabled
|
||||
private boolean session_enabled;
|
||||
|
||||
public SOAPHttpPage(String url, WSInterface interf) throws WSDLException{
|
||||
//if(!SOAPInterface.class.isAssignableFrom(interf) )
|
||||
// throw new ClassCastException("Class does not implement SOAPInterface!");
|
||||
this.url = url;
|
||||
this.interf = interf;
|
||||
this.session_enabled = false;
|
||||
methods = new HashMap<String, MethodCache>();
|
||||
|
||||
for(Method m : interf.getClass().getDeclaredMethods()){
|
||||
// check for public methods
|
||||
if((m.getModifiers() & Modifier.PUBLIC) > 0 &&
|
||||
!m.isAnnotationPresent(WSInterface.WSDisabled.class)){
|
||||
MethodCache chasch = new MethodCache(m);
|
||||
StringBuffer tmp = new StringBuffer(m.getName()+"(");
|
||||
|
||||
// Get the parameter names
|
||||
Annotation[][] paramAnnotation = m.getParameterAnnotations();
|
||||
|
||||
for(int i=0; i<paramAnnotation.length ;i++){
|
||||
for(Annotation annotation : paramAnnotation[i]){
|
||||
if(annotation instanceof WSInterface.WSParamName){
|
||||
WSInterface.WSParamName paramName = (WSInterface.WSParamName) annotation;
|
||||
chasch.paramName[i] = paramName.value();
|
||||
chasch.paramOptional[i] = paramName.optional();
|
||||
}
|
||||
}
|
||||
// if no name was found then use default
|
||||
if(chasch.paramName[i] == null)
|
||||
chasch.paramName[i] = "args"+i;
|
||||
|
||||
tmp.append(m.getParameterTypes()[i].getSimpleName()+" "+chasch.paramName[i]);
|
||||
if( i<paramAnnotation.length-1 ) tmp.append(", ");
|
||||
}
|
||||
tmp.append(") => ");
|
||||
|
||||
// the return parameter name
|
||||
WSInterface.WSReturnName returnName = m.getAnnotation(WSInterface.WSReturnName.class);
|
||||
if( WSReturnValueList.class.isAssignableFrom( m.getReturnType() ) ){
|
||||
Class<?> retClass = m.getReturnType();
|
||||
for(int i=0; i<retClass.getFields().length ;i++){
|
||||
if(i!=0) tmp.append(", ");
|
||||
WSReturnValueList.WSValueName retValName = retClass.getFields()[i]
|
||||
.getAnnotation( WSReturnValueList.WSValueName.class );
|
||||
if(retValName != null) chasch.returnName[i] = retValName.value();
|
||||
else chasch.returnName[i] = retClass.getFields()[i].getName();
|
||||
chasch.returnClass[i] = retClass.getFields()[i].getType();
|
||||
tmp.append(chasch.returnClass[i].getSimpleName()+" "+chasch.returnName[i]);
|
||||
}
|
||||
}
|
||||
else if( chasch.returnName.length>0 ){
|
||||
if(returnName != null) chasch.returnName[0] = returnName.value();
|
||||
else chasch.returnName[0] = "return";
|
||||
chasch.returnClass[0] = m.getReturnType();
|
||||
tmp.append(chasch.returnClass[0].getSimpleName()+" "+chasch.returnName[0]);
|
||||
}
|
||||
|
||||
// SOAP header?
|
||||
if(m.getAnnotation(WSInterface.WSHeader.class) != null)
|
||||
chasch.header = true;
|
||||
|
||||
// save in HashMap
|
||||
logger.info("New SOAP Method Registered: "+tmp);
|
||||
methods.put(m.getName(), chasch);
|
||||
}
|
||||
}
|
||||
|
||||
generateWSDL();
|
||||
|
||||
if(logger.isLoggable(Level.INFO)){
|
||||
try {
|
||||
// WSDL
|
||||
StringOutputStream out = new StringOutputStream();
|
||||
WSDLFactory factory = WSDLFactory.newInstance();
|
||||
WSDLWriter writer = factory.newWSDLWriter();
|
||||
writer.writeWSDL(wsdl, out);
|
||||
logger.info(out.toString());
|
||||
// WSDL Type
|
||||
out.clear();
|
||||
OutputFormat format = OutputFormat.createPrettyPrint();
|
||||
XMLWriter xmlWriter = new XMLWriter( out, format );
|
||||
xmlWriter.write( wsdlType );
|
||||
logger.info(out.toString());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Enables session support, if enabled then a new instance
|
||||
* of the SOAPInterface will be created, if disabled then
|
||||
* only the given object will be used as an static interface
|
||||
*
|
||||
* @param enabled is if session should be enabled
|
||||
*/
|
||||
public void enableSession(boolean enabled){
|
||||
this.session_enabled = enabled;
|
||||
}
|
||||
|
||||
|
||||
public void respond(HttpPrintStream out,
|
||||
Map<String, String> client_info,
|
||||
Map<String, Object> session,
|
||||
Map<String, String> cookie,
|
||||
Map<String, String> request) {
|
||||
|
||||
try {
|
||||
out.setHeader("Content-Type", "text/xml");
|
||||
out.flush();
|
||||
|
||||
if(request.containsKey("wsdl")){
|
||||
WSDLWriter writer = WSDLFactory.newInstance().newWSDLWriter();
|
||||
writer.writeWSDL(wsdl, out);
|
||||
}
|
||||
else if(request.containsKey("type")){
|
||||
OutputFormat format = OutputFormat.createPrettyPrint();
|
||||
XMLWriter writer = new XMLWriter( out, format );
|
||||
writer.write( wsdlType );
|
||||
}
|
||||
else{
|
||||
WSInterface obj = null;
|
||||
if(session_enabled){
|
||||
if( session.containsKey("SOAPInterface"))
|
||||
obj = (WSInterface)session.get("SOAPInterface");
|
||||
else{
|
||||
obj = interf.getClass().newInstance();
|
||||
session.put("SOAPInterface", obj);
|
||||
}
|
||||
}
|
||||
else{
|
||||
obj = interf;
|
||||
}
|
||||
|
||||
Document document = genSOAPResponse( request.get(""), obj);
|
||||
|
||||
OutputFormat format = OutputFormat.createCompactFormat();
|
||||
XMLWriter writer = new XMLWriter( out, format );
|
||||
writer.write( document );
|
||||
|
||||
|
||||
// DEBUG
|
||||
OutputFormat format2 = OutputFormat.createPrettyPrint();
|
||||
System.err.println("********** Request");
|
||||
System.err.println(request);
|
||||
System.out.println("********** Response");
|
||||
writer = new XMLWriter( System.out, format2 );
|
||||
writer.write( document );
|
||||
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.log(Level.WARNING, "Unhandled request", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a soap response for the given XML
|
||||
* @param xml is the XML request
|
||||
* @return a Document with the response
|
||||
*/
|
||||
public Document genSOAPResponse(String xml){
|
||||
try {
|
||||
WSInterface o = null;
|
||||
if(session_enabled) o = interf.getClass().newInstance();
|
||||
else o = interf;
|
||||
|
||||
return genSOAPResponse(xml, o );
|
||||
} catch (Exception e) {
|
||||
logger.log(Level.WARNING, "Exception in SOAP generation", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
protected Document genSOAPResponse(String xml, WSInterface obj){
|
||||
Document document = DocumentHelper.createDocument();
|
||||
Element envelope = document.addElement("soap:Envelope");
|
||||
try {
|
||||
envelope.addNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/");
|
||||
envelope.addNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
|
||||
envelope.addNamespace("xsd", "http://www.w3.org/2001/XMLSchema");
|
||||
|
||||
Element body = envelope.addElement( "soap:Body" );
|
||||
try{
|
||||
Element request = getXMLRoot(xml);
|
||||
if(request == null) return document;
|
||||
// Header
|
||||
if( request.element("Header") != null){
|
||||
Element header = envelope.addElement( "soap:Header" );
|
||||
prepareInvoke( obj, request.element("Header"), header );
|
||||
}
|
||||
|
||||
// Body
|
||||
if( request.element("Body") != null){
|
||||
prepareInvoke( obj, request.element("Body"), body );
|
||||
}
|
||||
}catch(Throwable e){
|
||||
body.clearContent();
|
||||
Element fault = body.addElement("soap:Fault");
|
||||
// The fault source
|
||||
if(e instanceof SOAPException || e instanceof SAXException || e instanceof DocumentException)
|
||||
fault.addElement("faultcode").setText( "soap:Client" );
|
||||
else
|
||||
fault.addElement("faultcode").setText( "soap:Server" );
|
||||
// The fault message
|
||||
if( e.getMessage() == null || e.getMessage().isEmpty())
|
||||
fault.addElement("faultstring").setText( ""+e.getClass().getSimpleName() );
|
||||
else
|
||||
fault.addElement("faultstring").setText( ""+e.getMessage() );
|
||||
logger.log(Level.WARNING, "Caught exception from SOAP Class", e);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.log(Level.WARNING, "Exception in SOAP generation", e);
|
||||
}
|
||||
|
||||
return document;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts an String XML to an Element
|
||||
*
|
||||
* @param msg is the string XML
|
||||
* @return the XML root Element
|
||||
*/
|
||||
private Element getXMLRoot(String xml) throws Exception {
|
||||
if(xml != null && !xml.isEmpty()){
|
||||
Document document = DocumentHelper.parseText(xml);
|
||||
return document.getRootElement();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Takes an XML Element and invokes all the
|
||||
* Child Elements as methods.
|
||||
*
|
||||
* @param obj is the object that the methods will be called from
|
||||
* @param requestRoot is the Element where the children lies
|
||||
* @param responseRoot is the root element of the response
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
private void prepareInvoke(WSInterface obj, Element requestRoot, Element responseRoot) throws Throwable{
|
||||
Iterator<Element> it = requestRoot.elementIterator();
|
||||
while( it.hasNext() ){
|
||||
Element e = it.next();
|
||||
if(methods.containsKey(e.getQName().getName())){
|
||||
MethodCache m = methods.get(e.getQName().getName());
|
||||
Object[] params = new Object[m.paramName.length];
|
||||
|
||||
// Get the parameter values
|
||||
for(int i=0; i<m.paramName.length ;i++){
|
||||
if(e.element(m.paramName[i]) != null)
|
||||
params[i] = Converter.fromString(
|
||||
e.element(m.paramName[i]).getTextTrim(),
|
||||
m.method.getParameterTypes()[i]);
|
||||
}
|
||||
// MultiPrintStream.out.println("invoking: "+m.method.getName()+" "+MultiPrintStream.out.dumpToString(params));
|
||||
// Invoke
|
||||
Object ret = invoke(obj, m.method, params);
|
||||
|
||||
// generate response XML
|
||||
if( m.returnClass.length>0 ){
|
||||
WSInterface.WSNamespace namespace = m.method.getAnnotation(WSInterface.WSNamespace.class);
|
||||
Element response = responseRoot.addElement("");
|
||||
if( namespace != null )
|
||||
response.addNamespace("m", namespace.value());
|
||||
else
|
||||
response.addNamespace("m", url+""+m.method.getName());
|
||||
response.setName("m:"+m.method.getName()+"Response");
|
||||
if( ret instanceof WSReturnValueList ){
|
||||
Field[] f = ret.getClass().getFields();
|
||||
for(int i=0; i<m.returnName.length ;i++ ){
|
||||
generateReturnXML(response,((WSReturnValueList)ret).getValue(f[i]) , m.returnName[i], m);
|
||||
}
|
||||
}
|
||||
else{
|
||||
generateReturnXML(response, ret, m.returnName[0], m);
|
||||
}
|
||||
}
|
||||
}
|
||||
else{
|
||||
throw new Exception("No such method: "+e.getQName().getName()+"!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Invokes a specified method
|
||||
*
|
||||
* @param m is the function
|
||||
* @param params a vector with arguments
|
||||
* @throws Throwable
|
||||
*/
|
||||
protected Object invoke(Object obj, Method m, Object[] params) throws Throwable{
|
||||
try {
|
||||
return m.invoke(obj, params );
|
||||
} catch (IllegalArgumentException e) {
|
||||
throw new SOAPException("Arguments missing for "+m.getName()+"!");
|
||||
} catch (IllegalAccessException e) {
|
||||
throw e;
|
||||
} catch (InvocationTargetException e) {
|
||||
throw e.getCause();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates an return XML Element. This function can
|
||||
* handle return values as XML Elements, SOAPObject and the
|
||||
* Java basic data types.
|
||||
*
|
||||
* @param root is the parent Element
|
||||
* @param ret is the object that is the return value
|
||||
* @param ename is the name of the parent Element
|
||||
* @param m is the method that returned the ret value
|
||||
*/
|
||||
private void generateReturnXML(Element root, Object ret, String ename, MethodCache m) throws IllegalArgumentException, IllegalAccessException{
|
||||
if(ret == null) return;
|
||||
if(byte[].class.isAssignableFrom(ret.getClass())){
|
||||
Element valueE = root.addElement( ename );
|
||||
valueE.addAttribute("type", "xsd:"+getClassSOAPName(ret.getClass()));
|
||||
String tmp = new sun.misc.BASE64Encoder().encode((byte[])ret);
|
||||
tmp = tmp.replaceAll("\\s", "");
|
||||
valueE.setText(tmp);
|
||||
}
|
||||
// return an array
|
||||
else if(ret.getClass().isArray()){
|
||||
Element array = root.addElement( (ename.equals("element") ? "Array" : ename) );
|
||||
String arrayType = "xsd:"+getClassSOAPName(ret.getClass());
|
||||
arrayType = arrayType.replaceFirst("\\[\\]", "["+Array.getLength(ret)+"]");
|
||||
|
||||
array.addAttribute("type", "soap:Array");
|
||||
array.addAttribute("soap:arrayType", arrayType);
|
||||
for(int i=0; i<Array.getLength(ret) ;i++){
|
||||
generateReturnXML(array, Array.get(ret, i), "element", m);
|
||||
}
|
||||
}
|
||||
else{
|
||||
Element objectE = root.addElement( ename ); //getClassSOAPName(ret.getClass())
|
||||
if(ret instanceof Element)
|
||||
objectE.add( (Element)ret );
|
||||
else if(ret instanceof WSObject){
|
||||
Field[] fields = ret.getClass().getFields();
|
||||
for(int i=0; i<fields.length ;i++){
|
||||
WSFieldName tmp = fields[i].getAnnotation(WSObject.WSFieldName.class);
|
||||
String name;
|
||||
if(tmp != null) name = tmp.value();
|
||||
else name = "field"+i;
|
||||
generateReturnXML(objectE, fields[i].get(ret), name, m);
|
||||
}
|
||||
}
|
||||
else {
|
||||
objectE.addAttribute("type", "xsd:"+getClassSOAPName(ret.getClass()));
|
||||
objectE.addText( ""+ret );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private String getClassSOAPName(Class<?> c){
|
||||
Class<?> cTmp = getClass(c);
|
||||
if(byte[].class.isAssignableFrom(c)){
|
||||
return "base64Binary";
|
||||
}
|
||||
else if( WSObject.class.isAssignableFrom(cTmp) ){
|
||||
return c.getSimpleName();
|
||||
}
|
||||
else{
|
||||
String ret = c.getSimpleName().toLowerCase();
|
||||
|
||||
if(cTmp == Integer.class) ret = ret.replaceAll("integer", "int");
|
||||
else if(cTmp == Character.class)ret = ret.replaceAll("character", "char");
|
||||
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
//********************************************************
|
||||
//********* WSDL Generation *************************
|
||||
|
||||
/**
|
||||
* Generates an WSDL document for the class
|
||||
*
|
||||
* @throws WSDLException
|
||||
*/
|
||||
private void generateWSDL() throws WSDLException{
|
||||
ArrayList<Class<?>> types = new ArrayList<Class<?>>();
|
||||
|
||||
String tns = url+"?wsdl";
|
||||
String xsd = "http://www.w3.org/2001/XMLSchema";
|
||||
String soap = "http://schemas.xmlsoap.org/wsdl/soap/";
|
||||
String wsdln = "http://schemas.xmlsoap.org/wsdl/";
|
||||
String td = url+"?type";
|
||||
|
||||
PopulatedExtensionRegistry extReg = new PopulatedExtensionRegistry();
|
||||
WSDLFactory factory = WSDLFactory.newInstance();
|
||||
String portTypeName = this.interf.getClass().getSimpleName()+"PortType";
|
||||
|
||||
wsdl = factory.newDefinition();
|
||||
wsdl.setQName(new QName(tns, this.interf.getClass().getSimpleName()));
|
||||
wsdl.setTargetNamespace(tns);
|
||||
wsdl.addNamespace("tns", tns);
|
||||
wsdl.addNamespace("xsd", xsd);
|
||||
wsdl.addNamespace("soap", soap);
|
||||
wsdl.addNamespace("wsdl", wsdln);
|
||||
wsdl.addNamespace("td", td);
|
||||
|
||||
Message exception = wsdl.createMessage();
|
||||
exception.setQName(new QName(tns, "exception"));
|
||||
exception.setUndefined(true);
|
||||
Part epart = wsdl.createPart();
|
||||
epart.setName("message");
|
||||
epart.setTypeName(new QName(xsd, "string"));
|
||||
exception.addPart(epart);
|
||||
wsdl.addMessage(exception);
|
||||
|
||||
Message empty = wsdl.createMessage();
|
||||
empty.setQName(new QName(tns, "empty"));
|
||||
empty.setUndefined(false);
|
||||
epart = wsdl.createPart();
|
||||
epart.setName("empty");
|
||||
epart.setTypeName(new QName(td, "empty"));
|
||||
empty.addPart(epart);
|
||||
wsdl.addMessage(empty);
|
||||
|
||||
// Types import
|
||||
Import imp = wsdl.createImport();
|
||||
imp.setNamespaceURI(td);
|
||||
imp.setLocationURI(td);
|
||||
wsdl.addImport(imp);
|
||||
|
||||
// PortType
|
||||
PortType portType = wsdl.createPortType();
|
||||
portType.setQName(new QName(tns, portTypeName));
|
||||
portType.setUndefined(false);
|
||||
for(MethodCache m : methods.values()){
|
||||
Operation operation = wsdl.createOperation();
|
||||
//********* Request Messages
|
||||
if(m.paramName.length > 0){
|
||||
Message msgIn = wsdl.createMessage();
|
||||
msgIn.setQName(new QName(tns, m.method.getName()+"Request"));
|
||||
msgIn.setUndefined(false);
|
||||
|
||||
//***** Documentation
|
||||
WSParamDocumentation tmpParamDoc = m.method.getAnnotation(WSInterface.WSParamDocumentation.class);
|
||||
if(tmpParamDoc != null){
|
||||
org.w3c.dom.Document xmldoc= new DocumentImpl();
|
||||
org.w3c.dom.Element paramDoc = xmldoc.createElement("wsdl:documentation");
|
||||
paramDoc.setTextContent(tmpParamDoc.value());
|
||||
msgIn.setDocumentationElement(paramDoc);
|
||||
}
|
||||
|
||||
// Parameters
|
||||
for(int i=0; i<m.paramName.length ;i++){
|
||||
// Parts
|
||||
Part part = wsdl.createPart();
|
||||
part.setName(m.paramName[i]);
|
||||
part.setTypeName(new QName( xsd,
|
||||
getClassSOAPName(m.method.getParameterTypes()[i])));
|
||||
if(m.paramOptional[i])
|
||||
part.getExtensionAttribute(new QName("minOccurs", "0"));
|
||||
msgIn.addPart(part);
|
||||
}
|
||||
wsdl.addMessage(msgIn);
|
||||
Input input = wsdl.createInput();
|
||||
input.setMessage(msgIn);
|
||||
operation.setInput(input);
|
||||
}
|
||||
else{
|
||||
Input input = wsdl.createInput();
|
||||
input.setMessage(empty);
|
||||
operation.setInput(input);
|
||||
}
|
||||
//********** Response Message
|
||||
if( m.returnName.length>0 ){
|
||||
Message msgOut = wsdl.createMessage();
|
||||
msgOut.setQName(new QName(tns, m.method.getName()+"Response"));
|
||||
msgOut.setUndefined(false);
|
||||
|
||||
for( int i=0; i<m.returnName.length ;i++ ){
|
||||
Class<?> retClass = m.returnClass[i];
|
||||
//MultiPrintStream.out.println(m.method.getName()+"=>"+m.returnName[i]+"="+retClass);
|
||||
|
||||
// Parts
|
||||
Part part = wsdl.createPart();
|
||||
part.setName( m.returnName[i] );
|
||||
msgOut.addPart(part);
|
||||
|
||||
Class<?> cTmp = getClass( retClass );
|
||||
// is an binary array
|
||||
if(byte[].class.isAssignableFrom( retClass )){
|
||||
part.setTypeName(new QName(xsd, "base64Binary"));
|
||||
}
|
||||
// is an array?
|
||||
else if( retClass.isArray()){
|
||||
part.setTypeName(new QName(td,
|
||||
"ArrayOf"+getClassSOAPName( retClass ).replaceAll("[\\[\\]]", "")));
|
||||
// add to type generation list
|
||||
if(!types.contains( retClass ))
|
||||
types.add( retClass );
|
||||
}
|
||||
else if( WSObject.class.isAssignableFrom(cTmp) ){
|
||||
// its an SOAPObject
|
||||
part.setTypeName(new QName(td, getClassSOAPName( retClass )));
|
||||
// add to type generation list
|
||||
if(!types.contains(cTmp))
|
||||
types.add(cTmp);
|
||||
}
|
||||
else{// its an Object
|
||||
part.setTypeName(new QName(xsd, getClassSOAPName( retClass )));
|
||||
}
|
||||
}
|
||||
|
||||
wsdl.addMessage(msgOut);
|
||||
Output output = wsdl.createOutput();
|
||||
output.setMessage(msgOut);
|
||||
operation.setOutput(output);
|
||||
}
|
||||
//************* Exceptions
|
||||
if(m.method.getExceptionTypes().length <= 0){
|
||||
Fault fault = wsdl.createFault();
|
||||
fault.setMessage(exception);
|
||||
operation.addFault(fault);
|
||||
}
|
||||
//************* Operations
|
||||
operation.setName(m.method.getName());
|
||||
operation.setUndefined(false);
|
||||
|
||||
//***** Documentation
|
||||
WSDocumentation tmpDoc = m.method.getAnnotation(WSInterface.WSDocumentation.class);
|
||||
if(tmpDoc != null){
|
||||
// <!-- example -->
|
||||
org.w3c.dom.Document xmldoc= new DocumentImpl();
|
||||
org.w3c.dom.Element doc = xmldoc.createElement("wsdl:documentation");
|
||||
doc.setTextContent(tmpDoc.value());
|
||||
operation.setDocumentationElement(doc);
|
||||
}
|
||||
|
||||
portType.addOperation(operation);
|
||||
}
|
||||
wsdl.addPortType(portType);
|
||||
|
||||
// Binding
|
||||
Binding binding = wsdl.createBinding();
|
||||
binding.setQName(new QName(tns, interf.getClass().getSimpleName()+"Binding"));
|
||||
binding.setPortType(portType);
|
||||
binding.setUndefined(false);
|
||||
|
||||
SOAPBinding soapBinding = (SOAPBinding)extReg.createExtension(Binding.class, SOAPConstants.Q_ELEM_SOAP_BINDING);
|
||||
soapBinding.setStyle("rpc");
|
||||
//soapBinding.setRequired(true);
|
||||
soapBinding.setTransportURI("http://schemas.xmlsoap.org/soap/http");
|
||||
binding.addExtensibilityElement(soapBinding);
|
||||
|
||||
for(MethodCache m : methods.values()){
|
||||
BindingOperation operation = wsdl.createBindingOperation();
|
||||
operation.setName(m.method.getName());
|
||||
|
||||
SOAPOperation soapOperation = (SOAPOperation)extReg.createExtension(BindingOperation.class, SOAPConstants.Q_ELEM_SOAP_OPERATION);
|
||||
soapOperation.setSoapActionURI(url+""+m.method.getName());
|
||||
operation.addExtensibilityElement(soapOperation);
|
||||
|
||||
// input
|
||||
BindingInput input = wsdl.createBindingInput();
|
||||
// Header
|
||||
if(m.header){
|
||||
SOAPHeader soapHeader = (SOAPHeader)extReg.createExtension(BindingInput.class, SOAPConstants.Q_ELEM_SOAP_HEADER);
|
||||
soapHeader.setUse("literal");
|
||||
soapHeader.setNamespaceURI(url+""+m.method.getName());
|
||||
input.addExtensibilityElement(soapHeader);
|
||||
}// Body
|
||||
else{
|
||||
SOAPBody soapBody = (SOAPBody)extReg.createExtension(BindingInput.class, SOAPConstants.Q_ELEM_SOAP_BODY);
|
||||
soapBody.setUse("literal");
|
||||
soapBody.setNamespaceURI(url+""+m.method.getName());
|
||||
input.addExtensibilityElement(soapBody);
|
||||
}
|
||||
operation.setBindingInput(input);
|
||||
|
||||
// output
|
||||
if(!m.method.getReturnType().equals( void.class )){
|
||||
BindingOutput output = wsdl.createBindingOutput();
|
||||
// Header
|
||||
if(m.header){
|
||||
SOAPHeader soapHeader = (SOAPHeader)extReg.createExtension(BindingInput.class, SOAPConstants.Q_ELEM_SOAP_HEADER);
|
||||
soapHeader.setUse("literal");
|
||||
soapHeader.setNamespaceURI(url+""+m.method.getName());
|
||||
output.addExtensibilityElement(soapHeader);
|
||||
}// Body
|
||||
else{
|
||||
SOAPBody soapBody = (SOAPBody)extReg.createExtension(BindingInput.class, SOAPConstants.Q_ELEM_SOAP_BODY);
|
||||
soapBody.setUse("literal");
|
||||
soapBody.setNamespaceURI(url+""+m.method.getName());
|
||||
output.addExtensibilityElement(soapBody);
|
||||
}
|
||||
operation.setBindingOutput(output);
|
||||
}
|
||||
|
||||
binding.addBindingOperation(operation);
|
||||
}
|
||||
wsdl.addBinding(binding);
|
||||
|
||||
// Service
|
||||
Port port = wsdl.createPort();
|
||||
port.setName( interf.getClass().getSimpleName()+"Port" );
|
||||
port.setBinding(binding);
|
||||
SOAPAddress addr = (SOAPAddress)extReg.createExtension(Port.class, SOAPConstants.Q_ELEM_SOAP_ADDRESS);
|
||||
addr.setLocationURI(url);
|
||||
port.addExtensibilityElement(addr);
|
||||
|
||||
Service ser = wsdl.createService();
|
||||
ser.setQName(new QName(tns, interf.getClass().getSimpleName()+"Service"));
|
||||
ser.addPort(port);
|
||||
wsdl.addService(ser);
|
||||
|
||||
// generate the complexTypes
|
||||
generateWSDLType(types);
|
||||
}
|
||||
|
||||
/**
|
||||
* This function generates the Type part of the WSDL.
|
||||
* Should be cabled after generateWSDL has finished.
|
||||
*
|
||||
*/
|
||||
private void generateWSDLType(ArrayList<Class<?>> types){
|
||||
wsdlType = DocumentHelper.createDocument();
|
||||
Element definitions = wsdlType.addElement( "wsdl:definitions" );
|
||||
definitions.addAttribute("targetNamespace", url);
|
||||
definitions.addNamespace("tns", url+"?type");
|
||||
definitions.addNamespace("xsd", "http://www.w3.org/2001/XMLSchema");
|
||||
definitions.addNamespace("wsdl", "http://schemas.xmlsoap.org/wsdl/");
|
||||
definitions.addNamespace("SOAP-ENC", "http://schemas.xmlsoap.org/soap/encoding/");
|
||||
|
||||
Element typeE = definitions.addElement("wsdl:types");
|
||||
Element schema = typeE.addElement("xsd:schema");
|
||||
schema.addAttribute("targetNamespace", url+"?type");
|
||||
|
||||
// empty type
|
||||
Element empty = schema.addElement("xsd:complexType");
|
||||
empty.addAttribute("name", "empty");
|
||||
empty.addElement("xsd:sequence");
|
||||
|
||||
for(int n=0; n<types.size() ;n++){
|
||||
Class<?> c = types.get(n);
|
||||
// Generate Array type
|
||||
if(c.isArray()){
|
||||
Class<?> ctmp = getClass(c);
|
||||
|
||||
Element type = schema.addElement("xsd:complexType");
|
||||
type.addAttribute("name",
|
||||
"ArrayOf"+getClassSOAPName(c).replaceAll("[\\[\\]]", ""));
|
||||
|
||||
/*// .Net can't handle this code
|
||||
Element complexContent = type.addElement("complexContent");
|
||||
|
||||
Element restriction = complexContent.addElement("restriction");
|
||||
restriction.addAttribute("base", "SOAP-ENC:Array");
|
||||
|
||||
Element attribute = restriction.addElement("attribute");
|
||||
attribute.addAttribute("ref", "SOAP-ENC:arrayType");
|
||||
attribute.addAttribute("wsdl:arrayType", "tns:"+getClassSOAPName(c));
|
||||
*/
|
||||
|
||||
Element sequence = type.addElement("xsd:sequence");
|
||||
|
||||
Element element = sequence.addElement("xsd:element");
|
||||
element.addAttribute("minOccurs", "0");
|
||||
element.addAttribute("maxOccurs", "unbounded");
|
||||
element.addAttribute("name", "element");
|
||||
element.addAttribute("nillable", "true");
|
||||
if(WSObject.class.isAssignableFrom(ctmp))
|
||||
element.addAttribute("type", "tns:"+getClassSOAPName(c).replace("[]", ""));
|
||||
else
|
||||
element.addAttribute("type", "xsd:"+getClassSOAPName(c).replace("[]", ""));
|
||||
|
||||
if(!types.contains(ctmp))
|
||||
types.add(ctmp);
|
||||
}
|
||||
// Generate SOAPObject type
|
||||
else if(WSObject.class.isAssignableFrom(c)){
|
||||
Element type = schema.addElement("xsd:complexType");
|
||||
type.addAttribute("name", getClassSOAPName(c));
|
||||
|
||||
Element sequence = type.addElement("xsd:sequence");
|
||||
|
||||
Field[] fields = c.getFields();
|
||||
for(int i=0; i<fields.length ;i++){
|
||||
WSFieldName tmp = fields[i].getAnnotation(WSObject.WSFieldName.class);
|
||||
String name;
|
||||
if(tmp != null) name = tmp.value();
|
||||
else name = "field"+i;
|
||||
|
||||
Element element = sequence.addElement("xsd:element");
|
||||
element.addAttribute("name", name);
|
||||
|
||||
// Check if the object is an SOAPObject
|
||||
Class<?> cTmp = getClass(fields[i].getType());
|
||||
if(WSObject.class.isAssignableFrom(cTmp)){
|
||||
element.addAttribute("type", "tns:"+getClassSOAPName(cTmp));
|
||||
if(!types.contains(cTmp))
|
||||
types.add(cTmp);
|
||||
}
|
||||
else{
|
||||
element.addAttribute("type", "xsd:"+getClassSOAPName(fields[i].getType()));
|
||||
}
|
||||
// Is the Field optional
|
||||
if(tmp != null && tmp.optional())
|
||||
element.addAttribute("minOccurs", "0");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Class<?> getClass(Class<?> c){
|
||||
if(c!=null && c.isArray()){
|
||||
return getClass(c.getComponentType());
|
||||
}
|
||||
return c;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue