Skip to content

Commit

Permalink
#33 Introduce CORS support. Added base filters for reusable components.
Browse files Browse the repository at this point in the history
  • Loading branch information
johnament committed Oct 16, 2016
1 parent 3e80605 commit d8645d7
Show file tree
Hide file tree
Showing 13 changed files with 436 additions and 31 deletions.
7 changes: 7 additions & 0 deletions pom.xml
Expand Up @@ -79,6 +79,7 @@
<javax.persistence.version>2.1.0</javax.persistence.version>
<hibernate.version>5.2.2.Final</hibernate.version>
<jersey.version>2.23.2</jersey.version>
<mockito-core.version>2.1.0</mockito-core.version>
</properties>
<modules>
<module>core</module>
Expand Down Expand Up @@ -380,6 +381,12 @@
<artifactId>jersey-cdi1x-servlet</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito-core.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down
4 changes: 4 additions & 0 deletions web-spi/pom.xml
Expand Up @@ -60,5 +60,9 @@
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,59 @@
/*
* Copyright 2016 Hammock and its contributors
*
* 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 ws.ament.hammock.web.base;

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 java.io.IOException;

public abstract class BaseAlwaysPostRequestFilter implements Filter {

@Override
public void init(FilterConfig filterConfig) throws ServletException {

}

@Override
public void doFilter(ServletRequest servletRequest,
ServletResponse servletResponse,
FilterChain filterChain) throws IOException, ServletException
{
HttpServletRequest request = (HttpServletRequest)servletRequest;
HttpServletResponse response = (HttpServletResponse)servletResponse;
try {
filterChain.doFilter(request, response);
}
finally {
doFilter(request, response);
}
}

protected abstract void doFilter(HttpServletRequest request, HttpServletResponse response);

@Override
public void destroy() {

}
}
@@ -0,0 +1,57 @@
/*
* Copyright 2016 Hammock and its contributors
*
* 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 ws.ament.hammock.web.base;

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 java.io.IOException;

public abstract class BaseInsteadOfRequestFilter implements Filter {

@Override
public void init(FilterConfig filterConfig) throws ServletException {

}

@Override
public void doFilter(ServletRequest servletRequest,
ServletResponse servletResponse,
FilterChain filterChain) throws IOException, ServletException
{
HttpServletResponse response = (HttpServletResponse)servletResponse;
HttpServletRequest request = (HttpServletRequest)servletRequest;

if (!doFilter(request, response)) {
filterChain.doFilter(request, response);
}
}

protected abstract boolean doFilter(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException;

@Override
public void destroy() {

}
}
@@ -0,0 +1,55 @@
/*
* Copyright 2016 Hammock and its contributors
*
* 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 ws.ament.hammock.web.base;

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 java.io.IOException;

public abstract class BasePreRequestFilter implements Filter {

@Override
public void init(FilterConfig filterConfig) throws ServletException {

}

@Override
public void doFilter(ServletRequest servletRequest,
ServletResponse servletResponse,
FilterChain filterChain) throws IOException, ServletException
{
HttpServletRequest request = (HttpServletRequest)servletRequest;
HttpServletResponse response = (HttpServletResponse)servletResponse;
doFilter(request, response);
filterChain.doFilter(request, response);
}

protected abstract void doFilter(HttpServletRequest request, HttpServletResponse response);

@Override
public void destroy() {

}
}
25 changes: 25 additions & 0 deletions web-spi/src/main/java/ws/ament/hammock/web/base/Constants.java
@@ -0,0 +1,25 @@
/*
* Copyright 2016 Hammock and its contributors
*
* 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 ws.ament.hammock.web.base;

import javax.servlet.DispatcherType;

public interface Constants {
DispatcherType[] DISPATCHER_TYPES = new DispatcherType[]{DispatcherType.REQUEST, DispatcherType.FORWARD, DispatcherType.ASYNC, DispatcherType.INCLUDE, DispatcherType.ERROR};
}
@@ -0,0 +1,84 @@
/*
* Copyright 2016 Hammock and its contributors
*
* 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 ws.ament.hammock.web.cors;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.Dependent;
import javax.enterprise.inject.Produces;
import javax.inject.Inject;
import javax.servlet.annotation.WebInitParam;

import org.apache.deltaspike.core.api.config.ConfigProperty;
import ws.ament.hammock.web.spi.FilterDescriptor;

import static ws.ament.hammock.web.base.Constants.DISPATCHER_TYPES;

@ApplicationScoped
public class CORSConfiguration {


@Inject
@ConfigProperty(name = "cors.enabled")
private boolean enabled;
@Inject
@ConfigProperty(name = "cors.origin")
private String origin;
@Inject
@ConfigProperty(name = "cors.headers")
private String headers;
@Inject
@ConfigProperty(name = "cors.credentials")
private String credentials;
@Inject
@ConfigProperty(name = "cors.methods")
private String methods;
@Inject
@ConfigProperty(name = "cors.maxAge")
private String maxAge;

@Produces
@Dependent
private FilterDescriptor resourceFilter = new FilterDescriptor("CORSFilter", new String[]{"/*"}, new String[]{"/*"},
DISPATCHER_TYPES,
new WebInitParam[]{}, true, null, CORSFilter.class);

public boolean isEnabled() {
return enabled;
}

public String getOrigin() {
return origin;
}

public String getHeaders() {
return headers;
}

public String getCredentials() {
return credentials;
}

public String getMethods() {
return methods;
}

public String getMaxAge() {
return maxAge;
}
}
44 changes: 44 additions & 0 deletions web-spi/src/main/java/ws/ament/hammock/web/cors/CORSFilter.java
@@ -0,0 +1,44 @@
/*
* Copyright 2016 Hammock and its contributors
*
* 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 ws.ament.hammock.web.cors;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ws.ament.hammock.web.base.BaseAlwaysPostRequestFilter;

@ApplicationScoped
public class CORSFilter extends BaseAlwaysPostRequestFilter {

@Inject
private CORSConfiguration corsConfiguration;

@Override
protected void doFilter(HttpServletRequest request, HttpServletResponse response) {
if(corsConfiguration.isEnabled()) {
response.addHeader("Access-Control-Allow-Origin", corsConfiguration.getOrigin());
response.addHeader("Access-Control-Allow-Headers", corsConfiguration.getHeaders());
response.addHeader("Access-Control-Allow-Credentials", corsConfiguration.getCredentials());
response.addHeader("Access-Control-Allow-Methods", corsConfiguration.getMethods());
response.addHeader("Access-Control-Max-Age", corsConfiguration.getMaxAge());
}
}
}
Expand Up @@ -22,13 +22,14 @@
import javax.enterprise.context.Dependent;
import javax.enterprise.inject.Produces;
import javax.inject.Inject;
import javax.servlet.DispatcherType;
import javax.servlet.annotation.WebInitParam;
import java.io.InputStream;

import org.apache.deltaspike.core.api.config.ConfigProperty;
import ws.ament.hammock.web.spi.FilterDescriptor;

import static ws.ament.hammock.web.base.Constants.DISPATCHER_TYPES;

@ApplicationScoped
public class ResourceManager {

Expand Down Expand Up @@ -62,6 +63,5 @@ InputStream load(String path) {
@Produces
@Dependent
private FilterDescriptor resourceFilter = new FilterDescriptor("ResourceFilter", new String[]{"/*"}, new String[]{"/*"},
new DispatcherType[]{DispatcherType.REQUEST, DispatcherType.FORWARD, DispatcherType.ASYNC, DispatcherType.INCLUDE, DispatcherType.ERROR},
new WebInitParam[]{}, true, null, ResourceRenderFilter.class);
DISPATCHER_TYPES, new WebInitParam[]{}, true, null, ResourceRenderFilter.class);
}

0 comments on commit d8645d7

Please sign in to comment.