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.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
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
73
74
75
76
77
78
79
80
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
118
119
120
121
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
131
132
133
134
135
136
137
138
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 }