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