Fixed some bugs

This commit is contained in:
Ziver Koc 2009-09-16 21:29:22 +00:00
parent 73be002969
commit 19a0e92d88
4 changed files with 126 additions and 62 deletions

View file

@ -18,24 +18,41 @@ public class MySQLConnection {
* @param user is the user name
* @param password is the password
*/
public MySQLConnection(String url,String db,String user, String password) throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException{
public MySQLConnection(String url, String db, String user, String password)
throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException{
Class.forName ("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection ("jdbc:mysql://"+url+"/"+db, user, password);
}
/**
* Runs a query and returns the result.<br>
* <b>NOTE:</b> Don't forget to close the ResultSet and the Statement or it can lead to memory leak tex: rows.getStatement().close();
* <b>NOTE:</b> Don't forget to close the ResultSet and the Statement or it
* can lead to memory leak: rows.getStatement().close();
*
* @param sql is the query to execute
* @return the data that the DB returned
*/
public synchronized ResultSet returnQuery(String sql) throws SQLException{
public synchronized ResultSet query(String sql) throws SQLException{
Statement s = conn.createStatement ();
s.executeQuery (sql);
s.executeQuery(sql);
return s.getResultSet();
}
/**
* Returns the first cell of the first row of the query
*
* @param sql is the SQL query to run, preferably with the LIMIT 1 at the end
* @return A SQL row if it exists or else null
*/
public synchronized String simpleQuery(String sql) throws SQLException{
Statement s = conn.createStatement ();
s.executeQuery(sql);
ResultSet result = s.getResultSet();
if(result.next())
return result.getString(0);
return null;
}
/**
* Runs a query in the MySQL server and returns effected rows
*
@ -52,6 +69,7 @@ public class MySQLConnection {
/**
* Runs a Prepared Statement.<br>
* <b>NOTE:</b> Don't forget to close the PreparedStatement or it can lead to memory leak
*
* @param sql is the SQL query to run
* @return The PreparedStatement
*/

View file

@ -63,7 +63,7 @@ public class MySQLQueue<E> implements Queue<E>{
@SuppressWarnings("unchecked")
public synchronized E peek() {
try {
ResultSet rs = db.returnQuery("SELECT * FROM "+table+" LIMIT 1");
ResultSet rs = db.query("SELECT * FROM "+table+" LIMIT 1");
if (rs.next()) {
return (E) Converter.toObject(rs.getBytes("data"));
}
@ -77,7 +77,7 @@ public class MySQLQueue<E> implements Queue<E>{
@SuppressWarnings("unchecked")
public synchronized E poll() {
try {
ResultSet rs = db.returnQuery("SELECT * FROM "+table+" LIMIT 1");
ResultSet rs = db.query("SELECT * FROM "+table+" LIMIT 1");
if (rs.next()) {
db.updateQuery("DELETE FROM "+table+" WHERE id="+rs.getInt("id")+" LIMIT 1");
return (E) Converter.toObject(rs.getBytes("data"));
@ -108,7 +108,7 @@ public class MySQLQueue<E> implements Queue<E>{
public boolean contains(Object arg0) {
try {
ResultSet rs = db.returnQuery("SELECT data FROM "+table+" WHERE data='"+Converter.toBytes(arg0)+"' LIMIT 1");
ResultSet rs = db.query("SELECT data FROM "+table+" WHERE data='"+Converter.toBytes(arg0)+"' LIMIT 1");
if (rs.next()) {
return true;
}
@ -135,7 +135,7 @@ public class MySQLQueue<E> implements Queue<E>{
public synchronized boolean remove(Object arg0) {
try {
ResultSet rs = db.returnQuery("DELETE FROM "+table+" WHERE data='"+Converter.toBytes(arg0)+"' LIMIT 1");
ResultSet rs = db.query("DELETE FROM "+table+" WHERE data='"+Converter.toBytes(arg0)+"' LIMIT 1");
rs.getStatement().close();
return true;
} catch (Exception e) {
@ -156,7 +156,7 @@ public class MySQLQueue<E> implements Queue<E>{
public int size() {
try {
ResultSet rs = db.returnQuery("SELECT count(*) FROM "+table);
ResultSet rs = db.query("SELECT count(*) FROM "+table);
if (rs.next()) {
return rs.getInt(1);
}

View file

@ -244,13 +244,13 @@ public class HttpServer extends Thread{
tmpb.append((char)in.read());
}
if(client_info.get("Content-Type").equals("application/x-www-form-urlencoded")){
if(client_info.get("Content-Type").contains("application/x-www-form-urlencoded")){
// get the variables
parseVariables(tmpb.toString(), request);
}
else if(client_info.get("Content-Type").equals("application/soap+xml") ||
client_info.get("Content-Type").equals("text/xml") ||
client_info.get("Content-Type").equals("text/plain")){
else if(client_info.get("Content-Type").contains("application/soap+xml") ||
client_info.get("Content-Type").contains("text/xml") ||
client_info.get("Content-Type").contains("text/plain")){
// save the variables
request.put("" , tmpb.toString());
}

View file

@ -37,13 +37,6 @@ import javax.wsdl.factory.WSDLFactory;
import javax.wsdl.xml.WSDLWriter;
import javax.xml.namespace.QName;
import zutil.network.http.HttpPage;
import zutil.network.http.HttpPrintStream;
import zutil.network.http.soap.SOAPInterface.WSDLDocumentation;
import zutil.network.http.soap.SOAPInterface.WSDLParamDocumentation;
import zutil.network.http.soap.SOAPObject.SOAPFieldName;
import zutil.MultiPrintStream;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
@ -53,6 +46,13 @@ import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import org.xml.sax.SAXException;
import zutil.MultiPrintStream;
import zutil.network.http.HttpPage;
import zutil.network.http.HttpPrintStream;
import zutil.network.http.soap.SOAPInterface.WSDLDocumentation;
import zutil.network.http.soap.SOAPInterface.WSDLParamDocumentation;
import zutil.network.http.soap.SOAPObject.SOAPFieldName;
import com.ibm.wsdl.extensions.PopulatedExtensionRegistry;
import com.ibm.wsdl.extensions.soap.SOAPConstants;
import com.sun.org.apache.xerces.internal.dom.DocumentImpl;
@ -89,16 +89,16 @@ import com.sun.org.apache.xerces.internal.dom.DocumentImpl;
*/
public class SOAPHttpPage implements HttpPage{
// valid methods for this soap page
private HashMap<String, MethodChasch> methods;
private HashMap<String, MethodCache> methods;
// contains an method and the names for the parameters
private class MethodChasch{
private class MethodCache{
String[] paramName;
boolean[] paramOptional;
String returnName;
Method method;
boolean header;
MethodChasch(Method m){
MethodCache(Method m){
method = m;
paramName = new String[method.getParameterTypes().length];
paramOptional = new boolean[method.getParameterTypes().length];
@ -122,13 +122,13 @@ public class SOAPHttpPage implements HttpPage{
this.url = url;
this.interf = interf;
this.session_enabled = false;
methods = new HashMap<String, MethodChasch>();
methods = new HashMap<String, MethodCache>();
for(Method m : interf.getClass().getDeclaredMethods()){
// check for public methods
if((m.getModifiers() & Modifier.PUBLIC) > 0 &&
!m.isAnnotationPresent(SOAPInterface.SOAPDisabled.class)){
MethodChasch chasch = new MethodChasch(m);
MethodCache chasch = new MethodCache(m);
StringBuffer tmp = new StringBuffer(m.getName()+"(");
// Get the parameter names
@ -218,11 +218,21 @@ public class SOAPHttpPage implements HttpPage{
if(session_enabled) session.put("SOAPInterface", obj);
}
Document document = soapResponse( request.get(""), obj);
OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter writer = new XMLWriter( out, format );
writer.write( document );
/*
// DEBUG
System.out.println("Request");
System.out.println(request);
System.out.println("Response");
writer = new XMLWriter( System.out, format );
writer.write( document );
*/
}
} catch (Exception e) {
e.printStackTrace(MultiPrintStream.out);
@ -238,24 +248,25 @@ public class SOAPHttpPage implements HttpPage{
try {
return soapResponse(xml, interf.getClass().newInstance());
} catch (Exception e) {
e.printStackTrace();
e.printStackTrace(MultiPrintStream.out);
}
return null;
}
protected Document soapResponse(String xml, SOAPInterface obj){
Document document = DocumentHelper.createDocument();
Element envelope = document.addElement("soap:Envelope");
try {
Document document = DocumentHelper.createDocument();
Element envelope = document.addElement("soap:Envelope");
envelope.add(new Namespace("soap", "http://www.w3.org/2001/12/soap-envelope"));
envelope.addAttribute("soap:encodingStyle", "http://www.w3.org/2001/12/soap-encoding");
envelope.add(new Namespace("soap", "http://schemas.xmlsoap.org/soap/envelope/"));
envelope.addAttribute("soap:encodingStyle", "http://schemas.xmlsoap.org/soap/envelope/");
Element header = envelope.addElement( "soap:Header" );
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 );
}
@ -278,13 +289,11 @@ public class SOAPHttpPage implements HttpPage{
fault.addElement("faultstring").setText( ""+e.getMessage() );
e.printStackTrace(MultiPrintStream.out);
}
return document;
} catch (Exception e) {
e.printStackTrace(MultiPrintStream.out);
}
return null;
return document;
}
/**
@ -301,7 +310,7 @@ public class SOAPHttpPage implements HttpPage{
while( it.hasNext() ){
Element e = it.next();
if(methods.containsKey(e.getQName().getName())){
MethodChasch m = methods.get(e.getQName().getName());
MethodCache m = methods.get(e.getQName().getName());
Object[] params = new Object[m.paramName.length];
// Get the param values
@ -318,7 +327,7 @@ public class SOAPHttpPage implements HttpPage{
// generate response xml
if(m.method.getReturnType() != void.class){
Element response = responseRoot.addElement(m.method.getName()+"Response");
createReturnXML(response, ret, m.returnName, m);
generateReturnXML(response, ret, m.returnName, m);
}
}
else{
@ -337,7 +346,7 @@ public class SOAPHttpPage implements HttpPage{
* @param ename is the name of the parent Element
* @param m is the method that returned the ret
*/
private void createReturnXML(Element root, Object ret, String ename, MethodChasch m) throws IllegalArgumentException, IllegalAccessException{
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 );
@ -355,7 +364,7 @@ public class SOAPHttpPage implements HttpPage{
array.addAttribute("type", "soap:Array");
array.addAttribute("soap:arrayType", arrayType);
for(int i=0; i<Array.getLength(ret) ;i++){
createReturnXML(array, Array.get(ret, i), "element", m);
generateReturnXML(array, Array.get(ret, i), "element", m);
}
}
else{
@ -369,7 +378,7 @@ public class SOAPHttpPage implements HttpPage{
String name;
if(tmp != null) name = tmp.value();
else name = "field"+i;
createReturnXML(objectE, fields[i].get(ret), name, m);
generateReturnXML(objectE, fields[i].get(ret), name, m);
}
}
else {
@ -485,13 +494,22 @@ public class SOAPHttpPage implements HttpPage{
Message exception = wsdl.createMessage();
exception.setQName(new QName(tns, "exception"));
exception.setUndefined(false);
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);
@ -502,7 +520,7 @@ public class SOAPHttpPage implements HttpPage{
PortType portType = wsdl.createPortType();
portType.setQName(new QName(tns, portTypeName));
portType.setUndefined(false);
for(MethodChasch m : methods.values()){
for(MethodCache m : methods.values()){
Operation operation = wsdl.createOperation();
//********* Request Messages
if(m.paramName.length > 0){
@ -535,6 +553,11 @@ public class SOAPHttpPage implements HttpPage{
input.setMessage(msgIn);
operation.setInput(input);
}
else{
Input input = wsdl.createInput();
input.setMessage(empty);
operation.setInput(input);
}
//********** Response Message
if(!m.method.getReturnType().equals( void.class )){
Message msgOut = wsdl.createMessage();
@ -611,30 +634,30 @@ public class SOAPHttpPage implements HttpPage{
soapBinding.setTransportURI("http://schemas.xmlsoap.org/soap/http");
binding.addExtensibilityElement(soapBinding);
for(MethodChasch m : methods.values()){
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("");
soapOperation.setSoapActionURI(url+""+m.method.getName());
operation.addExtensibilityElement(soapOperation);
// input
if(m.paramName.length > 0){
BindingInput input = wsdl.createBindingInput();
// Header
if(m.header){
SOAPHeader soapHeader = (SOAPHeader)extReg.createExtension(BindingInput.class, SOAPConstants.Q_ELEM_SOAP_HEADER);
soapHeader.setUse("literal");
input.addExtensibilityElement(soapHeader);
}// Body
else{
SOAPBody soapBody = (SOAPBody)extReg.createExtension(BindingInput.class, SOAPConstants.Q_ELEM_SOAP_BODY);
soapBody.setUse("literal");
input.addExtensibilityElement(soapBody);
}
operation.setBindingInput(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 )){
@ -643,11 +666,13 @@ public class SOAPHttpPage implements HttpPage{
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);
@ -683,12 +708,19 @@ public class SOAPHttpPage implements HttpPage{
wsdlType = DocumentHelper.createDocument();
Element definitions = wsdlType.addElement( "wsdl:definitions" );
definitions.addAttribute("targetNamespace", url+"?type");
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);
@ -699,6 +731,8 @@ public class SOAPHttpPage implements HttpPage{
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");
@ -707,6 +741,19 @@ public class SOAPHttpPage implements HttpPage{
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(SOAPObject.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);
@ -752,5 +799,4 @@ public class SOAPHttpPage implements HttpPage{
}
return c;
}
}