View Javadoc

1   /*
2    * Copyright 2011 The Kuali Foundation.
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.web;
17  
18  import org.apache.commons.fileupload.FileItem;
19  import org.apache.commons.fileupload.FileUploadException;
20  import org.apache.commons.fileupload.disk.DiskFileItemFactory;
21  import org.apache.commons.fileupload.servlet.ServletFileUpload;
22  import org.apache.commons.io.FilenameUtils;
23  
24  import javax.servlet.ServletException;
25  import javax.servlet.http.HttpServlet;
26  import javax.servlet.http.HttpServletRequest;
27  import javax.servlet.http.HttpServletResponse;
28  import java.io.IOException;
29  import java.util.*;
30  
31  /**
32  * User: Peri Subrahmanya
33  * Date: 4/7/11
34  * Time: 11:30 AM
35  * To change this template use File | Settings | File Templates.
36  */
37  public class CheckinServlet extends HttpServlet {
38      private static final String RESULTS_JSP =
39              "/checkinResults.jsp";
40      private Map<String, String> fieldsMap;
41      private HashMap<String, FileItem> fileItemMap;
42  
43      @Override
44      protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
45          List<FileItem> items = null;
46          try {
47              items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(req);
48          } catch (FileUploadException e) {
49              throw new ServletException("Cannot parse multipart request.", e);
50          }
51          for (FileItem item : items) {
52              if (item.isFormField()) {
53                  getFieldsMap().put(item.getFieldName(), item.getString());
54              }
55          }
56  
57          for (FileItem item : items) {
58              String filename = FilenameUtils.getName(item.getName());
59              fileItemMap = new HashMap<String, FileItem>();
60              fileItemMap.put(filename, item);
61              getFieldsMap().put("fileName", filename);
62          }
63          callService(req, resp);
64      }
65  
66      private void callService(HttpServletRequest req, HttpServletResponse resp) throws IOException {
67  //        try {
68  //            getDocumentStoreContentManager().checkin(null, getUUID(), "webappUser", "CheckinServlet.callService()");
69  //            RequestDispatcher rd = getServletContext().getRequestDispatcher(RESULTS_JSP);
70  //            req.setAttribute("result", "Successfully checked in");
71  //            rd.forward(req, resp);
72  //        } catch (Exception e) {
73  //            e.printStackTrace();
74  //            PrintWriter out = resp.getWriter();
75  //            out.println("Problem checking the file! Please refer application logs for details");
76  //            out.flush();
77  //            out.close();
78  //        }
79      }
80  
81      public Map<String, String> getFieldsMap() {
82          if (null == fieldsMap) {
83              fieldsMap = new HashMap<String, String>();
84          }
85          return fieldsMap;
86      }
87  
88      private String getUUID() {
89          Set<String> strings = getFieldsMap().keySet();
90          for (Iterator<String> iterator = strings.iterator(); iterator.hasNext();) {
91              String key = iterator.next();
92              if (key.equals("uuid")) {
93                  return getFieldsMap().get(key);
94              }
95          }
96          return null;
97      }
98  
99      public HashMap<String, FileItem> getFileItemMap() {
100         return fileItemMap;
101     }
102 
103     public FileItem getItem() {
104         String fileName = getFieldsMap().get("fileName");
105         return getFileItemMap().get(fileName);
106     }
107 }