Want to get rid of Google Ads, click here.
+ Reply to Thread
Results 1 to 7 of 7

Thread: Attachments and WS

  1. #1
    Senior Member
    Join Date
    Sep 2008
    Posts
    115

    Default Attachments and WS

    I've posted this on the HP forums as well, hoping someone can help.

    I'm having some issues with adding attachments to a Change Request in SM7.10. Using the CreateIncident JAVA example, I've created a 'CreateChange' example and have got it raising a change request, however I'm struggling with the attachment part.

    In soapUI I can get it working fine, using the following:

    <ns:attachments>
    <!--Zero or more repetitions:-->
    <com:attachment xm:contentType="text/plain" action="add" name="file.txt" type="text/plain">cid:12345</com:attachment>
    </ns:attachments>

    And then click add on the Attachments button and setiing the Part to "12345" (same as cid in the XML line). This works fine and creates the attachment in the new change request.

    I'm struggling with the JAVA bit however. If I use the sample code which is below:

    public static AttachmentType GetAttachmentType(File file)
    {
    // Creating a javax.activation.FileDataSource from the input file.
    File file = new File("c:\\file.txt");
    FileDataSource fileDataSource = new FileDataSource(file);
    DataHandler dataHandler = new DataHandler(fileDataSource);

    AttachmentType attachmentType = new AttachmentType();
    attachmentType.setLen(Integer.valueOf((int) file.length()));
    attachmentType.setName(file.getName());
    attachmentType.setAction("add");
    ContentType_type0 contentType = new ContentType_type0();
    contentType.setContentType_type0(fileDataSource.ge tContentType());
    attachmentType.setContentType(contentType);
    attachmentType.setType(fileDataSource.getContentTy pe());

    attachmentType.setBase64Binary(dataHandler);

    return attachmentType;
    }

    I get the following error in the log file:
    1924( 3332) 01/27/2010 14:05:29 RTE W Warning: incoming add attachment request 1 has no href attribute
    1924( 3332) 01/27/2010 14:05:29 RTE E Error: unable to match incoming add attachment request 1 with no href attribute to an attachment part

    If I set a ContentID or HREF I get:
    1924( 1640) 01/27/2010 14:07:16 RTE E Error: incoming attachment add request with href 12345 but no corresponding attachment

    What am I missing here? I feel like it's something really simple. Any help would be gratefully accepted!

    Leon

  2. #2
    Senior Member
    Join Date
    Jan 2002
    Location
    The Netherlands
    Posts
    930

    Default

    The issue seems to be with the actual envelope information. The attachment (as part of the SM SOAP Request) needs to be part of the incident, but also have its own attributes set. Maybe you can post the code for the actual SM request?

  3. #3
    Senior Member
    Join Date
    Sep 2008
    Posts
    115

    Default

    Thanks for replying. Here is a snippet of the function creating the change. I've removed the multiple lines that are setting different fields to conserve space.

    Code:
    private ChangeInstanceType createChangeInstance(Map arguments)
      {
    *SNIP REMOVED CODE THAT DOESN'T REALLY MATTER*
        ChangeInstanceType instance = new ChangeInstanceType();
        assets = new Assets_type1();
        value = (String) arguments.get(ARGUMENT_ASSETS);
        assets.addAssets(ChangeManagementServiceUtility.GetStringType(value));
        assets.setType("ArrayType");
    
        middle = new Middle_type2();
        middle.setType("StringType");
        middle.setAssets(assets);
        instance.setMiddle(middle);
    
        description = new Description_type2();
        value = (String) arguments.get(ARGUMENT_DESCRIPTION);
        description.addDescription(ChangeManagementServiceUtility.GetStringType(value));
        description.setType("ArrayType");
        structure = new DescriptionStructure_type2();
        structure.setType("StructureType");
        structure.setDescription(description);
        instance.setDescriptionStructure(structure);
    
        ArrayList files =
            ChangeManagementServiceUtility.ParseList((String) arguments.get(ARGUMENT_ATTACHMENT));
    
        if (files != null && files.size() > 0) {
          AttachmentsType attachments = new AttachmentsType();
          for (int i = 0; i < files.size(); i++) {
            File file =
                new File("." + File.separator + ChangeManagementServiceUtility.RESOURCES
                    + File.separator + files.get(i));
    //                new File((String) files.get(i));
                    System.out.println(file.getAbsolutePath());
            if (file.exists()) {
              System.out.println("File exists, adding attachment to attachments");
              System.out.println(file.toURI());
              attachments.addAttachment(ChangeManagementServiceUtility.GetAttachmentType(file));
            }
          }
          if (attachments.getAttachment() != null && attachments.getAttachment().length > 0) {
            System.out.println("Attachments exist, adding to instance");
            instance.setAttachments(attachments);
          }
        }
    
        close = new Close_type2();
        close.setType("StringType");
        instance.setClose(close);
    
        return instance;
      }
    And here is my GetAttachmentType function, which should be pretty much the same as the previous one I posted.

    Code:
      public static AttachmentType GetAttachmentType(File file)
      {
        //  Creating a javax.activation.FileDataSource from the input file.
        //File file = new File("c:\\LOGF56D.log");
        //  File file = new File("//devapps017.devdomau.local/Temp/Test.txt");
        FileDataSource fileDataSource = new FileDataSource(file);
        DataHandler dataHandler = new DataHandler(fileDataSource);
    
        AttachmentType attachmentType = new AttachmentType();
        attachmentType.setLen(Integer.valueOf((int) file.length()));
        attachmentType.setName(file.getName());
        attachmentType.setAction("add");
        ContentType_type0 contentType = new ContentType_type0();
        System.out.println(fileDataSource.getContentType());
        contentType.setContentType_type0(fileDataSource.getContentType());
        attachmentType.setContentType(contentType);
        attachmentType.setType(fileDataSource.getContentType());
        attachmentType.setBase64Binary(dataHandler);
        //System.out.println(attachmentType.getContentId());
    
        return attachmentType;
      }

  4. #4
    Senior Member
    Join Date
    Jan 2002
    Location
    The Netherlands
    Posts
    930

    Default

    I thought I has some reference code, but it seems I misplaced it. If no one else responds, I'll try tomorrow or over the weekend to get some review/check going.

  5. #5
    Senior Member
    Join Date
    Sep 2008
    Posts
    115

    Default

    Anyone else able to help here?

  6. #6
    Junior Member
    Join Date
    Oct 2009
    Posts
    9

    Default

    There is a explanation about this topic in the document 'Best Practices for Publishing and Consuming Web Services with Service ManagerŪ', I suppose the related to ServiceCenter also contains it. This document can be downloaded from HP web page. Moreover, in the SM/SC installation's folder there are examples about creating incidents with attachments.
    Last edited by gumarrita; 2010-04-29 at 21:12.

  7. #7
    Junior Member
    Join Date
    Oct 2009
    Posts
    9

    Thumbs up Finally Web Services with attachment working!

    Finally, I got it!

    What I've done was set the type field of the object AttachmentType. I'm working with SM 7.11, and a web application with Apache Tomahawk, so I don't wanna save files in the server, I send the files to the SM server. It needs the jars mail-1-14.jar for the class ByteArrayDataSource and activation-1.1.jar for the DataHandler class.

    Code:
                    //String uploadedFileName = FileUtil.trimFilePath(adjunto.getName());
                    //File uniqueFile = FileUtil.uniqueFile(new File("c:/upload"), uploadedFileName);
                    ByteArrayDataSource ds = new ByteArrayDataSource(adjunto.getBytes(),adjunto.getContentType());
                    
                    //FileUtil.write(uniqueFile, adjunto.getInputStream());
    
                    AttachmentType attach = new AttachmentType();
                    DataHandler handler = new DataHandler(ds);
                    ((Stub)puerto).addAttachment(handler);
                    
                    attach.setAction("add");
                    attach.setName(adjunto.getName()); 
                    attach.setLen((int)(adjunto.getSize()));
                    //attach.setCharset(adjunto.getContentType());
                    //attach.setContentType(adjunto.getContentType());
                    //attach.setHref(adjunto.getName());
                    attach.setType(adjunto.getContentType());
                    //attach.setContentId(adjunto.getName());
                    attach.set_value(adjunto.getBytes());
                    req.setAttachmentData(Boolean.TRUE);
                    req.setAttachmentInfo(Boolean.TRUE);
                    instancia.setAttachments(new AttachmentType[]{attach});
    Previous code snippet doesn't use the File object, but if you have a File object, replace the definition of ds object like that:

    Code:
    File myFile = new File("URLofmyfile");
    FileDataSource ds = new FileDataSource(myFile);
    Don't forget the validations for exceptions.


    Hope it be useful.
    Last edited by gumarrita; 2010-04-30 at 00:32. Reason: Imcomplete

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Bookmarks

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts