com.ibm.bluemix.samples.DownloadServlet.java Source code

Java tutorial

Introduction

Here is the source code for com.ibm.bluemix.samples.DownloadServlet.java

Source

/*-------------------------------------------------------------------*/
/*                                                                   */
/* Copyright IBM Corp. 2013 All Rights Reserved                      */
/*                                                                   */
/*-------------------------------------------------------------------*/
/*                                                                   */
/*        NOTICE TO USERS OF THE SOURCE CODE EXAMPLES                */
/*                                                                   */
/* The source code examples provided by IBM are only intended to     */
/* assist in the development of a working software program.          */
/*                                                                   */
/* International Business Machines Corporation provides the source   */
/* code examples, both individually and as one or more groups,       */
/* "as is" without warranty of any kind, either expressed or         */
/* implied, including, but not limited to the warranty of            */
/* non-infringement and the implied warranties of merchantability    */
/* and fitness for a particular purpose. The entire risk             */
/* as to the quality and performance of the source code              */
/* examples, both individually and as one or more groups, is with    */
/* you. Should any part of the source code examples prove defective, */
/* you (and not IBM or an authorized dealer) assume the entire cost  */
/* of all necessary servicing, repair or correction.                 */
/*                                                                   */
/* IBM does not warrant that the contents of the source code         */
/* examples, whether individually or as one or more groups, will     */
/* meet your requirements or that the source code examples are       */
/* error-free.                                                       */
/*                                                                   */
/* IBM may make improvements and/or changes in the source code       */
/* examples at any time.                                             */
/*                                                                   */
/* Changes may be made periodically to the information in the        */
/* source code examples; these changes may be reported, for the      */
/* sample code included herein, in new editions of the examples.     */
/*                                                                   */
/* References in the source code examples to IBM products, programs, */
/* or services do not imply that IBM intends to make these           */
/* available in all countries in which IBM operates. Any reference   */
/* to the IBM licensed program in the source code examples is not    */
/* intended to state or imply that IBM's licensed program must be    */
/* used. Any functionally equivalent program may be used.            */
/*-------------------------------------------------------------------*/
package com.ibm.bluemix.samples;

import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.simple.JSONObject;

/**
 * This method receives the clean text file or the analysis XML file from the
 * database and returns it to the user for downloading.
 */
@SuppressWarnings("serial")
public class DownloadServlet extends HttpServlet {

    /**
     * object of the PostgreSQLClient class to access and alter the database
     */
    private PostgreSQLClient db = new PostgreSQLClient();

    /**
     * Receives the clean text file or the analysis XML file from the database and returns it to the user
     * 
     * @param request
     *           contains the type of the requested file (text or XML) and the ID of the file in the database
     * @param response
     *           the file to be downloaded
     *           
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        System.out.println("Download Servlet");

        try {
            // Retrive file information and data
            JSONObject fileInfo = db.getFileInfo(request.getParameter("entry_id"));
            String contentType = null;
            String fileName = null;
            byte[] fileData = null;

            if (request.getParameter("type").equals("1")) {
                // Request for the original file
                fileData = db.getOriginalFile(request.getParameter("entry_id"));
                contentType = "text/plain";
                fileName = (String) fileInfo.get("file_name");
            } else {
                // Request for the output file
                fileData = db.getOutputFile(request.getParameter("entry_id"));
                contentType = "application/xml";
                fileName = (String) fileInfo.get("output_name");
            }

            // Set response headers
            response.setHeader("Content-disposition", "attachment; filename=" + fileName);
            response.setContentType(contentType);
            response.setStatus(200);

            // Write data to output stream
            OutputStream responseOutput = response.getOutputStream();
            responseOutput.write(fileData, 0, fileData.length);
            responseOutput.flush();
            responseOutput.close();

        } catch (Exception e) {
            request.setAttribute("msg", e.getMessage());
            e.printStackTrace(System.err);
        }
    }
}