Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Adding attachment through API

ArtUrlWWW March 12, 2014

Hi.

I am trying to add attachment to the issue, but I got exception.

My code is

MutableIssue issueObject = ComponentAccessor.getIssueFactory().getIssue();
.....................
System.out.println("/tmp/" + docFile.getFILENAME());

										File tmpFile = new File("/tmp/" + docFile.getFILENAME());

										FileOutputStream fileOuputStream = new FileOutputStream(tmpFile);
										fileOuputStream.write(docFile.getBASE64FILE());
										fileOuputStream.close();

										HashMap<String, Object> attachmentProperties = new HashMap<String, Object>();
CreateAttachmentParamsBean ab = new CreateAttachmentParamsBean(tmpFile, docFile.getFILENAME(), "application/octet-stream", reporterApp, issueObject, false, false, attachmentProperties, new Date(), true);

										ComponentAccessor.getAttachmentManager().createAttachment(ab);

I got exception

[INFO] [talledLocalContainer] com.atlassian.jira.web.util.AttachmentException: Exception trying to establish attachment directory. Check that the application server and JIRA have permissions to write to it: java.lang.NullPointerException
[INFO] [talledLocalContainer]   at com.atlassian.jira.issue.attachment.DefaultAttachmentStore.checkValidAttachmentDirectory(DefaultAttachmentStore.java:281)
[INFO] [talledLocalContainer]   at com.atlassian.jira.util.AttachmentUtils.checkValidAttachmentDirectory(AttachmentUtils.java:171)
[INFO] [talledLocalContainer]   at com.atlassian.jira.issue.managers.DefaultAttachmentManager.checkValidAttachmentDirectory(DefaultAttachmentManager.java:465)
[INFO] [talledLocalContainer]   at com.atlassian.jira.issue.managers.DefaultAttachmentManager.createAttachment(DefaultAttachmentManager.java:443)
[INFO] [talledLocalContainer]   at com.atlassian.jira.issue.managers.DefaultAttachmentManager.createAttachmentBean(DefaultAttachmentManager.java:421)
[INFO] [talledLocalContainer]   at com.atlassian.jira.issue.managers.DefaultAttachmentManager.createAttachment(DefaultAttachmentManager.java:485)
[INFO] [talledLocalContainer]   at name.khartn.jira.lotus.wsdl.SyncIssues.q(SyncIssues.java:249)
[INFO] [talledLocalContainer]   at name.khartn.jira.lotus.wsdl.SyncIssues.execute(SyncIssues.java:81)
[INFO] [talledLocalContainer]   at org.quartz.core.JobRunShell.run(JobRunShell.java:203)
[INFO] [talledLocalContainer]   at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:520)
[INFO] [talledLocalContainer] Caused by: java.lang.NullPointerException
[INFO] [talledLocalContainer]   at com.atlassian.jira.issue.IssueKey.from(IssueKey.java:59)
[INFO] [talledLocalContainer]   at com.atlassian.jira.issue.attachment.DefaultAttachmentStore.computeIssueKeyForOriginalProjectKey(DefaultAttachmentStore.java:144)
[INFO] [talledLocalContainer]   at com.atlassian.jira.issue.attachment.DefaultAttachmentStore.getAttachmentDirectory(DefaultAttachmentStore.java:138)
[INFO] [talledLocalContainer]   at com.atlassian.jira.issue.attachment.DefaultAttachmentStore.getAttachmentDirectory(DefaultAttachmentStore.java:92)
[INFO] [talledLocalContainer]   at com.atlassian.jira.issue.attachment.DefaultAttachmentStore.getAttachmentDirectory(DefaultAttachmentStore.java:67)
[INFO] [talledLocalContainer]   at com.atlassian.jira.issue.attachment.DefaultAttachmentStore.checkValidAttachmentDirectory(DefaultAttachmentStore.java:271)
[INFO] [talledLocalContainer]   ... 9 more

Folder /mnt/newDrive/atlassian/projects/plugin/target/jira/home/data/attachments exists and has 777 permissions (write for all).

I trying to get default path to the attachment. For this I called

System.out.println("+++ " + ComponentAccessor.getAttachmentPathManager().getAttachmentPath());

It's returns /mnt/newDrive/atlassian/projects/plugin/target/jira/home/data/attachments

After that, I trying to change default path to the attachments dir:

ComponentAccessor.getAttachmentPathManager().setCustomAttachmentPath("/tmp");

It doesn't help.

But if I add attachment throught Jira web interface (in Create Issue form, Add attachment field) - than attachment adds very well and path for attachment will be something like /mnt/newDrive/atlassian/projects/plugin/target/jira/home/data/attachments/SITE/SITE-3 or /tmp/SITE/SITE-4 (after changing default path to the attachments dir.)

I have found two issues https://jira.atlassian.com/browse/JRA-28412 and https://jira.atlassian.com/browse/JRA-19873with analog question.

Please advice, how to add attachments to the issues with Jira API?

Sorry my bad English.

Best regards, Arthur Khusnutdinov.

2 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

2 votes
Answer accepted
JulianA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 12, 2014

Maybe this can help you

private ChangeItemBean createAttachment(FileItemStream item, MutableIssue issue, ApplicationUser user)
    {
    	try
    	{
	    	final File tempFile = File.createTempFile("attachment"+new Date().getTime(), null);
	        FileOutputStream out = new FileOutputStream(tempFile);
	        IOUtils.copy(item.openStream(), out);
	        
	        AttachmentManager attachmentManager = ComponentAccessor.getAttachmentManager();
	
	        
	        CreateAttachmentParamsBean bean = new CreateAttachmentParamsBean.Builder(tempFile, 
	        																 item.getName(),
	        																 item.getContentType(),
	        																 user, 
	        																 issue).build();
	        
			return attachmentManager.createAttachment(bean);
    	}
    	catch (Exception e)
    	{
    		return null;
    	}

ArtUrlWWW March 12, 2014

Thank you very much! After issue creation with

Issue issue = ComponentAccessor.getIssueManager().createIssueObject(ComponentAccessor.getUserManager().getUser("admin"), issueObject);

attachments added OK with your code

CreateAttachmentParamsBean bean = new CreateAttachmentParamsBean.Builder(tmpFile, tmpFile.getName(), "application/octet-stream", reporterApp, issue).build();
										ComponentAccessor.getAttachmentManager().createAttachment(bean);

Thank you very much!

0 votes
Andreas Ebert
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 12, 2014

Your first line does not create a persistent issue (it will not be saved to the database):

MutableIssue issueObject = ComponentAccessor.getIssueFactory().getIssue();

Afaik, attachments can only be added to existing issues. Try saving the issue first (if you really want to create a new issue), and add the attachments to the created issue object afterwards.

ArtUrlWWW March 12, 2014

OK, I changed code to

..................
Issue issue = ComponentAccessor.getIssueManager().createIssueObject(ComponentAccessor.getUserManager().getUser("admin"), issueObject);
((IssueIndexManager) ComponentAccessor.getComponent(IssueIndexManager.class)).reIndex(issue);
...............
System.out.println("/tmp/" + docFile.getFILENAME());
File tmpFile = new File("/tmp/" + docFile.getFILENAME());

FileOutputStream fileOuputStream = new FileOutputStream(tmpFile);
fileOuputStream.write(docFile.getBASE64FILE());
fileOuputStream.close();

 HashMap<String, Object> attachmentProperties = new HashMap<String, Object>();
 System.out.println("+++ " + ComponentAccessor.getAttachmentPathManager().getAttachmentPath());

 CreateAttachmentParamsBean ab = new CreateAttachmentParamsBean(tmpFile, docFile.getFILENAME(), "application/octet-stream", reporterApp, issueObject, false, false, attachmentProperties, new Date(), false);
 ComponentAccessor.getAttachmentManager().createAttachment(ab);

And after that I got exception

[INFO] [talledLocalContainer] /tmp/snapshot1.png
[INFO] [talledLocalContainer] +++ /mnt/newDrive/atlassian/projects/plugin/target/jira/home/data/attachments
[INFO] [talledLocalContainer] com.atlassian.jira.exception.DataAccessException: org.ofbiz.core.entity.GenericEntityException: while inserting: [GenericEntity:FileAttachment][id,10000][zip,0][author,husnutdinov@bta-kazan.ru][filesize,211093][mimetype,image/png][created,Thu Mar 13 12:43:55 MSK 2014][issue,10002][filename,snapshot1.png][thumbnailable,0] (Java type java.util.Date not currently supported. Sorry.)
[INFO] [talledLocalContainer]   at com.atlassian.jira.ofbiz.DefaultOfBizDelegator.createValue(DefaultOfBizDelegator.java:395)
[INFO] [talledLocalContainer]   at com.atlassian.jira.ofbiz.WrappingOfBizDelegator.createValue(WrappingOfBizDelegator.java:219)
.....

This is very strange error... How I can fix this error?

TAGS
AUG Leaders

Atlassian Community Events