Java tutorial
/* * Copyright Tek Counsel LLC 2013 * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at: * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or * implied. See the License for the specific language governing * permissions and limitations under the License. */ package com.tc.webshell.servlets; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.text.SimpleDateFormat; import java.util.List; import java.util.Map; import java.util.Vector; import java.util.logging.Level; import java.util.logging.Logger; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import lotus.domino.Database; import lotus.domino.Document; import lotus.domino.Item; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; import org.apache.commons.io.IOUtils; import com.ibm.domino.osgi.core.context.ContextInfo; import com.tc.utils.XSPUtils; import com.tc.webshell.core.MapperFactory; import com.tc.webshell.core.Prompt; /** * * @author mwambler */ public class Upload extends WebshellServlet { private static final long serialVersionUID = -5588175892400326314L; public static final Logger logger = Logger.getLogger(Upload.class.getName()); /** * Processes requests for both HTTP * <code>GET</code> and * <code>POST</code> methods. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @SuppressWarnings("unchecked") protected void processRequest(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType("application/json"); DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(diskFileItemFactory); upload.setFileSizeMax(60000000); // Parse the request try { Database db = ContextInfo.getUserDatabase(); for (FileItem item : (List<FileItem>) upload.parseRequest(req)) { if (!item.isFormField()) { InputStream in = item.getInputStream(); File file = this.createTempFile("upload", item.getName()); OutputStream fout = new FileOutputStream(file); try { byte[] bytes = IOUtils.toByteArray(in); IOUtils.write(bytes, fout); Map<String, String> queryMap = XSPUtils.getQueryMap(req.getQueryString()); Document doc = null; if (queryMap.containsKey("documentId")) { doc = new DocFactory().attachToDocument(db, queryMap.get("documentId"), file); } else { doc = new DocFactory().buildDocument(req, db, file); } doc.save(); Prompt prompt = new Prompt(); prompt.setMessage("file uploaded successfully"); prompt.setTitle("info"); prompt.addProperty("noteId", doc.getNoteID()); prompt.addProperty("unid", doc.getUniversalID()); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd G 'at' HH:mm:ss z"); String strdate = formatter.format(doc.getCreated().toJavaDate()); prompt.addProperty("created", strdate); Vector<Item> items = doc.getItems(); for (Item notesItem : items) { prompt.addProperty(notesItem.getName(), notesItem.getText()); } String json = MapperFactory.mapper().writeValueAsString(prompt); this.compressResponse(req, res, json); doc.recycle(); } finally { in.close(); if (fout != null) { fout.close(); file.delete();//make sure we cleanup } } } else { // } break; } } catch (Exception e) { logger.log(Level.SEVERE, null, e); } finally { res.getOutputStream().close(); } } public File createTempFile(String prefix, String suffix) throws IOException { File tmp = File.createTempFile(prefix, suffix); return tmp; } // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> /** * Handles the HTTP * <code>GET</code> method. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** * Handles the HTTP * <code>POST</code> method. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** * Returns a short description of the servlet. * * @return a String containing servlet description */ @Override public String getServletInfo() { return "Short description"; }// </editor-fold> }