From b7fe83ed658c98a8fdf4aaa2f85a17f971527cb6 Mon Sep 17 00:00:00 2001 From: Christof Zellweger Date: Mon, 10 Apr 2017 13:31:00 +0200 Subject: [PATCH] CORS moved to domain project (to be used in dispatcherrest and queryrest), buildship / gradle wrapper upgrades --- .classpath | 27 ++------ .project | 13 ---- build.gradle | 10 ++- .../daq/queryrest/config/QueryRestConfig.java | 23 ++----- .../queryrest/config/QueryRestConfigCORS.java | 69 ------------------- src/main/resources/queryrest.properties | 12 ---- 6 files changed, 16 insertions(+), 138 deletions(-) delete mode 100644 src/main/java/ch/psi/daq/queryrest/config/QueryRestConfigCORS.java diff --git a/.classpath b/.classpath index 282cfc8..5e3ac91 100644 --- a/.classpath +++ b/.classpath @@ -1,27 +1,10 @@ - - - - - - - - - - - - - - - - - - - - - + + + + + - diff --git a/.project b/.project index ed655e6..e4d9fca 100644 --- a/.project +++ b/.project @@ -5,21 +5,11 @@ - - org.eclipse.wst.common.project.facet.core.builder - - - org.eclipse.jdt.core.javabuilder - - org.springframework.ide.eclipse.core.springbuilder - - - org.eclipse.buildship.core.gradleprojectbuilder @@ -27,10 +17,7 @@ - org.springframework.ide.eclipse.core.springnature - org.springsource.ide.eclipse.gradle.core.nature org.eclipse.jdt.core.javanature - org.eclipse.wst.common.project.facet.core.nature org.eclipse.buildship.core.gradleprojectnature diff --git a/build.gradle b/build.gradle index 34e544e..e2428cd 100644 --- a/build.gradle +++ b/build.gradle @@ -51,6 +51,10 @@ uploadArchives { } } -task dropIt(dependsOn: build) << { - exec{ executable "curl"; args "-X", "POST", "-F", "file=@build/libs/ch.psi.daq.queryrest-" + version + ".jar", "https://dropit.psi.ch/upload"; } -} \ No newline at end of file + +task dropIt(dependsOn: build) { + doLast { + exec{ executable "curl"; args "-X", "POST", "-F", "file=@build/libs/ch.psi.daq.queryrest-" + version + ".jar", "https://dropit.psi.ch/upload"; } + } +} + diff --git a/src/main/java/ch/psi/daq/queryrest/config/QueryRestConfig.java b/src/main/java/ch/psi/daq/queryrest/config/QueryRestConfig.java index 907098e..ec583bb 100644 --- a/src/main/java/ch/psi/daq/queryrest/config/QueryRestConfig.java +++ b/src/main/java/ch/psi/daq/queryrest/config/QueryRestConfig.java @@ -31,6 +31,7 @@ import com.fasterxml.jackson.dataformat.smile.SmileFactory; import ch.psi.daq.common.statistic.Statistics; import ch.psi.daq.domain.DataEvent; +import ch.psi.daq.domain.config.DomainConfigCORS; import ch.psi.daq.domain.query.backend.BackendQuery; import ch.psi.daq.domain.query.backend.analyzer.BackendQueryAnalyzer; import ch.psi.daq.domain.query.operation.Aggregation; @@ -53,7 +54,7 @@ import ch.psi.daq.queryrest.response.smile.SmileResponseStreamWriter; import ch.psi.daq.queryrest.response.smile.SmileTableResponseStreamWriter; @Configuration -@Import(value = QueryRestConfigCORS.class) +@Import(value = DomainConfigCORS.class) @PropertySource(value = {"classpath:queryrest.properties"}) @PropertySource(value = {"file:${user.home}/.config/daq/queryrest.properties"}, ignoreResourceNotFound = true) public class QueryRestConfig extends WebMvcConfigurerAdapter { @@ -61,8 +62,6 @@ public class QueryRestConfig extends WebMvcConfigurerAdapter { private static final String QUERYREST_DEFAULT_RESPONSE_AGGREGATIONS = "queryrest.default.response.aggregations"; private static final String QUERYREST_DEFAULT_RESPONSE_FIELDS = "queryrest.default.response.fields"; - private static final String QUERYREST_CORS_ALLOWEDORIGINS = "queryrest.cors.allowedorigins"; - private static final String QUERYREST_CORS_FORCEALLHEADERS = "queryrest.cors.forceallheaders"; // a nested configuration // this guarantees that the ordering of the properties file is as expected @@ -82,8 +81,8 @@ public class QueryRestConfig extends WebMvcConfigurerAdapter { public static final String BEAN_NAME_DEFAULT_RESPONSE_FIELDS = "defaultResponseFields"; public static final String BEAN_NAME_DEFAULT_RESPONSE_AGGREGATIONS = "defaultResponseAggregations"; - public static final String BEAN_NAME_CORS_ALLOWEDORIGINS = "corsAllowedorigins"; - public static final String BEAN_NAME_CORS_FORCEALLHEADERS = "corsForceallheaders"; +// public static final String BEAN_NAME_CORS_ALLOWEDORIGINS = "corsAllowedorigins"; +// public static final String BEAN_NAME_CORS_FORCEALLHEADERS = "corsForceallheaders"; @Resource private Environment env; @@ -224,18 +223,4 @@ public class QueryRestConfig extends WebMvcConfigurerAdapter { public Validator queryValidator() { return new QueryValidator(); } - - @Bean(name = BEAN_NAME_CORS_ALLOWEDORIGINS) - public String allowedOrigins() { - String value = env.getProperty(QUERYREST_CORS_ALLOWEDORIGINS, "http://localhost:8080, *"); - LOGGER.debug("Load '{}={}'", QUERYREST_CORS_ALLOWEDORIGINS, value); - return value; - } - - @Bean(name = BEAN_NAME_CORS_FORCEALLHEADERS) - public Boolean forceAllHeaders() { - Boolean value = env.getProperty(QUERYREST_CORS_FORCEALLHEADERS, Boolean.class, true); - LOGGER.debug("Load '{}={}'", QUERYREST_CORS_FORCEALLHEADERS, value); - return value; - } } diff --git a/src/main/java/ch/psi/daq/queryrest/config/QueryRestConfigCORS.java b/src/main/java/ch/psi/daq/queryrest/config/QueryRestConfigCORS.java deleted file mode 100644 index 4004376..0000000 --- a/src/main/java/ch/psi/daq/queryrest/config/QueryRestConfigCORS.java +++ /dev/null @@ -1,69 +0,0 @@ -package ch.psi.daq.queryrest.config; - -import java.util.Arrays; -import java.util.Set; -import java.util.stream.Collectors; - -import javax.annotation.PostConstruct; -import javax.annotation.Resource; - -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.PropertySource; -import org.springframework.core.env.Environment; -import org.springframework.web.servlet.config.annotation.CorsRegistry; -import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; - - -@Configuration -@PropertySource(value = {"classpath:queryrest.properties"}) -@PropertySource(value = {"file:${user.home}/.config/daq/queryrest.properties"}, ignoreResourceNotFound = true) -public class QueryRestConfigCORS extends WebMvcConfigurerAdapter { - - @Resource - private Environment env; - - @Resource(name = QueryRestConfig.BEAN_NAME_CORS_ALLOWEDORIGINS) - private String configuredOrigins; - - @Resource(name = QueryRestConfig.BEAN_NAME_CORS_FORCEALLHEADERS) - private Boolean forceAllHeaders; - - private String[] allowedOrigins; - - @PostConstruct - public void afterPropertiesSet() { - Set origs = Arrays.stream(configuredOrigins.split(",")).map(s -> s.trim()).collect(Collectors.toSet()); - allowedOrigins = origs.toArray(new String[origs.size()]); - } - - @Override - public void addCorsMappings(CorsRegistry registry) { - - boolean corsEnabled = Boolean.valueOf(env.getProperty("queryrest.cors.enable", "false")); - boolean forceAll = Boolean.valueOf(env.getProperty("queryrest.cors.forceallheaders", "false")); - - if (corsEnabled) { - - if (forceAll) { - registry - .addMapping("/**") - .allowedOrigins("*") - .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS") - .allowedHeaders("Origin", "Accept", "X-Requested-With", "Content-Type", "Access-Control-Request-Method", - "Access-Control-Request-Headers") - .allowCredentials(true) - .maxAge(1800); - } else if (!forceAll) { - // see https://spring.io/blog/2015/06/08/cors-support-in-spring-framework - registry - .addMapping("/**") - .allowedOrigins(allowedOrigins) - .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS") - .allowedHeaders("Origin", "Accept", "X-Requested-With", "Content-Type", "Access-Control-Request-Method", - "Access-Control-Request-Headers") - .allowCredentials(true) - .maxAge(1800); - } - } - } -} diff --git a/src/main/resources/queryrest.properties b/src/main/resources/queryrest.properties index f598b90..5ba6751 100644 --- a/src/main/resources/queryrest.properties +++ b/src/main/resources/queryrest.properties @@ -4,15 +4,3 @@ queryrest.default.response.fields=channel,backend,pulseId,globalSeconds,iocSecon # aggregation which are included in the response by default if aggregation is enabled for a given query queryrest.default.response.aggregations=min,mean,max - -# enables / disables the CORS servlet filter. Adds multiple CORS headers to the response -queryrest.cors.enable=true - -# Includes the CORS headers no matter what request or preflight was sent. If an Origin header is set, this header will be used. -# If no Origin header is set, '*' will be used. -queryrest.cors.forceallheaders=false - -# Defines the allowed origins for CORS requests. Only relevant if queryrest.enableCORS==true (see above). -# If this is set to '*', then all requests are allowed, from any source. If it's set to, say, http://ui-data-api.psi.ch, then only requests -# originating from that domain (Origin header set to that value) will be allowed. Otherwise a 403 error will be returned. -queryrest.cors.allowedorigins=*