-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
387 additions
and
1 deletion.
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
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
163 changes: 163 additions & 0 deletions
163
src/main/java/uk/ac/ebi/ega/accession/configuration/SwaggerConfiguration.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,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(); | ||
} | ||
|
||
} | ||
|
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
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
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
57 changes: 57 additions & 0 deletions
57
src/main/java/uk/ac/ebi/ega/accession/properties/SwaggerApiInfoContact.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 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; | ||
} | ||
} |
Oops, something went wrong.