0 Replies Latest reply on May 31, 2006 6:06 AM by balteo

    Beginner needs help with SOAP with attachments

    balteo

      Hello,

      I would like for a ws client to retrieve an image from a ws endpoint. I am totally confused as to what encoding style to choose as well as how to implement it.

      Here is the code for the client:

      package pack;
      
      
      import java.awt.Image;
      import java.io.*;
      
      import javax.servlet.*;
      import javax.servlet.http.*;
      
      public class ServletFetchesAttachment extends HttpServlet {
      
       protected void processRequest(HttpServletRequest request, HttpServletResponse response)
       throws ServletException, IOException {
      
       System.out.println("processRequest");
      
       PrintWriter out = response.getWriter();
       try {
       Image img = getProvidesAttachmentSEIPort().retrieveAttachment();
       } catch(java.rmi.RemoteException ex) {
       ex.printStackTrace();
       } catch(Exception ex) {
       ex.printStackTrace();
       }
       out.close();
       }
      
       // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
       /** Handles the HTTP <code>GET</code> method.
       * @param request servlet request
       * @param response servlet response
       */
       protected void doGet(HttpServletRequest request, HttpServletResponse response)
       throws ServletException, IOException {
       processRequest(request, response);
       }
      
       /** Handles the HTTP <code>POST</code> method.
       * @param request servlet request
       * @param response servlet response
       */
       protected void doPost(HttpServletRequest request, HttpServletResponse response)
       throws ServletException, IOException {
       processRequest(request, response);
       }
      
       /** Returns a short description of the servlet.
       */
       public String getServletInfo() {
       return "Short description";
       }
       // </editor-fold>
      
       private pack.ProvidesAttachment getProvidesAttachment() {
       pack.ProvidesAttachment providesAttachment = null;
       try {
       javax.naming.InitialContext ic = new javax.naming.InitialContext();
       providesAttachment = (pack.ProvidesAttachment) ic.lookup("java:comp/env/service/ProvidesAttachment");
       } catch(javax.naming.NamingException ex) {
       ex.printStackTrace();
       }
       return providesAttachment;
       }
      
       private pack.ProvidesAttachmentSEI getProvidesAttachmentSEIPort() {
       pack.ProvidesAttachmentSEI providesAttachmentSEIPort = null;
       try {
       providesAttachmentSEIPort = getProvidesAttachment().getProvidesAttachmentSEIPort();
       } catch(javax.xml.rpc.ServiceException ex) {
       ex.printStackTrace();
      
       }
       return providesAttachmentSEIPort;
       }
      
      
      }
      
      



      Here is the code for the endpoint:

      package pack;
      
      import java.awt.Image;
      import java.awt.Toolkit;
      import java.net.MalformedURLException;
      import java.net.URL;
      
      
      public class ProvidesAttachmentImpl implements ProvidesAttachmentSEI{
      
       public Image retrieveAttachment() throws java.rmi.RemoteException {
       URL url=null;
       Image image = null;
       try {
       url = new URL("file:///home//julien//tests//logo.gif");
       image = Toolkit.getDefaultToolkit().createImage(url);
      
       } catch (MalformedURLException ex) {
       ex.printStackTrace();
       }
       return image;
       }
      
      }
      


      Here is the wsdl:

      <?xml version="1.0" encoding="UTF-8"?>
      <definitions name="ProvidesAttachment" targetNamespace="urn:ProvidesAttachment/wsdl" xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:ns2="http://java.sun.com/jax-rpc-ri/internal" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="urn:ProvidesAttachment/wsdl" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
       <types>
       <schema targetNamespace="http://java.sun.com/jax-rpc-ri/internal" xmlns="http://www.w3.org/2001/XMLSchema" xmlns:soap11-enc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="http://java.sun.com/jax-rpc-ri/internal" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
       <import namespace="http://schemas.xmlsoap.org/soap/encoding/"/>
       <simpleType name="image">
       <restriction base="base64Binary"/></simpleType></schema>
       </types>
       <message name="ProvidesAttachmentSEI_retrieveAttachmentResponse">
       <part name="result" type="ns2:image"/>
      
       </message>
       <message name="ProvidesAttachmentSEI_retrieveAttachment">
       </message>
       <portType name="ProvidesAttachmentSEI">
       <operation name="retrieveAttachment">
       <input message="tns:ProvidesAttachmentSEI_retrieveAttachment"/>
       <output message="tns:ProvidesAttachmentSEI_retrieveAttachmentResponse"/>
       </operation>
       </portType>
      
       <binding name="ProvidesAttachmentSEIBinding" type="tns:ProvidesAttachmentSEI">
       <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
       <operation name="retrieveAttachment">
       <soap:operation soapAction=""/>
       <input>
       <soap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:ProvidesAttachment/wsdl" use="encoded"/>
       </input>
       <output>
       <soap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:ProvidesAttachment/wsdl" use="encoded"/>
      
       </output>
       </operation>
       </binding>
       <service name="ProvidesAttachment">
       <port binding="tns:ProvidesAttachmentSEIBinding" name="ProvidesAttachmentSEIPort">
       <soap:address location="http://ordinateur:8080/WebAppProvidesAttachment/ProvidesAttachment"/>
       </port>
       </service>
      </definitions>
      


      Any help greatly appreciated.

      Thanks in advance,

      Julien.