Jump to content
hypersk

Creating Attachments via WebServices throws "cannot be cast" exception

Recommended Posts

Hi guys.

I'm trying to do a simple Java application which consumes a login and a CreateAttachment methods for an integration with CA ServiceDesk, but my application fails when I try to run it.

I created a new topic because I cannot find a metion of this exception in any topic.

Here's my code:

package casd_attachment;

import java.io.*;
import javax.activation.*;
import org.apache.axis.client.Call;

public class Main {

    public static void main(String[] args) {
        
        try { // Call Web Service Operation
            //WS URL: http://192.168.1.120:8080/axis/services/USD_R11_WebService?WSDL
            com.ca.unicenterserviceplus.servicedesk.USDWebService service = new com.ca.unicenterserviceplus.servicedesk.USDWebService();
            com.ca.unicenterserviceplus.servicedesk.USDWebServiceSoap port = service.getUSDWebServiceSoap();

            // TODO initialize WS operation arguments here
            java.lang.String username = "ServiceDesk";
            java.lang.String password = "ServiceDesk";

            // TODO process result here
            int result = port.login(username, password);
            System.out.println("Result = "+result);

            int sid = result;
            java.lang.String repositoryHandle = "doc_rep:1002";
            //Let's try the persid of the existing incident 2043359
            java.lang.String objectHandle = "cr:400009";
            java.lang.String description = "Attachment test";
            java.lang.String fileName = "E:\\hello.txt";

            File file = new File(fileName);
            DataHandler dataHandler = new DataHandler(new FileDataSource(file));

            ((org.apache.axis.client.Stub)port)._setProperty(Call.ATTACHMENT_ENCAPSULATION_FORMAT, Call.ATTACHMENT_ENCAPSULATION_FORMAT_DIME);
            ((org.apache.axis.client.Stub)port).addAttachment(dataHandler);

             String attachID = port.createAttachment(sid, repositoryHandle, objectHandle, description, description.substring(0, 10));
             System.out.println(attachID);

        } catch (Exception ex) {
            // TODO handle custom exceptions here
            ex.printStackTrace();
        }

    }

}

The code compiles fine, but when I try to run it I get the following exception:

java.lang.ClassCastException: $Proxy28 cannot be cast to org.apache.axis.client.Stub

I also have tried changing the following line:

((org.apache.axis.client.Stub)port)._setProperty(Call.ATTACHMENT_ENCAPSULATION_FORMAT, Call.ATTACHMENT_ENCAPSULATION_FORMAT_DIME);

To this:

((javax.xml.rpc.Stub)port)._setProperty(org.apache.axis.client.Call.ATTACHMENT_ENCAPSULATION_FORMAT, Call.ATTACHMENT_ENCAPSULATION_FORMAT_DIME);

But at runtime, it throws the same exception (now applied to this class):

java.lang.ClassCastException: $Proxy28 cannot be cast to javax.xml.rpc.Stub

Any ideas?

Thanks in advance.

Share this post


Link to post
Share on other sites

Hello!

The code below is the servlet in charge of upload the file to the server, so when it was uploaded calls the USD-Method doHelpDeskAttachment to put the file into the ticket(USD-side). Finally erases uploaded file.

---Struts-----
         List errorMsgs = new LinkedList();
         FormFile attmntF = null;
         String fileNam = null;
         String attmntPath = null;
         String fullPath = null;
         Request sdReq = null;
try{
                
                attmntPath = this.getServlet().getServletContext().getInitParameter("attmntPath").toString();
                sdReq = (Request)request.getAttribute("hd_obj");
                
                attmntF = (FormFile) request.getAttribute("file");
                fileNam = attmntF.getFileName();
                fullPath = attmntPath + fileNam;

                File file = new File (fullPath);

                FileOutputStream out = new FileOutputStream(fullPath);
                out.write(attmntF.getFileData());
                out.flush();
                out.close();

                attmntF.destroy();

                if(sdReq.doHelpDeskAttachment(fileNam, fullPath)){
                     this.getServlet().getServletContext().log("Request: " + sdReq.getRequestNumberValue()
                             + " Attachment: " + sdReq.getAttachmentHandle());
                     sdReq.logOut();
                     request.setAttribute("hd_obj", sdReq);
                    
                     file.delete();
                     return mapping.findForward("success");
                }else{
                     sdReq.logOut();
                     errorMsgs.add("Error. No se adjuntó el archivo para el request: "+
                               sdReq.getRequestNumberValue());
                     request.setAttribute("hd_obj", sdReq);
                     return mapping.findForward("successNA");
                    }
                
            }catch(Exception e){
                errorMsgs.add("An unexpected error: " + e.getMessage());
                this.getServlet().getServletContext().log(e.getMessage());
                request.setAttribute("errorMsgs", errorMsgs);
                e.printStackTrace();
                return mapping.findForward("generalError");
            }

The following code is the final method to upload a file. As you can see one improvement has made and it is the use of FileDataSource and DataHandler data types. Hope it helps.

---Class---
import java.rmi.RemoteException;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.xml.rpc.holders.StringHolder;

   private USD_WebServiceSoap USD;

   public boolean doHelpDeskAttachment(String fileDesc, String fileName){
        try {
            FileDataSource fds = new FileDataSource(fileName);
            DataHandler dhandler = new DataHandler(fds);
((javax.xml.rpc.Stub) USD)._setProperty(org.apache.axis.client.Call.ATTACHMENT_ENCAPSULATION_FORMAT, org.apache.axis.client.Call.ATTACHMENT_ENCAPSULATION_FORMAT_DIME);
            ((org.apache.axis.client.Stub) USD).addAttachment(dhandler);
            attachmentHandle = USD.createAttachment(this.SID, this.repositoryHandle, this.requestHandleValue, fileDesc, fileName);
            if (getAttachmentHandle() == null) {
                return false;
            } else {
                return true;
            }
        } catch (RemoteException ex) {
            System.out.println(ex.getMessage());
            return false;
        }
    }

You're missing org.apache.axis.client:

((javax.xml.rpc.Stub) USD)._setProperty(org.apache.axis.client.Call.ATTACHMENT_ENCAPSULATION_FORMAT, org.apache.axis.client.Call.ATTACHMENT_ENCAPSULATION_FORMAT_DIME);

Edited by Jose Manuel

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×