Project

General

Profile

Download (5.77 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2016 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.cdm.remote.staticSwagger;
10

    
11
import java.io.IOException;
12
import java.io.InputStream;
13
import java.nio.charset.Charset;
14

    
15
import javax.servlet.http.HttpServletRequest;
16
import javax.servlet.http.HttpServletResponse;
17

    
18
import org.apache.commons.io.IOUtils;
19
import org.apache.logging.log4j.LogManager;
20
import org.apache.logging.log4j.Logger;
21
import org.springframework.stereotype.Controller;
22
import org.springframework.web.bind.annotation.PathVariable;
23
import org.springframework.web.bind.annotation.RequestMapping;
24
import org.springframework.web.bind.annotation.RequestMethod;
25
import org.springframework.web.bind.annotation.RequestParam;
26

    
27
import eu.etaxonomy.cdm.remote.config.SwaggerGroupsConfig;
28
import eu.etaxonomy.cdm.remote.controller.HttpStatusMessage;
29

    
30
/**
31
 * This controller replaces the dynamic swagger api doc endpoint which is
32
 * otherwise provided by springfox-swagger2. In contrast to the original controller
33
 * this implementation serves static json files which contain the api documentation.
34
 * <p>
35
 * The static api doc files are created by the integration test class <code>SwaggerStaticIT</code>.
36
 * <p>
37
 * For more details see {@link https://dev.e-taxonomy.eu/redmine/projects/edit/wiki/cdmlib-remote-webappConfigurationAndBootstrapping}
38
 *
39
 * @author a.kohlbecker
40
 * @since Feb 22, 2016
41
 */
42
@Controller
43
public class StaticSwaggerApiDoc {
44

    
45
    public static final Logger logger = LogManager.getLogger(StaticSwaggerApiDoc.class);
46

    
47
    public static final String SWAGGER_STATIC = "swagger-static";
48
    public static final String JSON = ".json";
49
    public static final String HOST = "{HOST}";
50
    public static final String HOST_REGEX = "\\{HOST\\}";
51
    public static final String BASE_PATH = "{BASE_PATH}";
52
    public static final String BASE_PATH_REGEX = "\\{BASE_PATH\\}";
53

    
54

    
55
    @RequestMapping(value = "configuration/ui", method = RequestMethod.GET)
56
    public void configurationUi(
57
            HttpServletResponse response) throws IOException {
58

    
59
            response.addHeader("Content-Type", "application/json;charset=utf-8");
60
            response.getOutputStream().write("{\"validatorUrl\":null}".getBytes());
61
    }
62

    
63
    @RequestMapping(value = "swagger-resources", method = RequestMethod.GET)
64
    public void swaggerResources(
65
             HttpServletRequest request,
66
            HttpServletResponse response) throws IOException {
67

    
68
        String resourceFile = SWAGGER_STATIC + "/swagger-resources" + JSON;
69
        InputStream staticDocStream = getClass().getClassLoader().getResourceAsStream(SWAGGER_STATIC + "/swagger-resources" + JSON);
70
        if(staticDocStream == null) {
71
            HttpStatusMessage.create("Static swagger recource file not found: " + resourceFile, 500).send(response);
72
        } else {
73
            response.addHeader("Content-Type", "application/json;charset=utf-8");
74
            IOUtils.copy(staticDocStream,  response.getOutputStream());
75
            staticDocStream.close();
76
        }
77
    }
78

    
79
    @RequestMapping(value = "swagger-resources/configuration/{filename}", method = RequestMethod.GET)
80
    public void swaggerResourcesConfiguration(
81
            @PathVariable("filename") String filename,
82
            HttpServletRequest request,
83
            HttpServletResponse response) throws IOException {
84

    
85

    
86
        String resourceFile = SWAGGER_STATIC + "/swagger-resources/configuration/" + filename + JSON;
87
        InputStream staticDocStream = getClass().getClassLoader().getResourceAsStream(resourceFile);
88
        if(staticDocStream == null) {
89
            HttpStatusMessage.create("Static swagger recource file not found: " + resourceFile, 500).send(response);
90
        } else {
91
            response.addHeader("Content-Type", "application/json;charset=utf-8");
92
            IOUtils.copy(staticDocStream,  response.getOutputStream());
93
            staticDocStream.close();
94
        }
95
    }
96

    
97
    @RequestMapping(value = "/v2/api-docs", method = RequestMethod.GET)
98
    public void apiDocs(
99
            @RequestParam(value = "group", required = true) String group,
100
             HttpServletRequest request,
101
            HttpServletResponse response) throws IOException {
102
        logger.debug("request scheme: " + request.getScheme());
103
        String hostValue = request.getServerName() + ":" + request.getServerPort();
104
        String basePathValue = request.getContextPath();
105

    
106
        SwaggerGroupsConfig groupConfig = SwaggerGroupsConfig.byGroupName(group);
107
        if(groupConfig == null) {
108
            HttpStatusMessage.create("Unknown swagger group name.", 400).send(response);
109
        } else {
110
            InputStream staticDocStream = getClass().getClassLoader().getResourceAsStream(SWAGGER_STATIC + "/api-docs/" + groupConfig.name() + JSON);
111
            if(staticDocStream == null) {
112
                HttpStatusMessage.create("Static swagger api doc file for group '" + group + "' not found.", 500).send(response);
113
            } else {
114
                response.addHeader("Content-Type", "application/json;charset=utf-8");
115
                Charset utf8 = Charset.forName("UTF-8");
116
                String staticDocText = IOUtils.toString(staticDocStream, utf8);
117
                logger.debug("staticDocStream read");
118
                staticDocText = staticDocText.replaceFirst(HOST_REGEX, hostValue);
119
                logger.debug("staticDocStream HOST_REGEX replaced");
120
                staticDocText = staticDocText.replaceFirst(BASE_PATH_REGEX, basePathValue);
121
                logger.debug("staticDocStream BASE_PATH_REGEX replaced");
122

    
123
                IOUtils.write(staticDocText, response.getOutputStream(), utf8);
124
                staticDocStream.close();
125
            }
126
        }
127
    }
128
}
    (1-1/1)