Author: marijan
Date: Sat Sep 15 16:26:59 2012
New Revision: 1385104
URL: http://svn.apache.org/viewvc?rev=1385104&view=rev
Log:
- move resource loaders
- modify documentmanager
- add freemarker view resolver
Added:
rave/sandbox/content-services/demo-portal/src/main/webapp/WEB-INF/freemarker/
rave/sandbox/content-services/demo-portal/src/main/webapp/WEB-INF/freemarker/views/
rave/sandbox/content-services/demo-portal/src/main/webapp/WEB-INF/freemarker/views/pages/
rave/sandbox/content-services/demo-portal/src/main/webapp/WEB-INF/freemarker/views/pages/footer.ftl
(with props)
rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/mvc/view/
rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/mvc/view/BinaryRepositoryController.java
(with props)
rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/mvc/view/BinaryResourceView.java
(with props)
rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/mvc/view/FreeMarkerTemplateHolder.java
(with props)
rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/mvc/view/FreeMarkerTemplateLoader.java
(with props)
rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/mvc/view/JcrRepositoryResourceLoader.java
(with props)
rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/mvc/view/JcrResourceUtils.java
(with props)
Modified:
rave/sandbox/content-services/demo-portal/src/main/webapp/WEB-INF/nested-dispatcher-servlet.xml
rave/sandbox/content-services/rave-jcr-integration/data-model/src/main/java/org/apache/rave/jcr/data/mapping/DefaultDocumentManager.java
rave/sandbox/content-services/rave-web-hmvc/pom.xml
Added:
rave/sandbox/content-services/demo-portal/src/main/webapp/WEB-INF/freemarker/views/pages/footer.ftl
URL:
http://svn.apache.org/viewvc/rave/sandbox/content-services/demo-portal/src/main/webapp/WEB-INF/freemarker/views/pages/footer.ftl?rev=1385104&view=auto
==============================================================================
---
rave/sandbox/content-services/demo-portal/src/main/webapp/WEB-INF/freemarker/views/pages/footer.ftl
(added)
+++
rave/sandbox/content-services/demo-portal/src/main/webapp/WEB-INF/freemarker/views/pages/footer.ftl
Sat Sep 15 16:26:59 2012
@@ -0,0 +1,12 @@
+<#-- @ftlvariable name="applicationProperties" type="java.util.Map" -->
+<#import "/spring.ftl" as spring />
+<footer>
+ <div class="container">
+ <div class="row">
+ <div class="span12 version">
+ <span>Apache Rave ${applicationProperties['portal.version']} -
FREEMARKER</span>
+ <a href="http://rave.apache.org"><img class="footer-logo"
alt="Apache Rave" title="Apache Rave" src="<@spring.url
'/static/images/rave-logo.png'/>"/></a>
+ </div>
+ </div>
+ </div>
+</footer>
\ No newline at end of file
Propchange:
rave/sandbox/content-services/demo-portal/src/main/webapp/WEB-INF/freemarker/views/pages/footer.ftl
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
rave/sandbox/content-services/demo-portal/src/main/webapp/WEB-INF/freemarker/views/pages/footer.ftl
------------------------------------------------------------------------------
svn:keywords = Id
Modified:
rave/sandbox/content-services/demo-portal/src/main/webapp/WEB-INF/nested-dispatcher-servlet.xml
URL:
http://svn.apache.org/viewvc/rave/sandbox/content-services/demo-portal/src/main/webapp/WEB-INF/nested-dispatcher-servlet.xml?rev=1385104&r1=1385103&r2=1385104&view=diff
==============================================================================
---
rave/sandbox/content-services/demo-portal/src/main/webapp/WEB-INF/nested-dispatcher-servlet.xml
(original)
+++
rave/sandbox/content-services/demo-portal/src/main/webapp/WEB-INF/nested-dispatcher-servlet.xml
Sat Sep 15 16:26:59 2012
@@ -273,13 +273,26 @@
</property>
</bean>
+ <!-- freemarker resolver -->
+ <bean id="freemarkerConfig"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
+ <property name="templateLoaderPath" value="/WEB-INF/freemarker/views/"/>
+ </bean>
+ <bean
class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
+ <property name="order" value="0"/>
+ <property name="cache" value="false"/>
+ <property name="prefix" value=""/>
+ <property name="suffix" value=".ftl"/>
+ </bean>
+
<!-- Resolves viewName names to protected .jsp resources within the
/WEB-INF/views directory -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
+ <property name="order" value="1"/>
<property name="prefix" value="/WEB-INF/jsp/views/"/>
<property name="suffix" value=".jsp"/>
<property name="requestContextAttribute" value="requestContext"/>
</bean>
+
<oxm:jaxb2-marshaller id="xmlMarshaller">
<oxm:class-to-be-bound name="org.apache.rave.portal.model.JpaUser"/>
<oxm:class-to-be-bound name="org.apache.rave.portal.model.JpaPage"/>
Modified:
rave/sandbox/content-services/rave-jcr-integration/data-model/src/main/java/org/apache/rave/jcr/data/mapping/DefaultDocumentManager.java
URL:
http://svn.apache.org/viewvc/rave/sandbox/content-services/rave-jcr-integration/data-model/src/main/java/org/apache/rave/jcr/data/mapping/DefaultDocumentManager.java?rev=1385104&r1=1385103&r2=1385104&view=diff
==============================================================================
---
rave/sandbox/content-services/rave-jcr-integration/data-model/src/main/java/org/apache/rave/jcr/data/mapping/DefaultDocumentManager.java
(original)
+++
rave/sandbox/content-services/rave-jcr-integration/data-model/src/main/java/org/apache/rave/jcr/data/mapping/DefaultDocumentManager.java
Sat Sep 15 16:26:59 2012
@@ -26,12 +26,15 @@ import javax.jcr.Credentials;
import javax.jcr.Repository;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
+import javax.servlet.ServletContext;
import org.apache.commons.lang.StringUtils;
import org.apache.jackrabbit.ocm.manager.ObjectContentManager;
import org.apache.jackrabbit.ocm.manager.impl.ObjectContentManagerImpl;
import org.apache.jackrabbit.ocm.mapper.Mapper;
import org.apache.jackrabbit.ocm.mapper.impl.annotation.AnnotationMapperImpl;
+import org.apache.rave.jcr.LazyRepositoryFactory;
+import org.apache.rave.jcr.LazyRepositoryFactoryListener;
import org.apache.rave.jcr.data.document.Document;
import org.apache.rave.jcr.data.document.impl.BinaryContent;
import org.apache.rave.jcr.data.document.impl.BinaryResource;
@@ -43,34 +46,43 @@ import org.slf4j.LoggerFactory;
/**
* @version "$Id$"
*/
-public class DefaultDocumentManager implements DocumentManager {
+public class DefaultDocumentManager implements DocumentManager,
LazyRepositoryFactoryListener {
private static Logger log =
LoggerFactory.getLogger(DefaultDocumentManager.class);
public static final char PATH_SEPARATOR = '/';
+ private Repository repository;
private Session session;
private ObjectContentManager manager;
+ private Credentials credentials;
+ private LazyRepositoryFactory factory;
+ private ServletContext context;
- public DefaultDocumentManager(Repository repository, Credentials
credentials) {
- try {
- this.session = repository.login(credentials);
- manager = createManager();
- } catch (RepositoryException e) {
- throw new IllegalStateException("Cannot login into repository: " +
e.getMessage());
- }
+ public DefaultDocumentManager(ServletContext context,
LazyRepositoryFactory factory, Credentials credentials) {
+ this.context = context;
+ this.credentials = credentials;
+ this.factory = factory;
+ repository = factory.getRepository(this);
}
+
public DefaultDocumentManager(Session session) {
this.session = session;
manager = createManager();
+
}
@Override
public <T extends Document> T fetchDocument(String path, Class<T> clazz) {
+
+ final ObjectContentManager ourManager = getManager();
+ if (ourManager == null) {
+ return null;
+ }
@SuppressWarnings("unchecked")
- final T document = (T) manager.getObject(clazz, path);
+ final T document = (T) ourManager.getObject(clazz, path);
log.info("Loaded Document: {}", document);
return document;
@@ -89,13 +101,18 @@ public class DefaultDocumentManager impl
if (!document.getPath().startsWith("/")) {
throw new IllegalArgumentException("Cannot save document with
relative path");
}
- if (manager.getObject(document.getPath()) != null) {
+
+ ObjectContentManager ourManager = getManager();
+ if (ourManager == null) {
+ return false;
+ }
+ if (ourManager.getObject(document.getPath()) != null) {
log.error("Object already exists {}", document.getPath());
return false;
}
createSubfolders(document.getPath());
- manager.insert(document);
- manager.save();
+ ourManager.insert(document);
+ ourManager.save();
return true;
}
@@ -117,19 +134,41 @@ public class DefaultDocumentManager impl
for (String pathPart : pathParts) {
if (StringUtils.isNotBlank(pathPart)) {
final String folderPath = parent.append(pathPart).toString();
- if (manager.getObject(folderPath) != null) {
+ final ObjectContentManager ourManager = getManager();
+ if (ourManager == null) {
+ return;
+ }
+
+ if (ourManager.getObject(folderPath) != null) {
log.debug("Path already exists {}", folderPath);
return;
}
final Document folder = new ContentFolder(folderPath);
- manager.insert(folder);
- manager.save();
+ ourManager.insert(folder);
+ ourManager.save();
parent.append(PATH_SEPARATOR);
}
}
}
+ private ObjectContentManager getManager() {
+ if (repository != null) {
+ try {
+ this.session = repository.login(credentials);
+ manager = createManager();
+ return manager;
+ } catch (RepositoryException e) {
+ throw new IllegalStateException("Cannot login into repository:
" + e.getMessage());
+ }
+ } else {
+
+ log.error("Repository was not initialized");
+ }
+
+ return null;
+ }
+
private ObjectContentManager createManager() {
@SuppressWarnings("rawtypes")
List<Class> classes = new ArrayList<Class>();
@@ -144,4 +183,19 @@ public class DefaultDocumentManager impl
Mapper mapper = new AnnotationMapperImpl(classes);
return new ObjectContentManagerImpl(session, mapper);
}
+
+
+ public void shutdown() {
+ factory.removeListener(this);
+ }
+
+ @Override
+ public void repositoryAvailable(Object factoryKey, Repository repository) {
+ this.repository = repository;
+ }
+
+ @Override
+ public void repositoryNotAvailable(Object factoryKey) {
+ this.repository = null;
+ }
}
Modified: rave/sandbox/content-services/rave-web-hmvc/pom.xml
URL:
http://svn.apache.org/viewvc/rave/sandbox/content-services/rave-web-hmvc/pom.xml?rev=1385104&r1=1385103&r2=1385104&view=diff
==============================================================================
--- rave/sandbox/content-services/rave-web-hmvc/pom.xml (original)
+++ rave/sandbox/content-services/rave-web-hmvc/pom.xml Sat Sep 15 16:26:59 2012
@@ -36,6 +36,11 @@
<dependencies>
<dependency>
+ <groupId>org.apache.rave.sandbox.jcr</groupId>
+ <artifactId>rave-jcr-data-model</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ <dependency>
<groupId>org.apache.rave</groupId>
<artifactId>rave-core</artifactId>
<version>${apache.rave.version}</version>
Added:
rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/mvc/view/BinaryRepositoryController.java
URL:
http://svn.apache.org/viewvc/rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/mvc/view/BinaryRepositoryController.java?rev=1385104&view=auto
==============================================================================
---
rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/mvc/view/BinaryRepositoryController.java
(added)
+++
rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/mvc/view/BinaryRepositoryController.java
Sat Sep 15 16:26:59 2012
@@ -0,0 +1,54 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.apache.rave.portal.web.mvc.view;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.rave.jcr.data.mapping.DocumentManager;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.web.servlet.ModelAndView;
+import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
+
+/**
+ * Serves binaries from JCR repository
+ *
+ * @version "$Id$"
+ */
+
+public class BinaryRepositoryController extends MultiActionController {
+ private static final Logger log =
LoggerFactory.getLogger(BinaryRepositoryController.class);
+
+ private DocumentManager documentManager;
+
+ public ModelAndView render(HttpServletRequest request, HttpServletResponse
response) {
+ final BinaryResourceView view = new
BinaryResourceView(documentManager, request.getRequestURI());
+ final Map<String, Object> model = new HashMap<String, Object>();
+ return new ModelAndView(view, model);
+ }
+
+ public void setDocumentManager(DocumentManager documentManager) {
+ this.documentManager = documentManager;
+ }
+}
Propchange:
rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/mvc/view/BinaryRepositoryController.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/mvc/view/BinaryRepositoryController.java
------------------------------------------------------------------------------
svn:keywords = Id
Added:
rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/mvc/view/BinaryResourceView.java
URL:
http://svn.apache.org/viewvc/rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/mvc/view/BinaryResourceView.java?rev=1385104&view=auto
==============================================================================
---
rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/mvc/view/BinaryResourceView.java
(added)
+++
rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/mvc/view/BinaryResourceView.java
Sat Sep 15 16:26:59 2012
@@ -0,0 +1,97 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.apache.rave.portal.web.mvc.view;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Map;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.rave.jcr.data.document.BinaryDocument;
+import org.apache.rave.jcr.data.document.impl.BinaryContent;
+import org.apache.rave.jcr.data.mapping.DocumentManager;
+import org.apache.rave.jcr.utils.JcrUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.web.servlet.view.AbstractView;
+
+/**
+ * @version "$Id$"
+ */
+public class BinaryResourceView extends AbstractView {
+
+ private static Logger log =
LoggerFactory.getLogger(BinaryResourceView.class);
+ private static final int DEFAULT_SIZE = 1024 << 10;
+
+ private final String binaryPath;
+
+
+ private DocumentManager documentManager;
+
+ public BinaryResourceView(DocumentManager documentManager, String
binaryPath) {
+ this.binaryPath = binaryPath;
+ this.documentManager = documentManager;
+ }
+
+
+ @Override
+ protected void renderMergedOutputModel(Map<String, Object> model,
HttpServletRequest request, HttpServletResponse response) throws Exception {
+
+
+ final BinaryContent binaryContent =
documentManager.fetchDocument(binaryPath, BinaryContent.class);
+ final ByteArrayOutputStream byteArrayOutputStream =
fillOutputStream(binaryContent);
+ setContentType(extractContentType(binaryContent));
+
+ try {
+ writeToResponse(response, byteArrayOutputStream);
+ } finally {
+ byteArrayOutputStream.close();
+ }
+
+ }
+
+ private ByteArrayOutputStream fillOutputStream(BinaryDocument content)
throws IOException {
+ final ByteArrayOutputStream outputStream = new
ByteArrayOutputStream(DEFAULT_SIZE);
+ if (content == null || content.getResource() == null) {
+ log.warn("Content was null: {}", binaryPath);
+ return outputStream;
+ }
+ final InputStream stream = content.getResource().getData();
+ final byte[] data = new byte[DEFAULT_SIZE];
+ int read;
+ while ((read = stream.read(data, 0, data.length)) != -1) {
+ outputStream.write(data, 0, read);
+ }
+ return outputStream;
+
+ }
+
+ private String extractContentType(BinaryDocument content) {
+ if (content == null || content.getResource() == null) {
+ return "plain/text";
+ }
+ return JcrUtils.parseMimeType(content.getResource().getData());
+ }
+
+
+}
Propchange:
rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/mvc/view/BinaryResourceView.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/mvc/view/BinaryResourceView.java
------------------------------------------------------------------------------
svn:keywords = Id
Added:
rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/mvc/view/FreeMarkerTemplateHolder.java
URL:
http://svn.apache.org/viewvc/rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/mvc/view/FreeMarkerTemplateHolder.java?rev=1385104&view=auto
==============================================================================
---
rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/mvc/view/FreeMarkerTemplateHolder.java
(added)
+++
rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/mvc/view/FreeMarkerTemplateHolder.java
Sat Sep 15 16:26:59 2012
@@ -0,0 +1,87 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.apache.rave.portal.web.mvc.view;
+
+import java.io.Serializable;
+
+/**
+ * Wrapper for freemarker templates loaded from JCR repository
+ *
+ * @version "$Id$"
+ */
+public class FreeMarkerTemplateHolder implements Serializable {
+
+
+ private static final long serialVersionUID = 1L;
+ private String name;
+ private String data;
+ private long lastModified;
+
+ public FreeMarkerTemplateHolder() {
+ }
+
+ public FreeMarkerTemplateHolder(String name, String data, long
lastModified) {
+ this.name = name;
+ this.data = data;
+ this.lastModified = lastModified;
+ }
+
+ public FreeMarkerTemplateHolder(String name, String data) {
+ this.name = name;
+ this.data = data;
+ this.lastModified = -1;
+ }
+
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public String getData() {
+ return data;
+ }
+
+ public void setData(String data) {
+ this.data = data;
+ }
+
+ public long getLastModified() {
+ return lastModified;
+ }
+
+ public void setLastModified(long lastModified) {
+ this.lastModified = lastModified;
+ }
+
+ @Override
+ public String toString() {
+ final StringBuilder sb = new StringBuilder();
+ sb.append("FreeMarkerTemplateHolder");
+ sb.append("{name='").append(name).append('\'');
+ sb.append(", data='").append(data).append('\'');
+ sb.append(", lastModified=").append(lastModified);
+ sb.append('}');
+ return sb.toString();
+ }
+}
Propchange:
rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/mvc/view/FreeMarkerTemplateHolder.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/mvc/view/FreeMarkerTemplateHolder.java
------------------------------------------------------------------------------
svn:keywords = Id
Added:
rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/mvc/view/FreeMarkerTemplateLoader.java
URL:
http://svn.apache.org/viewvc/rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/mvc/view/FreeMarkerTemplateLoader.java?rev=1385104&view=auto
==============================================================================
---
rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/mvc/view/FreeMarkerTemplateLoader.java
(added)
+++
rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/mvc/view/FreeMarkerTemplateLoader.java
Sat Sep 15 16:26:59 2012
@@ -0,0 +1,84 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.apache.rave.portal.web.mvc.view;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.Reader;
+import java.io.StringReader;
+
+import org.apache.commons.io.IOUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.core.io.Resource;
+import org.springframework.core.io.ResourceLoader;
+
+import freemarker.cache.TemplateLoader;
+
+/**
+ * Loads FreeMarker templates from JCR repository.
+ *
+ * @version "$Id$"
+ */
+public class FreeMarkerTemplateLoader implements TemplateLoader {
+
+ private static Logger log =
LoggerFactory.getLogger(FreeMarkerTemplateLoader.class);
+
+ private ResourceLoader loader;
+
+ @Override
+ public Object findTemplateSource(String name) throws IOException {
+ log.debug("Loading freemarker template from JCR repository {}", name);
+ final Resource resource = loader.getResource(name);
+ if (resource == null) {
+ return null;
+ }
+ // parse resource
+ final InputStream inputStream = resource.getInputStream();
+ String stringTemplate = IOUtils.toString(inputStream, "UTF-8");
+ return new FreeMarkerTemplateHolder(name, stringTemplate);
+ }
+
+ @Override
+ public long getLastModified(Object templateSource) {
+ if (templateSource == null) {
+ return -1;
+ }
+ return ((FreeMarkerTemplateHolder) templateSource).getLastModified();
+ }
+
+ @Override
+ public Reader getReader(Object templateSource, String encoding) throws
IOException {
+ return new StringReader(((FreeMarkerTemplateHolder)
templateSource).getData());
+ }
+
+ @Override
+ public void closeTemplateSource(Object templateSource) throws IOException {
+ // NOOP
+ }
+
+ public void setLoader(ResourceLoader loader) {
+ this.loader = loader;
+ }
+}
+
+
+
+
Propchange:
rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/mvc/view/FreeMarkerTemplateLoader.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/mvc/view/FreeMarkerTemplateLoader.java
------------------------------------------------------------------------------
svn:keywords = Id
Added:
rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/mvc/view/JcrRepositoryResourceLoader.java
URL:
http://svn.apache.org/viewvc/rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/mvc/view/JcrRepositoryResourceLoader.java?rev=1385104&view=auto
==============================================================================
---
rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/mvc/view/JcrRepositoryResourceLoader.java
(added)
+++
rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/mvc/view/JcrRepositoryResourceLoader.java
Sat Sep 15 16:26:59 2012
@@ -0,0 +1,77 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.apache.rave.portal.web.mvc.view;
+
+import org.apache.rave.jcr.data.document.impl.BinaryContent;
+import org.apache.rave.jcr.data.mapping.DocumentManager;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.core.io.InputStreamResource;
+import org.springframework.core.io.Resource;
+import org.springframework.core.io.ResourceLoader;
+import org.springframework.util.Assert;
+import org.springframework.util.ClassUtils;
+
+/**
+ * Used to load resources from JCR repository
+ *
+ * @version "$Id$"
+ */
+public class JcrRepositoryResourceLoader implements ResourceLoader {
+
+ private static Logger log =
LoggerFactory.getLogger(JcrRepositoryResourceLoader.class);
+
+
+ private final DocumentManager documentManager;
+
+ public JcrRepositoryResourceLoader(DocumentManager documentManager) {
+ this.documentManager = documentManager;
+ }
+
+
+ @Override
+ public Resource getResource(String absolutePath) {
+ Assert.notNull(absolutePath, "Resource path must not be null");
+ if (!absolutePath.startsWith("/") &&
!absolutePath.startsWith(JcrResourceUtils.JCR_URL_PREFIX)) {
+ log.error("Location provided was not absolute path nor JCR
protocol: {}", absolutePath);
+ absolutePath = '/' + absolutePath;
+ }
+ final BinaryContent content =
documentManager.fetchDocument(preparePath(absolutePath), BinaryContent.class);
+ if (content == null) {
+ log.warn("### Resource does not exists: {}", absolutePath);
+ return null;
+ }
+ return new InputStreamResource(content.getResource().getData());
+ }
+
+ private String preparePath(String absolutePath) {
+ if (absolutePath.startsWith("/")) {
+ return absolutePath;
+ }
+ return absolutePath.substring(JcrResourceUtils.JCR_URL_PREFIX_LENGTH);
+ }
+
+
+ @Override
+ public ClassLoader getClassLoader() {
+ return ClassUtils.getDefaultClassLoader();
+ }
+
+}
Propchange:
rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/mvc/view/JcrRepositoryResourceLoader.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/mvc/view/JcrRepositoryResourceLoader.java
------------------------------------------------------------------------------
svn:keywords = Id
Added:
rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/mvc/view/JcrResourceUtils.java
URL:
http://svn.apache.org/viewvc/rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/mvc/view/JcrResourceUtils.java?rev=1385104&view=auto
==============================================================================
---
rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/mvc/view/JcrResourceUtils.java
(added)
+++
rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/mvc/view/JcrResourceUtils.java
Sat Sep 15 16:26:59 2012
@@ -0,0 +1,33 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.apache.rave.portal.web.mvc.view;
+
+/**
+ * @version "$Id$"
+ */
+public final class JcrResourceUtils {
+
+ /**
+ * Prefix which indicates resource is accessed from JCR repository
+ */
+ public static final String JCR_URL_PREFIX = "jcr:";
+ public static final int JCR_URL_PREFIX_LENGTH = JCR_URL_PREFIX.length();
+
+}
Propchange:
rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/mvc/view/JcrResourceUtils.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/mvc/view/JcrResourceUtils.java
------------------------------------------------------------------------------
svn:keywords = Id