2 * Copyright (C) 2007-2012 Mathieu Baudier
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.
16 package org
.argeo
.jcr
.mvc
;
18 import java
.util
.List
;
19 import java
.util
.StringTokenizer
;
21 import javax
.jcr
.Session
;
22 import javax
.servlet
.http
.HttpServletRequest
;
23 import javax
.servlet
.http
.HttpServletResponse
;
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
;
42 public class JcrManagerController
implements MvcConstants
, JcrMvcConstants
{
43 private final static Log log
= LogFactory
44 .getLog(JcrManagerController
.class);
46 // Create a factory for disk-based file items
47 private FileItemFactory factory
= new DiskFileItemFactory();
49 // Create a new file upload handler
50 private ServletFileUpload upload
= new ServletFileUpload(factory
);
52 @SuppressWarnings("unchecked")
53 @RequestMapping("/upload/**")
54 @ModelAttribute(ANSWER_MODEL_KEY_AS_HTML
)
55 public ServerAnswer
upload(WebRequest webRequest
,
56 HttpServletRequest request
, HttpServletResponse response
)
59 Session session
= ((Session
) webRequest
.getAttribute(
60 REQUEST_ATTR_SESSION
, RequestAttributes
.SCOPE_REQUEST
));
61 JcrResourceAdapter resourceAdapter
= new JcrResourceAdapter(session
);
63 List
<FileItem
> items
= upload
.parseRequest(request
);
66 for (FileItem item
: items
) {
67 if (!item
.isFormField()) {
73 ByteArrayResource res
= new ByteArrayResource(arr
);
74 // String pathInfo = request.getPathInfo();
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());
85 path
.append(token
).append('/');
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");