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
|
|
14
|
import javax.servlet.http.HttpServletRequest;
|
15
|
import javax.servlet.http.HttpServletResponse;
|
16
|
|
17
|
import org.apache.commons.io.IOUtils;
|
18
|
import org.springframework.stereotype.Controller;
|
19
|
import org.springframework.web.bind.annotation.RequestMapping;
|
20
|
import org.springframework.web.bind.annotation.RequestMethod;
|
21
|
import org.springframework.web.bind.annotation.RequestParam;
|
22
|
|
23
|
import eu.etaxonomy.cdm.remote.config.SwaggerGroupsConfig;
|
24
|
import eu.etaxonomy.cdm.remote.controller.HttpStatusMessage;
|
25
|
|
26
|
/**
|
27
|
* This controller replaces the dynamic swagger api doc endpoint which is
|
28
|
* otherwise provided by springfox-swagger2. In contrast to the original controller
|
29
|
* this implementation serves static json files which contain the api documentation.
|
30
|
* <p>
|
31
|
* The static api doc files are created by the integration test class <code>SwaggerGroupsIT</code>.
|
32
|
* <p>
|
33
|
* For more details see {@link http://dev.e-taxonomy.eu/trac/wiki/cdmlib-remote-webappConfigurationAndBootstrapping}
|
34
|
*
|
35
|
* @author a.kohlbecker
|
36
|
* @date Feb 22, 2016
|
37
|
*
|
38
|
*/
|
39
|
|
40
|
@Controller
|
41
|
public class StaticSwaggerApiDoc {
|
42
|
|
43
|
public static final String SWAGGER_STATIC = "swagger-static";
|
44
|
public static final String HOST = "{HOST}";
|
45
|
public static final String HOST_REGEX = "\\{HOST\\}";
|
46
|
public static final String BASE_PATH = "{BASE_PATH}";
|
47
|
public static final String BASE_PATH_REGEX = "\\{BASE_PATH\\}";
|
48
|
|
49
|
|
50
|
@RequestMapping(value = "configuration/ui", method = RequestMethod.GET)
|
51
|
public void configurationUi(
|
52
|
HttpServletResponse response) throws IOException {
|
53
|
|
54
|
response.addHeader("Content-Type", "application/json;charset=utf-8");
|
55
|
response.getOutputStream().write("{\"validatorUrl\":null}".getBytes());
|
56
|
}
|
57
|
|
58
|
@RequestMapping(value = "swagger-resources", method = RequestMethod.GET)
|
59
|
public void swaggerResources(
|
60
|
HttpServletRequest request,
|
61
|
HttpServletResponse response) throws IOException {
|
62
|
|
63
|
|
64
|
InputStream staticDocStream = getClass().getClassLoader().getResourceAsStream(SWAGGER_STATIC + "/swagger-resources");
|
65
|
if(staticDocStream == null) {
|
66
|
HttpStatusMessage.create("Static swagger-resources not found.", 500).send(response);
|
67
|
} else {
|
68
|
response.addHeader("Content-Type", "application/json;charset=utf-8");
|
69
|
IOUtils.copy(staticDocStream, response.getOutputStream());
|
70
|
staticDocStream.close();
|
71
|
}
|
72
|
}
|
73
|
|
74
|
@RequestMapping(value = "/v2/api-docs", method = RequestMethod.GET)
|
75
|
public void apiDocs(
|
76
|
@RequestParam(value = "group", required = true) String group,
|
77
|
HttpServletRequest request,
|
78
|
HttpServletResponse response) throws IOException {
|
79
|
|
80
|
String hostValue = request.getServerName() + ":" + request.getServerPort();
|
81
|
String basePathValue = request.getContextPath();
|
82
|
|
83
|
SwaggerGroupsConfig groupConfig = SwaggerGroupsConfig.byGroupName(group);
|
84
|
if(groupConfig == null) {
|
85
|
HttpStatusMessage.create("Unknown swagger group name.", 400).send(response);
|
86
|
}
|
87
|
InputStream staticDocStream = getClass().getClassLoader().getResourceAsStream(SWAGGER_STATIC + "/api-docs/" + groupConfig.name());
|
88
|
if(staticDocStream == null) {
|
89
|
HttpStatusMessage.create("Static swagger api doc file for group '" + group + "' not found.", 500).send(response);
|
90
|
} else {
|
91
|
response.addHeader("Content-Type", "application/json;charset=utf-8");
|
92
|
String staticDocText = IOUtils.toString(staticDocStream);
|
93
|
staticDocText = staticDocText.replaceFirst(HOST_REGEX, hostValue);
|
94
|
staticDocText = staticDocText.replaceFirst(BASE_PATH_REGEX, basePathValue);
|
95
|
|
96
|
IOUtils.write(staticDocText, response.getOutputStream());
|
97
|
staticDocStream.close();
|
98
|
}
|
99
|
}
|
100
|
|
101
|
}
|