annotate DVN-web/src/main/java/de/mpiwg/monographs/servlet/SaveResource.java @ 4:9b408c9b05ab

Integration with LGServices.
author "jurzua <jurzua@mpiwg-berlin.mpg.de>"
date Thu, 07 May 2015 14:56:46 +0200
parents 2ae72563a29d
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
1 package de.mpiwg.monographs.servlet;
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
2
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
3 import java.io.File;
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
4 import java.io.FileOutputStream;
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
5 import java.io.IOException;
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
6 import java.io.PrintWriter;
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
7 import java.util.ArrayList;
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
8 import java.util.HashMap;
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
9 import java.util.Iterator;
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
10 import java.util.List;
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
11 import java.util.Map;
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
12 import java.util.logging.Level;
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
13 import java.util.logging.Logger;
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
14
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
15 import javax.ejb.EJB;
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
16 import javax.ejb.EJBException;
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
17 import javax.inject.Inject;
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
18 import javax.naming.Context;
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
19 import javax.naming.InitialContext;
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
20 import javax.naming.NamingException;
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
21 import javax.servlet.ServletException;
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
22 import javax.servlet.ServletInputStream;
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
23 import javax.servlet.http.HttpServletRequest;
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
24 import javax.servlet.http.HttpServletResponse;
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
25 import javax.servlet.http.Part;
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
26
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
27 import org.apache.commons.collections.map.HashedMap;
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
28 import org.apache.commons.fileupload.FileItem;
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
29 import org.apache.commons.fileupload.disk.DiskFileItemFactory;
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
30 import org.apache.commons.fileupload.servlet.ServletFileUpload;
4
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
31 import org.apache.commons.lang.StringUtils;
2
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
32 import org.codehaus.jettison.json.JSONException;
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
33 import org.codehaus.jettison.json.JSONObject;
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
34 import org.swordapp.server.AuthCredentials;
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
35 import org.swordapp.server.DepositReceipt;
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
36 import org.swordapp.server.ServiceDocumentAPI;
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
37 import org.swordapp.server.SwordAuthException;
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
38 import org.swordapp.server.SwordError;
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
39 import org.swordapp.server.SwordServerException;
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
40 import org.swordapp.server.UriRegistry;
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
41 import org.swordapp.server.servlets.SwordServlet;
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
42
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
43 import edu.harvard.iq.dvn.api.datadeposit.ReceiptGenerator;
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
44 import edu.harvard.iq.dvn.api.datadeposit.ServiceDocumentManagerImpl;
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
45 import edu.harvard.iq.dvn.api.datadeposit.SwordAuth;
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
46 import edu.harvard.iq.dvn.api.datadeposit.UrlManager;
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
47 import edu.harvard.iq.dvn.core.admin.VDCUser;
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
48 import edu.harvard.iq.dvn.core.study.EditStudyService;
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
49 import edu.harvard.iq.dvn.core.study.Study;
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
50 import edu.harvard.iq.dvn.core.study.StudyFileEditBean;
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
51 import edu.harvard.iq.dvn.core.study.StudyFileServiceLocal;
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
52 import edu.harvard.iq.dvn.core.study.StudyServiceLocal;
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
53 import edu.harvard.iq.dvn.core.study.TabularDataFile;
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
54 import edu.harvard.iq.dvn.core.util.FileUtil;
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
55
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
56 public class SaveResource extends AbstractMonographServlet {
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
57
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
58 private static final long serialVersionUID = 1L;
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
59
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
60
4
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
61 private static final String TMP_DIR = "/gazetteer-server/tmp/";
2
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
62
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
63
4
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
64 private int maxFileSize = 100 * 1024;
2
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
65 private int maxMemSize = 4 * 1024;
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
66
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
67 private static final Logger logger = Logger.getLogger("monographs.SaveResource");
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
68
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
69 @EJB
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
70 private StudyFileServiceLocal studyFileService;
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
71
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
72 @EJB
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
73 private StudyServiceLocal studyService;
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
74
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
75 @Inject
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
76 private SwordAuth swordAuth;
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
77
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
78
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
79 Map<String, String> params = new HashMap<String, String>();
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
80
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
81 public void init() throws ServletException {
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
82 super.init();
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
83 }
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
84
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
85 @Override
4
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
86 protected void doPost(HttpServletRequest request, HttpServletResponse response) {
2
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
87
4
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
88 try {
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
89 response.setContentType("application/json");
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
90 JSONObject jsonResponse = new JSONObject();
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
91
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
92 File file = getFile(request, response);
2
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
93
4
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
94 if (file != null && file.length() > 0) {
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
95 String studyGlobalId = params.get("studyId");
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
96 String user = params.get("user");
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
97 String password = params.get("password");
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
98 //String fileLabel = params.get("fileLabel");
2
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
99
4
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
100 if(StringUtils.isNotEmpty(studyGlobalId) &&
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
101 StringUtils.isNotEmpty(user) &&
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
102 StringUtils.isNotEmpty(password)/* &&
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
103 StringUtils.isNotEmpty(fileLabel)*/){
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
104 AuthCredentials authCredentials = new AuthCredentials(user, password, null);
2
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
105
4
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
106 try {
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
107 List<StudyFileEditBean> list = saveFile(file, studyGlobalId, authCredentials/*, fileLabel*/);
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
108
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
109 JSONObject fileMetatdata = new JSONObject();
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
110 fileMetatdata.put("id", list.get(0).getFileMetadata().getId());
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
111 fileMetatdata.put("label", list.get(0).getFileMetadata().getLabel());
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
112 fileMetatdata.put("version", list.get(0).getFileMetadata().getVersion());
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
113 fileMetatdata.put("description", list.get(0).getFileMetadata().getDescription());
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
114
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
115
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
116 jsonResponse.put("fileMetadata", fileMetatdata);
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
117 jsonResponse.put("status", "ok");
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
118
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
119 } catch (Exception e) {
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
120 logger.log(Level.SEVERE, "Error!!!!!!", e);
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
121 error(jsonResponse, e);
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
122 }
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
123 }else{
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
124 jsonResponse.put("status", "error");
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
125 jsonResponse.put("error", "The parameters studyId, user and password.");
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
126 jsonResponse.put("studyId", studyGlobalId);
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
127 jsonResponse.put("user", user);
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
128 jsonResponse.put("password", password);
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
129 //jsonResponse.put("fileLabel", fileLabel);
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
130 }
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
131
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
132 }else if(file == null){
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
133 jsonResponse.put("status", "error");
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
134 jsonResponse.put("message", "No file found in the request.");
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
135 }else {
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
136 jsonResponse.put("status", "error");
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
137 jsonResponse.put("message", "A file has been found as parameter, however it was empty. Check the temporal folder configured in Dataverse. " + TMP_DIR);
2
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
138 }
4
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
139
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
140 java.io.PrintWriter out = response.getWriter();
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
141 out.print(jsonResponse);
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
142 out.flush();
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
143 } catch (Exception e) {
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
144 e.printStackTrace();
2
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
145 }
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
146 }
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
147
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
148 private File getFile(HttpServletRequest request, HttpServletResponse response) throws IOException {
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
149 File file = null;
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
150 boolean isMultipart = ServletFileUpload.isMultipartContent(request);
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
151
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
152 if(isMultipart){
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
153 DiskFileItemFactory factory = new DiskFileItemFactory();
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
154 // maximum size that will be stored in memory
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
155 factory.setSizeThreshold(maxMemSize);
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
156 // Location to save data that is larger than maxMemSize.
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
157 factory.setRepository(new File(TMP_DIR));
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
158
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
159 // Create a new file upload handler
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
160 ServletFileUpload upload = new ServletFileUpload(factory);
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
161 // maximum file size to be uploaded.
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
162 upload.setSizeMax(maxFileSize);
4
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
163 StringBuilder sb = new StringBuilder();
2
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
164 try {
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
165 // Parse the request to get file items.
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
166 List fileItems = upload.parseRequest(request);
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
167
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
168 // Process the uploaded file items
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
169 Iterator i = fileItems.iterator();
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
170
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
171 while (i.hasNext()) {
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
172 FileItem fi = (FileItem) i.next();
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
173
4
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
174 sb.append(fi.getFieldName());
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
175
2
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
176 if(fi.isFormField()){
4
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
177 sb.append(" is Field");
2
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
178 params.put(fi.getFieldName(), fi.getString());
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
179 } else {
4
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
180 sb.append(" is FILE ***");
2
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
181 // Get the uploaded file parameters
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
182 String fileName = fi.getName();
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
183 // Write the file
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
184 if (fileName.lastIndexOf("\\") >= 0) {
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
185 file = new File(TMP_DIR + fileName.substring(fileName.lastIndexOf("\\")));
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
186 } else {
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
187 file = new File(TMP_DIR + fileName.substring(fileName.lastIndexOf("\\") + 1));
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
188 }
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
189 fi.write(file);
4
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
190 sb.append(" Size: " + file.length());
2
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
191 }
4
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
192 sb.append("\n");
2
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
193 }
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
194
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
195 } catch (Exception ex) {
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
196 ex.printStackTrace();
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
197 }
4
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
198
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
199 sb.append((file == null) ? "File is null" : "File Size=" + file.length() + ".\nWhen the file is empty, maybe the tmp folder is wrong configured.\nTMP_DIR: " + TMP_DIR);
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
200 logger.log(Level.INFO, "getFile Information:\n" + sb.toString());
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
201
2
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
202 return file;
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
203 }
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
204
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
205 return null;
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
206 }
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
207
4
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
208 private List<StudyFileEditBean> saveFile(File file, String studyGlobalId, AuthCredentials authCredentials/*, String fileLabel*/) throws Exception {
2
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
209 VDCUser vdcUser = swordAuth.auth(authCredentials);
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
210
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
211 Study study = getStudy(studyGlobalId);
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
212
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
213 try {
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
214 List<StudyFileEditBean> files = getFileList(file, study);
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
215 studyFileService.addFiles(study.getLatestVersion(), files, vdcUser);
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
216 return files;
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
217 } catch (EJBException ex) {
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
218 throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "Unable to add file(s) to study: " + ex.getMessage());
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
219 }
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
220 }
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
221
4
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
222 public List<StudyFileEditBean> getFileList(File uploadedInputFile, Study study) throws Exception {
2
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
223 List<StudyFileEditBean> fbList = new ArrayList<StudyFileEditBean>();
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
224
4
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
225 if (uploadedInputFile.length() != 0) {
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
226
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
227 File dir = new File(uploadedInputFile.getParentFile(), study.getId().toString() );
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
228 if ( !dir.exists() ) {
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
229 dir.mkdir();
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
230 }
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
231 File file = FileUtil.createTempFile(dir, uploadedInputFile.getName());
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
232 if (!uploadedInputFile.renameTo(file)) {
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
233 // in windows environment, rename doesn't work, so we will copy the file instead
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
234 FileUtil.copyFile(uploadedInputFile, file);
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
235 uploadedInputFile.delete();
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
236 }
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
237
9b408c9b05ab Integration with LGServices.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents: 2
diff changeset
238 StudyFileEditBean tempFileBean = new StudyFileEditBean(file, studyService.generateFileSystemNameSequence(), study);
2
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
239 tempFileBean.setSizeFormatted(file.length());
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
240 fbList.add(tempFileBean);
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
241 }
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
242
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
243 return fbList;
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
244 }
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
245
2ae72563a29d Commit of the project with the correct structure.
"jurzua <jurzua@mpiwg-berlin.mpg.de>"
parents:
diff changeset
246 }