Mercurial > hg > LGServices
view src/main/java/de/mpiwg/gazetteer/rest/SaveText.java @ 79:9fbdc98f81c6
Bug fix : comment out testing code for front end error handling.
author | Calvin Yeh <cyeh@mpiwg-berlin.mpg.de> |
---|---|
date | Wed, 03 May 2017 19:54:48 +0200 |
parents | f106f2487ac1 |
children |
line wrap: on
line source
package de.mpiwg.gazetteer.rest; import java.io.PrintWriter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.fileupload.servlet.ServletFileUpload; import org.apache.commons.lang.StringUtils; import org.json.JSONObject; import de.mpiwg.gazetteer.bo.LGBranch; import de.mpiwg.gazetteer.bo.LGFile; import de.mpiwg.gazetteer.utils.DataProvider; import de.mpiwg.gazetteer.utils.JSONUtils; import de.mpiwg.gazetteer.utils.exceptions.GazetteerException; import de.mpiwg.gazetteer.utils.exceptions.VersioningException; public class SaveText extends AbstractServletMethod{ public static String name = "save"; public static void execute(HttpServletRequest request, HttpServletResponse response) throws Exception{ response.setContentType("application/json"); JSONObject json = new JSONObject(); if (ServletFileUpload.isMultipartContent(request)) { String text = getRequestPart(request, "text"); ;//getRequestPart(request, "text"); Long userId = getRequestLongPart(request, "userId"); Long branchId = getRequestLongPart(request, "branchId"); Long userPreviousFileId = getRequestLongPart(request, "userPreviousFileId"); System.out.println("%%%%% SaveText [branchId=" + branchId + ", userId=" + userId + "]"); if(StringUtils.isNotEmpty(text) && userId != null && branchId != null && userPreviousFileId != null){ //text = new String(text.getBytes("iso-8859-1"), "UTF-8"); try { LGFile file = DataProvider.getInstance().saveFile(branchId, text, userId, userPreviousFileId); LGBranch branch = DataProvider.getInstance().getBranch(file.getBranchId()); JSONObject jsonBranch = JSONUtils.branch2JSON(branch); JSONObject jsonFile = JSONUtils.file2JSON(file, true); json.put("branch", jsonBranch); json.put("file", jsonFile); json.put("status", "ok"); // TODO DEBUG just added for testing error case! //json.put("status", "error"); } catch (GazetteerException e){ json.put("status", "error"); json.put("message", e.getMessage()); json.put("code", e.getCode()); if(e instanceof VersioningException){ JSONObject curentFile = JSONUtils.file2JSON(((VersioningException) e).getCurrentFile(), false); JSONObject userFile = JSONUtils.file2JSON(((VersioningException) e).getUserFile(), false); json.put("currentFile", curentFile); json.put("userFile", userFile); } } catch (Exception e) { e.printStackTrace(); json.put("status", "error"); json.put("message", e.getMessage()); json.put("code", GazetteerException.CODE); } }else{ json.put("status", "error"); json.put("message", "Following parameters are mandatory: text, userId, branchId, userPreviousFileId."); } } else { json.put("status", "error"); json.put("message", "Content-type wrong. It should be: multipart/form-data"); } PrintWriter out = response.getWriter(); out.print(json.toString()); out.flush(); } }