Move package
authorMathieu Baudier <mbaudier@argeo.org>
Fri, 25 Feb 2011 10:00:46 +0000 (10:00 +0000)
committerMathieu Baudier <mbaudier@argeo.org>
Fri, 25 Feb 2011 10:00:46 +0000 (10:00 +0000)
git-svn-id: https://svn.argeo.org/commons/trunk@4194 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

server/runtime/org.argeo.server.jcr.mvc/META-INF/MANIFEST.MF [deleted file]
server/runtime/org.argeo.server.jcr.mvc/src/main/java/org/argeo/server/jcr/mvc/JcrBrowserController.java [new file with mode: 0644]
server/runtime/org.argeo.server.jcr.mvc/src/main/java/org/argeo/server/jcr/mvc/JcrManagerController.java [new file with mode: 0644]
server/runtime/org.argeo.server.jcr.mvc/src/main/java/org/argeo/server/jcr/mvc/JcrMvcConstants.java [new file with mode: 0644]
server/runtime/org.argeo.server.jcr.mvc/src/main/java/org/argeo/server/jcr/mvc/JcrXmlServerSerializer.java [new file with mode: 0644]
server/runtime/org.argeo.server.jcr.mvc/src/main/java/org/argeo/server/jcr/mvc/OpenSessionInViewJcrInterceptor.java [new file with mode: 0644]
server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/server/jcr/mvc/JcrBrowserController.java [deleted file]
server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/server/jcr/mvc/JcrManagerController.java [deleted file]
server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/server/jcr/mvc/JcrMvcConstants.java [deleted file]
server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/server/jcr/mvc/JcrXmlServerSerializer.java [deleted file]
server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/server/jcr/mvc/OpenSessionInViewJcrInterceptor.java [deleted file]

diff --git a/server/runtime/org.argeo.server.jcr.mvc/META-INF/MANIFEST.MF b/server/runtime/org.argeo.server.jcr.mvc/META-INF/MANIFEST.MF
deleted file mode 100644 (file)
index b3f3477..0000000
+++ /dev/null
@@ -1,7 +0,0 @@
-Manifest-Version: 1.0
-Bundle-ManifestVersion: 2
-Bundle-Name: Mvc
-Bundle-SymbolicName: org.argeo.server.jcr.mvc
-Bundle-Version: 1.0.0.qualifier
-Bundle-Vendor: Argeo
-Bundle-RequiredExecutionEnvironment: J2SE-1.5
diff --git a/server/runtime/org.argeo.server.jcr.mvc/src/main/java/org/argeo/server/jcr/mvc/JcrBrowserController.java b/server/runtime/org.argeo.server.jcr.mvc/src/main/java/org/argeo/server/jcr/mvc/JcrBrowserController.java
new file mode 100644 (file)
index 0000000..e255e7e
--- /dev/null
@@ -0,0 +1,89 @@
+/*
+ * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
+ *
+ * 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 org.argeo.server.jcr.mvc;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import javax.jcr.Item;
+import javax.jcr.NodeIterator;
+import javax.jcr.RepositoryException;
+import javax.jcr.Session;
+import javax.jcr.Value;
+import javax.jcr.query.Query;
+import javax.jcr.query.QueryResult;
+import javax.jcr.query.Row;
+import javax.jcr.query.RowIterator;
+
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.context.request.RequestAttributes;
+import org.springframework.web.context.request.WebRequest;
+
+@Controller
+public class JcrBrowserController implements JcrMvcConstants {
+
+       @RequestMapping("/getJcrItem.*")
+       public Item getJcrItem(WebRequest webRequest,
+                       @RequestParam("path") String path) throws RepositoryException {
+               return ((Session) webRequest.getAttribute(REQUEST_ATTR_SESSION,
+                               RequestAttributes.SCOPE_REQUEST)).getItem(path);
+       }
+
+       @RequestMapping("/queryJcrNodes.*")
+       public List<String> queryJcrNodes(WebRequest webRequest,
+                       @RequestParam("statement") String statement,
+                       @RequestParam("language") String language)
+                       throws RepositoryException {
+               Session session = ((Session) webRequest.getAttribute(
+                               REQUEST_ATTR_SESSION, RequestAttributes.SCOPE_REQUEST));
+               Query query = session.getWorkspace().getQueryManager().createQuery(
+                               statement, language);
+               NodeIterator nit = query.execute().getNodes();
+               List<String> paths = new ArrayList<String>();
+               while (nit.hasNext()) {
+                       paths.add(nit.nextNode().getPath());
+               }
+               return paths;
+       }
+
+       @RequestMapping("/queryJcrTable.*")
+       public List<List<String>> queryJcrTable(WebRequest webRequest,
+                       @RequestParam("statement") String statement,
+                       @RequestParam("language") String language)
+                       throws RepositoryException {
+               Session session = ((Session) webRequest.getAttribute(
+                               REQUEST_ATTR_SESSION, RequestAttributes.SCOPE_REQUEST));
+               Query query = session.getWorkspace().getQueryManager().createQuery(
+                               statement, language);
+               QueryResult queryResult = query.execute();
+               List<List<String>> results = new ArrayList<List<String>>();
+               results.add(Arrays.asList(queryResult.getColumnNames()));
+               RowIterator rit = queryResult.getRows();
+
+               while (rit.hasNext()) {
+                       Row row = rit.nextRow();
+                       List<String> lst = new ArrayList<String>();
+                       for (Value value : row.getValues()) {
+                               lst.add(value.getString());
+                       }
+               }
+               return results;
+       }
+}
diff --git a/server/runtime/org.argeo.server.jcr.mvc/src/main/java/org/argeo/server/jcr/mvc/JcrManagerController.java b/server/runtime/org.argeo.server.jcr.mvc/src/main/java/org/argeo/server/jcr/mvc/JcrManagerController.java
new file mode 100644 (file)
index 0000000..5b90f03
--- /dev/null
@@ -0,0 +1,97 @@
+/*
+ * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
+ *
+ * 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 org.argeo.server.jcr.mvc;
+
+import java.util.List;
+import java.util.StringTokenizer;
+
+import javax.jcr.Session;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.commons.fileupload.FileItem;
+import org.apache.commons.fileupload.FileItemFactory;
+import org.apache.commons.fileupload.disk.DiskFileItemFactory;
+import org.apache.commons.fileupload.servlet.ServletFileUpload;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.argeo.jcr.JcrResourceAdapter;
+import org.argeo.server.ServerAnswer;
+import org.argeo.server.mvc.MvcConstants;
+import org.springframework.core.io.ByteArrayResource;
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.ModelAttribute;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.context.request.RequestAttributes;
+import org.springframework.web.context.request.WebRequest;
+
+@Controller
+public class JcrManagerController implements MvcConstants, JcrMvcConstants {
+       private final static Log log = LogFactory
+                       .getLog(JcrManagerController.class);
+
+       // Create a factory for disk-based file items
+       private FileItemFactory factory = new DiskFileItemFactory();
+
+       // Create a new file upload handler
+       private ServletFileUpload upload = new ServletFileUpload(factory);
+
+       @SuppressWarnings("unchecked")
+       @RequestMapping("/upload/**")
+       @ModelAttribute(ANSWER_MODEL_KEY_AS_HTML)
+       public ServerAnswer upload(WebRequest webRequest,
+                       HttpServletRequest request, HttpServletResponse response)
+                       throws Exception {
+
+               Session session = ((Session) webRequest.getAttribute(
+                               REQUEST_ATTR_SESSION, RequestAttributes.SCOPE_REQUEST));
+               JcrResourceAdapter resourceAdapter = new JcrResourceAdapter(session);
+               // Parse the request
+               List<FileItem> items = upload.parseRequest(request);
+
+               byte[] arr = null;
+               for (FileItem item : items) {
+                       if (!item.isFormField()) {
+                               arr = item.get();
+                               break;
+                       }
+               }
+
+               ByteArrayResource res = new ByteArrayResource(arr);
+               // String pathInfo = request.getPathInfo();
+
+               StringBuffer path = new StringBuffer("/");
+               StringTokenizer st = new StringTokenizer(request.getPathInfo(), "/");
+               st.nextToken();// skip /upload/
+               while (st.hasMoreTokens()) {
+                       String token = st.nextToken();
+                       if (!st.hasMoreTokens()) {
+                               resourceAdapter.mkdirs(path.toString());
+                               path.append(token);
+                       } else {
+                               path.append(token).append('/');
+                       }
+               }
+               // String path = '/' + pathInfo.substring(1).substring(
+               // pathInfo.indexOf('/'));
+               if (log.isDebugEnabled())
+                       log.debug("Upload to " + path);
+               resourceAdapter.update(path.toString(), res);
+               return ServerAnswer.ok("File " + path + " imported");
+       }
+
+}
diff --git a/server/runtime/org.argeo.server.jcr.mvc/src/main/java/org/argeo/server/jcr/mvc/JcrMvcConstants.java b/server/runtime/org.argeo.server.jcr.mvc/src/main/java/org/argeo/server/jcr/mvc/JcrMvcConstants.java
new file mode 100644 (file)
index 0000000..158098e
--- /dev/null
@@ -0,0 +1,21 @@
+/*
+ * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
+ *
+ * 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 org.argeo.server.jcr.mvc;
+
+public interface JcrMvcConstants {
+       public final static String REQUEST_ATTR_SESSION = "jcrSession";
+}
diff --git a/server/runtime/org.argeo.server.jcr.mvc/src/main/java/org/argeo/server/jcr/mvc/JcrXmlServerSerializer.java b/server/runtime/org.argeo.server.jcr.mvc/src/main/java/org/argeo/server/jcr/mvc/JcrXmlServerSerializer.java
new file mode 100644 (file)
index 0000000..679a6ef
--- /dev/null
@@ -0,0 +1,129 @@
+/*
+ * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
+ *
+ * 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 org.argeo.server.jcr.mvc;
+
+import javax.jcr.Node;
+import javax.jcr.NodeIterator;
+import javax.jcr.RepositoryException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
+
+import org.argeo.ArgeoException;
+import org.argeo.server.ServerSerializer;
+import org.springframework.xml.dom.DomContentHandler;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.NodeList;
+import org.xml.sax.SAXException;
+
+public class JcrXmlServerSerializer implements ServerSerializer {
+       private String contentTypeCharset = "UTF-8";
+
+       private final DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
+                       .newInstance();
+       private final TransformerFactory transformerFactory = TransformerFactory
+                       .newInstance();
+
+       public void serialize(Object obj, HttpServletRequest request,
+                       HttpServletResponse response) {
+               if (!(obj instanceof Node))
+                       throw new ArgeoException("Only " + Node.class + " is supported");
+
+               String noRecurseStr = request.getParameter("noRecurse");
+               boolean noRecurse = noRecurseStr != null && noRecurseStr.equals("true");
+
+               String depthStr = request.getParameter("depth");
+               String downloadStr = request.getParameter("download");
+
+               Node node = (Node) obj;
+
+               try {
+                       String contentType = "text/xml;charset=" + contentTypeCharset;
+                       // download case
+                       if (downloadStr != null && downloadStr.equals("true")) {
+                               String fileName = node.getName().replace(':', '_') + ".xml";
+                               contentType = contentType + ";name=\"" + fileName + "\"";
+                               response.setHeader("Content-Disposition",
+                                               "attachment; filename=\"" + fileName + "\"");
+                               response.setHeader("Expires", "0");
+                               response
+                                               .setHeader("Cache-Control", "no-cache, must-revalidate");
+                               response.setHeader("Pragma", "no-cache");
+                       }
+
+                       response.setContentType(contentType);
+                       if (depthStr == null) {
+                               node.getSession().exportDocumentView(node.getPath(),
+                                               response.getOutputStream(), true, noRecurse);
+                       } else {
+                               int depth = Integer.parseInt(depthStr);
+                               Document document = documentBuilderFactory.newDocumentBuilder()
+                                               .newDocument();
+                               serializeLevelToDom(node, document, 0, depth);
+                               Transformer transformer = transformerFactory.newTransformer();
+                               transformer.transform(new DOMSource(document),
+                                               new StreamResult(response.getOutputStream()));
+                       }
+               } catch (Exception e) {
+                       throw new ArgeoException("Cannot serialize " + node, e);
+               }
+       }
+
+       protected void serializeLevelToDom(Node currentJcrNode,
+                       org.w3c.dom.Node currentDomNode, int currentDepth, int targetDepth)
+                       throws RepositoryException, SAXException {
+               DomContentHandler domContentHandler = new DomContentHandler(
+                               currentDomNode);
+               currentJcrNode.getSession().exportDocumentView(
+                               currentJcrNode.getPath(), domContentHandler, true, true);
+
+               if (currentDepth == targetDepth)
+                       return;
+
+               // TODO: filter
+               NodeIterator nit = currentJcrNode.getNodes();
+               while (nit.hasNext()) {
+                       Node nextJcrNode = nit.nextNode();
+                       org.w3c.dom.Node nextDomNode;
+                       if (currentDomNode instanceof Document)
+                               nextDomNode = ((Document) currentDomNode).getDocumentElement();
+                       else {
+                               String name = currentJcrNode.getName();
+                               NodeList nodeList = ((Element) currentDomNode)
+                                               .getElementsByTagName(name);
+                               if (nodeList.getLength() < 1)
+                                       throw new ArgeoException("No elment named " + name
+                                                       + " under " + currentDomNode);
+                               // we know it is the last one added
+                               nextDomNode = nodeList.item(nodeList.getLength() - 1);
+                       }
+                       // recursive call
+                       serializeLevelToDom(nextJcrNode, nextDomNode, currentDepth + 1,
+                                       targetDepth);
+               }
+       }
+
+       public void setContentTypeCharset(String contentTypeCharset) {
+               this.contentTypeCharset = contentTypeCharset;
+       }
+
+}
diff --git a/server/runtime/org.argeo.server.jcr.mvc/src/main/java/org/argeo/server/jcr/mvc/OpenSessionInViewJcrInterceptor.java b/server/runtime/org.argeo.server.jcr.mvc/src/main/java/org/argeo/server/jcr/mvc/OpenSessionInViewJcrInterceptor.java
new file mode 100644 (file)
index 0000000..1eb4e3c
--- /dev/null
@@ -0,0 +1,70 @@
+/*
+ * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
+ *
+ * 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 org.argeo.server.jcr.mvc;
+
+import javax.jcr.Session;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.springframework.ui.ModelMap;
+import org.springframework.web.context.request.RequestAttributes;
+import org.springframework.web.context.request.WebRequest;
+import org.springframework.web.context.request.WebRequestInterceptor;
+
+public class OpenSessionInViewJcrInterceptor implements WebRequestInterceptor,
+               JcrMvcConstants {
+       private final static Log log = LogFactory
+                       .getLog(OpenSessionInViewJcrInterceptor.class);
+
+       private Session session;
+
+       public void preHandle(WebRequest request) throws Exception {
+               if (log.isTraceEnabled())
+                       log.trace("preHandle: " + request);
+               // Authentication auth = SecurityContextHolder.getContext()
+               // .getAuthentication();
+               // if (auth != null)
+               // log.debug("auth=" + auth + ", authenticated="
+               // + auth.isAuthenticated() + ", name=" + auth.getName());
+               // else
+               // log.debug("No auth");
+
+               // FIXME: find a safer way to initialize
+               // FIXME: not really needed to initialize here
+               // session.getRepository();
+               request.setAttribute(REQUEST_ATTR_SESSION, session,
+                               RequestAttributes.SCOPE_REQUEST);
+       }
+
+       public void postHandle(WebRequest request, ModelMap model) throws Exception {
+               // if (log.isDebugEnabled())
+               // log.debug("postHandle: " + request);
+       }
+
+       public void afterCompletion(WebRequest request, Exception ex)
+                       throws Exception {
+               if (log.isTraceEnabled())
+                       log.trace("afterCompletion: " + request);
+               // FIXME: only close session that were open
+               session.logout();
+       }
+
+       public void setSession(Session session) {
+               this.session = session;
+       }
+
+}
diff --git a/server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/server/jcr/mvc/JcrBrowserController.java b/server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/server/jcr/mvc/JcrBrowserController.java
deleted file mode 100644 (file)
index e255e7e..0000000
+++ /dev/null
@@ -1,89 +0,0 @@
-/*
- * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
- *
- * 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 org.argeo.server.jcr.mvc;
-
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.List;
-
-import javax.jcr.Item;
-import javax.jcr.NodeIterator;
-import javax.jcr.RepositoryException;
-import javax.jcr.Session;
-import javax.jcr.Value;
-import javax.jcr.query.Query;
-import javax.jcr.query.QueryResult;
-import javax.jcr.query.Row;
-import javax.jcr.query.RowIterator;
-
-import org.springframework.stereotype.Controller;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RequestParam;
-import org.springframework.web.context.request.RequestAttributes;
-import org.springframework.web.context.request.WebRequest;
-
-@Controller
-public class JcrBrowserController implements JcrMvcConstants {
-
-       @RequestMapping("/getJcrItem.*")
-       public Item getJcrItem(WebRequest webRequest,
-                       @RequestParam("path") String path) throws RepositoryException {
-               return ((Session) webRequest.getAttribute(REQUEST_ATTR_SESSION,
-                               RequestAttributes.SCOPE_REQUEST)).getItem(path);
-       }
-
-       @RequestMapping("/queryJcrNodes.*")
-       public List<String> queryJcrNodes(WebRequest webRequest,
-                       @RequestParam("statement") String statement,
-                       @RequestParam("language") String language)
-                       throws RepositoryException {
-               Session session = ((Session) webRequest.getAttribute(
-                               REQUEST_ATTR_SESSION, RequestAttributes.SCOPE_REQUEST));
-               Query query = session.getWorkspace().getQueryManager().createQuery(
-                               statement, language);
-               NodeIterator nit = query.execute().getNodes();
-               List<String> paths = new ArrayList<String>();
-               while (nit.hasNext()) {
-                       paths.add(nit.nextNode().getPath());
-               }
-               return paths;
-       }
-
-       @RequestMapping("/queryJcrTable.*")
-       public List<List<String>> queryJcrTable(WebRequest webRequest,
-                       @RequestParam("statement") String statement,
-                       @RequestParam("language") String language)
-                       throws RepositoryException {
-               Session session = ((Session) webRequest.getAttribute(
-                               REQUEST_ATTR_SESSION, RequestAttributes.SCOPE_REQUEST));
-               Query query = session.getWorkspace().getQueryManager().createQuery(
-                               statement, language);
-               QueryResult queryResult = query.execute();
-               List<List<String>> results = new ArrayList<List<String>>();
-               results.add(Arrays.asList(queryResult.getColumnNames()));
-               RowIterator rit = queryResult.getRows();
-
-               while (rit.hasNext()) {
-                       Row row = rit.nextRow();
-                       List<String> lst = new ArrayList<String>();
-                       for (Value value : row.getValues()) {
-                               lst.add(value.getString());
-                       }
-               }
-               return results;
-       }
-}
diff --git a/server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/server/jcr/mvc/JcrManagerController.java b/server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/server/jcr/mvc/JcrManagerController.java
deleted file mode 100644 (file)
index 5b90f03..0000000
+++ /dev/null
@@ -1,97 +0,0 @@
-/*
- * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
- *
- * 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 org.argeo.server.jcr.mvc;
-
-import java.util.List;
-import java.util.StringTokenizer;
-
-import javax.jcr.Session;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-
-import org.apache.commons.fileupload.FileItem;
-import org.apache.commons.fileupload.FileItemFactory;
-import org.apache.commons.fileupload.disk.DiskFileItemFactory;
-import org.apache.commons.fileupload.servlet.ServletFileUpload;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import org.argeo.jcr.JcrResourceAdapter;
-import org.argeo.server.ServerAnswer;
-import org.argeo.server.mvc.MvcConstants;
-import org.springframework.core.io.ByteArrayResource;
-import org.springframework.stereotype.Controller;
-import org.springframework.web.bind.annotation.ModelAttribute;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.context.request.RequestAttributes;
-import org.springframework.web.context.request.WebRequest;
-
-@Controller
-public class JcrManagerController implements MvcConstants, JcrMvcConstants {
-       private final static Log log = LogFactory
-                       .getLog(JcrManagerController.class);
-
-       // Create a factory for disk-based file items
-       private FileItemFactory factory = new DiskFileItemFactory();
-
-       // Create a new file upload handler
-       private ServletFileUpload upload = new ServletFileUpload(factory);
-
-       @SuppressWarnings("unchecked")
-       @RequestMapping("/upload/**")
-       @ModelAttribute(ANSWER_MODEL_KEY_AS_HTML)
-       public ServerAnswer upload(WebRequest webRequest,
-                       HttpServletRequest request, HttpServletResponse response)
-                       throws Exception {
-
-               Session session = ((Session) webRequest.getAttribute(
-                               REQUEST_ATTR_SESSION, RequestAttributes.SCOPE_REQUEST));
-               JcrResourceAdapter resourceAdapter = new JcrResourceAdapter(session);
-               // Parse the request
-               List<FileItem> items = upload.parseRequest(request);
-
-               byte[] arr = null;
-               for (FileItem item : items) {
-                       if (!item.isFormField()) {
-                               arr = item.get();
-                               break;
-                       }
-               }
-
-               ByteArrayResource res = new ByteArrayResource(arr);
-               // String pathInfo = request.getPathInfo();
-
-               StringBuffer path = new StringBuffer("/");
-               StringTokenizer st = new StringTokenizer(request.getPathInfo(), "/");
-               st.nextToken();// skip /upload/
-               while (st.hasMoreTokens()) {
-                       String token = st.nextToken();
-                       if (!st.hasMoreTokens()) {
-                               resourceAdapter.mkdirs(path.toString());
-                               path.append(token);
-                       } else {
-                               path.append(token).append('/');
-                       }
-               }
-               // String path = '/' + pathInfo.substring(1).substring(
-               // pathInfo.indexOf('/'));
-               if (log.isDebugEnabled())
-                       log.debug("Upload to " + path);
-               resourceAdapter.update(path.toString(), res);
-               return ServerAnswer.ok("File " + path + " imported");
-       }
-
-}
diff --git a/server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/server/jcr/mvc/JcrMvcConstants.java b/server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/server/jcr/mvc/JcrMvcConstants.java
deleted file mode 100644 (file)
index 158098e..0000000
+++ /dev/null
@@ -1,21 +0,0 @@
-/*
- * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
- *
- * 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 org.argeo.server.jcr.mvc;
-
-public interface JcrMvcConstants {
-       public final static String REQUEST_ATTR_SESSION = "jcrSession";
-}
diff --git a/server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/server/jcr/mvc/JcrXmlServerSerializer.java b/server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/server/jcr/mvc/JcrXmlServerSerializer.java
deleted file mode 100644 (file)
index 679a6ef..0000000
+++ /dev/null
@@ -1,129 +0,0 @@
-/*
- * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
- *
- * 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 org.argeo.server.jcr.mvc;
-
-import javax.jcr.Node;
-import javax.jcr.NodeIterator;
-import javax.jcr.RepositoryException;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-import javax.xml.parsers.DocumentBuilderFactory;
-import javax.xml.transform.Transformer;
-import javax.xml.transform.TransformerFactory;
-import javax.xml.transform.dom.DOMSource;
-import javax.xml.transform.stream.StreamResult;
-
-import org.argeo.ArgeoException;
-import org.argeo.server.ServerSerializer;
-import org.springframework.xml.dom.DomContentHandler;
-import org.w3c.dom.Document;
-import org.w3c.dom.Element;
-import org.w3c.dom.NodeList;
-import org.xml.sax.SAXException;
-
-public class JcrXmlServerSerializer implements ServerSerializer {
-       private String contentTypeCharset = "UTF-8";
-
-       private final DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
-                       .newInstance();
-       private final TransformerFactory transformerFactory = TransformerFactory
-                       .newInstance();
-
-       public void serialize(Object obj, HttpServletRequest request,
-                       HttpServletResponse response) {
-               if (!(obj instanceof Node))
-                       throw new ArgeoException("Only " + Node.class + " is supported");
-
-               String noRecurseStr = request.getParameter("noRecurse");
-               boolean noRecurse = noRecurseStr != null && noRecurseStr.equals("true");
-
-               String depthStr = request.getParameter("depth");
-               String downloadStr = request.getParameter("download");
-
-               Node node = (Node) obj;
-
-               try {
-                       String contentType = "text/xml;charset=" + contentTypeCharset;
-                       // download case
-                       if (downloadStr != null && downloadStr.equals("true")) {
-                               String fileName = node.getName().replace(':', '_') + ".xml";
-                               contentType = contentType + ";name=\"" + fileName + "\"";
-                               response.setHeader("Content-Disposition",
-                                               "attachment; filename=\"" + fileName + "\"");
-                               response.setHeader("Expires", "0");
-                               response
-                                               .setHeader("Cache-Control", "no-cache, must-revalidate");
-                               response.setHeader("Pragma", "no-cache");
-                       }
-
-                       response.setContentType(contentType);
-                       if (depthStr == null) {
-                               node.getSession().exportDocumentView(node.getPath(),
-                                               response.getOutputStream(), true, noRecurse);
-                       } else {
-                               int depth = Integer.parseInt(depthStr);
-                               Document document = documentBuilderFactory.newDocumentBuilder()
-                                               .newDocument();
-                               serializeLevelToDom(node, document, 0, depth);
-                               Transformer transformer = transformerFactory.newTransformer();
-                               transformer.transform(new DOMSource(document),
-                                               new StreamResult(response.getOutputStream()));
-                       }
-               } catch (Exception e) {
-                       throw new ArgeoException("Cannot serialize " + node, e);
-               }
-       }
-
-       protected void serializeLevelToDom(Node currentJcrNode,
-                       org.w3c.dom.Node currentDomNode, int currentDepth, int targetDepth)
-                       throws RepositoryException, SAXException {
-               DomContentHandler domContentHandler = new DomContentHandler(
-                               currentDomNode);
-               currentJcrNode.getSession().exportDocumentView(
-                               currentJcrNode.getPath(), domContentHandler, true, true);
-
-               if (currentDepth == targetDepth)
-                       return;
-
-               // TODO: filter
-               NodeIterator nit = currentJcrNode.getNodes();
-               while (nit.hasNext()) {
-                       Node nextJcrNode = nit.nextNode();
-                       org.w3c.dom.Node nextDomNode;
-                       if (currentDomNode instanceof Document)
-                               nextDomNode = ((Document) currentDomNode).getDocumentElement();
-                       else {
-                               String name = currentJcrNode.getName();
-                               NodeList nodeList = ((Element) currentDomNode)
-                                               .getElementsByTagName(name);
-                               if (nodeList.getLength() < 1)
-                                       throw new ArgeoException("No elment named " + name
-                                                       + " under " + currentDomNode);
-                               // we know it is the last one added
-                               nextDomNode = nodeList.item(nodeList.getLength() - 1);
-                       }
-                       // recursive call
-                       serializeLevelToDom(nextJcrNode, nextDomNode, currentDepth + 1,
-                                       targetDepth);
-               }
-       }
-
-       public void setContentTypeCharset(String contentTypeCharset) {
-               this.contentTypeCharset = contentTypeCharset;
-       }
-
-}
diff --git a/server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/server/jcr/mvc/OpenSessionInViewJcrInterceptor.java b/server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/server/jcr/mvc/OpenSessionInViewJcrInterceptor.java
deleted file mode 100644 (file)
index 1eb4e3c..0000000
+++ /dev/null
@@ -1,70 +0,0 @@
-/*
- * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
- *
- * 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 org.argeo.server.jcr.mvc;
-
-import javax.jcr.Session;
-
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import org.springframework.ui.ModelMap;
-import org.springframework.web.context.request.RequestAttributes;
-import org.springframework.web.context.request.WebRequest;
-import org.springframework.web.context.request.WebRequestInterceptor;
-
-public class OpenSessionInViewJcrInterceptor implements WebRequestInterceptor,
-               JcrMvcConstants {
-       private final static Log log = LogFactory
-                       .getLog(OpenSessionInViewJcrInterceptor.class);
-
-       private Session session;
-
-       public void preHandle(WebRequest request) throws Exception {
-               if (log.isTraceEnabled())
-                       log.trace("preHandle: " + request);
-               // Authentication auth = SecurityContextHolder.getContext()
-               // .getAuthentication();
-               // if (auth != null)
-               // log.debug("auth=" + auth + ", authenticated="
-               // + auth.isAuthenticated() + ", name=" + auth.getName());
-               // else
-               // log.debug("No auth");
-
-               // FIXME: find a safer way to initialize
-               // FIXME: not really needed to initialize here
-               // session.getRepository();
-               request.setAttribute(REQUEST_ATTR_SESSION, session,
-                               RequestAttributes.SCOPE_REQUEST);
-       }
-
-       public void postHandle(WebRequest request, ModelMap model) throws Exception {
-               // if (log.isDebugEnabled())
-               // log.debug("postHandle: " + request);
-       }
-
-       public void afterCompletion(WebRequest request, Exception ex)
-                       throws Exception {
-               if (log.isTraceEnabled())
-                       log.trace("afterCompletion: " + request);
-               // FIXME: only close session that were open
-               session.logout();
-       }
-
-       public void setSession(Session session) {
-               this.session = session;
-       }
-
-}