2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org
.argeo
.jcr
.mvc
;
19 import java
.util
.List
;
20 import java
.util
.StringTokenizer
;
22 import javax
.jcr
.Session
;
23 import javax
.servlet
.http
.HttpServletRequest
;
24 import javax
.servlet
.http
.HttpServletResponse
;
26 import org
.apache
.commons
.fileupload
.FileItem
;
27 import org
.apache
.commons
.fileupload
.FileItemFactory
;
28 import org
.apache
.commons
.fileupload
.disk
.DiskFileItemFactory
;
29 import org
.apache
.commons
.fileupload
.servlet
.ServletFileUpload
;
30 import org
.apache
.commons
.logging
.Log
;
31 import org
.apache
.commons
.logging
.LogFactory
;
32 import org
.argeo
.jcr
.JcrResourceAdapter
;
33 import org
.argeo
.server
.ServerAnswer
;
34 import org
.argeo
.server
.mvc
.MvcConstants
;
35 import org
.springframework
.core
.io
.ByteArrayResource
;
36 import org
.springframework
.stereotype
.Controller
;
37 import org
.springframework
.web
.bind
.annotation
.ModelAttribute
;
38 import org
.springframework
.web
.bind
.annotation
.RequestMapping
;
39 import org
.springframework
.web
.context
.request
.RequestAttributes
;
40 import org
.springframework
.web
.context
.request
.WebRequest
;
43 public class JcrManagerController
implements MvcConstants
, JcrMvcConstants
{
44 private final static Log log
= LogFactory
45 .getLog(JcrManagerController
.class);
47 // Create a factory for disk-based file items
48 private FileItemFactory factory
= new DiskFileItemFactory();
50 // Create a new file upload handler
51 private ServletFileUpload upload
= new ServletFileUpload(factory
);
53 @SuppressWarnings("unchecked")
54 @RequestMapping("/upload/**")
55 @ModelAttribute(ANSWER_MODEL_KEY_AS_HTML
)
56 public ServerAnswer
upload(WebRequest webRequest
,
57 HttpServletRequest request
, HttpServletResponse response
)
60 Session session
= ((Session
) webRequest
.getAttribute(
61 REQUEST_ATTR_SESSION
, RequestAttributes
.SCOPE_REQUEST
));
62 JcrResourceAdapter resourceAdapter
= new JcrResourceAdapter(session
);
64 List
<FileItem
> items
= upload
.parseRequest(request
);
67 for (FileItem item
: items
) {
68 if (!item
.isFormField()) {
74 ByteArrayResource res
= new ByteArrayResource(arr
);
75 // String pathInfo = request.getPathInfo();
77 StringBuffer path
= new StringBuffer("/");
78 StringTokenizer st
= new StringTokenizer(request
.getPathInfo(), "/");
79 st
.nextToken();// skip /upload/
80 while (st
.hasMoreTokens()) {
81 String token
= st
.nextToken();
82 if (!st
.hasMoreTokens()) {
83 resourceAdapter
.mkdirs(path
.toString());
86 path
.append(token
).append('/');
89 // String path = '/' + pathInfo.substring(1).substring(
90 // pathInfo.indexOf('/'));
91 if (log
.isDebugEnabled())
92 log
.debug("Upload to " + path
);
93 resourceAdapter
.update(path
.toString(), res
.getInputStream());
94 return ServerAnswer
.ok("File " + path
+ " imported");