22
33package io .swagger .petstore ;
44
5+ import org .apache .catalina .Context ;
6+ import org .apache .catalina .Wrapper ;
57import org .apache .catalina .startup .Tomcat ;
6-
8+ import org .apache .catalina .webresources .DirResourceSet ;
9+ import org .apache .catalina .webresources .JarResourceSet ;
10+ import org .apache .catalina .webresources .StandardRoot ;
11+
12+ import javax .servlet .Servlet ;
13+ import javax .servlet .http .HttpServlet ;
14+ import javax .servlet .http .HttpServletRequest ;
15+ import javax .servlet .http .HttpServletResponse ;
716import java .io .File ;
17+ import java .io .IOException ;
18+ import java .io .InputStream ;
19+ import java .io .OutputStream ;
820import java .net .URL ;
921import java .nio .file .Files ;
22+ import java .security .CodeSource ;
1023
1124public class Main {
1225 Tomcat tomcat ;
@@ -19,59 +32,37 @@ public void startServer(int port) throws Exception {
1932
2033 tomcat .setPort (port );
2134 tomcat .getConnector ();
22- URL webappUrl = Main .class .getClassLoader ().getResource ("webapp" );
23-
24- String webappDirLocation ;
25-
26- // Handle the case where the webapp is inside a JAR
27- if (webappUrl .getProtocol ().equals ("jar" )) {
28- // Extract the JAR file path and the entry path
29- String jarPath = webappUrl .getPath ().substring (5 , webappUrl .getPath ().indexOf ("!" ));
30- String entryPath = webappUrl .getPath ().substring (webappUrl .getPath ().indexOf ("!" ) + 2 );
31-
32- // Create a temporary directory to extract the webapp resources
33- File tempDir = Files .createTempDirectory ("webapp" ).toFile ();
34- tempDir .deleteOnExit ();
35-
36- // Extract the JAR entry to the temporary directory
37- try (java .util .jar .JarFile jar = new java .util .jar .JarFile (new File (jarPath ))) {
38- java .util .Enumeration <java .util .jar .JarEntry > entries = jar .entries ();
39- while (entries .hasMoreElements ()) {
40- java .util .jar .JarEntry entry = entries .nextElement ();
41- if (entry .getName ().startsWith (entryPath ) && !entry .isDirectory ()) {
42- File file = new File (tempDir , entry .getName ().substring (entryPath .length ()));
43- file .getParentFile ().mkdirs ();
44- try (java .io .InputStream is = jar .getInputStream (entry );
45- java .io .FileOutputStream fos = new java .io .FileOutputStream (file )) {
46- while (is .available () > 0 ) {
47- fos .write (is .read ());
48- }
49- }
50- }
51- }
52- }
53- System .out .println (tempDir );
54- //also extract inflector.yaml
55- try (java .io .InputStream is = Main .class .getClassLoader ().getResourceAsStream ("inflector.yaml" )) {
56- if (is != null ) {
57- File inflectorFile = new File ("./" , "inflector.yaml" );
58- try (java .io .FileOutputStream fos = new java .io .FileOutputStream (inflectorFile )) {
59- byte [] buffer = new byte [1024 ];
60- int bytesRead ;
61- while ((bytesRead = is .read (buffer )) != -1 ) {
62- fos .write (buffer , 0 , bytesRead );
63- }
64- }
65- }
35+
36+ Context ctx = tomcat .addContext ("" , null );
37+
38+ StandardRoot resources = new StandardRoot (ctx );
39+ CodeSource src = Main .class .getProtectionDomain ().getCodeSource ();
40+ if (src != null ) {
41+ URL jar = src .getLocation ();
42+ String jarPath = new File (jar .toURI ()).getAbsolutePath ();
43+ if (jarPath .endsWith (".jar" )) {
44+ resources .addJarResources (new JarResourceSet (resources , "/" , jarPath , "/webapp" ));
45+ }else {
46+ URL webappUrl = Main .class .getClassLoader ().getResource ("webapp" );
47+ String webappDirLocation = new File (webappUrl .toURI ()).getAbsolutePath ();
48+ resources .addPreResources (new DirResourceSet (resources , "/" , webappDirLocation , "/" ));
6649 }
50+ }
51+ ctx .setResources (resources );
52+ ctx .addWelcomeFile ("index.html" );
6753
54+ // Swagger-Inflector / Jersey servlet
55+ Wrapper jerseyServlet = Tomcat .addServlet (ctx , "jersey-container-servlet" ,
56+ "org.glassfish.jersey.servlet.ServletContainer" );
6857
69- webappDirLocation = tempDir .getAbsolutePath ();
70- } else {
71- // Handle the case where the webapp is in the filesystem
72- webappDirLocation = new File (webappUrl .toURI ()).getAbsolutePath ();
73- }
74- tomcat .addWebapp ("" , new File (webappDirLocation ).getAbsolutePath ());
58+ jerseyServlet .addInitParameter ("javax.ws.rs.Application" , "io.swagger.oas.inflector.OpenAPIInflector" );
59+ jerseyServlet .setLoadOnStartup (1 );
60+
61+ ctx .addServletMappingDecoded ("/api/*" , "jersey-container-servlet" );
62+
63+ Wrapper defaultServlet = Tomcat .addServlet (ctx , "default" , "org.apache.catalina.servlets.DefaultServlet" );
64+ defaultServlet .setLoadOnStartup (1 );
65+ ctx .addServletMappingDecoded ("/" , "default" );
7566
7667 System .out .println ("Swagger Petstore running at http://localhost:" + port );
7768 tomcat .start ();
0 commit comments