Task :
I have to a develop client which access a webservice (which in turn will communicate with another dummy webservice) to give back the response to the client.
What I have developed so far :
I have developed a client and two webservices.
My Code :
dummy webservice
- a webservice which will give a hard coded response
package groups.yashey; import javax.jws.WebService; import javax.jws.WebMethod; import javax.xml.soap.SOAPMessage; @WebService public interface SendFaxResponseInterface { @WebMethod public void sendFaxResponse(); } package groups.yashey; import javax.jws.WebService; import javax.jws.WebMethod; import java.io.FileOutputStream; import javax.xml.soap.MessageFactory; import javax.xml.soap.SOAPHeaderElement; import javax.xml.soap.SOAPBody; import javax.xml.soap.SOAPBodyElement; import javax.xml.soap.SOAPEnvelope; import javax.xml.soap.SOAPHeader; import javax.xml.soap.SOAPMessage; import javax.xml.soap.SOAPPart; @WebService public class SendFaxResponseWebService implements SendFaxResponseInterface { @WebMethod public void sendFaxResponse() { MessageFactory factory = null; SOAPMessage soapMsg = null; SOAPPart part = null; SOAPEnvelope envelope = null; SOAPHeader header = null; SOAPBody body = null; SOAPHeaderElement headerElement = null; SOAPBodyElement element = null; SOAPBodyElement soapElement1 = null; SOAPBodyElement soapElement2 = null; SOAPBodyElement soapChildElementOfSoapElement2 = null; SOAPBodyElement soapSecondChildElementOfSoapElement2 = null; UserDetailsBean userBeanObj = new UserDetailsBean(); userBeanObj.setUsername("admin"); userBeanObj.setPassword("admin"); if( userBeanObj.getUsername().equals("admin") && userBeanObj.getPassword().equals("admin") ) { try{ factory = MessageFactory.newInstance(); soapMsg = factory.createMessage(); part = soapMsg.getSOAPPart(); envelope = part.getEnvelope(); header = envelope.getHeader(); body = envelope.getBody(); headerElement = header.addHeaderElement(envelope.createName("Response","","http://ws.easylink.com/RequestResponse/2011/01")); headerElement.addChildElement("SenderKey").addTextNode("http://xoa.xpedite.com/JobSubmit"); headerElement.addChildElement("RequestID").addTextNode("BNP_TEST_JOB14"); headerElement.addChildElement("ProcessingID").addTextNode("0ACD321E-1163-130724184717275"); element = body.addBodyElement(envelope.createName("jobSubmitResult","","http://ws.easylink.com/JobSubmit/2011/01")); soapElement1 = (SOAPBodyElement) element.addChildElement("Status"); soapElement1.addChildElement("StatusCode").addTextNode("0"); soapElement1.addChildElement("SubmissionTime").addTextNode("2013-07-24T18:47:17.275Z"); soapElement1.addChildElement("CompletionTime").addTextNode("2013-07-24T18:47:18.180Z"); element.addChildElement("SubmitId").addTextNode("BNPTest1"); soapElement2 = (SOAPBodyElement) element.addChildElement("MessageResult"); soapElement2.addChildElement("MessageId").addTextNode("BNP_TEST1_MSG14"); soapChildElementOfSoapElement2 = (SOAPBodyElement) soapElement2.addChildElement("Status"); soapChildElementOfSoapElement2.addChildElement("StatusCode").addTextNode("0"); soapChildElementOfSoapElement2.addChildElement("StatusMessage").addTextNode("OK"); soapChildElementOfSoapElement2.addChildElement("SubmissionTime").addTextNode("2013-07-24T18:47:17.338Z"); soapChildElementOfSoapElement2.addChildElement("CompletionTime").addTextNode("2013-07-24T18:47:18.180Z"); soapSecondChildElementOfSoapElement2 = (SOAPBodyElement) soapElement2.addChildElement("JobId"); soapSecondChildElementOfSoapElement2.addChildElement("XDN").addTextNode("flusme"); soapSecondChildElementOfSoapElement2.addChildElement("MRN").addTextNode("1130365"); // writing the soap message to console //soapMsg.writeTo(System.out); // writing the soap message to file soapMsg.writeTo(new FileOutputStream("NewSoapMessage.xml")); System.out.println("Soap message written to the file"); }catch(Exception e){ e.printStackTrace(); } } } }
another Webservice
- this is responsible to access the dummy webservice on the client request
package groups.yashey; import javax.jws.WebService; import javax.jws.WebMethod; @WebService public interface SendFaxClientWSInterface { @WebMethod public void sendFax(); } package groups.yashey; import javax.jws.WebService; import javax.jws.WebMethod; @WebService public class SendFaxClientWS implements SendFaxClientWSInterface { @WebMethod public void sendFax() { new SendFaxResponseWebServiceService(). getSendFaxResponseWebServicePort().sendFaxResponse(); } }
client
- a stand alone program to call the webservice.
import groups.yashey.*; public class SendFaxWebServiceClient { public static void main(String...ar) { new SendFaxClientWSService().getSendFaxClientWSPort().sendFax(); } }
extra helper classes
- this class helps to get the data to the webservice
package groups.yashey; public class UserDetailsBean { private String username; private String password; public UserDetailsBean() { System.out.println("UserDetailsBean Object is Created"); } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
- these classes are used to publish the webservices
Errors Iam getting :package groups.yashey; import groups.yashey.SendFaxResponseInterface; import groups.yashey.SendFaxResponseWebService; import groups.yashey.SendFaxClientWSInterface; import groups.yashey.SendFaxClientWS; import javax.xml.ws.Endpoint; public class PublishWebService1 { public static void main(String...ar) { SendFaxResponseInterface sendFaxWebService = new SendFaxResponseWebService(); //SendFaxClientWSInterface sendFaxWebServiceClient = new SendFaxClientWS(); Endpoint.publish("http://localhost:7770/sendFaxWebService", sendFaxWebService); //Endpoint.publish("http://localhost:7771/sendFaxWebServiceClient", sendFaxWebServiceClient); System.out.println("SendFax webservice is ready..."); //System.out.println("SendFax webservice client is also ready..."); } } package groups.yashey; import groups.yashey.SendFaxResponseInterface; import groups.yashey.SendFaxResponseWebService; import groups.yashey.SendFaxClientWSInterface; import groups.yashey.SendFaxClientWS; import javax.xml.ws.Endpoint; public class PublishWebService2 { public static void main(String...ar) { SendFaxClientWSInterface sendFaxWebServiceClient = new SendFaxClientWS(); Endpoint.publish("http://localhost:7771/sendFaxWebServiceClient", sendFaxWebServiceClient); //System.out.println("SendFax webservice is ready..."); System.out.println("SendFax webservice client is also ready..."); } }
- both are run time errors
- one is saying my service is not an interface
- other one throwing IllegalArgumentException at my dummy web service
C:\Users\Yaswanth\Desktop>java SendFaxWebServiceClient Exception in thread "main" javax.xml.ws.soap.SOAPFaultException: groups.yashey.SendFaxResponseWebService is not an interface at com.sun.xml.internal.ws.fault.SOAP11Fault.getProtocolException(SOAP11Fault.java:178) at com.sun.xml.internal.ws.fault.SOAPFaultBuilder.createException(SOAPFaultBuilder.java:111) at com.sun.xml.internal.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:108) at com.sun.xml.internal.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:78) at com.sun.xml.internal.ws.client.sei.SEIStub.invoke(SEIStub.java:107) at com.sun.proxy.$Proxy27.sendFax(Unknown Source) at SendFaxWebServiceClient.main(SendFaxWebServiceClient.java:9) C:\Users\Yaswanth\Desktop\MyOwn WebServices>java groups.yashey.PublishWebService2 Sep 2, 2014 1:06:21 PM com.sun.xml.internal.ws.model.RuntimeModeler getRequestWrapperClass INFO: Dynamically creating request wrapper Class groups.yashey.jaxws.SendFax Sep 2, 2014 1:06:21 PM com.sun.xml.internal.ws.model.RuntimeModeler getResponseWrapperClass INFO: Dynamically creating response wrapper bean Class groups.yashey.jaxws.SendFaxResponse SendFax webservice client is also ready... Sep 2, 2014 1:08:08 PM com.sun.xml.internal.ws.model.RuntimeModeler getResponseWrapperClass INFO: Dynamically creating response wrapper bean Class groups.yashey.jaxws.SendFaxResponseResponse Sep 2, 2014 1:08:08 PM com.sun.xml.internal.ws.server.sei.EndpointMethodHandler invoke SEVERE: groups.yashey.SendFaxResponseWebService is not an interface java.lang.IllegalArgumentException: groups.yashey.SendFaxResponseWebService is not an interface at java.lang.reflect.Proxy.getProxyClass0(Proxy.java:470) at java.lang.reflect.Proxy.newProxyInstance(Proxy.java:690) at com.sun.xml.internal.ws.client.WSServiceDelegate$4.run(WSServiceDelegate.java:629) at java.security.AccessController.doPrivileged(Native Method) at com.sun.xml.internal.ws.client.WSServiceDelegate.createProxy(WSServiceDelegate.java:625) at com.sun.xml.internal.ws.client.WSServiceDelegate.createEndpointIFBaseProxy(WSServiceDelegate.java:610) at com.sun.xml.internal.ws.client.WSServiceDelegate.getPort(WSServiceDelegate.java:311) at com.sun.xml.internal.ws.client.WSServiceDelegate.getPort(WSServiceDelegate.java:293) at com.sun.xml.internal.ws.client.WSServiceDelegate.getPort(WSServiceDelegate.java:275) at javax.xml.ws.Service.getPort(Service.java:92) at groups.yashey.SendFaxResponseWebServiceService.getSendFaxResponseWebServicePort(SendFaxResponseWebServiceService.java:56) at groups.yashey.CallSendFaxService.callSendFaxService(CallSendFaxService.java:5) at groups.yashey.SendFaxClientWS.sendFax(SendFaxClientWS.java:12) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at com.sun.xml.internal.ws.api.server.InstanceResolver$1.invoke(InstanceResolver.java:235) at com.sun.xml.internal.ws.server.InvokerTube$2.invoke(InvokerTube.java:135) at com.sun.xml.internal.ws.server.sei.EndpointMethodHandler.invoke(EndpointMethodHandler.java:246) at com.sun.xml.internal.ws.server.sei.SEIInvokerTube.processRequest(SEIInvokerTube.java:82) at com.sun.xml.internal.ws.api.pipe.Fiber.__doRun(Fiber.java:587) at com.sun.xml.internal.ws.api.pipe.Fiber._doRun(Fiber.java:546) at com.sun.xml.internal.ws.api.pipe.Fiber.doRun(Fiber.java:531) at com.sun.xml.internal.ws.api.pipe.Fiber.runSync(Fiber.java:428) at com.sun.xml.internal.ws.server.WSEndpointImpl$2.process(WSEndpointImpl.java:232) at com.sun.xml.internal.ws.transport.http.HttpAdapter$HttpToolkit.handle(HttpAdapter.java:460) at com.sun.xml.internal.ws.transport.http.HttpAdapter.handle(HttpAdapter.java:233) at com.sun.xml.internal.ws.transport.http.server.WSHttpHandler.handleExchange(WSHttpHandler.java:95) at com.sun.xml.internal.ws.transport.http.server.WSHttpHandler.handle(WSHttpHandler.java:80) at com.sun.net.httpserver.Filter$Chain.doFilter(Filter.java:65) at sun.net.httpserver.AuthFilter.doFilter(AuthFilter.java:65) at com.sun.net.httpserver.Filter$Chain.doFilter(Filter.java:68) at sun.net.httpserver.ServerImpl$Exchange$LinkHandler.handle(ServerImpl.java:557) at com.sun.net.httpserver.Filter$Chain.doFilter(Filter.java:65) at sun.net.httpserver.ServerImpl$Exchange.run(ServerImpl.java:529) at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918) at java.lang.Thread.run(Thread.java:662)