33import io .swagger .annotations .Api ;
44import io .swagger .annotations .ApiOperation ;
55import io .swagger .annotations .ApiParam ;
6+ import io .swagger .annotations .ApiResponse ;
7+ import io .swagger .annotations .ApiResponses ;
68import io .swagger .codegen .CliOption ;
79import io .swagger .codegen .Codegen ;
810import io .swagger .codegen .CodegenConfig ;
911import io .swagger .codegen .CodegenType ;
1012import io .swagger .codegen .utils .SecureFileUtils ;
13+ import io .swagger .generator .exception .ApiException ;
1114import io .swagger .generator .exception .BadRequestException ;
1215import io .swagger .generator .model .Generated ;
1316import io .swagger .generator .model .GeneratorInput ;
1417import io .swagger .generator .model .ResponseCode ;
1518import io .swagger .generator .online .Generator ;
1619import org .apache .commons .io .FileUtils ;
1720import org .apache .commons .lang3 .StringUtils ;
21+ import org .slf4j .Logger ;
22+ import org .slf4j .LoggerFactory ;
1823
1924import javax .servlet .http .HttpServletRequest ;
20- import javax .ws .rs .*;
25+ import javax .ws .rs .GET ;
26+ import javax .ws .rs .POST ;
27+ import javax .ws .rs .Path ;
28+ import javax .ws .rs .PathParam ;
29+ import javax .ws .rs .Produces ;
2130import javax .ws .rs .core .Context ;
2231import javax .ws .rs .core .MediaType ;
2332import javax .ws .rs .core .Response ;
2433import java .io .File ;
25- import java .util .*;
34+ import java .io .IOException ;
35+ import java .util .ArrayList ;
36+ import java .util .HashMap ;
37+ import java .util .List ;
38+ import java .util .Map ;
39+ import java .util .UUID ;
2640
2741@ Path ("/gen" )
28- @ Api (value = "/gen" , description = "Resource for generating swagger components" )
42+ @ Api (value = "/gen" )
2943@ SuppressWarnings ("static-method" )
3044public class SwaggerResource {
31- static List <String > clients = new ArrayList <String >();
32- static List <String > servers = new ArrayList <String >();
33- private static Map <String , Generated > fileMap = new HashMap <String , Generated >();
45+ private static final Logger LOGGER = LoggerFactory .getLogger (SwaggerResource .class );
46+
47+ static List <String > clients = new ArrayList <>();
48+ static List <String > servers = new ArrayList <>();
49+ private static Map <String , Generated > fileMap = new HashMap <>();
3450
3551 static {
3652 List <CodegenConfig > extensions = Codegen .getExtensions ();
@@ -43,8 +59,8 @@ public class SwaggerResource {
4359 }
4460 }
4561
46- Collections .sort (clients , String .CASE_INSENSITIVE_ORDER );
47- Collections .sort (servers , String .CASE_INSENSITIVE_ORDER );
62+ clients .sort (String .CASE_INSENSITIVE_ORDER );
63+ servers .sort (String .CASE_INSENSITIVE_ORDER );
4864 }
4965
5066 @ GET
@@ -55,20 +71,21 @@ public class SwaggerResource {
5571 notes = "A valid `fileId` is generated by the `/clients/{language}` or `/servers/{language}` POST "
5672 + "operations. The fileId code can be used just once, after which a new `fileId` will need to "
5773 + "be requested." , response = String .class , tags = {"clients" , "servers" })
58- public Response downloadFile (@ PathParam ("fileId" ) String fileId ) throws Exception {
74+ @ ApiResponses (value = {
75+ @ ApiResponse (code = 200 , message = "File successfully downloaded. Response contains ZIP file bytes." ),
76+ @ ApiResponse (code = 404 , message = "File with the given fileId not found or already downloaded." ),
77+ @ ApiResponse (code = 500 , message = "Server error while reading or returning the file." )
78+ })
79+ public Response downloadFile (@ PathParam ("fileId" ) String fileId ) {
5980 Generated g = fileMap .get (fileId );
60- System . out . println ( "looking for fileId " + fileId );
61- System . out . println ( "got filename " + g .getFilename ());
62- if ( g .getFilename () != null ) {
81+ LOGGER . info ( "Looking for fileId: {}" , fileId );
82+ if ( g != null && g .getFilename () != null ) {
83+ LOGGER . info ( "Got filename: {}" , g .getFilename ());
6384 SecureFileUtils .validatePath (g .getFilename ());
64- File file = new java .io .File (g .getFilename ());
65- byte [] bytes = org .apache .commons .io .FileUtils .readFileToByteArray (file );
85+ final File file = new java .io .File (g .getFilename ());
6686
67- try {
68- FileUtils .deleteDirectory (file .getParentFile ());
69- } catch (Exception e ) {
70- System .out .println ("failed to delete file " + file .getAbsolutePath ());
71- }
87+ byte [] bytes = getFileBytes (file );
88+ removeFile (fileId , file );
7289
7390 return Response
7491 .ok (bytes , "application/zip" )
@@ -80,6 +97,23 @@ public Response downloadFile(@PathParam("fileId") String fileId) throws Exceptio
8097 }
8198 }
8299
100+ private static byte [] getFileBytes (File file ) {
101+ try {
102+ return FileUtils .readFileToByteArray (file );
103+ } catch (IOException e ) {
104+ throw new IllegalStateException ("Cannot read the file: " + file .getAbsolutePath (), e );
105+ }
106+ }
107+
108+ private static void removeFile (String fileId , File file ) {
109+ try {
110+ FileUtils .deleteDirectory (file .getParentFile ());
111+ fileMap .remove (fileId );
112+ } catch (Exception e ) {
113+ LOGGER .error ("Failed to delete file: {} " , file .getAbsolutePath ());
114+ }
115+ }
116+
83117 @ POST
84118 @ Path ("/clients/{language}" )
85119 @ ApiOperation (
@@ -90,7 +124,7 @@ public Response generateClient(
90124 @ Context HttpServletRequest request ,
91125 @ ApiParam (value = "The target language for the client library" , required = true ) @ PathParam ("language" ) String language ,
92126 @ ApiParam (value = "Configuration for building the client library" , required = true ) GeneratorInput opts )
93- throws Exception {
127+ throws ApiException {
94128
95129 String filename = Generator .generateClient (language , opts );
96130 String host = getHost (request );
@@ -101,7 +135,6 @@ public Response generateClient(
101135 g .setFilename (filename );
102136 g .setFriendlyName (language + "-client" );
103137 fileMap .put (code , g );
104- System .out .println (code + ", " + filename );
105138 String link = host + "/api/gen/download/" + code ;
106139 return Response .ok ().entity (new ResponseCode (code , link )).build ();
107140 } else {
@@ -117,7 +150,7 @@ public Response generateClient(
117150 public Response getClientOptions (
118151 @ SuppressWarnings ("unused" ) @ Context HttpServletRequest request ,
119152 @ ApiParam (value = "The target language for the client library" , required = true ) @ PathParam ("language" ) String language )
120- throws Exception {
153+ throws ApiException {
121154
122155 Map <String , CliOption > opts = Generator .getOptions (language );
123156
@@ -136,7 +169,7 @@ public Response getClientOptions(
136169 public Response getServerOptions (
137170 @ SuppressWarnings ("unused" ) @ Context HttpServletRequest request ,
138171 @ ApiParam (value = "The target language for the server framework" , required = true ) @ PathParam ("framework" ) String framework )
139- throws Exception {
172+ throws ApiException {
140173
141174 Map <String , CliOption > opts = Generator .getOptions (framework );
142175
@@ -173,14 +206,13 @@ public Response serverOptions() {
173206 value = "Generates a server library" ,
174207 notes = "Accepts a `GeneratorInput` options map for spec location and generation options." ,
175208 response = ResponseCode .class , tags = "servers" )
176- public Response generateServerForLanguage (@ Context HttpServletRequest request , @ ApiParam (
177- value = "framework" , required = true ) @ PathParam ("framework" ) String framework ,
178- @ ApiParam (value = "parameters" , required = true ) GeneratorInput opts ) throws Exception {
209+ public Response generateServerForLanguage (@ Context HttpServletRequest request , @ ApiParam (value = "framework" , required = true ) @ PathParam ("framework" ) String framework ,
210+ @ ApiParam (value = "parameters" , required = true ) GeneratorInput opts ) throws ApiException {
179211 if (framework == null ) {
180212 throw new BadRequestException ("Framework is required" );
181213 }
182214 String filename = Generator .generateServer (framework , opts );
183- System . out . println ( "generated name: " + filename );
215+ LOGGER . info ( "Generated filename: {}" , filename );
184216
185217 String host = getHost (request );
186218
@@ -190,7 +222,6 @@ public Response generateServerForLanguage(@Context HttpServletRequest request, @
190222 g .setFilename (filename );
191223 g .setFriendlyName (framework + "-server" );
192224 fileMap .put (code , g );
193- System .out .println (code + ", " + filename );
194225 String link = host + "/api/gen/download/" + code ;
195226 return Response .ok ().entity (new ResponseCode (code , link )).build ();
196227 } else {
0 commit comments