MetadataApi.java

package fr.metabocloud.core.api;

import com.github.dozermapper.core.DozerBeanMapperBuilder;
import fr.metabocloud.about.openapi.api.AboutApi;
import fr.metabocloud.about.openapi.model.*;
import fr.metabocloud.core.model.AppInfos;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RestController;

/**
 * Controller for the "/about" endpoint that provides metadata about the REST
 * API.
 *
 * <p>
 * The metadata provided includes general information about the API, its version,
 * associated documentation, as well as accessibility and ownership.
 * </p>
 *
 * @author Faustine Souc
 * @since 1.0.0
 */

@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
public class MetadataApi implements AboutApi {

    /**
     * Handle GET requests to retrieve metadata associated to the REST API.
     *
     * <p>
     * This method processes HTTP GET requests directed to the "/about"
     * endpoint. It formats metadata coming from the `src/main/resources/infos.properties` file
     * into JSON and return it.
     * </p>
     *
     * <pre>
     * {
     *   "main": {
     *     "name": "The name of the application",
     *     "short_description": "A short description of the project",
     *     "keywords": [
     *       "keyword 1",
     *       "keyword 2",
     *     ]
     *   },
     *   "documentation": {
     *     "long_description": "A longer, detailed description of the application",
     *     "doc_swagger": "The path to the Swagger API documentation endpoint",
     *     "doc_readthedocs": "The path to the ReadTheDocs documentation endpoint"
     *   },
     *   "version": {
     *     "status": "The current development status of the application",
     *     "version": "The version of the application",
     *     "short_commit_sha1": "The last Git commit short SHA-1 hash",
     *     "commit_sha1": "The last Git commit SHA-1 hash",
     *     "build_timestamp": "A timestamp indicating when the application build occurred (YYYY-MM-DD hh:mm:ss)"
     *   },
     *   "ownership_accessibility": {
     *     "license": "he name of the license under which the application is distributed",
     *     "authors_microservice_code": "The authors responsible for creating the algorithm or tool used in the application",
     *     "authors_algorithm_code": "The authors responsible for creating the microservice associated with the application",
     *     "is_public": "The accessibility level of the application",
     *     "url_repo_git": "The URL of the Git repository hosting the application's source code",
     *     "publications_ref": [
     *       "publication 1",
     *       "publication 2"
     *     ]
     *   }
     * }
     * </pre>
     *
     * @return A {@link ResponseEntity<About>} containing the metadata associated to the API.
     */

    @Override
    public ResponseEntity<About> getInformation() {
        // Build response structure
        final var information = new About();
        information.setMain(new AboutMain());
        information.setDocumentation(new AboutDocumentation());
        information.setVersion(new AboutVersion());
        information.setOwnershipAccessibility(new AboutOwnershipAccessibility());
        // Object with the information from the infos.properties file
        final var appInfo = new AppInfos();
        // Mapping
        final var mapper = DozerBeanMapperBuilder.buildDefault();
        final var main = mapper.map(appInfo, AboutMain.class);
        final var doc = mapper.map(appInfo, AboutDocumentation.class);
        final var version = mapper.map(appInfo, AboutVersion.class);
        final var ownAccess = mapper.map(appInfo, AboutOwnershipAccessibility.class);
        // Set up the response object
        information.setMain(main);
        information.setDocumentation(doc);
        information.setVersion(version);
        information.setOwnershipAccessibility(ownAccess);
        // Return
        return ResponseEntity.ok(information);
    }
}