]> git.argeo.org Git - lgpl/argeo-commons.git/blob - jcr/mvc/JcrManagerController.java
Prepare next development cycle
[lgpl/argeo-commons.git] / jcr / mvc / JcrManagerController.java
1 /*
2 * Copyright (C) 2007-2012 Mathieu Baudier
3 *
4 * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
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.argeo.jcr.mvc;
17
18 import java.util.List;
19 import java.util.StringTokenizer;
20
21 import javax.jcr.Session;
22 import javax.servlet.http.HttpServletRequest;
23 import javax.servlet.http.HttpServletResponse;
24
25 import org.apache.commons.fileupload.FileItem;
26 import org.apache.commons.fileupload.FileItemFactory;
27 import org.apache.commons.fileupload.disk.DiskFileItemFactory;
28 import org.apache.commons.fileupload.servlet.ServletFileUpload;
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.argeo.jcr.JcrResourceAdapter;
32 import org.argeo.server.ServerAnswer;
33 import org.argeo.server.mvc.MvcConstants;
34 import org.springframework.core.io.ByteArrayResource;
35 import org.springframework.stereotype.Controller;
36 import org.springframework.web.bind.annotation.ModelAttribute;
37 import org.springframework.web.bind.annotation.RequestMapping;
38 import org.springframework.web.context.request.RequestAttributes;
39 import org.springframework.web.context.request.WebRequest;
40
41 @Controller
42 public class JcrManagerController implements MvcConstants, JcrMvcConstants {
43 private final static Log log = LogFactory
44 .getLog(JcrManagerController.class);
45
46 // Create a factory for disk-based file items
47 private FileItemFactory factory = new DiskFileItemFactory();
48
49 // Create a new file upload handler
50 private ServletFileUpload upload = new ServletFileUpload(factory);
51
52 @SuppressWarnings("unchecked")
53 @RequestMapping("/upload/**")
54 @ModelAttribute(ANSWER_MODEL_KEY_AS_HTML)
55 public ServerAnswer upload(WebRequest webRequest,
56 HttpServletRequest request, HttpServletResponse response)
57 throws Exception {
58
59 Session session = ((Session) webRequest.getAttribute(
60 REQUEST_ATTR_SESSION, RequestAttributes.SCOPE_REQUEST));
61 JcrResourceAdapter resourceAdapter = new JcrResourceAdapter(session);
62 // Parse the request
63 List<FileItem> items = upload.parseRequest(request);
64
65 byte[] arr = null;
66 for (FileItem item : items) {
67 if (!item.isFormField()) {
68 arr = item.get();
69 break;
70 }
71 }
72
73 ByteArrayResource res = new ByteArrayResource(arr);
74 // String pathInfo = request.getPathInfo();
75
76 StringBuffer path = new StringBuffer("/");
77 StringTokenizer st = new StringTokenizer(request.getPathInfo(), "/");
78 st.nextToken();// skip /upload/
79 while (st.hasMoreTokens()) {
80 String token = st.nextToken();
81 if (!st.hasMoreTokens()) {
82 resourceAdapter.mkdirs(path.toString());
83 path.append(token);
84 } else {
85 path.append(token).append('/');
86 }
87 }
88 // String path = '/' + pathInfo.substring(1).substring(
89 // pathInfo.indexOf('/'));
90 if (log.isDebugEnabled())
91 log.debug("Upload to " + path);
92 resourceAdapter.update(path.toString(), res.getInputStream());
93 return ServerAnswer.ok("File " + path + " imported");
94 }
95
96 }