/* Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.activiti.rest.service.api.runtime.task;

import java.io.InputStream;

import javax.servlet.http.HttpServletResponse;

import org.activiti.engine.ActivitiException;
import org.activiti.engine.ActivitiObjectNotFoundException;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.task.Attachment;
import org.apache.commons.io.IOUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author Frederik Heremans
 */
@RestController
public class TaskAttachmentContentResource extends TaskBaseResource {

  @RequestMapping(value="/runtime/tasks/{taskId}/attachments/{attachmentId}/content", method = RequestMethod.GET, produces="application/json")
  public @ResponseBody byte[] getAttachmentContent(@PathVariable("taskId") String taskId, 
      @PathVariable("attachmentId") String attachmentId, HttpServletResponse response) {
    
    HistoricTaskInstance task = getHistoricTaskFromRequest(taskId);
    Attachment attachment = taskService.getAttachment(attachmentId);
    
    if (attachment == null || !task.getId().equals(attachment.getTaskId())) {
      throw new ActivitiObjectNotFoundException("Task '" + task.getId() +"' doesn't have an attachment with id '" + attachmentId + "'.", Attachment.class);
    }
    
    InputStream attachmentStream = taskService.getAttachmentContent(attachmentId);
    if (attachmentStream == null) {
      throw new ActivitiObjectNotFoundException("Attachment with id '" + attachmentId + 
          "' doesn't have content associated with it.", Attachment.class);
    }
    
    MediaType mediaType = null;
    if (attachment.getType() != null) {
      try {
        mediaType = MediaType.valueOf(attachment.getType());
        response.setContentType(attachment.getType());
      } catch (Exception e) {
        // ignore if unknown media type
      }
    }
    
    if (mediaType == null) {
      response.setContentType("application/octet-stream");
    }
    
    try {
      return IOUtils.toByteArray(attachmentStream);
    } catch (Exception e) {
      throw new ActivitiException("Error creating attachment data", e);
    }
  }
}

@@ -963,25 +963,162 @@
 rk.http.
-MediaType
+HttpHeaders;%0Aimport org.springframework.http.HttpStatus;%0Aimport org.springframework.http.MediaType;%0Aimport org.springframework.http.ResponseEntity
 ;%0Aimport
@@ -1301,69 +1301,8 @@
 od;%0A
-import org.springframework.web.bind.annotation.ResponseBody;%0A
 impo
@@ -1602,37 +1602,8 @@
 .GET
-, produces=%22application/json%22
 )%0A  
@@ -1613,17 +1613,16 @@
 lic 
-@
 Response
 Body
@@ -1621,19 +1621,22 @@
 onse
-Body 
+Entity%3C
 byte%5B%5D
+%3E
  get
@@ -2464,24 +2464,77 @@
 %0A    %7D%0A    %0A
+    HttpHeaders responseHeaders = new HttpHeaders();%0A
     MediaTyp
@@ -2682,32 +2682,44 @@
 response
+Headers
 .set
+(%22
 Content
+-
 Type
-(
+%22, 
 attachme
@@ -2869,24 +2869,36 @@
 onse
+Headers
 .set
+(%22
 Content
+-
 Type
-(
+%22, 
 %22app
@@ -2956,16 +2956,43 @@
  return 
+new ResponseEntity%3Cbyte%5B%5D%3E(
 IOUtils.
@@ -3020,16 +3020,49 @@
 tStream)
+, responseHeaders, HttpStatus.OK)
 ;%0A    %7D 

use ResponseEntity in TaskAttachmentContentResource