1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.plugins.swaggerui.web;
35
36 import fr.paris.lutece.plugins.swaggerui.service.SwaggerFileService;
37 import fr.paris.lutece.portal.service.template.AppTemplateService;
38 import fr.paris.lutece.portal.service.util.AppPathService;
39 import fr.paris.lutece.util.html.HtmlTemplate;
40 import java.io.IOException;
41 import java.io.OutputStream;
42 import java.nio.charset.Charset;
43 import java.nio.charset.StandardCharsets;
44 import java.nio.file.Files;
45 import java.nio.file.Paths;
46 import java.util.concurrent.ConcurrentHashMap;
47 import java.util.Locale;
48 import java.util.Map;
49 import javax.servlet.ServletException;
50 import javax.servlet.http.HttpServlet;
51 import javax.servlet.http.HttpServletRequest;
52 import javax.servlet.http.HttpServletResponse;
53
54
55
56
57 public class SwaggerServlet extends HttpServlet
58 {
59
60 private static final long serialVersionUID = -5713203328367191908L;
61 private static final String CONTENT_TYPE_JSON = "application/json";
62 private static final String CONTENT_TYPE_YAML = "application/x-yaml";
63 private static final String MARK_HOST = "host";
64 private static final String MARK_PORT = "port";
65 private static final String MARK_CONTEXT = "context";
66
67
68
69
70
71
72
73
74
75
76
77
78
79 protected void processRequest( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException
80 {
81 String strPathInfo = request.getPathInfo();
82 int nPos = strPathInfo.indexOf( "/plugins" );
83 String strFile = strPathInfo.substring( nPos );
84 String strFileUrl = AppPathService.getAbsolutePathFromRelativePath( strFile );
85 String strFileContent = readFile( strFileUrl, StandardCharsets.UTF_8 );
86
87 Map<String, String> model = new ConcurrentHashMap<String, String>( );
88 model.put( MARK_HOST, request.getServerName( ) );
89 model.put( MARK_PORT, String.valueOf( request.getServerPort( ) ) );
90 model.put( MARK_CONTEXT, request.getContextPath( ) );
91
92 HtmlTemplate template = AppTemplateService.getTemplateFromStringFtl( strFileContent, Locale.getDefault( ), model );
93 String strNewContent = template.getHtml( );
94
95 String strContentType = CONTENT_TYPE_JSON;
96 if( strFile.endsWith( SwaggerFileService.EXT_YAML))
97 {
98 strContentType = CONTENT_TYPE_YAML;
99 }
100 response.setContentType( strContentType );
101
102 OutputStream out = response.getOutputStream( );
103 out.write( strNewContent.getBytes( StandardCharsets.UTF_8 ) );
104 out.flush( );
105 out.close( );
106
107 }
108
109
110
111
112
113
114
115
116 private static String readFile( String strFilePath, Charset encoding ) throws IOException
117 {
118 byte [ ] encoded = Files.readAllBytes( Paths.get( strFilePath ) );
119 return new String( encoded, encoding );
120 }
121
122
123
124
125
126
127
128
129
130
131
132
133
134 @Override
135 protected void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException
136 {
137 processRequest( request, response );
138 }
139
140
141
142
143
144
145
146
147
148
149
150
151
152 @Override
153 protected void doPost( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException
154 {
155 processRequest( request, response );
156 }
157
158
159
160
161
162
163 @Override
164 public String getServletInfo( )
165 {
166 return "Servlet serving swagger files with dynamic host and basepath";
167 }
168
169 }