| package org.openntf.smartnsf; |
| |
| import java.io.File; |
| import java.io.IOException; |
| import java.util.List; |
| |
| import javax.servlet.http.HttpServletRequest; |
| import javax.servlet.http.HttpServletResponse; |
| |
| import lotus.domino.Database; |
| import lotus.domino.Document; |
| import lotus.domino.MIMEEntity; |
| import lotus.domino.MIMEHeader; |
| import lotus.domino.NotesException; |
| import lotus.domino.Stream; |
| |
| import org.openntf.xrest.xsp.exec.Context; |
| import org.openntf.xrest.xsp.exec.CustomRestHandler; |
| |
| import com.ibm.commons.util.io.json.JsonJavaObject; |
| import com.ibm.commons.util.io.json.util.JsonWriter; |
| import com.ibm.domino.services.HttpServiceConstants; |
| import com.ibm.xsp.extlib.util.ExtLibUtil; |
| import com.ibm.xsp.http.fileupload.FileItem; |
| import com.ibm.xsp.http.fileupload.disk.DiskFileItemFactory; |
| import com.ibm.xsp.http.fileupload.servlet.ServletFileUpload; |
| |
| public class Upload implements CustomRestHandler { |
| |
| public void processCall(Context context, String path) throws Exception { |
| JsonJavaObject result = new JsonJavaObject(); |
| |
| HttpServletResponse res = context.getResponse(); |
| HttpServletRequest req = context.getRequest(); |
| |
| try { |
| |
| |
| if (!req.getMethod().equals("POST")) { |
| |
| res.setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED); |
| return; |
| |
| } |
| |
| |
| boolean isMultipart = ServletFileUpload.isMultipartContent(req); |
| |
| if (!isMultipart) { |
| throw (new Exception("that's not multipart content: we need that to continue")); |
| } |
| |
| |
| DiskFileItemFactory factory = new DiskFileItemFactory(); |
| |
| |
| |
| |
| File repository = (File) req.getAttribute("javax.servlet.context.tempdir"); |
| factory.setRepository(repository); |
| |
| |
| ServletFileUpload upload = new ServletFileUpload(factory); |
| |
| |
| List<FileItem> items = upload.parseRequest(req); |
| |
| |
| Database dbCurrent = ExtLibUtil.getCurrentDatabase(); |
| Document docTarget = dbCurrent.createDocument(); |
| |
| |
| MIMEEntity mime = docTarget.createMIMEEntity("Files"); |
| |
| |
| |
| for (FileItem item : items) { |
| |
| if (item.isFormField()) { |
| |
| System.out.println("form field: " + item.getFieldName() + " : " + item.getString()); |
| |
| docTarget.replaceItemValue(item.getFieldName(), item.getString()); |
| |
| } else { |
| |
| processUploadedFile(item, mime); |
| } |
| } |
| |
| docTarget.save(); |
| |
| res.setStatus(HttpServletResponse.SC_OK); |
| |
| result.put("status", "success"); |
| |
| |
| |
| } catch (Exception e) { |
| |
| e.printStackTrace(); |
| |
| res.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); |
| |
| |
| result.put("status", "error"); |
| |
| } |
| |
| System.out.println("Test4"); |
| res.setContentType(HttpServiceConstants.CONTENTTYPE_APPLICATION_JSON_UTF8); |
| JsonWriter jsw = new JsonWriter(res.getWriter(),true); |
| jsw.outObject(result); |
| jsw.close(); |
| } |
| |
| |
| |
| |
| private void processUploadedFile(FileItem item, MIMEEntity mime) throws IOException, NotesException { |
| |
| System.out.println("- processing uploaded file: " + item.getName()); |
| |
| MIMEEntity child = mime.createChildEntity(); |
| |
| MIMEHeader header = child.createHeader("content-disposition"); |
| header.setHeaderVal("attachment;filename=\"" + item.getName() + "\""); |
| |
| Stream stream = ExtLibUtil.getCurrentSession().createStream(); |
| stream.write(item.get()); |
| |
| child.setContentFromBytes(stream, item.getContentType(), MIMEEntity.ENC_IDENTITY_BINARY); |
| child.decodeContent(); |
| |
| } |
| } |