Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#33 Introduce CORS support. Added base filters for reusable components.
- Loading branch information
Showing
13 changed files
with
436 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
web-spi/src/main/java/ws/ament/hammock/web/base/BaseAlwaysPostRequestFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() { | ||
|
||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
web-spi/src/main/java/ws/ament/hammock/web/base/BaseInsteadOfRequestFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() { | ||
|
||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
web-spi/src/main/java/ws/ament/hammock/web/base/BasePreRequestFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
25
web-spi/src/main/java/ws/ament/hammock/web/base/Constants.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}; | ||
} |
84 changes: 84 additions & 0 deletions
84
web-spi/src/main/java/ws/ament/hammock/web/cors/CORSConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
44
web-spi/src/main/java/ws/ament/hammock/web/cors/CORSFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.