Mercurial > hg > LGDataverses
diff 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 |
line wrap: on
line diff
--- a/DVN-web/src/main/java/de/mpiwg/monographs/servlet/SaveResource.java Tue Mar 10 15:47:07 2015 +0100 +++ b/DVN-web/src/main/java/de/mpiwg/monographs/servlet/SaveResource.java Thu May 07 14:56:46 2015 +0200 @@ -28,6 +28,7 @@ import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; +import org.apache.commons.lang.StringUtils; import org.codehaus.jettison.json.JSONException; import org.codehaus.jettison.json.JSONObject; import org.swordapp.server.AuthCredentials; @@ -57,10 +58,10 @@ private static final long serialVersionUID = 1L; - private static final String TMP_DIR = "/home/jurzua/glassfish3/glassfish/domains/domain1/config/files/temp/monographs/"; + private static final String TMP_DIR = "/gazetteer-server/tmp/"; - private int maxFileSize = 50 * 1024; + private int maxFileSize = 100 * 1024; private int maxMemSize = 4 * 1024; private static final Logger logger = Logger.getLogger("monographs.SaveResource"); @@ -82,53 +83,69 @@ } @Override - protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, - IOException { + protected void doPost(HttpServletRequest request, HttpServletResponse response) { - logger.info("SaveResource"); - - response.setContentType("application/json"); - JSONObject jsonResponse = new JSONObject(); + try { + response.setContentType("application/json"); + JSONObject jsonResponse = new JSONObject(); + + File file = getFile(request, response); - File file = getFile(request, response); + if (file != null && file.length() > 0) { + String studyGlobalId = params.get("studyId"); + String user = params.get("user"); + String password = params.get("password"); + //String fileLabel = params.get("fileLabel"); - if (file != null) { - String studyGlobalId = params.get("studyId"); - String user = params.get("user"); - String password = params.get("password"); - AuthCredentials authCredentials = new AuthCredentials(user, password, null); + if(StringUtils.isNotEmpty(studyGlobalId) && + StringUtils.isNotEmpty(user) && + StringUtils.isNotEmpty(password)/* && + StringUtils.isNotEmpty(fileLabel)*/){ + AuthCredentials authCredentials = new AuthCredentials(user, password, null); - try { - List<StudyFileEditBean> list = saveFile(file, studyGlobalId, authCredentials); - - JSONObject fileMetatdata = new JSONObject(); - fileMetatdata.put("id", list.get(0).getFileMetadata().getId()); - fileMetatdata.put("label", list.get(0).getFileMetadata().getLabel()); - fileMetatdata.put("version", list.get(0).getFileMetadata().getVersion()); - fileMetatdata.put("description", list.get(0).getFileMetadata().getDescription()); - - - jsonResponse.put("fileMetadata", fileMetatdata); - - } catch (Exception e) { - logger.log(Level.SEVERE, "Error!!!!!!", e); - error(jsonResponse, e); + try { + List<StudyFileEditBean> list = saveFile(file, studyGlobalId, authCredentials/*, fileLabel*/); + + JSONObject fileMetatdata = new JSONObject(); + fileMetatdata.put("id", list.get(0).getFileMetadata().getId()); + fileMetatdata.put("label", list.get(0).getFileMetadata().getLabel()); + fileMetatdata.put("version", list.get(0).getFileMetadata().getVersion()); + fileMetatdata.put("description", list.get(0).getFileMetadata().getDescription()); + + + jsonResponse.put("fileMetadata", fileMetatdata); + jsonResponse.put("status", "ok"); + + } catch (Exception e) { + logger.log(Level.SEVERE, "Error!!!!!!", e); + error(jsonResponse, e); + } + }else{ + jsonResponse.put("status", "error"); + jsonResponse.put("error", "The parameters studyId, user and password."); + jsonResponse.put("studyId", studyGlobalId); + jsonResponse.put("user", user); + jsonResponse.put("password", password); + //jsonResponse.put("fileLabel", fileLabel); + } + + }else if(file == null){ + jsonResponse.put("status", "error"); + jsonResponse.put("message", "No file found in the request."); + }else { + jsonResponse.put("status", "error"); + jsonResponse.put("message", "A file has been found as parameter, however it was empty. Check the temporal folder configured in Dataverse. " + TMP_DIR); } - }else{ - try { - jsonResponse.put("error", "No file found in the request."); - } catch (JSONException e) { - logger.log(Level.SEVERE, e.getMessage()); - error(jsonResponse, e); - } + + java.io.PrintWriter out = response.getWriter(); + out.print(jsonResponse); + out.flush(); + } catch (Exception e) { + e.printStackTrace(); } - java.io.PrintWriter out = response.getWriter(); - out.print(jsonResponse); - out.flush(); } private File getFile(HttpServletRequest request, HttpServletResponse response) throws IOException { - File file = null; boolean isMultipart = ServletFileUpload.isMultipartContent(request); @@ -143,7 +160,7 @@ ServletFileUpload upload = new ServletFileUpload(factory); // maximum file size to be uploaded. upload.setSizeMax(maxFileSize); - + StringBuilder sb = new StringBuilder(); try { // Parse the request to get file items. List fileItems = upload.parseRequest(request); @@ -154,9 +171,13 @@ while (i.hasNext()) { FileItem fi = (FileItem) i.next(); + sb.append(fi.getFieldName()); + if(fi.isFormField()){ + sb.append(" is Field"); params.put(fi.getFieldName(), fi.getString()); } else { + sb.append(" is FILE ***"); // Get the uploaded file parameters String fileName = fi.getName(); // Write the file @@ -166,20 +187,25 @@ file = new File(TMP_DIR + fileName.substring(fileName.lastIndexOf("\\") + 1)); } fi.write(file); + sb.append(" Size: " + file.length()); } + sb.append("\n"); } } catch (Exception ex) { ex.printStackTrace(); } + + 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); + logger.log(Level.INFO, "getFile Information:\n" + sb.toString()); + return file; } return null; } - private List<StudyFileEditBean> saveFile(File file, String studyGlobalId, AuthCredentials authCredentials) throws SwordAuthException, - SwordServerException, SwordError, IOException { + private List<StudyFileEditBean> saveFile(File file, String studyGlobalId, AuthCredentials authCredentials/*, String fileLabel*/) throws Exception { VDCUser vdcUser = swordAuth.auth(authCredentials); Study study = getStudy(studyGlobalId); @@ -187,19 +213,29 @@ try { List<StudyFileEditBean> files = getFileList(file, study); studyFileService.addFiles(study.getLatestVersion(), files, vdcUser); - System.out.println("AAAA"); return files; } catch (EJBException ex) { throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "Unable to add file(s) to study: " + ex.getMessage()); } } - public List<StudyFileEditBean> getFileList(File file, Study study) throws IOException { + public List<StudyFileEditBean> getFileList(File uploadedInputFile, Study study) throws Exception { List<StudyFileEditBean> fbList = new ArrayList<StudyFileEditBean>(); - if (file.length() != 0) { - StudyFileEditBean tempFileBean = new StudyFileEditBean(file, studyService.generateFileSystemNameSequence(), - study); + if (uploadedInputFile.length() != 0) { + + File dir = new File(uploadedInputFile.getParentFile(), study.getId().toString() ); + if ( !dir.exists() ) { + dir.mkdir(); + } + File file = FileUtil.createTempFile(dir, uploadedInputFile.getName()); + if (!uploadedInputFile.renameTo(file)) { + // in windows environment, rename doesn't work, so we will copy the file instead + FileUtil.copyFile(uploadedInputFile, file); + uploadedInputFile.delete(); + } + + StudyFileEditBean tempFileBean = new StudyFileEditBean(file, studyService.generateFileSystemNameSequence(), study); tempFileBean.setSizeFormatted(file.length()); fbList.add(tempFileBean); }