View Javadoc
1   /*
2    * Copyright (c) 2002-2016, Mairie de Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
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   * SwaggerServlet serve swagger files with dynamic host and base path
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       * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
69       *
70       * @param request
71       *            servlet request
72       * @param response
73       *            servlet response
74       * @throws ServletException
75       *             the servlet Exception
76       * @throws IOException
77       *             the io exception
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      * Read a File and returns its content as a string.
111      * @param strFilePath The file path
112      * @param encoding The encoding
113      * @return The file content
114      * @throws IOException If an error occurs
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      * Handles the HTTP <code>GET</code> method.
124      *
125      * @param request
126      *            servlet request
127      * @param response
128      *            servlet response
129      * @throws ServletException
130      *             the servlet Exception
131      * @throws IOException
132      *             the io exception
133      */
134     @Override
135     protected void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException
136     {
137         processRequest( request, response );
138     }
139 
140     /**
141      * Handles the HTTP <code>POST</code> method.
142      *
143      * @param request
144      *            servlet request
145      * @param response
146      *            servlet response
147      * @throws ServletException
148      *             the servlet Exception
149      * @throws IOException
150      *             the io exception
151      */
152     @Override
153     protected void doPost( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException
154     {
155         processRequest( request, response );
156     }
157 
158     /**
159      * Returns a short description of the servlet.
160      *
161      * @return message
162      */
163     @Override
164     public String getServletInfo( )
165     {
166         return "Servlet serving swagger files with dynamic host and basepath";
167     }
168 
169 }