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.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
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
72
73
74
75
76
77
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
115
116
117
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
127
128
129
130
131
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 }