CA Service Management

 View Only
  • 1.  createAttachment webservice ca servicedesk

    Posted May 12, 2021 11:51 PM
    Hello good day

    My problem is that I can't use the createAttachment method because I don't know how to upload the file, I don't know what I'm missing

    my code:
    try {

    int sid = wsUSD.login("xxxxxxxxxx", "xxxxxx");

    System.out.println("SID del CA: " + sid);
    try {
    FileDataSource fds = new FileDataSource(filename);
    DataHandler dhandler = new DataHandler(fds);

    String handle = wsUSD.createAttachment(sid, "doc_rep:1002", "cr:11494369", "comment", filename);
    System.out.println("Este es el Handle de archivo: " + handle);

    } catch (Exception ex){

    System.out.println("Creating attachment...FAIL: " + ex);

    }
    wsUSD.logout(sid);
    System.out.println("logout ");


    } catch (Exception ex) {
    System.out.println("creatin SID...FAIL: " + ex);
    }

    I am using java 8 with jax-ws



  • 2.  RE: createAttachment webservice ca servicedesk

    Broadcom Employee
    Posted May 13, 2021 03:24 AM
    Hi Alan,

    1. Have a sample java code (specifically, SampleNewResourceWithAttachment) provided in the Samples folder (~%NX_ROOT\samples\sdk\rest\java). This sample code program demonstrates how to create a new Change Order ticket with an attachment document all in one step using REST API.

    2. These similar post might also be helpful:

    https://forum.servicedeskusers.com/forums/topic/910-resolved-createattachment-method-policy-limit-exceeded/?tab=comments#comment-2405

    https://community.broadcom.com/enterprisesoftware/communities/community-home/digestviewer/viewthread?MessageKey=9e8ce268-4201-4ffa-84b9-15a49dfd257a&CommunityKey=e2cd3188-49bc-4f38-88f1-74260f56fa66&tab=digestviewer#bm9e8ce268-4201-4ffa-84b9-15a49dfd257a


    ------------------------------
    Kind Regards,
    Brian
    ------------------------------



  • 3.  RE: createAttachment webservice ca servicedesk

    Posted May 18, 2021 06:40 AM
    A good SOAP example is in $NX_ROOT/samples/sdk/websvc/java/test3_attachments/create_attachment.java. There you can see ,how to set the DIME encapsulation as documented and required.
     // Setup the client soap request
    FileDataSource fds = new FileDataSource(filename);
    DataHandler dhandler = new DataHandler(fds);

    ((org.apache.axis.client.Stub)usd)._setProperty(Call.ATTACHMENT_ENCAPSULATION_FORMAT, Call.ATTACHMENT_ENCAPSULATION_FORMAT_DIME);
    ((org.apache.axis.client.Stub)usd).addAttachment(dhandler);

    Hope that helps
    .....Michael

    ------------------------------
    Principal Services Consultant
    HCL Enterprise Studio
    Germany
    ------------------------------



  • 4.  RE: createAttachment webservice ca servicedesk

    Posted Jul 20, 2021 05:21 PM
    Hello Michaell,

    The example does not work in SDM 17.3, the log pdm_tomcat show 

    java.lang.LinkageError: loader constraint violation for class org.apache.axis.attachments.AttachmentPart: when selecting overriding method org.apache.axis.attachments.AttachmentPart.setDataHandler(Ljavax/activation/DataHandler;)V the class loader org.apache.catalina.loader.ParallelWebappClassLoader @65afeb6d (instance of org.apache.catalina.loader.ParallelWebappClassLoader, child of java.net.URLClassLoader @18be83e4 java.net.URLClassLoader) of the selected method's type org.apache.axis.attachments.AttachmentPart, and the class loader java.net.URLClassLoader @18be83e4 (instance of java.net.URLClassLoader, child of java.net.URLClassLoader @8bd1b6a java.net.URLClassLoader) for its super type javax.xml.soap.AttachmentPart have different Class objects for the type javax.activation.DataHandler used in the signature

    I think the problem is the activation.jar and mail.jar that no longer work with java 11, I appreciate your comments.

    Greetings,


  • 5.  RE: createAttachment webservice ca servicedesk

    Broadcom Employee
    Posted Jul 21, 2021 08:28 AM
    just FYI...

    I have previously and successfully ran the SampleNewResourceWithAttachment provided in the Samples folder (~%NX_ROOT\samples\sdk\rest\java) on r17.3 (RU3).

    ------------------------------
    Kind Regards,
    Brian
    ------------------------------



  • 6.  RE: createAttachment webservice ca servicedesk

    Posted Jul 21, 2021 12:39 PM
    Edited by miguel jimenez Jul 21, 2021 12:40 PM
    Hello Brian, 

    Thanks for your prompt response, we had already validated that option and it worked, but the client does not want to publish port 8050, we must necessarily use tomcat on port 8080. I did tests with the new jars in the directory C:\Program Files (x86)\CA\Service Desk Manager\bopcfg\www\CATALINA_BASE\shared\lib

    jakarta.activation-1.2.1.jar
    jakarta.mail-1.6.3.jar

    The code example is this:

    FileDataSource fds = new FileDataSource(rutaArchivo);
    DataHandler dhandler = new DataHandler(fds);
    // Affix DIME type header to attachment before sending

    ((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);

    or 

    ((org.apache.axis.client.Stub) usd)._setProperty(Call.ATTACHMENT_ENCAPSULATION_FORMAT,
    Call.ATTACHMENT_ENCAPSULATION_FORMAT_DIME);
    ((org.apache.axis.client.Stub) usd).addAttachment(dhandler);

    The attachment uploads with a limit of 1mb, if it is greater I receive the message from

    <soapenv:Body xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Fault>
    <faultcode>soapenv:Server.generalException</faultcode>
    <faultstring>Error - could not perform the operation, policy limit exceeded</faultstring>
    <detail>
    <ns1:hostname xmlns:ns1="http://xml.apache.org/axis/">WIN-VGUGCV3E7ES</ns1:hostname>
    </detail>
    </soapenv:Fault>
    </soapenv:Body>

    I am currently testing with SOAPMessage and I get the same message :(, the code I use is this:

    SOAPConnection connection = SOAPConnectionFactory.newInstance().createConnection();
    SOAPMessage message = MessageFactory.newInstance().createMessage();
    SOAPPart part = message.getSOAPPart();
    SOAPEnvelope envelope = part.getEnvelope();
    SOAPBody body = envelope.getBody();
    SOAPBodyElement operation = body.addBodyElement(
    envelope.createName("createAttachment", "ser", "http://www.ca.com/UnicenterServicePlus/ServiceDesk"));

    DataHandler dh = new DataHandler(new FileDataSource(file));
    AttachmentPart attachment = message.createAttachmentPart(dh);

    SOAPElement sidElement = operation.addChildElement("sid", "");
    SOAPElement repositoryHandleElement = operation.addChildElement("repositoryHandle", "");
    SOAPElement objectHandleElement = operation.addChildElement("objectHandle", "");
    SOAPElement descriptionElement = operation.addChildElement("description", "");
    SOAPElement fileNameElement = operation.addChildElement("fileName", "");

    message.addAttachmentPart(attachment);

    sidElement.addTextNode(String.valueOf(sid));
    repositoryHandleElement.addTextNode(repository);
    objectHandleElement.addTextNode(persid);
    descriptionElement.addTextNode(attachmentDescription);
    fileNameElement.addTextNode(filePath);

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
       message.writeTo(out);
       System.out.println(out.toByteArray());
    } catch (IOException ex) {
       Logger.getLogger(ServicioWebSD.class.getName()).log(Level.SEVERE, null, ex);
    }

    SOAPMessage result = connection.call(message, url);

    part = result.getSOAPPart();
    envelope = part.getEnvelope();
    body = envelope.getBody();
    if (!body.hasFault()) {
    Iterator iterator = result.getAttachments();
    if (iterator.hasNext()) {
        dh = ((AttachmentPart) iterator.next()).getDataHandler();
        String fname = dh.getName();
    }


    But with Soapui works!!!


  • 7.  RE: createAttachment webservice ca servicedesk

    Posted Jul 21, 2021 06:52 PM
    With the soapmessage it didn't work either, I always got the same error, in the end I made it work with HttpURLConnection

    Maybe someone will use it, I share the class and Use it:

    HttpPostMultipart multipart = new HttpPostMultipart("http://localhost:8088/axis/services/USD_R11_WebService", "utf-8", headers);

    String xml = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n"
    + " <soapenv:Body>\n"
    + " <ser:createAttachment xmlns:ser=\"http://www.ca.com/UnicenterServicePlus/ServiceDesk\">\n"
    + " <sid>" + String.valueOf(sid) + "</sid>\n"
    + " <repositoryHandle>" + repositorio + "</repositoryHandle>\n"
    + " <objectHandle>" + persid + "</objectHandle>\n"
    + " <description>"+ descripcionAdjunto +"</description>\n"
    + " <fileName>" + rutaArchivo + "</fileName>\n"
    + " </ser:createAttachment>\n"
    + " </soapenv:Body>\n"
    + "</soapenv:Envelope>";

    multipart.addXmlField(xml);

    multipart.addFilePart(new File(rutaArchivo));

    String response = multipart.finish();

    Source: https://blog.cpming.top/p/httpurlconnection-multipart-form-data


  • 8.  RE: createAttachment webservice ca servicedesk

    Posted Jul 21, 2021 06:54 PM
    Edited by miguel jimenez Jul 21, 2021 06:54 PM
    import java.io.*;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import java.util.Iterator;
    import java.util.Map;
    import java.util.UUID;

    /**
    *
    * @author Miguel
    */
    public class HttpPostMultipart {

    private final String boundary;
    private static final String LINE = "\r\n";
    private static final String CONTENT_ID = "<xxxx>";
    private HttpURLConnection httpConn;
    private String charset;
    private OutputStream outputStream;
    private PrintWriter writer;

    public HttpPostMultipart(String requestURL, String charset, Map<String, String> headers) throws IOException {
    this.charset = charset;
    boundary = UUID.randomUUID().toString();
    URL url = new URL(requestURL);
    httpConn = (HttpURLConnection) url.openConnection();
    httpConn.setUseCaches(false);
    httpConn.setDoOutput(true); // indicates POST method
    httpConn.setDoInput(true);
    httpConn.setRequestProperty("Content-Type", "multipart/related; type=\"text/xml\"; start=\"" + CONTENT_ID + "\";boundary=\"" + boundary + "\"");
    httpConn.setRequestProperty("SOAPAction", "");
    if (headers != null && headers.size() > 0) {
    Iterator<String> it = headers.keySet().iterator();
    while (it.hasNext()) {
    String key = it.next();
    String value = headers.get(key);
    httpConn.setRequestProperty(key, value);
    }
    }
    outputStream = httpConn.getOutputStream();
    writer = new PrintWriter(new OutputStreamWriter(outputStream, charset), true);
    }

    public void addXmlField(String xml) {
    writer.append("--" + boundary).append(LINE);
    writer.append("Content-Type: text/xml; charset=UTF-8").append(LINE);
    writer.append("Content-Transfer-Encoding: 8bit").append(LINE);
    writer.append("Content-ID: " + CONTENT_ID).append(LINE);
    writer.append(LINE);
    writer.append(xml).append(LINE);
    writer.flush();
    }

    public void addFilePart(File uploadFile)
    throws IOException {
    String fileName = uploadFile.getName();
    writer.append("--" + boundary).append(LINE);
    writer.append("Content-Type: application/octet-stream; name=" + fileName).append(LINE);
    writer.append("Content-Transfer-Encoding: binary").append(LINE);
    writer.append("Content-ID: <" + fileName + ">:").append(LINE);
    writer.append("Content-Disposition: form-data; name=\"" + fileName + "\"; filename=\"" + fileName + "\"").append(LINE);
    writer.append(LINE);
    writer.flush();

    FileInputStream inputStream = new FileInputStream(uploadFile);
    byte[] buffer = new byte[4096];
    int bytesRead = -1;
    while ((bytesRead = inputStream.read(buffer)) != -1) {
    outputStream.write(buffer, 0, bytesRead);
    }
    outputStream.flush();
    inputStream.close();
    writer.append(LINE);
    writer.flush();
    }

    public String finish() throws IOException {
    String response = "";
    writer.flush();
    writer.append("--" + boundary + "--").append(LINE);
    writer.close();

    // checks server's status code first
    int status = httpConn.getResponseCode();
    if (status == HttpURLConnection.HTTP_OK) {
    ByteArrayOutputStream result = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int length;
    while ((length = httpConn.getInputStream().read(buffer)) != -1) {
    result.write(buffer, 0, length);
    }
    response = result.toString(this.charset);
    httpConn.disconnect();
    } else {
    throw new IOException("Server returned non-OK status: " + status);
    }
    return response;
    }

    }