4522 |
15 Sep 08 |
nicklas |
1 |
<%-- $Id$ |
4522 |
15 Sep 08 |
nicklas |
2 |
------------------------------------------------------------------ |
4522 |
15 Sep 08 |
nicklas |
Copyright (C) 2008 Nicklas Nordborg |
4522 |
15 Sep 08 |
nicklas |
4 |
|
4522 |
15 Sep 08 |
nicklas |
5 |
This file is part of BASE - BioArray Software Environment. |
4522 |
15 Sep 08 |
nicklas |
6 |
Available at http://base.thep.lu.se/ |
4522 |
15 Sep 08 |
nicklas |
7 |
|
4522 |
15 Sep 08 |
nicklas |
8 |
BASE is free software; you can redistribute it and/or |
4522 |
15 Sep 08 |
nicklas |
9 |
modify it under the terms of the GNU General Public License |
4522 |
15 Sep 08 |
nicklas |
10 |
as published by the Free Software Foundation; either version 3 |
4522 |
15 Sep 08 |
nicklas |
11 |
of the License, or (at your option) any later version. |
4522 |
15 Sep 08 |
nicklas |
12 |
|
4522 |
15 Sep 08 |
nicklas |
13 |
BASE is distributed in the hope that it will be useful, |
4522 |
15 Sep 08 |
nicklas |
14 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
4522 |
15 Sep 08 |
nicklas |
15 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
4522 |
15 Sep 08 |
nicklas |
16 |
GNU General Public License for more details. |
4522 |
15 Sep 08 |
nicklas |
17 |
|
4522 |
15 Sep 08 |
nicklas |
18 |
You should have received a copy of the GNU General Public License |
4522 |
15 Sep 08 |
nicklas |
19 |
along with BASE. If not, see <http://www.gnu.org/licenses/>. |
4522 |
15 Sep 08 |
nicklas |
20 |
------------------------------------------------------------------ |
4522 |
15 Sep 08 |
nicklas |
21 |
|
4522 |
15 Sep 08 |
nicklas |
22 |
@author Nicklas |
4522 |
15 Sep 08 |
nicklas |
23 |
--%> |
5558 |
28 Jan 11 |
nicklas |
24 |
<%@ page pageEncoding="UTF-8" session="false" contentType="application/json" |
4522 |
15 Sep 08 |
nicklas |
25 |
import="net.sf.basedb.core.SessionControl" |
4522 |
15 Sep 08 |
nicklas |
26 |
import="net.sf.basedb.core.DbControl" |
4522 |
15 Sep 08 |
nicklas |
27 |
import="net.sf.basedb.core.Config" |
4522 |
15 Sep 08 |
nicklas |
28 |
import="net.sf.basedb.core.File" |
5136 |
15 Oct 09 |
nicklas |
29 |
import="net.sf.basedb.core.InvalidDataException" |
7654 |
15 Mar 19 |
nicklas |
30 |
import="net.sf.basedb.core.Path" |
4522 |
15 Sep 08 |
nicklas |
31 |
import="net.sf.basedb.util.Values" |
4867 |
31 Mar 09 |
nicklas |
32 |
import="net.sf.basedb.util.FileUtil" |
7654 |
15 Mar 19 |
nicklas |
33 |
import="net.sf.basedb.util.excel.XlsxToCsvUtil" |
5558 |
28 Jan 11 |
nicklas |
34 |
import="net.sf.basedb.util.error.ThrowableUtil" |
4522 |
15 Sep 08 |
nicklas |
35 |
import="net.sf.basedb.clients.web.Base" |
4522 |
15 Sep 08 |
nicklas |
36 |
import="net.sf.basedb.clients.web.WebException" |
5558 |
28 Jan 11 |
nicklas |
37 |
import="org.json.simple.JSONObject" |
4867 |
31 Mar 09 |
nicklas |
38 |
import="java.io.OutputStreamWriter" |
4867 |
31 Mar 09 |
nicklas |
39 |
import="java.io.Writer" |
4867 |
31 Mar 09 |
nicklas |
40 |
import="java.io.Reader" |
7626 |
07 Mar 19 |
nicklas |
41 |
import="java.nio.charset.Charset" |
7626 |
07 Mar 19 |
nicklas |
42 |
import="java.nio.charset.CodingErrorAction" |
7626 |
07 Mar 19 |
nicklas |
43 |
import="java.nio.charset.CharsetEncoder" |
7626 |
07 Mar 19 |
nicklas |
44 |
import="java.nio.charset.CharacterCodingException" |
4522 |
15 Sep 08 |
nicklas |
45 |
%> |
4522 |
15 Sep 08 |
nicklas |
46 |
<% |
6124 |
13 Sep 12 |
nicklas |
47 |
response.setHeader("Cache-Control", "no-cache, max-age=0"); |
4522 |
15 Sep 08 |
nicklas |
48 |
final SessionControl sc = Base.getExistingSessionControl(pageContext, true); |
4522 |
15 Sep 08 |
nicklas |
49 |
final String ID = sc.getId(); |
4522 |
15 Sep 08 |
nicklas |
50 |
final String cmd = request.getParameter("cmd"); |
4522 |
15 Sep 08 |
nicklas |
51 |
final String root = request.getContextPath()+"/"; |
4522 |
15 Sep 08 |
nicklas |
52 |
final int itemId = Values.getInt(request.getParameter("item_id")); |
7654 |
15 Mar 19 |
nicklas |
53 |
final String path = Values.getStringOrNull(request.getParameter("path")); |
4522 |
15 Sep 08 |
nicklas |
54 |
DbControl dc = null; |
4522 |
15 Sep 08 |
nicklas |
55 |
out.clear(); |
5558 |
28 Jan 11 |
nicklas |
56 |
JSONObject json = new JSONObject(); |
5558 |
28 Jan 11 |
nicklas |
57 |
json.put("status", "ok"); |
4522 |
15 Sep 08 |
nicklas |
58 |
try |
4522 |
15 Sep 08 |
nicklas |
59 |
{ |
5558 |
28 Jan 11 |
nicklas |
60 |
if ("GetFileInfo".equals(cmd)) |
4522 |
15 Sep 08 |
nicklas |
61 |
{ |
7954 |
12 May 21 |
nicklas |
62 |
dc = sc.newDbControl(":Get file information"); |
7654 |
15 Mar 19 |
nicklas |
63 |
File f = null; |
7654 |
15 Mar 19 |
nicklas |
64 |
if (itemId != 0) |
7654 |
15 Mar 19 |
nicklas |
65 |
{ |
7654 |
15 Mar 19 |
nicklas |
66 |
f = File.getById(dc, itemId); |
7654 |
15 Mar 19 |
nicklas |
67 |
} |
7654 |
15 Mar 19 |
nicklas |
68 |
else |
7654 |
15 Mar 19 |
nicklas |
69 |
{ |
7654 |
15 Mar 19 |
nicklas |
70 |
f = File.getByPath(dc, new Path(path, Path.Type.FILE), false); |
7654 |
15 Mar 19 |
nicklas |
71 |
} |
5558 |
28 Jan 11 |
nicklas |
72 |
json.put("id", f.getId()); |
5558 |
28 Jan 11 |
nicklas |
73 |
json.put("name", f.getName()); |
5558 |
28 Jan 11 |
nicklas |
74 |
json.put("characterSet", f.getCharacterSet()); |
7654 |
15 Mar 19 |
nicklas |
75 |
json.put("mimeType", f.getMimeType()); |
7654 |
15 Mar 19 |
nicklas |
76 |
if (Values.getBoolean(request.getParameter("checktype"))) |
7654 |
15 Mar 19 |
nicklas |
77 |
{ |
7654 |
15 Mar 19 |
nicklas |
78 |
if (XlsxToCsvUtil.seemsLikeAnExcelFile(f)) |
7654 |
15 Mar 19 |
nicklas |
79 |
{ |
7654 |
15 Mar 19 |
nicklas |
80 |
XlsxToCsvUtil util = new XlsxToCsvUtil(); |
7654 |
15 Mar 19 |
nicklas |
81 |
try |
7654 |
15 Mar 19 |
nicklas |
82 |
{ |
7654 |
15 Mar 19 |
nicklas |
83 |
util.readWorkbook(f.getDownloadStream(0)); |
7654 |
15 Mar 19 |
nicklas |
84 |
json.put("excelMode", true); |
7654 |
15 Mar 19 |
nicklas |
85 |
json.put("sheets", util.getSheetNames()); |
7654 |
15 Mar 19 |
nicklas |
86 |
} |
7654 |
15 Mar 19 |
nicklas |
87 |
catch (Exception ex) |
7654 |
15 Mar 19 |
nicklas |
88 |
{} // It was maybe not an Excel file after all |
7654 |
15 Mar 19 |
nicklas |
89 |
} |
7654 |
15 Mar 19 |
nicklas |
90 |
} |
4522 |
15 Sep 08 |
nicklas |
91 |
dc.close(); |
4522 |
15 Sep 08 |
nicklas |
92 |
} |
4867 |
31 Mar 09 |
nicklas |
93 |
else if ("StoreFile".equals(cmd)) |
4867 |
31 Mar 09 |
nicklas |
94 |
{ |
7626 |
07 Mar 19 |
nicklas |
95 |
String charset = null; |
4867 |
31 Mar 09 |
nicklas |
96 |
try |
4867 |
31 Mar 09 |
nicklas |
97 |
{ |
5136 |
15 Oct 09 |
nicklas |
98 |
int size = Values.getInt(request.getParameter("size"), -1); |
7626 |
07 Mar 19 |
nicklas |
99 |
charset = Values.getStringOrNull(request.getParameter("charset")); |
7626 |
07 Mar 19 |
nicklas |
100 |
|
7954 |
12 May 21 |
nicklas |
101 |
dc = sc.newDbControl(":Store file"); |
4867 |
31 Mar 09 |
nicklas |
102 |
File f = File.getById(dc, itemId); |
4867 |
31 Mar 09 |
nicklas |
103 |
Reader in = request.getReader(); |
4867 |
31 Mar 09 |
nicklas |
104 |
if (charset == null) |
4867 |
31 Mar 09 |
nicklas |
105 |
{ |
7626 |
07 Mar 19 |
nicklas |
106 |
charset = Values.getString(f.getCharacterSet(), "UTF-8"); |
4867 |
31 Mar 09 |
nicklas |
107 |
} |
7626 |
07 Mar 19 |
nicklas |
108 |
boolean charsetModified = !charset.equals(f.getCharacterSet()); |
7626 |
07 Mar 19 |
nicklas |
109 |
if (charsetModified) f.setCharacterSet(charset); |
7626 |
07 Mar 19 |
nicklas |
110 |
Charset cs = Charset.forName(charset); |
7626 |
07 Mar 19 |
nicklas |
111 |
if (!cs.canEncode()) |
7626 |
07 Mar 19 |
nicklas |
112 |
{ |
7626 |
07 Mar 19 |
nicklas |
113 |
throw new UnsupportedOperationException("Encoding is not supported by '" + charset + "'."); |
7626 |
07 Mar 19 |
nicklas |
114 |
} |
7626 |
07 Mar 19 |
nicklas |
115 |
CharsetEncoder encoder = Charset.forName(charset).newEncoder(); |
7626 |
07 Mar 19 |
nicklas |
116 |
encoder.onMalformedInput(CodingErrorAction.REPORT); |
7626 |
07 Mar 19 |
nicklas |
117 |
encoder.onUnmappableCharacter(CodingErrorAction.REPORT); |
4867 |
31 Mar 09 |
nicklas |
118 |
Writer toFile = new OutputStreamWriter( |
7626 |
07 Mar 19 |
nicklas |
119 |
f.getUploadStream(false, f.isCompressed()), encoder); |
5136 |
15 Oct 09 |
nicklas |
120 |
int copied = (int)FileUtil.copy(in, toFile); |
5136 |
15 Oct 09 |
nicklas |
121 |
if (size >= 0 && copied != size) |
5136 |
15 Oct 09 |
nicklas |
122 |
{ |
5136 |
15 Oct 09 |
nicklas |
123 |
throw new InvalidDataException("Incomplete file; expected " + size + |
5136 |
15 Oct 09 |
nicklas |
124 |
" bytes; got " + copied + "."); |
5136 |
15 Oct 09 |
nicklas |
125 |
} |
4867 |
31 Mar 09 |
nicklas |
126 |
toFile.flush(); |
4867 |
31 Mar 09 |
nicklas |
127 |
toFile.close(); |
4867 |
31 Mar 09 |
nicklas |
128 |
dc.commit(); |
7626 |
07 Mar 19 |
nicklas |
129 |
json.put("message", "File saved " + (charsetModified ? " with character set " + charset : " successfully")); |
4867 |
31 Mar 09 |
nicklas |
130 |
} |
7626 |
07 Mar 19 |
nicklas |
131 |
catch (CharacterCodingException ex) |
7626 |
07 Mar 19 |
nicklas |
132 |
{ |
7626 |
07 Mar 19 |
nicklas |
133 |
throw new Exception("Save failed: The text contains characters that can't be encoded in '" + charset + "'."); |
7626 |
07 Mar 19 |
nicklas |
134 |
} |
4867 |
31 Mar 09 |
nicklas |
135 |
catch (Exception ex) |
4867 |
31 Mar 09 |
nicklas |
136 |
{ |
5558 |
28 Jan 11 |
nicklas |
137 |
throw new Exception("Save failed: " + ex.getMessage(), ex); |
4867 |
31 Mar 09 |
nicklas |
138 |
} |
4867 |
31 Mar 09 |
nicklas |
139 |
} |
4522 |
15 Sep 08 |
nicklas |
140 |
else |
4522 |
15 Sep 08 |
nicklas |
141 |
{ |
4522 |
15 Sep 08 |
nicklas |
142 |
throw new WebException("popup", "Invalid command", "The command {1} is not recognised as a valid command.", cmd); |
4522 |
15 Sep 08 |
nicklas |
143 |
} |
4522 |
15 Sep 08 |
nicklas |
144 |
} |
5558 |
28 Jan 11 |
nicklas |
145 |
catch (Throwable t) |
5558 |
28 Jan 11 |
nicklas |
146 |
{ |
5558 |
28 Jan 11 |
nicklas |
147 |
t.printStackTrace(); |
5558 |
28 Jan 11 |
nicklas |
148 |
json.clear(); |
5558 |
28 Jan 11 |
nicklas |
149 |
json.put("status", "error"); |
5558 |
28 Jan 11 |
nicklas |
150 |
json.put("message", t.getMessage()); |
5558 |
28 Jan 11 |
nicklas |
151 |
json.put("stacktrace", ThrowableUtil.stackTraceToString(t)); |
5558 |
28 Jan 11 |
nicklas |
152 |
} |
4522 |
15 Sep 08 |
nicklas |
153 |
finally |
4522 |
15 Sep 08 |
nicklas |
154 |
{ |
5558 |
28 Jan 11 |
nicklas |
155 |
json.writeJSONString(out); |
5558 |
28 Jan 11 |
nicklas |
156 |
out.flush(); |
4522 |
15 Sep 08 |
nicklas |
157 |
if (dc != null) dc.close(); |
4522 |
15 Sep 08 |
nicklas |
158 |
} |
4522 |
15 Sep 08 |
nicklas |
159 |
%> |