3
3
*/
4
4
package edu .caltech .ipac .firefly .server .servlets ;
5
5
6
+ import edu .caltech .ipac .firefly .data .TableServerRequest ;
6
7
import edu .caltech .ipac .firefly .server .Counters ;
7
8
import edu .caltech .ipac .firefly .server .ServerContext ;
8
9
import edu .caltech .ipac .firefly .server .cache .UserCache ;
9
10
import edu .caltech .ipac .firefly .server .util .Logger ;
10
11
import edu .caltech .ipac .firefly .server .util .StopWatch ;
11
12
import edu .caltech .ipac .firefly .server .util .ipactable .DataGroupReader ;
12
13
import edu .caltech .ipac .firefly .server .util .ipactable .DataGroupWriter ;
14
+ import edu .caltech .ipac .firefly .server .util .ipactable .JsonTableUtil ;
13
15
import edu .caltech .ipac .firefly .server .util .multipart .UploadFileInfo ;
14
16
import edu .caltech .ipac .util .DataGroup ;
15
17
import edu .caltech .ipac .util .FileUtil ;
16
18
import edu .caltech .ipac .util .IpacTableUtil ;
17
19
import edu .caltech .ipac .util .StringUtils ;
18
20
import edu .caltech .ipac .util .cache .StringKey ;
21
+ import edu .caltech .ipac .util .download .URLDownload ;
22
+ import edu .caltech .ipac .firefly .data .FileInfo ;
19
23
import org .apache .commons .fileupload .FileItemIterator ;
20
24
import org .apache .commons .fileupload .FileItemStream ;
21
25
import org .apache .commons .fileupload .servlet .ServletFileUpload ;
24
28
import javax .servlet .http .HttpServletResponse ;
25
29
import java .io .*;
26
30
import java .util .HashMap ;
31
+ import org .json .simple .JSONObject ;
32
+ import java .util .Iterator ;
33
+ import java .util .Map .Entry ;
34
+ import java .util .Map ;
35
+ import java .net .URL ;
27
36
28
37
/**
29
38
* Date: Feb 16, 2011
@@ -37,7 +46,7 @@ public class AnyFileUpload extends BaseHttpServlet {
37
46
public static final String PRELOAD_PARAM = "preload" ;
38
47
public static final String FILE_TYPE = "type" ;
39
48
public static final String CACHE_KEY = "cacheKey" ;
40
- private enum FileType {FITS , TABLE , REGION , UNKNOWN }
49
+ private enum FileType {FITS , TABLE , REGION , XML , UNKNOWN }
41
50
42
51
protected void processRequest (HttpServletRequest req , HttpServletResponse res ) throws Exception {
43
52
doFileUpload (req , res );
@@ -53,6 +62,8 @@ public static void doFileUpload(HttpServletRequest req, HttpServletResponse res)
53
62
ServletFileUpload upload = new ServletFileUpload ();
54
63
FileItemIterator iter = upload .getItemIterator (req );
55
64
FileItemStream file = null ;
65
+ String url = null ;
66
+
56
67
HashMap <String , String > params = new HashMap <>();
57
68
58
69
while (iter .hasNext ()) {
@@ -65,45 +76,123 @@ public static void doFileUpload(HttpServletRequest req, HttpServletResponse res)
65
76
String name = item .getFieldName ();
66
77
String value = FileUtil .readFile (item .openStream ());
67
78
params .put (name , value );
79
+ if (name .equals ("URL" )) {
80
+ url = new String (value );
81
+ }
68
82
}
69
83
}
70
84
71
85
String dest = getParam (DEST_PARAM , params , req );
72
86
String preload = getParam (PRELOAD_PARAM , params , req );
73
87
String overrideCacheKey = getParam (CACHE_KEY , params , req );
74
88
String fileType = getParam (FILE_TYPE , params , req );
89
+ String fileAnalysis = getParam ("fileAnalysis" , params , req );
75
90
76
- if (file != null ) {
77
- String fileName = file .getName ();
78
- InputStream inStream = new BufferedInputStream (file .openStream (), IpacTableUtil .FILE_IO_BUFFER_SIZE );
79
- String ext = resolveExt (fileName );
80
- FileType fType = resolveType (fileType , ext , file .getContentType ());
81
- File destDir = resolveDestDir (dest , fType );
82
- // boolean doPreload = resolvePreload(preload, fType); // we are no longer pre-loading fits files. performance gain was not significant.
91
+ if (file != null || url != null ) {
92
+ UploadFileInfo fi ;
93
+ String ext ;
94
+ File destDir ;
95
+ FileType fType ;
96
+ File uf ;
97
+ String fileName ;
98
+ FileInfo urlDownloadInfo = null ;
99
+
100
+ if (file != null ) {
101
+ fileName = file .getName ();
102
+ } else {
103
+ int idx = url .lastIndexOf ('/' );
104
+ fileName = (idx >= 0 ) ? url .substring (idx + 1 ) : new String (url );
105
+ }
106
+ ext = resolveExt (fileName );
107
+ fType = resolveType (fileType , ext , (file != null ? file .getContentType () : null ));
108
+ destDir = resolveDestDir (dest , fType );
109
+ uf = File .createTempFile ("upload_" , ext , destDir ); // other parts of system depend on file name starting with "upload_"
110
+ if (file != null ) {
111
+ InputStream inStream = new BufferedInputStream (file .openStream (), IpacTableUtil .FILE_IO_BUFFER_SIZE );
112
+ FileUtil .writeToFile (inStream , uf );
113
+ } else {
114
+ urlDownloadInfo = URLDownload .getDataToFile (new URL (url ), uf );
115
+ }
83
116
84
- File uf = File .createTempFile ("upload_" , ext , destDir ); // other parts of system depend on file name starting with "upload_"
85
117
String rPathInfo = ServerContext .replaceWithPrefix (uf );
118
+ fi = new UploadFileInfo (rPathInfo , uf , fileName , (file != null ? file .getContentType () : null ));
119
+
120
+ JSONObject analysisResult = null ;
121
+ if (fileAnalysis != null && fileAnalysis .equalsIgnoreCase ("true" )) {
122
+ if (url != null && urlDownloadInfo != null &&
123
+ !(urlDownloadInfo .getResponseCodeMsg ().equals ("OK" ))) {
124
+ throw new Exception ("invalid upload from URL: " + urlDownloadInfo .getResponseCodeMsg ());
125
+ }
126
+
127
+ analysisResult = createAnalysisResult (fi );
128
+ }
86
129
87
- UploadFileInfo fi = new UploadFileInfo (rPathInfo ,uf ,fileName ,file .getContentType ());
88
- FileUtil .writeToFile (inStream , uf );
89
130
if (fType == FileType .TABLE ) {
90
131
uf = File .createTempFile ("upload_" , ".tbl" , destDir ); // cleaned ipac file.
91
132
rPathInfo = ServerContext .replaceWithPrefix (uf );
92
133
DataGroup dg = DataGroupReader .readAnyFormat (fi .getFile (), 0 );
93
134
DataGroupWriter .write (new DataGroupWriter .IpacTableHandler (uf , dg ));
94
- fi = new UploadFileInfo (rPathInfo ,uf ,fileName ,file .getContentType ());
135
+ fi = new UploadFileInfo (rPathInfo , uf , fileName , ( file != null ? file .getContentType () : null ));
95
136
}
96
- String fileCacheKey = overrideCacheKey != null ? overrideCacheKey : rPathInfo ;
137
+ String fileCacheKey = overrideCacheKey != null ? overrideCacheKey : rPathInfo ;
97
138
UserCache .getInstance ().put (new StringKey (fileCacheKey ), fi );
98
139
140
+ if (analysisResult != null ) {
141
+ String resultS = analysisResult .toJSONString ();
142
+ String fFormat = (String )analysisResult .get ("fileFormat" );
143
+
144
+ if (!StringUtils .isEmpty (resultS )) {
145
+ fileCacheKey = fileCacheKey + "::" + fFormat + "::" + resultS ;
146
+ }
147
+ }
148
+
99
149
sendReturnMsg (res , 200 , null , fileCacheKey );
150
+
100
151
Counters .getInstance ().increment (Counters .Category .Upload , fi .getContentType ());
101
152
102
153
}
103
154
104
155
StopWatch .getInstance ().printLog ("Upload File" );
105
156
}
106
157
158
+
159
+ private static JSONObject createAnalysisResult (UploadFileInfo fi ) throws Exception {
160
+ JSONObject analysisModel = null ;
161
+ DataGroupReader .Format fileFormat = null ;
162
+ DataGroup dgAnalysis = null ;
163
+ String analysisSummary = "" ;
164
+
165
+ JSONObject analysisResult = new JSONObject ();
166
+ File f = fi .getFile ();
167
+ long size = f .length ();
168
+
169
+ fileFormat = DataGroupReader .guessFormat (f );
170
+ dgAnalysis = DataGroupReader .readAnyFormatHeader (f , fileFormat );
171
+ if (dgAnalysis != null ) {
172
+ analysisSummary = dgAnalysis .getTitle ();
173
+ if (!analysisSummary .contains ("invalid" )) {
174
+ analysisModel = toJsonAnalysisTableModel (dgAnalysis , fileFormat , size );
175
+ }
176
+ } else {
177
+ analysisSummary = "invalid " + fileFormat .toString () + " file" ;
178
+ }
179
+
180
+ if (analysisSummary .startsWith ("invalid" )) {
181
+ throw new Exception (analysisSummary );
182
+ }
183
+
184
+ analysisResult .put ("status" , 200 );
185
+ analysisResult .put ("message" , "" );
186
+ analysisResult .put ("fileCacheKey" , fi .getPname ());
187
+ analysisResult .put ("analysisSummary" , analysisSummary );
188
+ analysisResult .put ("fileFormat" , fileFormat .toString ());
189
+ if (analysisModel != null ) {
190
+ analysisResult .put ("analysisModel" , analysisModel );
191
+ }
192
+
193
+ return analysisResult ;
194
+ }
195
+
107
196
private static String getParam (String key , HashMap <String , String > params , HttpServletRequest req ) {
108
197
if (key == null ) return null ;
109
198
if (params .containsKey (key )) {
@@ -148,8 +237,10 @@ private static FileType resolveType(String fileType, String fileExtension, Strin
148
237
ftype = FileType .FITS ;
149
238
} else if (fileExtension .matches ("\\ .tbl|\\ .csv|\\ .tsv" )) {
150
239
ftype = FileType .TABLE ;
151
- } else if (fileExtension .matches (".reg" )) {
240
+ } else if (fileExtension .matches ("\\ .reg" )) {
152
241
ftype = FileType .REGION ;
242
+ } else if (fileExtension .matches ("\\ .xml|\\ .vot" )) {
243
+ ftype = FileType .XML ;
153
244
}
154
245
} else {
155
246
// guess using contentType
@@ -171,6 +262,32 @@ private boolean resolvePreload(String preload, FileType fileType) {
171
262
}
172
263
}
173
264
265
+ private static JSONObject toJsonAnalysisTableModel (DataGroup dg , DataGroupReader .Format ff , long size ) {
266
+ JSONObject tableModel = new JSONObject ();
267
+ JSONObject tableData = JsonTableUtil .toJsonTableData (dg , null );
268
+ String tblId = "UPLOAD_ANALYSIS" ;
269
+
270
+ tableModel .put ("tableData" , tableData );
271
+ tableModel .put ("tbl_id" , tblId );
272
+ tableModel .put ("title" , dg .getTitle ());
273
+ tableModel .put ("totalRows" , dg .values ().size ());
274
+ tableModel .put ("fileFormat" , ff .toString ());
275
+ tableModel .put ("highlightedRow" , 0 );
276
+ tableModel .put ("size" , size );
277
+
278
+ JSONObject tableMeta = new JSONObject ();
279
+ Iterator <Entry <String , DataGroup .Attribute >> attributes = dg .getAttributes ().entrySet ().iterator ();
280
+
281
+ while ( attributes .hasNext () ) {
282
+ Map .Entry <String , DataGroup .Attribute > entry = attributes .next ();
283
+ DataGroup .Attribute att = entry .getValue ();
284
+
285
+ tableMeta .put (att .getKey (), att .getValue ());
286
+ }
287
+
288
+ tableModel .put ("tableMeta" , tableMeta );
289
+ return tableModel ;
290
+ }
174
291
175
292
}
176
293
0 commit comments