23f02be1248ef0a331e95666ecd5fa225490de4c
[friendica-addons.git/.git] / js_upload / file-uploader / server / OctetStreamReader.java
1 /*
2  *  Copyright 2010 Blue Lotus Software, LLC.
3  *  Copyright 2010 John Yeary <jyeary@bluelotussoftware.com>.
4  *  Copyright 2010 Allan O'Driscoll
5  *
6  * Dual Licensed MIT and GPL v.2 
7  *
8  * The MIT License
9  *
10  * Permission is hereby granted, free of charge, to any person obtaining a copy
11  * of this software and associated documentation files (the "Software"), to deal
12  * in the Software without restriction, including without limitation the rights
13  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14  * copies of the Software, and to permit persons to whom the Software is
15  * furnished to do so, subject to the following conditions:
16  *
17  * The above copyright notice and this permission notice shall be included in
18  * all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26  * THE SOFTWARE.
27  *
28  *
29  * The GNU General Public License (GPL) Version 2, June 1991
30  * This program is free software; you can redistribute it and/or modify it under the terms of the
31  * GNU General Public License as published by the Free Software Foundation; version 2 of the License.
32
33  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
34  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
35  * See the GNU General Public License for more details.
36
37  * You should have received a copy of the GNU General Public License along with this program;
38  * if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
39  */
40 package com.bluelotussoftware.apache.commons.fileupload.example;
41
42 import java.io.File;
43 import java.io.FileNotFoundException;
44 import java.io.FileOutputStream;
45 import java.io.IOException;
46 import java.io.InputStream;
47 import java.io.PrintWriter;
48 import javax.servlet.ServletConfig;
49 import javax.servlet.ServletException;
50 import javax.servlet.http.HttpServlet;
51 import javax.servlet.http.HttpServletRequest;
52 import javax.servlet.http.HttpServletResponse;
53 import org.apache.commons.io.IOUtils;
54
55 /**
56  * Reads an <code>application/octet-stream</code> and writes it to a file.
57  * @author John Yeary <jyeary@bluelotussoftware.com>
58  * @author Allan O'Driscoll
59  * @version 1.0
60  */
61 public class OctetStreamReader extends HttpServlet {
62
63     private static final long serialVersionUID = 6748857432950840322L;
64     private static final String DESTINATION_DIR_PATH = "files";
65     private static String realPath;
66
67     /**
68      * {@inheritDoc}
69      * @param config
70      * @throws ServletException
71      */
72     @Override
73     public void init(ServletConfig config) throws ServletException {
74         super.init(config);
75         realPath = getServletContext().getRealPath(DESTINATION_DIR_PATH) + "/";
76     }
77
78     /** 
79      * Handles the HTTP <code>POST</code> method.
80      * @param request servlet request
81      * @param response servlet response
82      * @throws ServletException if a servlet-specific error occurs
83      * @throws IOException if an I/O error occurs
84      */
85     @Override
86     protected void doPost(HttpServletRequest request, HttpServletResponse response)
87             throws ServletException {
88
89         PrintWriter writer = null;
90         InputStream is = null;
91         FileOutputStream fos = null;
92
93         try {
94             writer = response.getWriter();
95         } catch (IOException ex) {
96             log(OctetStreamReader.class.getName() + "has thrown an exception: " + ex.getMessage());
97         }
98
99         String filename = request.getHeader("X-File-Name");
100         try {
101             is = request.getInputStream();
102             fos = new FileOutputStream(new File(realPath + filename));
103             IOUtils.copy(is, fos);
104             response.setStatus(response.SC_OK);
105             writer.print("{success: true}");
106         } catch (FileNotFoundException ex) {
107             response.setStatus(response.SC_INTERNAL_SERVER_ERROR);
108             writer.print("{success: false}");
109             log(OctetStreamReader.class.getName() + "has thrown an exception: " + ex.getMessage());
110         } catch (IOException ex) {
111             response.setStatus(response.SC_INTERNAL_SERVER_ERROR);
112             writer.print("{success: false}");
113             log(OctetStreamReader.class.getName() + "has thrown an exception: " + ex.getMessage());
114         } finally {
115             try {
116                 fos.close();
117                 is.close();
118             } catch (IOException ignored) {
119             }
120         }
121
122         writer.flush();
123         writer.close();
124     }
125 }