This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Web Services and the fly likes How to access byte array web method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Web Services
Bookmark "How to access byte array web method" Watch "How to access byte array web method" New topic
Author

How to access byte array web method

Avinash Annamaneni
Greenhorn

Joined: Nov 21, 2010
Posts: 7

Hi guys,
i am new to web services, please help me out!!

Let me explain what i have to do and what i have done.

Task : I have to access a web method which looks like this

Sample method : basically i have to access a method which takes byte[] input and gives byte[] output (dynamically)

@WebMethod()
public byte[] sayGoodbye(byte[] input) {
byte[] temp = input;
System.out.println("The byte array is --> "+ temp.toString());
return temp;
}


Back ground info. on the web service
1. The web service is deployed in Web Logic Server
2. I need to access that method from JBOSS SOA 5.0 AS

Things i have tried

1. Create stud, skeleton using clinetGen and access the method from a standalone java class...it is working, but i am not allowed to proceed in this way, as this way we are dependent on weblogic.jar and webservices.jar of Web Logic.

2. So i used WSimport to create the stud, skeleton and wrote a standalone java class to access the method, i am getting the error : Cannot create service instance, that particular service is not present in WSDL. (but it is present in wsdl, i am able to see it in WSDL) Can anyone tell me why i am getting error like this???

3. I also tried to create a SOAP message and access it, in this way i am not able to add byte[] data to the SOAP message. Can we actually do it in any way???

/** Create a service and add at least one port to it. **/
Service service = Service.create(serviceName);
service.addPort(portName, SOAPBinding.SOAP11HTTP_BINDING, endpointUrl);

/** Create a Dispatch instance from a service.**/
Dispatch<SOAPMessage> dispatch = service.createDispatch(portName, SOAPMessage.class, Service.Mode.MESSAGE);

/** Create SOAPMessage request. **/
// compose a request message
MessageFactory mf = MessageFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);


/** Create a message. This example works with the SOAPPART. **/
SOAPMessage request = mf.createMessage();
SOAPPart part = request.getSOAPPart();


/** Obtain the SOAPEnvelope and header and body elements. **/
SOAPEnvelope env = part.getEnvelope();
SOAPHeader header = env.getHeader();
SOAPBody body = env.getBody();


/** Construct the message payload. **/
SOAPElement operation = body.addChildElement("sayGoddbye", "ns1", "http://webservices.samples.jboss.org/"); // sayGoodbye is the web method
SOAPElement value = operation.addChildElement("arg0");
value.addTextNode(finalstr); // 'finalstr' is a String, here i need to add a byte[], how do i do that???

request.saveChanges();

/** Invoke the service endpoint. **/
SOAPMessage responseMessage = dispatch.invoke(request);


I want to know, how to access a web method which takes in byte[] and gives byte[] , dynamically???

Is there any other way to access this web method using JAX WS and not JAX RPC???

Thank you guys
Naren Chivukula
Ranch Hand

Joined: Feb 03, 2004
Posts: 576

Hi Avinash,

Create stud, skeleton using clinetGen and access the method from a standalone java class...it is working


Are you using Weblogic container jars in your standalone java client program classpath to make it work here?

Can anyone tell me why i am getting error like this???

I don't have enough details to understand the problem. Can you post your WSDL?

I want to know, how to access a web method which takes in byte[] and gives byte[] , dynamically???

Is there any other way to access this web method using JAX WS and not JAX RPC???

You can do it in both JAX-WS and JAX-RPC. Usually byte[] in java is mapped to xsd:base64Binary in WSDL. But you can also use xsd:HexBinary.

I also tried to create a SOAP message and access it, in this way i am not able to add byte[] data to the SOAP message. Can we actually do it in any way???

You can try using the same method as above by converting byte[] to base64Binary string. Refer this post for encoding and decoding classes.


Cheers,
Naren
(OCEEJBD6, SCWCD5, SCDJWS, SCJP1.4 and Oracle SQL 1Z0-051)
Avinash Annamaneni
Greenhorn

Joined: Nov 21, 2010
Posts: 7

Hi Naren,

- Yes i am using weblogic.jar and webservices.jar in that standalone java class's classpath to make it work.
- WSDL file , i have attached
- I did the encoding of the byte array to base64Binary String in this way, but it is not working?? please check


// Encoding to base64Binary



// inside main()




//////////////////////////// WSDL File /////////////////////////////////////




Thank you

Naren Chivukula
Ranch Hand

Joined: Feb 03, 2004
Posts: 576

Hi Avinash,
I did the encoding of the byte array to base64Binary String in this way, but it is not working?? please check


What is the exception you are getting? Can you post detailed stack trace on client and service ends clearly?
Avinash Annamaneni
Greenhorn

Joined: Nov 21, 2010
Posts: 7

Hi Naren,

This is the exception i am getting on the client side

org.w3c.dom.DOMException: INVALID_CHARACTER_ERR: An invalid or illegal XML character is specified.
at com.sun.org.apache.xerces.internal.dom.CoreDocumentImpl.checkQName(Unknown Source)
at com.sun.org.apache.xerces.internal.dom.ElementNSImpl.setName(Unknown Source)
at com.sun.org.apache.xerces.internal.dom.ElementNSImpl.<init>(Unknown Source)
at com.sun.xml.internal.messaging.saaj.soap.impl.ElementImpl.<init>(Unknown Source)
at com.sun.xml.internal.messaging.saaj.soap.impl.ElementFactory.createElement(Unknown Source)
at com.sun.xml.internal.messaging.saaj.soap.SOAPDocumentImpl.createElement(Unknown Source)
at com.sun.xml.internal.messaging.saaj.soap.impl.ElementImpl.createElement(Unknown Source)
at com.sun.xml.internal.messaging.saaj.soap.impl.ElementImpl.addElement(Unknown Source)
at com.sun.xml.internal.messaging.saaj.soap.impl.ElementImpl.addChildElement(Unknown Source)
at com.sun.xml.internal.messaging.saaj.soap.impl.ElementImpl.addChildElement(Unknown Source)
at com.hex.test.DynamicUPMCall.main(DynamicUPMCall.java:180)
This is the response ---> env:ClientEndpoint {http://webservices.samples.jboss.org/}HelloWorldPort does not contain operation meta data for: {http://webservices.samples.jboss.org/}sayGoddbye



Exception on the server side

14:12:40,054 ERROR [SOAPFaultHelperJAXWS] SOAP request exception
org.jboss.ws.core.CommonSOAPFaultException: Endpoint {http://webservices.samples.jboss.org/}HelloWorldPort does not contain operation meta data for: {http://webservices.samples.jboss.org/}sayGoddbye
at org.jboss.ws.core.server.ServiceEndpointInvoker.getDispatchDestination(ServiceEndpointInvoker.java:474)
at org.jboss.ws.core.server.ServiceEndpointInvoker.invoke(ServiceEndpointInvoker.java:177)
at org.jboss.wsf.stack.jbws.RequestHandlerImpl.processRequest(RequestHandlerImpl.java:468)
at org.jboss.wsf.stack.jbws.RequestHandlerImpl.handleRequest(RequestHandlerImpl.java:293)
at org.jboss.wsf.stack.jbws.RequestHandlerImpl.doPost(RequestHandlerImpl.java:203)
at org.jboss.wsf.stack.jbws.RequestHandlerImpl.handleHttpRequest(RequestHandlerImpl.java:129)
at org.jboss.wsf.common.servlet.AbstractEndpointServlet.service(AbstractEndpointServlet.java:85)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:235)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:190)
at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:92)
at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.process(SecurityContextEstablishmentValve.java:126)
at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:70)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:330)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:829)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:598)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:619)
14:12:40,148 ERROR [SOAPFaultHelperJAXRPC] SOAP request exception
javax.xml.rpc.soap.SOAPFaultException: Endpoint {http://webservices.samples.jboss.org/}HelloWorldPort does not contain operation meta data for: {http://webservices.samples.jboss.org/}sayGoddbye
at org.jboss.ws.core.jaxrpc.SOAPFaultHelperJAXRPC.exceptionToFaultMessage(SOAPFaultHelperJAXRPC.java:189)
at org.jboss.ws.core.jaxws.SOAPFaultHelperJAXWS.exceptionToFaultMessage(SOAPFaultHelperJAXWS.java:183)
at org.jboss.ws.core.jaxws.binding.SOAP11BindingJAXWS.createFaultMessageFromException(SOAP11BindingJAXWS.java:102)
at org.jboss.ws.core.CommonSOAPBinding.bindFaultMessage(CommonSOAPBinding.java:650)
at org.jboss.ws.core.server.ServiceEndpointInvoker.invoke(ServiceEndpointInvoker.java:283)
at org.jboss.wsf.stack.jbws.RequestHandlerImpl.processRequest(RequestHandlerImpl.java:468)
at org.jboss.wsf.stack.jbws.RequestHandlerImpl.handleRequest(RequestHandlerImpl.java:293)
at org.jboss.wsf.stack.jbws.RequestHandlerImpl.doPost(RequestHandlerImpl.java:203)
at org.jboss.wsf.stack.jbws.RequestHandlerImpl.handleHttpRequest(RequestHandlerImpl.java:129)
at org.jboss.wsf.common.servlet.AbstractEndpointServlet.service(AbstractEndpointServlet.java:85)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:235)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:190)
at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:92)
at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.process(SecurityContextEstablishmentValve.java:126)
at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:70)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:330)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:829)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:598)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:619)


Thank you
Naren Chivukula
Ranch Hand

Joined: Feb 03, 2004
Posts: 576



sayGoodbye operation doesn't exist in your WSDL. Are you using right WSDL for this?
Avinash Annamaneni
Greenhorn

Joined: Nov 21, 2010
Posts: 7

Hi Naren,

The wsdl i have sent to you is not for this method...the problem i am getting with that wsdl's client is Cannot create service

The WSDl for the sayGoodbye operation is this

WSDL

<definitions name='HelloWorldService' targetNamespace='http://webservices.samples.jboss.org/' xmlns='http://schemas.xmlsoap.org/wsdl/' xmlns:soap='http://schemas.xmlsoap.org/wsdl/soap/' xmlns:tns='http://webservices.samples.jboss.org/' xmlns:xsd='http://www.w3.org/2001/XMLSchema'>
<types>
<xs:schema targetNamespace='http://webservices.samples.jboss.org/' version='1.0' xmlns:tns='http://webservices.samples.jboss.org/' xmlns:xs='http://www.w3.org/2001/XMLSchema'>
<xs:element name='sayGoodbye' type='tns:sayGoodbye'/>
<xs:element name='sayGoodbyeResponse' type='tns:sayGoodbyeResponse'/>
<xs:element name='sayHello' type='tns:sayHello'/>
<xs:element name='sayHelloResponse' type='tns:sayHelloResponse'/>
<xs:complexType name='sayHello'>
<xs:sequence>
<xs:element minOccurs='0' name='arg0' type='xs:string'/>
</xs:sequence>
</xs:complexType>
<xs:complexType name='sayHelloResponse'>
<xs:sequence>
<xs:element minOccurs='0' name='return' type='xs:string'/>
</xs:sequence>
</xs:complexType>
<xs:complexType name='sayGoodbye'>
<xs:sequence>
<xs:element minOccurs='0' name='arg0' type='xs:base64Binary'/>
</xs:sequence>
</xs:complexType>
<xs:complexType name='sayGoodbyeResponse'>
<xs:sequence>
<xs:element minOccurs='0' name='return' type='xs:base64Binary'/>
</xs:sequence>
</xs:complexType>
</xs:schema>
</types>
<message name='HelloWorld_sayHello'>
<part element='tns:sayHello' name='sayHello'></part>
</message>
<message name='HelloWorld_sayGoodbye'>
<part element='tns:sayGoodbye' name='sayGoodbye'></part>
</message>
<message name='HelloWorld_sayHelloResponse'>
<part element='tns:sayHelloResponse' name='sayHelloResponse'></part>
</message>
<message name='HelloWorld_sayGoodbyeResponse'>
<part element='tns:sayGoodbyeResponse' name='sayGoodbyeResponse'></part>
</message>
<portType name='HelloWorld'>
<operation name='sayGoodbye' parameterOrder='sayGoodbye'>
<input message='tns:HelloWorld_sayGoodbye'></input>
<output message='tns:HelloWorld_sayGoodbyeResponse'></output>
</operation>
<operation name='sayHello' parameterOrder='sayHello'>
<input message='tns:HelloWorld_sayHello'></input>
<output message='tns:HelloWorld_sayHelloResponse'></output>
</operation>
</portType>
<binding name='HelloWorldBinding' type='tns:HelloWorld'>
<soap:binding style='document' transport='http://schemas.xmlsoap.org/soap/http'/>
<operation name='sayGoodbye'>
<soapperation soapAction=''/>
<input>
<soap:body use='literal'/>
</input>
<output>
<soap:body use='literal'/>
</output>
</operation>
<operation name='sayHello'>
<soapperation soapAction=''/>
<input>
<soap:body use='literal'/>
</input>
<output>
<soap:body use='literal'/>
</output>
</operation>
</binding>
<service name='HelloWorldService'>
<port binding='tns:HelloWorldBinding' name='HelloWorldPort'>
<soap:address location='http://localhost:8080/SampleWS/HelloWorld'/>
</port>
</service>
</definitions>


Thank you
Naren Chivukula
Ranch Hand

Joined: Feb 03, 2004
Posts: 576

Hi Avinash,

The actual operation name in WSDL is 'sayGoodbye'.

But, you are trying to access non-existant 'sayGoddbye' operation. Correct the operation name!
Avinash Annamaneni
Greenhorn

Joined: Nov 21, 2010
Posts: 7

Hi Naren,

Silly me...i changed the name of the opertaion,

now i am able to reach it but i am not getting the response back

and i am getting the following error in the clinet side:

org.w3c.dom.DOMException: INVALID_CHARACTER_ERR: An invalid or illegal XML character is specified.
at com.sun.org.apache.xerces.internal.dom.CoreDocumentImpl.checkQName(Unknown Source)
at com.sun.org.apache.xerces.internal.dom.ElementNSImpl.setName(Unknown Source)
at com.sun.org.apache.xerces.internal.dom.ElementNSImpl.<init>(Unknown Source)
at com.sun.xml.internal.messaging.saaj.soap.impl.ElementImpl.<init>(Unknown Source)
at com.sun.xml.internal.messaging.saaj.soap.impl.ElementFactory.createElement(Unknown Source)
at com.sun.xml.internal.messaging.saaj.soap.SOAPDocumentImpl.createElement(Unknown Source)
at com.sun.xml.internal.messaging.saaj.soap.impl.ElementImpl.createElement(Unknown Source)
at com.sun.xml.internal.messaging.saaj.soap.impl.ElementImpl.addElement(Unknown Source)
at com.sun.xml.internal.messaging.saaj.soap.impl.ElementImpl.addChildElement(Unknown Source)
at com.sun.xml.internal.messaging.saaj.soap.impl.ElementImpl.addChildElement(Unknown Source)
at com.hex.test.DynamicUPMCall.main(DynamicUPMCall.java:182)


The method i have mentioned in my 1st post is the actual method i am trying to reach...the temp.toString is getting printed in the server, and no error in the server...but no response in the client.

Thank you
Naren Chivukula
Ranch Hand

Joined: Feb 03, 2004
Posts: 576

Hi Avinash,
Clearly, you response SOAP message is badly formed for the parser to understand. Can you post your reponse SOAP message?

If you don't know how to catch hold of resposne SOAP message, you can try to get it from SOAPUI. It's an excellent tool to troubleshoot.
Avinash Annamaneni
Greenhorn

Joined: Nov 21, 2010
Posts: 7

Hi Naren,

I have solved the issue...thank you

Problem - I want to know, how to access a web method which takes in byte[] and gives byte[] , dynamically???

Solution - You can do this in the following way
- encoding the byte array to base64Binary String
- adding the String to the SOAP message (Element), using the .addTextNode(); method
- then decode the output byte[] to normal byte[]
and get the value from the byte array.

Encoding the Byte[] to base64Binary



Decoding the output



Viewing data from byte[] output

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to access byte array web method
 
Similar Threads
creating soap requests in java
SAAJ - Empty SOAP Response / Null Pointer Exception
SOAP Request object is null
SAAJ client to simple hello world method
Webservice Help!