enable cors

This commit is contained in:
Benjamin Gamard 2018-01-24 15:39:27 +01:00
parent d0646f12e6
commit 9ba49f35ff
5 changed files with 70 additions and 19 deletions

View File

@ -1,17 +1,5 @@
package com.sismics.docs.core.dao.jpa; package com.sismics.docs.core.dao.jpa;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.Query;
import com.google.common.base.Joiner; import com.google.common.base.Joiner;
import com.sismics.docs.core.constant.AuditLogType; import com.sismics.docs.core.constant.AuditLogType;
import com.sismics.docs.core.dao.jpa.criteria.TagCriteria; import com.sismics.docs.core.dao.jpa.criteria.TagCriteria;
@ -24,6 +12,11 @@ import com.sismics.docs.core.util.jpa.QueryUtil;
import com.sismics.docs.core.util.jpa.SortCriteria; import com.sismics.docs.core.util.jpa.SortCriteria;
import com.sismics.util.context.ThreadLocalContext; import com.sismics.util.context.ThreadLocalContext;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.Query;
import java.util.*;
/** /**
* Tag DAO. * Tag DAO.
* *
@ -204,7 +197,7 @@ public class TagDao {
} }
if (criteria.getNameLike() != null) { if (criteria.getNameLike() != null) {
criteriaList.add("t.TAG_NAME_C like :nameLike"); criteriaList.add("t.TAG_NAME_C like :nameLike");
parameterMap.put("nameLike", "%" + criteria.getNameLike() + "%"); parameterMap.put("nameLike", criteria.getNameLike() + "%");
} }
criteriaList.add("t.TAG_DELETEDATE_D is null"); criteriaList.add("t.TAG_DELETEDATE_D is null");

View File

@ -28,7 +28,7 @@ public class EnvironmentUtil {
* @return Running under Microsoft Windows * @return Running under Microsoft Windows
*/ */
public static boolean isWindows() { public static boolean isWindows() {
return OS.indexOf("win") >= 0; return OS.contains("win");
} }
/** /**
@ -37,7 +37,7 @@ public class EnvironmentUtil {
* @return Running under Mac OS * @return Running under Mac OS
*/ */
public static boolean isMacOs() { public static boolean isMacOs() {
return OS.indexOf("mac") >= 0; return OS.contains("mac");
} }
/** /**
@ -46,7 +46,7 @@ public class EnvironmentUtil {
* @return Running under UNIX * @return Running under UNIX
*/ */
public static boolean isUnix() { public static boolean isUnix() {
return OS.indexOf("nix") >= 0 || OS.indexOf("nux") >= 0 || OS.indexOf("aix") > 0; return OS.contains("nix") || OS.contains("nux") || OS.contains("aix");
} }
/** /**

View File

@ -0,0 +1,47 @@
package com.sismics.util.filter;
import com.sismics.util.EnvironmentUtil;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* Filter used to handle CORS requests.
*
* @author bgamard
*/
public class CorsFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) {
// NOP
}
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
if (EnvironmentUtil.isDevMode() && request.getHeader("origin") != null) {
// Add CORS in dev mode
response.addHeader("Access-Control-Allow-Origin", request.getHeader("origin"));
response.addHeader("Access-Control-Allow-Credentials", "true");
response.addHeader("Access-Control-Max-Age", "3600");
response.addHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");
response.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
}
if ("OPTIONS".equals(request.getMethod())) {
// Handle preflight request
response.getWriter().print("{ \"status\": \"ok\" }");
} else {
filterChain.doFilter(req, res);
}
}
@Override
public void destroy() {
// NOP
}
}

View File

@ -32,7 +32,7 @@ public class RequestContextFilter implements Filter {
private static final Logger log = LoggerFactory.getLogger(RequestContextFilter.class); private static final Logger log = LoggerFactory.getLogger(RequestContextFilter.class);
@Override @Override
public void init(FilterConfig filterConfig) throws ServletException { public void init(FilterConfig filterConfig) {
// Initialize the app directory // Initialize the app directory
if (!filterConfig.getServletContext().getServerInfo().startsWith("Grizzly")) { if (!filterConfig.getServletContext().getServerInfo().startsWith("Grizzly")) {
EnvironmentUtil.setWebappContext(true); EnvironmentUtil.setWebappContext(true);

View File

@ -13,6 +13,18 @@
<listener-class>com.twelvemonkeys.servlet.image.IIOProviderContextListener</listener-class> <listener-class>com.twelvemonkeys.servlet.image.IIOProviderContextListener</listener-class>
</listener> </listener>
<!-- This filter handles CORS -->
<filter>
<filter-name>corsFilter</filter-name>
<filter-class>com.sismics.util.filter.CorsFilter</filter-class>
<async-supported>true</async-supported>
</filter>
<filter-mapping>
<filter-name>corsFilter</filter-name>
<url-pattern>/api/*</url-pattern>
</filter-mapping>
<!-- This filter is used to process a couple things in the request context --> <!-- This filter is used to process a couple things in the request context -->
<filter> <filter>
<filter-name>requestContextFilter</filter-name> <filter-name>requestContextFilter</filter-name>
@ -23,7 +35,6 @@
<filter-mapping> <filter-mapping>
<filter-name>requestContextFilter</filter-name> <filter-name>requestContextFilter</filter-name>
<url-pattern>/api/*</url-pattern> <url-pattern>/api/*</url-pattern>
<url-pattern>*.jsp</url-pattern>
</filter-mapping> </filter-mapping>
<!-- These filters are used to secure URLs --> <!-- These filters are used to secure URLs -->