View Javadoc
1   /*
2    * Copyright (c) 2002-2017, 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.transparency.rs;
35  
36  import fr.paris.lutece.plugins.rest.service.RestConstants;
37  import fr.paris.lutece.portal.service.util.AppPathService;
38  import fr.paris.lutece.util.json.ErrorJsonResponse;
39  import fr.paris.lutece.util.json.JsonUtil;
40  
41  import java.io.File;
42  import java.io.IOException;
43  import java.net.MalformedURLException;
44  import java.net.URL;
45  import java.util.HashMap;
46  import java.util.Map;
47  import javax.servlet.http.HttpServletRequest;
48  import javax.ws.rs.GET;
49  import javax.ws.rs.Path;
50  import javax.ws.rs.PathParam;
51  import javax.ws.rs.Produces;
52  import javax.ws.rs.core.Context;
53  import javax.ws.rs.core.MediaType;
54  import javax.ws.rs.core.Response;
55  import org.apache.log4j.Logger;
56  import org.codehaus.jackson.map.ObjectMapper;
57  import org.codehaus.jackson.node.ObjectNode;
58  
59  /**
60   * SwaggerRest
61   */
62  @Path( RestConstants.BASE_PATH + Constants.API_PATH + Constants.VERSION_PATH )
63  public class SwaggerRest
64  {
65      private final Logger _logger = Logger.getLogger( RestConstants.REST_LOGGER );
66      private final static String BASE_INFOS_SCHEMES = "schemes";
67      private final static String BASE_INFOS_HOST = "host";
68      private final static String BASE_INFOS_BASE_PATH = "basePath";
69  
70      /**
71       * Get Swagger.json
72       * 
73       * @param request
74       * @param strVersion
75       * @return the swagger.json
76       * @throws java.net.MalformedURLException
77       * @throws java.io.IOException
78       */
79      @GET
80      @Path( Constants.SWAGGER_PATH )
81      @Produces( MediaType.APPLICATION_JSON )
82      public Response getSwagger( @Context HttpServletRequest request, @PathParam( Constants.VERSION ) String strVersion ) throws MalformedURLException,
83              IOException
84      {
85          File fileJson = new File( getJsonFilePath( strVersion ) );
86          if ( fileJson.exists( ) )
87          {
88              Map<String, String> mapBaseInfos = getBaseInfos( AppPathService.getBaseUrl( request ), strVersion );
89  
90              ObjectMapper mapper = new ObjectMapper( );
91              ObjectNode objectNode = mapper.readValue( fileJson, ObjectNode.class );
92  
93              if ( objectNode.path( BASE_INFOS_HOST ).isMissingNode( ) )
94              {
95                  objectNode.put( BASE_INFOS_HOST, mapBaseInfos.get( BASE_INFOS_HOST ) );
96              }
97              if ( objectNode.path( BASE_INFOS_SCHEMES ).isMissingNode( ) )
98              {
99                  objectNode.putArray( BASE_INFOS_SCHEMES ).add( mapBaseInfos.get( BASE_INFOS_SCHEMES ) );
100             }
101             if ( objectNode.path( BASE_INFOS_BASE_PATH ).isMissingNode( ) )
102             {
103                 objectNode.put( BASE_INFOS_BASE_PATH, mapBaseInfos.get( BASE_INFOS_BASE_PATH ) );
104             }
105             String strSwaggerJson = mapper.writerWithDefaultPrettyPrinter( ).writeValueAsString( objectNode );
106             return Response.status( Response.Status.OK ).entity( strSwaggerJson ).build( );
107         }
108         _logger.error( Constants.ERROR_NOT_FOUND_RESOURCE );
109         return Response.status( Response.Status.NOT_FOUND )
110                 .entity( JsonUtil.buildJsonResponse( new ErrorJsonResponse( Response.Status.NOT_FOUND.name( ), Constants.ERROR_NOT_FOUND_RESOURCE ) ) ).build( );
111     }
112 
113     /**
114      * Get the swagger.json file path
115      * 
116      * @param strVersion
117      * @return
118      */
119     private String getJsonFilePath( String strVersion )
120     {
121         return AppPathService.getWebAppPath( ) + Constants.SWAGGER_DIRECTORY_PATH + Constants.API_PATH + Constants.SWAGGER_PATH
122                 + Constants.SWAGGER_VERSION_PATH + strVersion + Constants.SWAGGER_JSON;
123     }
124 
125     /**
126      * Get the base informations (host, scheme, baseUrl)
127      * 
128      * @param strBaseUrl
129      * @param strVersion
130      * @return
131      * @throws MalformedURLException
132      */
133     private Map<String, String> getBaseInfos( String strBaseUrl, String strVersion ) throws MalformedURLException
134     {
135         Map<String, String> map = new HashMap<>( );
136         URL url = new URL( strBaseUrl );
137 
138         String strScheme = url.getProtocol( );
139         String strHost = url.getHost( );
140         String strBasePath = url.getPath( );
141         int nPort = url.getPort( );
142 
143         if ( nPort != -1 )
144         {
145             strHost += ":" + nPort;
146         }
147 
148         strBasePath = strBasePath + Constants.SWAGGER_REST_PATH + Constants.API_PATH + Constants.SWAGGER_VERSION_PATH + strVersion;
149 
150         map.put( BASE_INFOS_SCHEMES, strScheme );
151         map.put( BASE_INFOS_HOST, strHost );
152         map.put( BASE_INFOS_BASE_PATH, strBasePath );
153 
154         return map;
155     }
156 }