It's all about the answers!

Ask a question

Downloading RTC attachments on server side


Dmitry A. Lesin (21212950) | asked Sep 24 '14, 1:50 a.m.
edited Sep 24 '14, 1:54 a.m.
Hello!
I'm trying to extract RTC work item attachments and save them as files. My code looks like next:

      ...
        // Get all references for work item
        IWorkItemReferences wiReferences = getWorkItemReferences(wi,
                iWorkItemCommon);

        // Get attachments from references
        if (wiReferences != null) {
            List<IReference> references = wiReferences
                    .getReferences(WorkItemEndPoints.ATTACHMENT);
            for (IReference iReference : references) {
                IAttachment attachment = getAttachment(iReference);
                if (attachment != null)
                    saveAttachment("/home/homecredit", attachment);
            }
        }


    /**
     * Save the attachment.
     */
    private void saveAttachment(String tmp, IAttachment attachment) {
        try {
            File fileAttach = new File(attachment.getName());
            OutputStream out = new FileOutputStream(fileAttach);
            try {
                IContent content = attachment.getContent();
               
                // ???

            } finally {
                out.close();
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
        } catch (IOException e) {
            // TODO Auto-generated catch block
        }
    }

  /**
     * Further analyze an item referenced by an URI
     */
    public IAttachment getAttachment(IReference iReference) {
        IAttachment attachment = null;
        URI uri = iReference.createURI();
        try {
            System.out.println(" Resolving URI: " + uri.toString());
            // get the location from the URI
            Location location = Location.location(uri);
            // resolve the item by location
            IAuditableCommon common = getService(IAuditableCommon.class);
            IAuditable referenced = common
                    .resolveAuditableByLocation(location, ItemProfile
                            .createFullProfile(location.getItemType()), null);
            if (referenced instanceof IAttachment)
                attachment = (IAttachment) referenced;
        } catch (TeamRepositoryException e) {
            saveMessageToLog("Can not get attachment because of:\n" + e);
        }
        return attachment;
    }

But I stumbled when I was trying to get the real content of the attachment to save it with output stream. Can somebody help me on this way?
I know about next code on the client side:

    OutputStream out = new FileOutputStream(save);
    teamRepository.contentManager().retrieveContent(attachment.getContent(), out, null);

But I can not use it on the server side!

Thank you very much for any recommendations and simple thoughts!

One answer



permanent link
Ralph Schoon (55.8k23642) | answered Sep 24 '14, 4:35 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER
This is working code for the client: https://rsjazz.wordpress.com/2012/09/21/downloading-attachments-from-work-items/

I would suggest to search the API. I tried with IContentService and had a hit. I have not looked into this server side service, but it might be the way to go.

Your answer


Register or to post your answer.