You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@marmotta.apache.org by tk...@apache.org on 2013/04/12 10:57:46 UTC
[4/5] git commit: fixes cors bug
fixes cors bug
Project: http://git-wip-us.apache.org/repos/asf/incubator-marmotta/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-marmotta/commit/86b0f948
Tree: http://git-wip-us.apache.org/repos/asf/incubator-marmotta/tree/86b0f948
Diff: http://git-wip-us.apache.org/repos/asf/incubator-marmotta/diff/86b0f948
Branch: refs/heads/develop
Commit: 86b0f948c334578e2c0b448cf4ebd2341c1e40c1
Parents: b93049f
Author: tkurz <tk...@apache.org>
Authored: Fri Apr 12 09:55:41 2013 +0200
Committer: tkurz <tk...@apache.org>
Committed: Fri Apr 12 09:55:41 2013 +0200
----------------------------------------------------------------------
.../src/main/webapp/WEB-INF/web.xml | 31 +++-
.../src/main/webapp/WEB-INF/web.xml | 30 +++-
.../core/servlet/MarmottaOptionsFilter.java | 136 +++++++++++++++
3 files changed, 189 insertions(+), 8 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/86b0f948/build/archetypes/marmotta-archetype-webapp/src/main/resources/archetype-resources/src/main/webapp/WEB-INF/web.xml
----------------------------------------------------------------------
diff --git a/build/archetypes/marmotta-archetype-webapp/src/main/resources/archetype-resources/src/main/webapp/WEB-INF/web.xml b/build/archetypes/marmotta-archetype-webapp/src/main/resources/archetype-resources/src/main/webapp/WEB-INF/web.xml
index a44d704..e6a8fde 100644
--- a/build/archetypes/marmotta-archetype-webapp/src/main/resources/archetype-resources/src/main/webapp/WEB-INF/web.xml
+++ b/build/archetypes/marmotta-archetype-webapp/src/main/resources/archetype-resources/src/main/webapp/WEB-INF/web.xml
@@ -52,27 +52,50 @@
<param-value>/tmp/lmf</param-value>
</context-param>
- <!-- this filter performs startup configurations for first installation -->
+
+ <!-- handle OPTIONS requests -->
<filter>
- <filter-name>MarmottaPreStartupFilter</filter-name>
- <filter-class>org.apache.marmotta.platform.core.servlet.MarmottaPreStartupFilter</filter-class>
+ <filter-name>MarmottaOptionsFilter</filter-name>
+ <filter-class>org.apache.marmotta.platform.core.servlet.MarmottaOptionsFilter</filter-class>
</filter>
<filter-mapping>
- <filter-name>MarmottaPreStartupFilter</filter-name>
+ <filter-name>MarmottaOptionsFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- enables a cors filter and maps it to any request -->
<!-- for more fine grained configuration have a look at http://software.dzhuvinov.com/cors-filter-configuration.html -->
<filter>
+ <!-- The CORS filter with parameters -->
<filter-name>CORS</filter-name>
<filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>
+
+ <init-param>
+ <param-name>cors.supportedMethods</param-name>
+ <param-value>GET, HEAD, POST, PUT, DELETE, OPTIONS</param-value>
+ </init-param>
+
+ <init-param>
+ <param-name>cors.supportedHeaders</param-name>
+ <param-value>Origin, Accept</param-value>
+ </init-param>
+
</filter>
<filter-mapping>
<filter-name>CORS</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
+ <!-- this filter performs startup configurations for first installation -->
+ <filter>
+ <filter-name>MarmottaPreStartupFilter</filter-name>
+ <filter-class>org.apache.marmotta.platform.core.servlet.MarmottaPreStartupFilter</filter-class>
+ </filter>
+ <filter-mapping>
+ <filter-name>MarmottaPreStartupFilter</filter-name>
+ <url-pattern>/*</url-pattern>
+ </filter-mapping>
+
<!-- Serve static resources from file system and from .jar files of the respective modules -->
<filter>
<filter-name>MarmottaResourceFilter</filter-name>
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/86b0f948/launchers/marmotta-webapp/src/main/webapp/WEB-INF/web.xml
----------------------------------------------------------------------
diff --git a/launchers/marmotta-webapp/src/main/webapp/WEB-INF/web.xml b/launchers/marmotta-webapp/src/main/webapp/WEB-INF/web.xml
index 465a199..d2e79d7 100644
--- a/launchers/marmotta-webapp/src/main/webapp/WEB-INF/web.xml
+++ b/launchers/marmotta-webapp/src/main/webapp/WEB-INF/web.xml
@@ -49,27 +49,49 @@
<param-value>/tmp/marmotta</param-value>
</context-param>
- <!-- this filter performs startup configurations for first installation -->
+ <!-- handle OPTIONS requests -->
<filter>
- <filter-name>MarmottaPreStartupFilter</filter-name>
- <filter-class>org.apache.marmotta.platform.core.servlet.MarmottaPreStartupFilter</filter-class>
+ <filter-name>MarmottaOptionsFilter</filter-name>
+ <filter-class>org.apache.marmotta.platform.core.servlet.MarmottaOptionsFilter</filter-class>
</filter>
<filter-mapping>
- <filter-name>MarmottaPreStartupFilter</filter-name>
+ <filter-name>MarmottaOptionsFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- enables a cors filter and maps it to any request -->
<!-- for more fine grained configuration have a look at http://software.dzhuvinov.com/cors-filter-configuration.html -->
<filter>
+ <!-- The CORS filter with parameters -->
<filter-name>CORS</filter-name>
<filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>
+
+ <init-param>
+ <param-name>cors.supportedMethods</param-name>
+ <param-value>GET, HEAD, POST, PUT, DELETE, OPTIONS</param-value>
+ </init-param>
+
+ <init-param>
+ <param-name>cors.supportedHeaders</param-name>
+ <param-value>Origin, Accept</param-value>
+ </init-param>
+
</filter>
<filter-mapping>
<filter-name>CORS</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
+ <!-- this filter performs startup configurations for first installation -->
+ <filter>
+ <filter-name>MarmottaPreStartupFilter</filter-name>
+ <filter-class>org.apache.marmotta.platform.core.servlet.MarmottaPreStartupFilter</filter-class>
+ </filter>
+ <filter-mapping>
+ <filter-name>MarmottaPreStartupFilter</filter-name>
+ <url-pattern>/*</url-pattern>
+ </filter-mapping>
+
<!-- Serve static resources from file system and from .jar files of the respective modules -->
<filter>
<filter-name>MarmottaResourceFilter</filter-name>
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/86b0f948/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/servlet/MarmottaOptionsFilter.java
----------------------------------------------------------------------
diff --git a/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/servlet/MarmottaOptionsFilter.java b/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/servlet/MarmottaOptionsFilter.java
new file mode 100644
index 0000000..e81eb1b
--- /dev/null
+++ b/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/servlet/MarmottaOptionsFilter.java
@@ -0,0 +1,136 @@
+/**
+ * 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.marmotta.platform.core.servlet;
+
+import org.apache.marmotta.platform.core.api.config.ConfigurationService;
+import org.jboss.resteasy.spi.DefaultOptionsMethodException;
+import org.jboss.resteasy.spi.ResteasyProviderFactory;
+import org.slf4j.Logger;
+
+import javax.inject.Inject;
+import javax.servlet.Filter;
+import javax.servlet.FilterChain;
+import javax.servlet.FilterConfig;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.ext.ExceptionMapper;
+import javax.ws.rs.ext.Provider;
+import java.io.IOException;
+import java.util.Arrays;
+
+/**
+ * This filter checks for OPTIONS requests. If the response returned by the other filters throws an exception
+ * org.jboss.resteasy.spi.DefaultOptionsMethodException, the filter writes to the response the default options
+ * of the Apache Marmotta system.
+ *
+ * <p/>
+ * Author: Sebastian Schaffert
+ */
+public class MarmottaOptionsFilter implements Filter {
+
+ @Inject
+ private Logger log;
+
+ @Inject
+ private ConfigurationService configurationService;
+
+ /**
+ * Called by the web container to indicate to a filter that it is being placed into
+ * service. The servlet container calls the init method exactly once after instantiating the
+ * filter. The init method must complete successfully before the filter is asked to do any
+ * filtering work. <br><br>
+ * <p/>
+ * The web container cannot place the filter into service if the init method either<br>
+ * 1.Throws a ServletException <br>
+ * 2.Does not return within a time period defined by the web container
+ */
+ @Override
+ public void init(FilterConfig filterConfig) throws ServletException {
+ ResteasyProviderFactory.getInstance().addExceptionMapper(new OptionsMapper());
+ }
+
+ /**
+ * The <code>doFilter</code> method of the Filter is called by the container
+ * each time a request/response pair is passed through the chain due
+ * to a client request for a resource at the end of the chain. The FilterChain passed in to this
+ * method allows the Filter to pass on the request and response to the next entity in the
+ * chain.<p>
+ * A typical implementation of this method would follow the following pattern:- <br>
+ * 1. Examine the request<br>
+ * 2. Optionally wrap the request object with a custom implementation to
+ * filter content or headers for input filtering <br>
+ * 3. Optionally wrap the response object with a custom implementation to
+ * filter content or headers for output filtering <br>
+ * 4. a) <strong>Either</strong> invoke the next entity in the chain using the FilterChain object (<code>chain.doFilter()</code>), <br>
+ * * 4. b) <strong>or</strong> not pass on the request/response pair to the next entity in the filter chain to block the request processing<br>
+ * * 5. Directly set headers on the response after invocation of the next entity in the filter chain.
+ */
+ @Override
+ public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
+
+
+ try {
+
+ chain.doFilter(request,response);
+
+ } catch (DefaultOptionsMethodException ex) {
+ if(response instanceof HttpServletResponse) {
+ HttpServletResponse resp = (HttpServletResponse)response;
+ HttpServletRequest req = (HttpServletRequest)request;
+
+ if(req.getMethod().equalsIgnoreCase("OPTIONS")) {
+ resp.setStatus(200);
+ resp.resetBuffer();
+
+ }
+ }
+ }
+
+
+
+ }
+
+ /**
+ * Called by the web container to indicate to a filter that it is being taken out of service. This
+ * method is only called once all threads within the filter's doFilter method have exited or after
+ * a timeout period has passed. After the web container calls this method, it will not call the
+ * doFilter method again on this instance of the filter. <br><br>
+ * <p/>
+ * This method gives the filter an opportunity to clean up any resources that are being held (for
+ * example, memory, file handles, threads) and make sure that any persistent state is synchronized
+ * with the filter's current state in memory.
+ */
+ @Override
+ public void destroy() {
+ //To change body of implemented methods use File | Settings | File Templates.
+ }
+
+ @Provider
+ protected static class OptionsMapper implements ExceptionMapper<DefaultOptionsMethodException> {
+
+ @Override
+ public Response toResponse(DefaultOptionsMethodException exception) {
+ throw exception;
+ }
+
+ }
+}