/*
 * ################################################################
 *
 * ProActive Parallel Suite(TM): The Java(TM) library for
 *    Parallel, Distributed, Multi-Core Computing for
 *    Enterprise Grids & Clouds
 *
 * Copyright (C) 1997-2015 INRIA/University of
 *                 Nice-Sophia Antipolis/ActiveEon
 * Contact: proactive@ow2.org or contact@activeeon.com
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Affero General Public License
 * as published by the Free Software Foundation; version 3 of
 * the License.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 *
 * If needed, contact us to obtain a release under GPL Version 2 or 3
 * or a different license than the AGPL.
 *
 *  Initial developer(s):               The ProActive Team
 *                        http://proactive.inria.fr/team_members.htm
 *  Contributor(s):
 *
 * ################################################################
 * $$PROACTIVE_INITIAL_DEV$$
 */
package org.ow2.proactive_grid_cloud_portal.scheduler.server;

import org.codehaus.jettison.json.JSONObject;
import org.ow2.proactive_grid_cloud_portal.common.client.json.JSONUtils;
import org.ow2.proactive_grid_cloud_portal.common.server.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;


/**
 * The servlet which is called when the result of a task is wanted to be downloaded
 * @author ahagea
 *
 */
@SuppressWarnings("serial")
public class DownloadTaskResultServlet extends HttpServlet {

    private static final Logger LOGGER = LoggerFactory.getLogger(DownloadTaskResultServlet.class);

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        download(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        download(request, response);
    }

    private void download(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String jobId = request.getParameter("jobId");
        String taskId = request.getParameter("taskId");
        String destination = request.getParameter("destination");
        String sessionId = request.getParameter("sessionId");

        InputStream is = null;
        ServletOutputStream out = null;
        try {

            JSONObject json = new JSONObject(((SchedulerServiceImpl) Service.get()).getTaskResultMetadata(sessionId, jobId, taskId));
            String contentType;
            if (destination.equals("file")) {
                contentType = "application/octet-stream";
            } else if (json.has("contentType")) {
                contentType = json.get("contentType").toString();
            } else {
                contentType = "text/plain";
            }
            response.setContentType(contentType);

            if (destination.equals("file")) {
                if (json.has("fileName")) {
                    response.setHeader("Content-disposition", "attachment; filename=" + json.get("fileName").toString());
                } else if (json.has("fileExtension")) {
                    response.setHeader("Content-disposition", "attachment; filename=job" + jobId + "_" + taskId +
                            "_result" + json.get("fileExtension").toString());
                } else {
                    response.setHeader("Content-disposition", "attachment; filename=job" + jobId + "_" + taskId +
                            "_result");
                }
            }
            response.setHeader("Location", "job" + jobId + "_" + taskId + ".result");

            out = response.getOutputStream();
            if (contentType.startsWith("text/")) {
                is = ((SchedulerServiceImpl) Service.get()).getTaskResult(sessionId, jobId, taskId);
            } else {
                is = ((SchedulerServiceImpl) Service.get()).getTaskSerializedResult(sessionId, jobId, taskId);
            }

            int buf;
            while ((buf = is.read()) != -1) {
                out.write(buf);
            }

        } catch (Throwable t) {
            LOGGER.warn("Failed to download result", t);
            String str = "Failed to download result: " + JSONUtils.getJsonErrorMessage(t);
            out.write(str.getBytes());
        } finally {
            if (is != null)
                is.close();
            out.flush();
            out.close();
        }
    }
}

@@ -1538,16 +1538,60 @@
 Object;%0A
+import org.ow2.proactive.web.WebProperties;%0A
 import o
@@ -3371,29 +3371,51 @@
 son.has(
-%22contentType%22
+WebProperties.METADATA_CONTENT_TYPE
 )) %7B%0A   
@@ -3454,21 +3454,43 @@
 get(
-%22contentType%22
+WebProperties.METADATA_CONTENT_TYPE
 ).to
@@ -3704,26 +3704,48 @@
 son.has(
-%22fileName%22
+WebProperties.METADATA_FILE_NAME
 )) %7B%0A   
@@ -3842,18 +3842,40 @@
 get(
-%22fileName%22
+WebProperties.METADATA_FILE_NAME
 ).to
@@ -3921,31 +3921,53 @@
 son.has(
-%22fileExtension%22
+WebProperties.METADATA_FILE_EXTENSION
 )) %7B%0A   
@@ -4130,23 +4130,45 @@
 get(
-%22fileExtension%22
+WebProperties.METADATA_FILE_EXTENSION
 ).to

Use metadata properties names from WebProperties class