Created
August 12, 2011 15:55
-
-
Save marharrUK/1142344 to your computer and use it in GitHub Desktop.
GeneralInterface
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.softsols.communication; | |
| import java.io.ByteArrayInputStream; | |
| import java.io.ByteArrayOutputStream; | |
| import java.io.IOException; | |
| import java.io.InputStream; | |
| import java.io.OutputStream; | |
| import java.net.HttpURLConnection; | |
| import java.net.URL; | |
| import java.util.List; | |
| import org.ksoap2.SoapEnvelope; | |
| import org.ksoap2.serialization.PropertyInfo; | |
| import org.ksoap2.serialization.SoapObject; | |
| import org.ksoap2.serialization.SoapPrimitive; | |
| import org.ksoap2.serialization.SoapSerializationEnvelope; | |
| import org.ksoap2.transport.HttpTransportSE; | |
| import org.ksoap2.transport.ServiceConnection; | |
| import org.simpleframework.xml.Serializer; | |
| import org.simpleframework.xml.core.Persister; | |
| import org.xmlpull.v1.XmlPullParserException; | |
| public class GeneralInterface { | |
| public static final String CRLF = System.getProperty("line.separator"); | |
| private static final String NAMESPACE = "http://www.fastnetasp.com/GeneralInterface"; | |
| //private String NAMESPACE = "http://www.fastnetasp.com/AgilityWS"; | |
| //private String URL = "http://192.168.193.68/Agility33ws/mobileexpert.asmx"; | |
| private String URL = "http://192.168.193.68/AgilityDev/services/generalinterface.asmx"; | |
| private boolean _loggedIn; | |
| private HttpTransportSE _androidHttpTransport = new HttpTransportSE(URL); | |
| private SoapObject digDeeper(SoapObject obj, String name) {return (SoapObject)obj.getProperty(name);} | |
| /** GeneralReliableHandler **/ | |
| /** Sends Web Service Requests to the Agility GeneralInterface **/ | |
| public HandlerResult soapGeneralReliableHandler(String serviceModule, String serviceCode, String params, String login, String password) { | |
| String METHOD_NAME = "GeneralReliableHandler"; | |
| String status = ""; | |
| String message = ""; | |
| try { | |
| SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); | |
| MessageData ms = new MessageData(login, soapGetSequence("fastNet", "syGeneralInterfaceQueue", login, password)); | |
| request.addPropertyIfValue(getPropertyInfo("ServiceModule", NAMESPACE, String.class), serviceModule ); | |
| request.addPropertyIfValue(getPropertyInfo("ServiceCode", NAMESPACE, String.class), serviceCode ); | |
| request.addPropertyIfValue(getPropertyInfo("MessageData", NAMESPACE, ms.getClass()), ms ); | |
| request.addPropertyIfValue(getPropertyInfo("Parameters", NAMESPACE, String.class), params ); | |
| request.addPropertyIfValue(getPropertyInfo("login", NAMESPACE, String.class), login) ; | |
| request.addPropertyIfValue(getPropertyInfo("password", NAMESPACE, String.class), password ); | |
| SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); | |
| envelope.setOutputSoapObject(request); | |
| envelope.implicitTypes = true; | |
| envelope.dotNet = true; | |
| HttpTransportSE androidHttpTransport = new HttpTransportSE(URL,3000); | |
| // androidHttpTransport.debug = true; | |
| androidHttpTransport.call(NAMESPACE + "/" + METHOD_NAME, envelope); | |
| //System.out.println(androidHttpTransport.requestDump); | |
| //System.out.println(androidHttpTransport.responseDump); | |
| Object result = envelope.getResponse(); | |
| if(result.getClass() == SoapObject.class){ | |
| SoapObject so = (SoapObject) result; | |
| status = so.getProperty(0).toString(); | |
| if("OK".equalsIgnoreCase(status)){ | |
| message = ""; | |
| } | |
| if("FAIL".equalsIgnoreCase(status)){ | |
| message = so.getProperty("Message") + ""; | |
| } | |
| } | |
| } catch (Exception e) { | |
| status = "ERROR"; | |
| message = e.getMessage(); | |
| } | |
| return new HandlerResult(status, message, ""); | |
| } | |
| /** GeneralReciever **/ | |
| /** Retrieves Web Service messages from the Agility GeneralInterface for that queue**/ | |
| public GeneralReceiverResponse soapGeneralReceiver(String queueName, String login, String password) { | |
| String METHOD_NAME = "GeneralReceiver"; | |
| // String status = ""; | |
| // String message = ""; | |
| try { | |
| //strLog += "Testing SOAP web service call:" + CRLF; | |
| SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); | |
| int serverID = soapGetSequence("fastNet", "syGeneralInterfaceQueue", login, password); | |
| MessageData ms = new MessageData(queueName, serverID); | |
| request.addPropertyIfValue(getPropertyInfo("MessageData", NAMESPACE, ms.getClass()), ms ); | |
| request.addPropertyIfValue(getPropertyInfo("login", NAMESPACE, String.class), login); | |
| request.addPropertyIfValue(getPropertyInfo("password", NAMESPACE, String.class), password); | |
| SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); | |
| envelope.setOutputSoapObject(request); | |
| envelope.implicitTypes = true; | |
| envelope.dotNet = true; | |
| MyTransport androidHttpTransport = new MyTransport(URL,20000); | |
| //HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); | |
| //androidHttpTransport.debug = true; | |
| androidHttpTransport.call(NAMESPACE + "/" + METHOD_NAME, envelope); | |
| //System.out.println(androidHttpTransport.requestDump); | |
| //System.out.println(androidHttpTransport.responseDump); | |
| Object result = envelope.getResponse(); | |
| if(result.getClass() == SoapObject.class){ | |
| SoapObject so = (SoapObject) result; | |
| if("OK".equalsIgnoreCase(so.getPropertySafelyAsString("Status"))){ | |
| GeneralReceiverResponse grr = new GeneralReceiverResponse(so) ; | |
| HandlerResult hr = soapGeneralReliableHandler("GeneralInterface", "ReceiverCallBack", ConvertToXML(new CallBackMessage("Processed", grr.getSenderMessageID(), null)), login, password); | |
| return grr; | |
| } | |
| else | |
| return new GeneralReceiverResponse(so.getPropertySafelyAsString("Status"), null, "", "", 0); | |
| } | |
| else{ | |
| return new GeneralReceiverResponse("ERROR", null, "", "SoapEnvelope Error", 0); | |
| } | |
| } catch (Exception e) { | |
| return new GeneralReceiverResponse("ERROR", null, "", "", 0); | |
| } | |
| } | |
| /** GeneralHandler **/ | |
| /** Sends Web Service Requests to the Agility GeneralInterface **/ | |
| public HandlerResult soapGeneralHandler(String serviceModule, String serviceCode, String params, String login, String password) { | |
| String METHOD_NAME = "GeneralHandler"; | |
| String status = ""; | |
| String message = ""; | |
| try { | |
| SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); | |
| request.addPropertyIfValue(getPropertyInfo("ServiceModule", NAMESPACE, String.class), serviceModule ); | |
| request.addPropertyIfValue(getPropertyInfo("ServiceCode", NAMESPACE, String.class), serviceCode ); | |
| request.addPropertyIfValue(getPropertyInfo("Parameters", NAMESPACE, String.class), params ); | |
| request.addPropertyIfValue(getPropertyInfo("login", NAMESPACE, String.class), login) ; | |
| request.addPropertyIfValue(getPropertyInfo("password", NAMESPACE, String.class), password ); | |
| SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); | |
| envelope.setOutputSoapObject(request); | |
| envelope.implicitTypes = true; | |
| envelope.dotNet = true; | |
| //HttpTransportSE androidHttpTransport = new HttpTransportSE(URL,20000); | |
| // androidHttpTransport.debug = true; | |
| _androidHttpTransport.call(NAMESPACE + "/" + METHOD_NAME, envelope); | |
| //System.out.println(androidHttpTransport.requestDump); | |
| //System.out.println(androidHttpTransport.responseDump); | |
| Object result = envelope.getResponse(); | |
| if(result.getClass() == SoapObject.class){ | |
| SoapObject so = (SoapObject) result; | |
| status = so.getProperty(0).toString(); | |
| if("OK".equalsIgnoreCase(status)){ | |
| message = ""; | |
| } | |
| if("FAIL".equalsIgnoreCase(status)){ | |
| message = so.getProperty("Message") + ""; | |
| } | |
| } | |
| } catch (Exception e) { | |
| status = "ERROR"; | |
| message = e.getMessage(); | |
| } | |
| return new HandlerResult(status, message, ""); | |
| } | |
| /** getPropertyInfo **/ | |
| /** creates PropertyInfo object for use when generating KSOAP envelopes **/ | |
| public static PropertyInfo getPropertyInfo(String name, String namespace, Object classType){ | |
| PropertyInfo pi = new PropertyInfo(); | |
| pi.setName(name); | |
| pi.setNamespace(namespace); | |
| pi.setType(classType); | |
| return pi; | |
| } | |
| /** LoginService **/ | |
| /** Quick method used to login to test web service **/ | |
| public static HandlerResult soapLoginService(String url, String login, String password) { | |
| String METHOD_NAME = "LoginService"; | |
| String status = ""; | |
| String message = ""; | |
| try { | |
| SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); | |
| request.addPropertyIfValue(getPropertyInfo("login", NAMESPACE, String.class), login); | |
| request.addPropertyIfValue(getPropertyInfo("password", NAMESPACE, String.class), password); | |
| SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); | |
| envelope.setOutputSoapObject(request); | |
| envelope.implicitTypes = true; | |
| envelope.dotNet = true; | |
| HttpTransportSE androidHttpTransport = new HttpTransportSE(url,5000); | |
| // androidHttpTransport.debug = true; | |
| androidHttpTransport.call(NAMESPACE + "/" + METHOD_NAME, envelope); | |
| // System.out.println(androidHttpTransport.requestDump); | |
| // System.out.println(androidHttpTransport.responseDump); | |
| Object result = envelope.getResponse(); | |
| if(result.getClass() == SoapObject.class){ | |
| SoapObject so = (SoapObject) result; | |
| status += so.getProperty("Status"); | |
| if("OK".equalsIgnoreCase(status)){ | |
| message = ""; | |
| } | |
| else{ | |
| message += so.getProperty("Message"); | |
| } | |
| } | |
| else{ | |
| status = "ERROR"; | |
| message = "SoapEnvelope Error"; | |
| } | |
| } catch (Exception e) { | |
| status = "ERROR"; | |
| message = e.getMessage(); | |
| } | |
| return new HandlerResult(status, message, ""); | |
| } | |
| /** LoginService **/ | |
| /** Quick method used to login to test web service **/ | |
| public HandlerResult soapLoginService(String login, String password) { | |
| String METHOD_NAME = "LoginService"; | |
| String status = ""; | |
| String message = ""; | |
| try { | |
| SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); | |
| request.addPropertyIfValue(getPropertyInfo("login", NAMESPACE, String.class), login); | |
| request.addPropertyIfValue(getPropertyInfo("password", NAMESPACE, String.class), password); | |
| SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); | |
| envelope.setOutputSoapObject(request); | |
| envelope.implicitTypes = true; | |
| envelope.dotNet = true; | |
| HttpTransportSE androidHttpTransport = new HttpTransportSE(URL,5000); | |
| // androidHttpTransport.debug = true; | |
| androidHttpTransport.call(NAMESPACE + "/" + METHOD_NAME, envelope); | |
| // System.out.println(androidHttpTransport.requestDump); | |
| // System.out.println(androidHttpTransport.responseDump); | |
| Object result = envelope.getResponse(); | |
| if(result.getClass() == SoapObject.class){ | |
| SoapObject so = (SoapObject) result; | |
| status += so.getProperty("Status"); | |
| if("OK".equalsIgnoreCase(status)){ | |
| message = ""; | |
| } | |
| else{ | |
| message += so.getProperty("Message"); | |
| } | |
| } | |
| else{ | |
| status = "ERROR"; | |
| message = "SoapEnvelope Error"; | |
| } | |
| } catch (Exception e) { | |
| status = "ERROR"; | |
| message = e.getMessage(); | |
| } | |
| return new HandlerResult(status, message, ""); | |
| } | |
| /** GetSequence **/ | |
| /** Gets the next sequence number for the given database and table **/ | |
| public int soapGetSequence(String databaseName, String sequenceName, String login, String password) { | |
| String METHOD_NAME = "getSequence"; | |
| String status = ""; | |
| try { | |
| SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); | |
| // Build Soap envelope | |
| request.addPropertyIfValue(getPropertyInfo("DatabaseName", NAMESPACE, String.class), databaseName); | |
| request.addPropertyIfValue(getPropertyInfo("SequenceName", NAMESPACE, String.class), sequenceName); | |
| request.addPropertyIfValue(getPropertyInfo("login", NAMESPACE, String.class), login); | |
| request.addPropertyIfValue(getPropertyInfo("password", NAMESPACE, String.class), password); | |
| SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); | |
| envelope.setOutputSoapObject(request); | |
| envelope.implicitTypes = true; | |
| envelope.dotNet = true; | |
| //HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); | |
| // androidHttpTransport.debug = true; | |
| _androidHttpTransport.call(NAMESPACE + "/" + METHOD_NAME, envelope); | |
| // System.out.println(androidHttpTransport.requestDump); | |
| // System.out.println(androidHttpTransport.responseDump); | |
| Object result = envelope.getResponse(); | |
| if(result.getClass() == SoapPrimitive.class){ | |
| SoapPrimitive sp = (SoapPrimitive) result; | |
| status += sp.toString(); | |
| } | |
| else{ | |
| status = "-999"; | |
| } | |
| } catch (Exception e) { | |
| status = "-998"; | |
| } | |
| return Integer.parseInt(status); | |
| } | |
| /** ConvertToXML **/ | |
| /** Converts the given Object to an XML String using the Simple-XML framework **/ | |
| public String ConvertToXML(Object obj){ | |
| Serializer serializer = new Persister(); | |
| ByteArrayOutputStream baos = new ByteArrayOutputStream(); | |
| try { | |
| serializer.write(obj, baos); | |
| } catch (IOException e) { | |
| // TODO Auto-generated catch block | |
| e.printStackTrace(); | |
| return e.getMessage(); | |
| } catch (Exception e) { | |
| // TODO Auto-generated catch block | |
| e.printStackTrace(); | |
| return e.getMessage(); | |
| } | |
| return "<?xml version=\"1.0\" encoding=\"utf-16\"?>" + CRLF + baos.toString(); | |
| } | |
| public class MyTransport extends HttpTransportSE{ | |
| private String myUrl; | |
| public MyTransport(String url, int timeout) { | |
| super(url, timeout); | |
| myUrl = url; | |
| } | |
| /** | |
| * set the desired soapAction header field | |
| * | |
| * @param soapAction | |
| * the desired soapAction | |
| * @param envelope | |
| * the envelope containing the information for the soap call. | |
| */ | |
| public void call(String soapAction, SoapEnvelope envelope) throws IOException, XmlPullParserException { | |
| if (soapAction == null) | |
| soapAction = "\"\""; | |
| byte[] requestData = createRequestData(envelope); | |
| requestDump = debug ? new String(requestData) : null; | |
| responseDump = null; | |
| ServiceConnection connection = new MyServiceConnectionSE(myUrl); | |
| //ServiceConnection connection = getServiceConnection(); | |
| connection.setRequestProperty("User-Agent", "kSOAP/2.0"); | |
| connection.setRequestProperty("SOAPAction", soapAction); | |
| connection.setRequestProperty("Content-Type", "text/xml"); | |
| connection.setRequestProperty("Connection", "close"); | |
| connection.setRequestProperty("Content-Length", "" + requestData.length); | |
| connection.setRequestMethod("POST"); | |
| connection.connect(); | |
| OutputStream os = connection.openOutputStream(); | |
| os.write(requestData, 0, requestData.length); | |
| os.flush(); | |
| os.close(); | |
| requestData = null; | |
| InputStream is; | |
| try { | |
| //connection.connect(); | |
| is = connection.openInputStream(); | |
| } catch (IOException e) { | |
| is = connection.getErrorStream(); | |
| if (is == null) { | |
| connection.disconnect(); | |
| throw (e); | |
| } | |
| } | |
| if (debug) { | |
| ByteArrayOutputStream bos = new ByteArrayOutputStream(); | |
| byte[] buf = new byte[256]; | |
| while (true) { | |
| int rd = is.read(buf, 0, 256); | |
| if (rd == -1) | |
| break; | |
| bos.write(buf, 0, rd); | |
| } | |
| bos.flush(); | |
| buf = bos.toByteArray(); | |
| responseDump = new String(buf); | |
| is.close(); | |
| is = new ByteArrayInputStream(buf); | |
| } | |
| parseResponse(envelope, is); | |
| is.close(); | |
| connection.disconnect(); | |
| //connection = null; | |
| String test = "just some code to test"; | |
| test += ""; | |
| } | |
| //class MyTransport | |
| public class MyServiceConnectionSE implements ServiceConnection { | |
| private HttpURLConnection connection; | |
| /** | |
| * Constructor taking the url to the endpoint for this soap communication | |
| * @param url the url to open the connection to. | |
| */ | |
| public MyServiceConnectionSE(String url) throws IOException { | |
| connection = (HttpURLConnection) new URL(url).openConnection(); | |
| connection.setUseCaches(false); | |
| connection.setDoOutput(true); | |
| connection.setDoInput(true); | |
| connection.setReadTimeout(30000); | |
| } | |
| public void connect() throws IOException { | |
| connection.connect(); | |
| } | |
| public void disconnect() { | |
| connection.disconnect(); | |
| } | |
| public void setRequestProperty(String string, String soapAction) { | |
| connection.setRequestProperty(string, soapAction); | |
| } | |
| public void setRequestMethod(String requestMethod) throws IOException { | |
| connection.setRequestMethod(requestMethod); | |
| } | |
| public OutputStream openOutputStream() throws IOException { | |
| return connection.getOutputStream(); | |
| } | |
| public InputStream openInputStream() throws IOException { | |
| return connection.getInputStream(); | |
| } | |
| public InputStream getErrorStream() { | |
| return connection.getErrorStream(); | |
| } | |
| @Override | |
| public String getHost() { | |
| // TODO Auto-generated method stub | |
| return null; | |
| } | |
| @Override | |
| public String getPath() { | |
| // TODO Auto-generated method stub | |
| return null; | |
| } | |
| @Override | |
| public int getPort() { | |
| // TODO Auto-generated method stub | |
| return 0; | |
| } | |
| @Override | |
| public List getResponseProperties() throws IOException { | |
| // TODO Auto-generated method stub | |
| return null; | |
| } | |
| //public class MyServiceConnectionSE | |
| } | |
| //public class MyTransport | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment