Custom Upload, and Attachment Handler

ryan McCullough March 11, 2012

I'm writing an app that needs to use java to recoeve a small upload, and attach it to several issues (upload can be duplicated if necessary). I have created a handler servlet, but I'm struggeling with finding the proper API classes that handle the attachment code.

does Jira have a temp folder thats suitable for uploading to?

where should the file end up?

which classes handle file upload?

Thanks for your help...

2 answers

2 votes
Andy Brook [Plugin People]
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, 2012

1)

You probably need:

MultiPartRequestWrapper mprw = (MultiPartRequestWrapper) ServletActionContext.getMultiPartRequest();
if (mprw!=null && mprw.getFileNames().hasMoreElements())
{
  byte[] data=null;
  Enumeration<String> fileNames = mprw.getFileNames();
  if (fileNames.hasMoreElements())
  {
    while(fileNames.hasMoreElements())
    {
      String fileName = fileNames.nextElement();
      File file = mprw.getFile(fileName);		
...

2)

Try getting ComponentAccessor.getComponent(JiraHome.class);

That will give you .getDataDirectory() , which is your JIRA_HOME/data folder, allowing you to create a temp file in JIRA_HOME/tmp or your own location.

ryan McCullough March 12, 2012

Any Ideas on how to programatically attach a File object to an issue?

Andy Brook [Plugin People]
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, 2012

points encourage additional answers :) You need to create an attachment.

AttachmentManager attachmentManager = ComponentManager.getInstance().getAttachmentManager();

ryan McCullough March 14, 2012
for some reason, the ... 
MultiPartRequestWrapper mprw = (MultiPartRequestWrapper) ServletActionContext.getMultiPartRequest();

is resolving to null?

is there something special I need in my servlet config?

&lt;servlet name="dataHandler" key="bamHandler" class="com.foo.BarServlet"&gt;
     &lt;description&gt;&lt;/description&gt;
     &lt;url-pattern&gt;/dataHandler&lt;/url-pattern&gt;
&lt;/servlet&gt;

Thanks again :)

 
ryan McCullough March 14, 2012
For some reasong the mprw is comming back null? any ideas?

MultiPartRequestWrapper mprw = ServletActionContext.getMultiPartRequest();



Also, is there something special I need in my servlet config to support MultiPart Requests?

<servlet name="dataHandler" key="bamHandler" class="com.foo.BarServlet">
     <description></description>
     <url-pattern>/dataHandler</url-pattern>
</servlet>

Thanks again :)

ryan McCullough March 14, 2012
for some reason, the ... 
MultiPartRequestWrapper mprw = (MultiPartRequestWrapper) ServletActionContext.getMultiPartRequest();

is resolving to null?

is there something special I need in my servlet config?

&lt;servlet name="dataHandler" key="bamHandler" class="com.foo.BarServlet"&gt;
     &lt;description&gt;&lt;/description&gt;
     &lt;url-pattern&gt;/dataHandler&lt;/url-pattern&gt;
&lt;/servlet&gt;

Thanks again Andy :)

 
0 votes
Andy Brook [Plugin People]
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 14, 2012

Uh no, the ServletActionContext is part of webwork, well, it may be a utiloity class, but I've only used it inside an action. The patter to use is [HTML form] -> posts -> [Action] from which the above code works to extract the posted data.

Not to say your approach woudn't work, but Ive not had a compelling reason to even try, this works.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events