// $Id$ /** * Copyright (C) 2014 EDIT * European Distributed Institute of Taxonomy * http://www.e-taxonomy.eu * * The contents of this file are subject to the Mozilla Public License Version 1.1 * See LICENSE.TXT at the top of this package for the full license terms. */ package eu.etaxonomy.cdm.remote.config; import java.util.Collection; import javax.persistence.Entity; import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.type.filter.AnnotationTypeFilter; import com.mangofactory.swagger.configuration.SpringSwaggerConfig; import com.mangofactory.swagger.plugin.SwaggerSpringMvcPlugin; import com.wordnik.swagger.model.ApiInfo; import eu.etaxonomy.cdm.model.CdmAssignableTypeFilter; import eu.etaxonomy.cdm.model.CdmTypeScanner; import eu.etaxonomy.cdm.model.common.CdmBase; /** *

NOTE:

* For a detailed overview on the spring MVC and application context configuration and * bootstrapping of this web application see: * {@link http://dev.e-taxonomy.eu/trac/wiki/cdmlib-remote-webappConfigurationAndBootstrapping} * * */ @Configuration public class CdmSwaggerConfig { private static final String LSID_AUTHORITY_SERVICES = "LSID authority services"; private static final String DATA_EXPORT = "Data export"; private static final String GENERIC_REST_API = "Generic REST API"; private static final String WEB_PORTAL_SERVICES = "Web Portal Services"; private static final String CATALOGUE_SERVICES = "Catalogue Services"; public static final Logger logger = Logger.getLogger(CdmSwaggerConfig.class); private SpringSwaggerConfig springSwaggerConfig; Collection> allCdmTypes = null; public CdmSwaggerConfig() { super(); logger.debug("contructor"); } /** * Required to autowire SpringSwaggerConfig */ @Autowired public void setSpringSwaggerConfig(SpringSwaggerConfig springSwaggerConfig) { this.springSwaggerConfig = springSwaggerConfig; logger.debug("setSpringSwaggerConfig"); } /** * Every SwaggerSpringMvcPlugin bean is picked up by the swagger-mvc framework - allowing for multiple * swagger groups i.e. same code base multiple swagger resource listings. */ // @Bean(name="swaggerPluginDefault") @Bean(name="swaggerPluginGenericAPI") public SwaggerSpringMvcPlugin swaggerPluginGenericAPI(){ logger.debug("swaggerSpringMvcPlugin"); configureModelConverters(); return new SwaggerSpringMvcPlugin(this.springSwaggerConfig) .apiInfo(apiInfo(GENERIC_REST_API, "" + "

The CDM REST API is a RESTful interface to resources stored in the CDM." + " The RESTful architecture allows accessing the various resources like Taxa, " + "Names, References, Media, etc by stable URIs. Due to security constraints " + "and to assure the integration of data, currently only read operations " + "(= HTTP GET) are permitted, write operations may be available in the future.

" + "

The architecture directly exposes domain model entities, i.e. it provides " + "direct serializations of the objects as they are stored in the CDM but also " + "returns TDOs of the CDM entities in some cases. The API Service provides an easy to use way " + "to transfer CDM data to web based clients." + "

" + "

For more information like usage of this service please refer to " + "http://cybertaxonomy.eu/cdmlib/rest-api.html")) // below regex excludes the paths of all other groups, // !!! also hide the manage and progress controller .includePatterns("/(?!portal/)(?!taxon/oai)(?!reference/oai)(?!name_catalogue/)(?!authority/)(?!csv/)(?!checklist)(?!manage/)(?!progress/).*") .ignoredParameterTypes(allCdmTpyes()) .swaggerGroup(GENERIC_REST_API) // .excludeAnnotations(Deprecated.class) // TODO ; } @Bean(name="swaggerPluginPortal") public SwaggerSpringMvcPlugin swaggerPluginPortal(){ logger.debug("swaggerSpringMvcPlugin"); configureModelConverters(); return new SwaggerSpringMvcPlugin(this.springSwaggerConfig) .apiInfo(apiInfo(WEB_PORTAL_SERVICES, "

The Portal Service is a specialization to the " + "Generic REST API as needed by CDM Dataportal " + " that adds some fields like localized representations to the pure CDM entities. Another important difference " + " is the initialization depth of the CDM entities. The Portal Service enpoints provide far bigger parts of the " + " object graph.

")) .includePatterns("/portal/.*") .ignoredParameterTypes(allCdmTpyes()) .swaggerGroup(WEB_PORTAL_SERVICES); } @Bean(name="swaggerPluginNameCatalogue") public SwaggerSpringMvcPlugin swaggerPluginNameCatalogue(){ configureModelConverters(); return new SwaggerSpringMvcPlugin(this.springSwaggerConfig) .apiInfo(apiInfo( CATALOGUE_SERVICES, "

These web services are optimized for using names taxonomic information and occurence data in workflow environments " + " but are suitabale for all applicatoins in which fast response times are crucial." + " Additional detailed documentation of these services can also be found at:

" + "" )) .includePatterns("/name_catalogue.*", "/occurrence_catalogue.*") .ignoredParameterTypes(allCdmTpyes()) .swaggerGroup(CATALOGUE_SERVICES); } @Bean(name="swaggerPluginOAIPMH") public SwaggerSpringMvcPlugin swaggerPluginOAIPMH(){ logger.debug("swaggerSpringMvcPlugin"); configureModelConverters(); return new SwaggerSpringMvcPlugin(this.springSwaggerConfig) .apiInfo(apiInfo( "OAI-PMH", "

This is an automatcially created documentation on the OAI-PMH service which is atually dedicated to REST services." + " Since OAI-PMH is not a REST service in the original sense, you may want to refer to" + " the more specific OAI-PMH documentation or" + " to the service endpoints them self, which also provide a good, comprehensive and selfexplanatory" + " level of documentation:

" + "")) .includePatterns("/reference/oai.*", "/taxon/oai.*") .ignoredParameterTypes(allCdmTpyes()) .swaggerGroup("OAI-PMH"); } @Bean(name="swaggerPluginLSID") public SwaggerSpringMvcPlugin swaggerPluginLSID(){ logger.debug("swaggerSpringMvcPlugin"); configureModelConverters(); return new SwaggerSpringMvcPlugin(this.springSwaggerConfig) .apiInfo(apiInfo(LSID_AUTHORITY_SERVICES, "")) .includePatterns("/authority/.*") .ignoredParameterTypes(allCdmTpyes()) .swaggerGroup(LSID_AUTHORITY_SERVICES); } @Bean(name="swaggerPluginDataExport") public SwaggerSpringMvcPlugin swaggerPluginDataExport(){ logger.debug("swaggerSpringMvcPlugin"); configureModelConverters(); return new SwaggerSpringMvcPlugin(this.springSwaggerConfig) .apiInfo(apiInfo(DATA_EXPORT, "")) .includePatterns("/csv/.*", "/checklist.*") .ignoredParameterTypes(allCdmTpyes()) .swaggerGroup(DATA_EXPORT); } /** * */ private void configureModelConverters() { // fully skip the creation of cdm model documentation // since it will be too excessive to scan the huge cdm model // which in fact has cycles. // not sure if this is working !!! // String emptyJSON = "{}"; // OverrideConverter sessionConverter = new OverrideConverter(); // sessionConverter.add(Session.class.getName(), emptyJSON); // ModelConverters.addConverter(sessionConverter, true); } //TODO failes to convert json to model // "org.json4s.package$MappingException: Did not find value // which can be converted into java.lang.String" /** * @return */ private Class>[] allCdmTpyes() { if (allCdmTypes == null) { allCdmTypes = allCdmTypes(); allCdmTypes.add(eu.etaxonomy.cdm.api.service.pager.Pager.class); allCdmTypes.add(eu.etaxonomy.cdm.api.facade.DerivedUnitFacade.class); } return allCdmTypes.toArray(new Class[allCdmTypes.size()]); } /** * @return */ private Collection> allCdmTypes() { boolean includeAbstract = true; boolean includeInterfaces = false; Collection> classes = null; CdmTypeScanner scanner = new CdmTypeScanner(includeAbstract, includeInterfaces); scanner.addIncludeFilter(new AnnotationTypeFilter(Entity.class)); scanner.addIncludeFilter(new CdmAssignableTypeFilter(CdmBase.class, includeAbstract, includeInterfaces)); classes = scanner.scanTypesIn("eu/etaxonomy/cdm/model"); return classes; } private ApiInfo apiInfo(String title, String description) { ApiInfo apiInfo = new ApiInfo( title, description, null, // terms of service "EditSupport@bgbm.org", "Mozilla Public License 2.0", "http://www.mozilla.org/MPL/2.0/" ); return apiInfo; } }