Skip to content

Commit

Permalink
Added springfox to project
Browse files Browse the repository at this point in the history
  • Loading branch information
jorizci committed Aug 31, 2018
1 parent 8644447 commit fbe3c74
Show file tree
Hide file tree
Showing 17 changed files with 387 additions and 1 deletion.
16 changes: 16 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,22 @@
<artifactId>postgresql</artifactId>
<version>9.4-1206-jdbc42</version>
</dependency>
<!-- documentation -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-bean-validators</artifactId>
<version>2.9.2</version>
</dependency>
</dependencies>

<repositories>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package uk.ac.ebi.ega.accession.analysis.rest;

import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import uk.ac.ebi.ampt2d.commons.accession.core.AccessioningService;
Expand All @@ -25,6 +26,7 @@

@RestController
@RequestMapping(value = "/v1/analysis")
@Api(tags = "Analysis")
public class AnalysisRestController extends BasicRestController<AnalysisDTO, Analysis, String, String> {

public AnalysisRestController(AccessioningService<Analysis, String, String> analysisAccessionService) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package uk.ac.ebi.ega.accession.array.rest;

import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import uk.ac.ebi.ampt2d.commons.accession.core.AccessioningService;
Expand All @@ -25,6 +26,7 @@

@RestController
@RequestMapping(value = "/v1/array")
@Api(tags = "Array")
public class ArrayRestController extends BasicRestController<ArrayDTO, Array, String, String> {

public ArrayRestController(AccessioningService<Array, String, String> arrayAccessionService) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
/*
*
* Copyright 2018 EMBL - European Bioinformatics Institute
*
* 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 uk.ac.ebi.ega.accession.configuration;

import com.fasterxml.classmate.TypeResolver;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.request.async.DeferredResult;
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.ResponseMessageBuilder;
import springfox.documentation.schema.AlternateTypeRule;
import springfox.documentation.schema.WildcardType;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.ResponseMessage;
import springfox.documentation.service.Tag;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger.web.UiConfiguration;
import springfox.documentation.swagger.web.UiConfigurationBuilder;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import uk.ac.ebi.ega.accession.properties.SwaggerApiInfoProperties;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;

import static springfox.documentation.schema.AlternateTypeRules.newRule;

@Configuration
@EnableSwagger2
@EnableConfigurationProperties(SwaggerApiInfoProperties.class)
@Import({BeanValidatorPluginsConfiguration.class})
public class SwaggerConfiguration {

@Autowired
private TypeResolver typeResolver;

@Autowired
private SwaggerApiInfoProperties swaggerApiInfoProperties;

@Bean
public Docket metadataApi() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(getScanRestServicesPathPredicate())
.build()
.apiInfo(getApiInfo())
.pathMapping("/")
.tags(
new Tag("Analysis", "Analysis Rest Controller"),
new Tag("Array", "Array Rest Controller"),
new Tag("Dac", "Dac Rest Controller"),
new Tag("Dataset", "Dataset Rest Controller"),
new Tag("Experiment", "Experiment Rest Controller"),
new Tag("File", "File Rest Controller"),
new Tag("Policy", "Policy Rest Controller"),
new Tag("Run", "Run Rest Controller"),
new Tag("Sample controlled", "Sample (Controlled) Rest Controller"),
new Tag("Sample open access", "Sample (Open Access) Rest Controller"),
new Tag("Study", "Study Rest Controller")

)
.globalResponseMessage(RequestMethod.POST, getResponseMessagesForPostAndPatch())
.globalResponseMessage(RequestMethod.PATCH, getResponseMessagesForPostAndPatch())
.directModelSubstitute(LocalDate.class, String.class)
.genericModelSubstitutes(ResponseEntity.class)
.alternateTypeRules(getSubstitutionRules());
}

private List<ResponseMessage> getResponseMessagesForPostAndPatch() {
return Arrays.asList(new ResponseMessageBuilder()
.code(201)
.message("Created")
.build(),
new ResponseMessageBuilder()
.code(400)
.message("Validation error")
.build(),
new ResponseMessageBuilder()
.code(401)
.message("Unauthorized")
.build(),
new ResponseMessageBuilder()
.code(403)
.message("Forbidden")
.build(),
new ResponseMessageBuilder()
.code(404)
.message("Not Found")
.build());
}

private Predicate<String> getScanRestServicesPathPredicate() {
return Predicates.and(
Predicates.not(PathSelectors.regex("/actuator.*")), // Hide spring-actuator
Predicates.not(PathSelectors.regex("/error.*")), // Hide spring-data error
Predicates.not(PathSelectors.regex("/profile.*")) // Hide spring-data profile
);
}

private ApiInfo getApiInfo() {
return new ApiInfoBuilder()
.contact(new Contact(swaggerApiInfoProperties.getContact().getName(),
swaggerApiInfoProperties.getContact().getUrl(),
swaggerApiInfoProperties.getContact().getEmail()))
.license(swaggerApiInfoProperties.getLicense())
.licenseUrl(swaggerApiInfoProperties.getLicenseUrl())
.termsOfServiceUrl(swaggerApiInfoProperties.getTermsOfServiceUrl())
.title(swaggerApiInfoProperties.getTitle())
.description(swaggerApiInfoProperties.getDescription())
.version(swaggerApiInfoProperties.getVersion())
.build();
}

private AlternateTypeRule[] getSubstitutionRules() {
AlternateTypeRule[] alternateTypeRules = new AlternateTypeRule[2];
alternateTypeRules[0] = newRule(typeResolver.resolve(DeferredResult.class,
typeResolver.resolve(ResponseEntity.class, WildcardType.class)),
typeResolver.resolve(WildcardType.class));
alternateTypeRules[1] = newRule(typeResolver.resolve(Iterable.class, WildcardType.class),
typeResolver.resolve(List.class, WildcardType.class));
return alternateTypeRules;
}

@Bean
UiConfiguration uiConfig() {
return UiConfigurationBuilder.builder()
.deepLinking(true)
.displayRequestDuration(true)
.filter(false)
.validatorUrl("")
.build();
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package uk.ac.ebi.ega.accession.dac.rest;

import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import uk.ac.ebi.ampt2d.commons.accession.core.AccessioningService;
Expand All @@ -25,6 +26,7 @@

@RestController
@RequestMapping(value = "/v1/dac")
@Api(tags = "Dac")
public class DacRestController extends BasicRestController<DacDTO, Dac, String, String> {

public DacRestController(AccessioningService<Dac, String, String> dacAccessionService) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package uk.ac.ebi.ega.accession.dataset.rest;

import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import uk.ac.ebi.ampt2d.commons.accession.core.AccessioningService;
Expand All @@ -25,6 +26,7 @@

@RestController
@RequestMapping(value = "/v1/dataset")
@Api(tags = "Dataset")
public class DatasetRestController extends BasicRestController<DatasetDTO, Dataset, String, String> {

public DatasetRestController(AccessioningService<Dataset, String, String> runAccessionService) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package uk.ac.ebi.ega.accession.experiment.rest;

import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import uk.ac.ebi.ampt2d.commons.accession.core.AccessioningService;
Expand All @@ -25,6 +26,7 @@

@RestController
@RequestMapping(value = "/v1/experiment")
@Api(tags = "Experiment")
public class ExperimentRestController extends BasicRestController<ExperimentDTO, Experiment, String, String> {

public ExperimentRestController(AccessioningService<Experiment, String, String> runAccessionService) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package uk.ac.ebi.ega.accession.file.rest;

import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import uk.ac.ebi.ampt2d.commons.accession.core.AccessioningService;
Expand All @@ -25,6 +26,7 @@

@RestController
@RequestMapping(value = "/v1/file")
@Api(tags = "File")
public class FileRestController extends BasicRestController<FileDTO, FileModel, String, String> {

public FileRestController(AccessioningService<FileModel, String, String> fileAccessionService) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package uk.ac.ebi.ega.accession.policy.rest;

import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import uk.ac.ebi.ampt2d.commons.accession.core.AccessioningService;
Expand All @@ -25,6 +26,7 @@

@RestController
@RequestMapping(value = "/v1/policy")
@Api(tags = "Policy")
public class PolicyRestController extends BasicRestController<PolicyDTO, Policy, String, String> {

public PolicyRestController(AccessioningService<Policy, String, String> policyAccessionService) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
*
* Copyright 2018 EMBL - European Bioinformatics Institute
*
* 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 uk.ac.ebi.ega.accession.properties;

import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.URL;

public class SwaggerApiInfoContact {

private String name;

@URL(message="Contact URL is not valid.")
private String url;

@Email(message="Contact email address is not valid.")
private String email;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}
}
Loading

0 comments on commit fbe3c74

Please sign in to comment.