Oak Standalone throws ClassNotFoundException: remoting/protectedHandlersConfig.xml
package org.apache.jackrabbit.oak.standalone;
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
+
import javax.jcr.Repository;
+import javax.servlet.ServletContext;
-import org.apache.jackrabbit.server.remoting.davex.DavexServletService;
import org.apache.jackrabbit.server.remoting.davex.JcrRemotingServlet;
import org.apache.jackrabbit.webdav.simple.SimpleWebdavServlet;
import org.springframework.beans.factory.annotation.Autowired;
@@ -29,6 +33,8 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.embedded.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
+import org.springframework.core.io.ClassPathResource;
+import org.springframework.core.io.Resource;
/**
* Configures the Webdav and Davex servlet to enabled remote
@@ -43,6 +49,8 @@ public class RemotingInitializer {
@Autowired
private Repository repository;
+ @Autowired
+ private ServletContext servletContext;
@Bean
public ServletRegistrationBean webDavServlet() {
@@ -51,10 +59,15 @@ public class RemotingInitializer {
public Repository getRepository() {
return repository;
}
+
+ @Override
+ public ServletContext getServletContext() {
+ return RemotingInitializer.this.getServletContext();
+ }
}, "/repository/*");
bean.addInitParameter(SimpleWebdavServlet.INIT_PARAM_RESOURCE_PATH_PREFIX, "/repository");
- bean.addInitParameter(SimpleWebdavServlet.INIT_PARAM_RESOURCE_CONFIG, "remoting/webdav-config.xml");
+ bean.addInitParameter(SimpleWebdavServlet.INIT_PARAM_RESOURCE_CONFIG, "/remoting/webdav-config.xml");
return bean;
}
@@ -66,17 +79,43 @@ public class RemotingInitializer {
public Repository getRepository() {
return repository;
}
+
+ @Override
+ public ServletContext getServletContext() {
+ return RemotingInitializer.this.getServletContext();
+ }
}, "/server/*");
bean.addInitParameter(JcrRemotingServlet.INIT_PARAM_RESOURCE_PATH_PREFIX, "/server");
- bean.addInitParameter(JcrRemotingServlet.INIT_PARAM_BATCHREAD_CONFIG, "remoting/batchread.properties");
+ bean.addInitParameter(JcrRemotingServlet.INIT_PARAM_BATCHREAD_CONFIG, "/remoting/batchread.properties");
- //TODO By docs this is meant to point to a file which gets loaded
- //but servlet always reads it as File not via input stream. Hence using
- //actual class
bean.addInitParameter(JcrRemotingServlet.INIT_PARAM_PROTECTED_HANDLERS_CONFIG,
- "remoting/protectedHandlersConfig.xml");
+ "/remoting/protectedHandlersConfig.xml");
bean.addInitParameter(JcrRemotingServlet.INIT_PARAM_HOME, davHome);
return bean;
}
+
+ /**
+ * Creates a proxy ServletContext which delegates the resource loading to Spring Resource support
+ * Without this default embedded server ServletContext based resource loading was failing
+ */
+ private ServletContext getServletContext(){
+ return (ServletContext) Proxy.newProxyInstance(getClass().getClassLoader(), new Class[]{ServletContext.class},
+ new InvocationHandler(){
+ @Override
+ public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
+ if ("getResourceAsStream".equals(method.getName())){
+ return getResource((String) args[0]).getInputStream();
+ }
+ if ("getResource".equals(method.getName())){
+ return getResource((String) args[0]).getURL();
+ }
+ return method.invoke(servletContext, args);
+ }
+ });
+ }
+
+ private Resource getResource(String path) {
+ return new ClassPathResource(path);
+ }
}
