diff --git a/pom.xml b/pom.xml index fcf4ea1710ff9fb071e3e5a1ab65f3d476886d60..53220f05dad2e8785c097479b92a3c09cbc594fc 100644 --- a/pom.xml +++ b/pom.xml @@ -14,7 +14,7 @@ <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> - <version>1.3.0.RELEASE</version> + <version>1.3.2.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> diff --git a/src/main/java/org/hbp/mip/MIPApplication.java b/src/main/java/org/hbp/mip/MIPApplication.java index 3981d5d78b45b44b22eaf0104de7a5d1aca7fb92..c8713635413a092f1a31dc3ef2ea52785b9dce29 100644 --- a/src/main/java/org/hbp/mip/MIPApplication.java +++ b/src/main/java/org/hbp/mip/MIPApplication.java @@ -24,6 +24,7 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import io.swagger.annotations.Api; import org.hbp.mip.model.User; +import org.hbp.mip.utils.CORSFilter; import org.hbp.mip.utils.HibernateUtil; import org.hibernate.Query; import org.hibernate.Session; @@ -35,7 +36,6 @@ import org.springframework.boot.autoconfigure.security.oauth2.resource.UserInfoT import org.springframework.boot.context.embedded.FilterRegistrationBean; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.core.Authentication; @@ -48,6 +48,7 @@ import org.springframework.security.oauth2.client.resource.OAuth2ProtectedResour import org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeResourceDetails; import org.springframework.security.oauth2.config.annotation.web.configuration.EnableOAuth2Client; import org.springframework.security.oauth2.provider.OAuth2Authentication; +import org.springframework.security.web.access.channel.ChannelProcessingFilter; import org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint; import org.springframework.security.web.authentication.www.BasicAuthenticationFilter; import org.springframework.security.web.csrf.CsrfFilter; @@ -55,12 +56,8 @@ import org.springframework.security.web.csrf.CsrfToken; import org.springframework.security.web.csrf.CsrfTokenRepository; import org.springframework.security.web.csrf.HttpSessionCsrfTokenRepository; import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.filter.OncePerRequestFilter; -import org.springframework.web.servlet.config.annotation.CorsRegistry; -import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; -import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import org.springframework.web.util.WebUtils; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.RequestHandlerSelectors; @@ -84,9 +81,8 @@ import java.security.Principal; @SpringBootApplication @RestController @EnableOAuth2Client -@Api(value = "/", description = "MIP API") @EnableSwagger2 -@Configuration +@Api(value = "/", description = "MIP API") public class MIPApplication extends WebSecurityConfigurerAdapter { @Autowired @@ -145,7 +141,6 @@ public class MIPApplication extends WebSecurityConfigurerAdapter { } @RequestMapping("/user") - @ResponseBody public Principal user(Principal principal, HttpServletResponse response) { ObjectMapper mapper = new ObjectMapper(); @@ -163,18 +158,10 @@ public class MIPApplication extends WebSecurityConfigurerAdapter { return principal; } - /*@RequestMapping("/logout") - public void logout(HttpServletResponse response) { - - Cookie cookie = new Cookie("user", null); - cookie.setPath("/"); - cookie.setMaxAge(0); - response.addCookie(cookie); - }*/ - @Override protected void configure(HttpSecurity http) throws Exception { // @formatter:off + http.addFilterBefore(new CORSFilter(), ChannelProcessingFilter.class); http.antMatcher("/**") .authorizeRequests() .antMatchers("/", "/frontend/**", "/webjars/**").permitAll() @@ -230,12 +217,6 @@ public class MIPApplication extends WebSecurityConfigurerAdapter { response.addCookie(cookie); } } - - /*response.addHeader("Access-Control-Allow-Origin", "*"); - response.addHeader("Access-Control-Allow-Headers","*"); - response.addHeader("Access-Control-Allow-Methods","GET, POST, PUT, OPTIONS"); - response.addHeader("Access-Control-Allow-Credentials","true");*/ - filterChain.doFilter(request, response); } }; @@ -247,14 +228,4 @@ public class MIPApplication extends WebSecurityConfigurerAdapter { return repository; } - @Bean - public WebMvcConfigurer corsConfigurer() { - return new WebMvcConfigurerAdapter() { - @Override - public void addCorsMappings(CorsRegistry registry) { - registry.addMapping("/**").allowedOrigins("http://frontend"); - } - }; - } - } diff --git a/src/main/java/org/hbp/mip/controllers/ApiException.java b/src/main/java/org/hbp/mip/controllers/ApiException.java deleted file mode 100644 index ef51b43dbeae2b75032b1369a140f9417dcc5387..0000000000000000000000000000000000000000 --- a/src/main/java/org/hbp/mip/controllers/ApiException.java +++ /dev/null @@ -1,14 +0,0 @@ -/** - * Created by mirco on 04.12.15. - */ - -package org.hbp.mip.controllers; - -public class ApiException extends Exception { - private int code; - - public ApiException(int code, String msg) { - super(msg); - this.code = code; - } -} diff --git a/src/main/java/org/hbp/mip/controllers/ApiOriginFilter.java b/src/main/java/org/hbp/mip/controllers/ApiOriginFilter.java deleted file mode 100644 index e0fb244fc00ec76496a8d12ca7187ee7303e636d..0000000000000000000000000000000000000000 --- a/src/main/java/org/hbp/mip/controllers/ApiOriginFilter.java +++ /dev/null @@ -1,29 +0,0 @@ -/** - * Created by mirco on 04.12.15. - */ - -package org.hbp.mip.controllers; - -import javax.servlet.*; -import javax.servlet.http.HttpServletResponse; -import java.io.IOException; - -public class ApiOriginFilter implements Filter { - @Override - public void doFilter(ServletRequest request, ServletResponse response, - FilterChain chain) throws IOException, ServletException { - HttpServletResponse res = (HttpServletResponse) response; - res.addHeader("Access-Control-Allow-Origin", "*"); - res.addHeader("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT"); - res.addHeader("Access-Control-Allow-Headers", "Content-Type"); - chain.doFilter(request, response); - } - - @Override - public void destroy() { - } - - @Override - public void init(FilterConfig filterConfig) throws ServletException { - } -} \ No newline at end of file diff --git a/src/main/java/org/hbp/mip/controllers/ApiResponseMessage.java b/src/main/java/org/hbp/mip/controllers/ApiResponseMessage.java deleted file mode 100644 index 798562cda5e33cd823bd09893dd8d1886392655e..0000000000000000000000000000000000000000 --- a/src/main/java/org/hbp/mip/controllers/ApiResponseMessage.java +++ /dev/null @@ -1,73 +0,0 @@ -/** - * Created by mirco on 04.12.15. - */ - -package org.hbp.mip.controllers; - -import javax.xml.bind.annotation.XmlTransient; - -@javax.xml.bind.annotation.XmlRootElement -public class ApiResponseMessage { - public static final int ERROR = 1; - public static final int WARNING = 2; - public static final int INFO = 3; - public static final int OK = 4; - public static final int TOO_BUSY = 5; - - int code; - String type; - String message; - - public ApiResponseMessage() { - } - - public ApiResponseMessage(int code, String message) { - this.code = code; - switch (code) { - case ERROR: - setType("error"); - break; - case WARNING: - setType("warning"); - break; - case INFO: - setType("info"); - break; - case OK: - setType("ok"); - break; - case TOO_BUSY: - setType("too busy"); - break; - default: - setType("unknown"); - break; - } - this.message = message; - } - - @XmlTransient - public int getCode() { - return code; - } - - public void setCode(int code) { - this.code = code; - } - - public String getType() { - return type; - } - - public void setType(String type) { - this.type = type; - } - - public String getMessage() { - return message; - } - - public void setMessage(String message) { - this.message = message; - } -} diff --git a/src/main/java/org/hbp/mip/controllers/ArticlesApi.java b/src/main/java/org/hbp/mip/controllers/ArticlesApi.java index 33fdd341a42e44577d41ca2a93bbb7331638d953..291b5272eeecf58b9b8213c89b8db207a5d126ba 100644 --- a/src/main/java/org/hbp/mip/controllers/ArticlesApi.java +++ b/src/main/java/org/hbp/mip/controllers/ArticlesApi.java @@ -36,7 +36,7 @@ public class ArticlesApi { @ApiParam(value = "Only ask results matching status", allowableValues = "{values=[draft, published, closed]}") @RequestParam(value = "status", required = false) String status, @ApiParam(value = "Only ask articles from own team") @RequestParam(value = "team", required = false) Boolean team, Principal principal - ) throws NotFoundException { + ) { // Get current user User user = MIPApplication.getUser(principal); @@ -92,7 +92,7 @@ public class ArticlesApi { public ResponseEntity<Void> addAnArticle( @RequestBody @ApiParam(value = "Article to create", required = true) Article article, Principal principal - ) throws NotFoundException { + ) { // Get current user User user = MIPApplication.getUser(principal); @@ -120,7 +120,7 @@ public class ArticlesApi { @RequestMapping(value = "/{slug}", method = RequestMethod.GET) public ResponseEntity<Article> getAnArticle( @ApiParam(value = "slug", required = true) @PathVariable("slug") String slug - ) throws NotFoundException { + ) { // Query DB Session session = HibernateUtil.getSessionFactory().getCurrentSession(); @@ -141,7 +141,7 @@ public class ArticlesApi { @ApiParam(value = "slug", required = true) @PathVariable("slug") String slug, @RequestBody @ApiParam(value = "Article to update", required = true) Article article, Principal principal - ) throws NotFoundException { + ) { // Get current user User user = MIPApplication.getUser(principal); @@ -161,7 +161,7 @@ public class ArticlesApi { @RequestMapping(value = "/{slug}", method = RequestMethod.DELETE) public ResponseEntity<Void> deleteAnArticle( @ApiParam(value = "slug", required = true) @PathVariable("slug") String slug - ) throws NotFoundException { + ) { // TODO : Implement delete method diff --git a/src/main/java/org/hbp/mip/controllers/DatasetsApi.java b/src/main/java/org/hbp/mip/controllers/DatasetsApi.java index 0996117034edc3d331060d4ae95fa962e699a436..6050b1b4e360babe9c3e9949e5bfe4173530df65 100644 --- a/src/main/java/org/hbp/mip/controllers/DatasetsApi.java +++ b/src/main/java/org/hbp/mip/controllers/DatasetsApi.java @@ -30,7 +30,7 @@ public class DatasetsApi { @RequestMapping(value = "/{code}", method = RequestMethod.GET) public ResponseEntity<Dataset> getADataset( @ApiParam(value = "code", required = true) @PathVariable("code") String code - ) throws NotFoundException { + ) { // Query DB Session session = HibernateUtil.getSessionFactory().getCurrentSession(); diff --git a/src/main/java/org/hbp/mip/controllers/GroupsApi.java b/src/main/java/org/hbp/mip/controllers/GroupsApi.java index 73a3378c33aa595e3a6cd6d6b1a2d1df8a7f75fc..db971ebc1ca3414b82f51dac7919c85d3f5918ac 100644 --- a/src/main/java/org/hbp/mip/controllers/GroupsApi.java +++ b/src/main/java/org/hbp/mip/controllers/GroupsApi.java @@ -25,7 +25,7 @@ public class GroupsApi { @ApiOperation(value = "Get the root group (containing all subgroups)", response = Group.class) @ApiResponses(value = { @ApiResponse(code = 200, message = "Success") }) @RequestMapping(method = RequestMethod.GET) - public ResponseEntity<Group> getTheRootGroup() throws NotFoundException { + public ResponseEntity<Group> getTheRootGroup() { // Set up root group String rootCode = "root"; diff --git a/src/main/java/org/hbp/mip/controllers/ModelsApi.java b/src/main/java/org/hbp/mip/controllers/ModelsApi.java index 002e9fbe72e5c4f879795b92bfcf4656eed3316c..0c7b8e975a580b921b955890c40d9caf5b0b0ef0 100644 --- a/src/main/java/org/hbp/mip/controllers/ModelsApi.java +++ b/src/main/java/org/hbp/mip/controllers/ModelsApi.java @@ -40,7 +40,7 @@ public class ModelsApi { @ApiParam(value = "Only ask own models") @RequestParam(value = "own", required = false) Boolean own, @ApiParam(value = "Only ask models from own team") @RequestParam(value = "team", required = false) Boolean team, Principal principal - ) throws NotFoundException { + ) { // Get current user User user = MIPApplication.getUser(principal); @@ -91,7 +91,7 @@ public class ModelsApi { public ResponseEntity<Void> addAModel( @RequestBody @ApiParam(value = "Model to create", required = true) Model model, Principal principal - ) throws NotFoundException { + ) { // Get current user User user = MIPApplication.getUser(principal); @@ -116,7 +116,7 @@ public class ModelsApi { @RequestMapping(value = "/{slug}.svg", produces = {"image/svg+xml"}, method = RequestMethod.GET) public ResponseEntity<String> getSVG( @ApiParam(value = "slug", required = true) @PathVariable("slug") String slug - ) throws NotFoundException { + ) { // Query DB Session session = HibernateUtil.getSessionFactory().getCurrentSession(); @@ -134,7 +134,7 @@ public class ModelsApi { @RequestMapping(value = "/{slug}", method = RequestMethod.GET) public ResponseEntity<Model> getAModel( @ApiParam(value = "slug", required = true) @PathVariable("slug") String slug - ) throws NotFoundException { + ) { // Query DB Session session = HibernateUtil.getSessionFactory().getCurrentSession(); @@ -207,7 +207,7 @@ public class ModelsApi { @ApiParam(value = "slug", required = true) @PathVariable("slug") String slug, @RequestBody @ApiParam(value = "Model to update", required = true) Model model, Principal principal - ) throws NotFoundException { + ) { // Get current user User user = MIPApplication.getUser(principal); @@ -228,7 +228,7 @@ public class ModelsApi { @ApiParam(value = "slug", required = true) @PathVariable("slug") String slug, @RequestBody @ApiParam(value = "Model to update", required = true) Model model, Principal principal - ) throws NotFoundException { + ) { // Get current user User user = MIPApplication.getUser(principal); @@ -265,7 +265,7 @@ public class ModelsApi { @RequestMapping(value = "/{slug}", method = RequestMethod.DELETE) public ResponseEntity<Void> deleteAModel( @ApiParam(value = "slug", required = true) @PathVariable("slug") String slug - ) throws NotFoundException { + ) { // TODO : Implement delete method diff --git a/src/main/java/org/hbp/mip/controllers/NotFoundException.java b/src/main/java/org/hbp/mip/controllers/NotFoundException.java deleted file mode 100644 index 770a417af9009ab1eddccc97c7335761376029a7..0000000000000000000000000000000000000000 --- a/src/main/java/org/hbp/mip/controllers/NotFoundException.java +++ /dev/null @@ -1,14 +0,0 @@ -/** - * Created by mirco on 04.12.15. - */ - -package org.hbp.mip.controllers; - -public class NotFoundException extends ApiException { - private int code; - - public NotFoundException(int code, String msg) { - super(code, msg); - this.code = code; - } -} diff --git a/src/main/java/org/hbp/mip/controllers/RequestsApi.java b/src/main/java/org/hbp/mip/controllers/RequestsApi.java index 471e74216ef982135b5e13627dd224089f518250..b892cb507ce91d2560f49a48baf9a5fc8dd7cfbb 100644 --- a/src/main/java/org/hbp/mip/controllers/RequestsApi.java +++ b/src/main/java/org/hbp/mip/controllers/RequestsApi.java @@ -28,7 +28,7 @@ public class RequestsApi { @RequestMapping(method = RequestMethod.POST) public ResponseEntity<Dataset> postRequests( @RequestBody @ApiParam(value = "Query to process", required = true) Query query - ) throws NotFoundException { + ) { Dataset dataset = CSVUtil.parseValues(DATA_FILE, query); diff --git a/src/main/java/org/hbp/mip/controllers/StatsApi.java b/src/main/java/org/hbp/mip/controllers/StatsApi.java index 74c6556930c41fe03cff9ab6b6f59dcecbb068aa..7138675522f8a0a27db614249ca8d7c6fa199392 100644 --- a/src/main/java/org/hbp/mip/controllers/StatsApi.java +++ b/src/main/java/org/hbp/mip/controllers/StatsApi.java @@ -25,7 +25,7 @@ public class StatsApi { @ApiOperation(value = "Get general statistics", response = GeneralStats.class) @ApiResponses(value = {@ApiResponse(code = 200, message = "Found"), @ApiResponse(code = 404, message = "Not found") }) @RequestMapping(method = RequestMethod.GET) - public ResponseEntity<GeneralStats> getGeneralStatistics() throws NotFoundException { + public ResponseEntity<GeneralStats> getGeneralStatistics() { GeneralStats stats = new GeneralStats(); Session session = HibernateUtil.getSessionFactory().getCurrentSession(); @@ -50,7 +50,7 @@ public class StatsApi { @RequestMapping(value = "/{code}", produces = { "application/json" }, method = RequestMethod.GET) public ResponseEntity<List<Statistics>> getTheStatisticsForAGroupOrAVariable( @ApiParam(value = "code of the group or variable",required=true ) @PathVariable("code") String code - ) throws NotFoundException { + ) { // TODO: Implement this method return new ResponseEntity<List<Statistics>>(HttpStatus.OK); } diff --git a/src/main/java/org/hbp/mip/controllers/UsersApi.java b/src/main/java/org/hbp/mip/controllers/UsersApi.java index 4f3602bda6fdb6a85fb0c0dc714a5e8d2cffd30f..63110bc83a27920ca76f48fa593dfee84572b430 100644 --- a/src/main/java/org/hbp/mip/controllers/UsersApi.java +++ b/src/main/java/org/hbp/mip/controllers/UsersApi.java @@ -27,7 +27,7 @@ public class UsersApi { @RequestMapping(value = "/{username}", method = RequestMethod.GET) public ResponseEntity<User> getAUser( @ApiParam(value = "username", required = true) @PathVariable("username") String username - ) throws NotFoundException { + ) { // Query DB Session session = HibernateUtil.getSessionFactory().getCurrentSession(); diff --git a/src/main/java/org/hbp/mip/controllers/VariablesApi.java b/src/main/java/org/hbp/mip/controllers/VariablesApi.java index f7fb7f2b1470cda6c2e998b4e59dab91b44ff396..a6d76a0f529e62a15d195b8fa4451074bdd82ede 100644 --- a/src/main/java/org/hbp/mip/controllers/VariablesApi.java +++ b/src/main/java/org/hbp/mip/controllers/VariablesApi.java @@ -36,7 +36,7 @@ public class VariablesApi { @ApiParam(value = "Boolean value formatted like : (\"0\") or (\"1\") or (\"false\") or (\"true\")") @RequestParam(value = "isGrouping", required = false) String isGrouping, @ApiParam(value = "Boolean value formatted like : (\"0\") or (\"1\") or (\"false\") or (\"true\")") @RequestParam(value = "isCovariable", required = false) String isCovariable, @ApiParam(value = "Boolean value formatted like : (\"0\") or (\"1\") or (\"false\") or (\"true\")") @RequestParam(value = "isFilter", required = false) String isFilter - ) throws NotFoundException { + ) { // Get variales from DB Session session = HibernateUtil.getSessionFactory().getCurrentSession(); @@ -74,7 +74,7 @@ public class VariablesApi { @RequestMapping(value = "/{code}", method = RequestMethod.GET) public ResponseEntity<Variable> getAVariable( @ApiParam(value = "code of the variable ( multiple codes are allowed, separated by \",\" )", required = true) @PathVariable("code") String code - ) throws NotFoundException { + ) { // Query DB Session session = HibernateUtil.getSessionFactory().getCurrentSession(); @@ -94,7 +94,7 @@ public class VariablesApi { public ResponseEntity<List<Value>> getValuesFromAVariable( @ApiParam(value = "code", required = true) @PathVariable("code") String code, @ApiParam(value = "Pattern to match") @RequestParam(value = "q", required = false) String q - ) throws NotFoundException { + ) { // Query DB Session session = HibernateUtil.getSessionFactory().getCurrentSession(); diff --git a/src/main/java/org/hbp/mip/utils/CORSFilter.java b/src/main/java/org/hbp/mip/utils/CORSFilter.java new file mode 100644 index 0000000000000000000000000000000000000000..30bea8de4bc436b3a9aa685424e89d7f6f7c92ab --- /dev/null +++ b/src/main/java/org/hbp/mip/utils/CORSFilter.java @@ -0,0 +1,24 @@ +package org.hbp.mip.utils; + +import javax.servlet.*; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +/** + * Created by mirco on 12.02.16. + */ +public class CORSFilter implements Filter { + + public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { + HttpServletResponse response = (HttpServletResponse) res; + response.setHeader("Access-Control-Allow-Origin", "*"); + response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE"); + response.setHeader("Access-Control-Max-Age", "3600"); + response.setHeader("Access-Control-Allow-Headers", "x-requested-with"); + chain.doFilter(req, res); + } + + public void init(FilterConfig filterConfig) {} + + public void destroy() {} +}