Custom Upload, and Attachment Handler

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

This widget could not be displayed.

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.

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

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

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

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 :)

 
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 :)

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 :)

 
This widget could not be displayed.

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
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Posted 9 hours ago in Statuspage

What are your best incident management tips and stories? #HugOps

 👋Community members! Downtime happens. And great incident response takes a village. Teams like Support, Dev, SRE, Ops, IT, and Marketing have to come together to resolve the problem while keep...

28 views 0 3
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you