11
11
import com .google .common .annotations .VisibleForTesting ;
12
12
import io .lighty .core .controller .api .AbstractLightyModule ;
13
13
import io .lighty .core .controller .api .LightyServices ;
14
+ import io .lighty .modules .northbound .restconf .community .impl .CommunityRestConf ;
14
15
import io .lighty .modules .northbound .restconf .community .impl .config .RestConfConfiguration ;
15
16
import io .lighty .server .LightyJettyServerProvider ;
16
17
import java .util .Set ;
18
+ import javax .servlet .ServletException ;
17
19
import javax .ws .rs .core .Application ;
18
- import org .eclipse .jetty . server . handler . ContextHandlerCollection ;
20
+ import org .eclipse .jdt . annotation . Nullable ;
19
21
import org .eclipse .jetty .servlet .DefaultServlet ;
20
- import org .eclipse .jetty .servlet .ServletContextHandler ;
21
- import org .eclipse .jetty .servlet .ServletHolder ;
22
- import org .glassfish .jersey .server .ResourceConfig ;
23
- import org .glassfish .jersey .servlet .ServletContainer ;
22
+ import org .opendaylight .aaa .web .ServletDetails ;
23
+ import org .opendaylight .aaa .web .WebContext ;
24
+ import org .opendaylight .aaa .web .WebContextSecurer ;
25
+ import org .opendaylight .aaa .web .servlet .jersey2 .JerseyServletSupport ;
26
+ import org .opendaylight .restconf .openapi .api .OpenApiService ;
24
27
import org .opendaylight .restconf .openapi .impl .OpenApiServiceImpl ;
25
28
import org .opendaylight .restconf .openapi .jaxrs .JaxRsOpenApi ;
26
29
import org .opendaylight .restconf .openapi .jaxrs .OpenApiBodyWriter ;
30
+ import org .opendaylight .yangtools .concepts .Registration ;
27
31
import org .slf4j .Logger ;
28
32
import org .slf4j .LoggerFactory ;
29
33
@@ -40,71 +44,77 @@ public class OpenApiLighty extends AbstractLightyModule {
40
44
private final RestConfConfiguration restConfConfiguration ;
41
45
private final LightyJettyServerProvider jettyServerBuilder ;
42
46
private final LightyServices lightyServices ;
47
+ private final WebContextSecurer webContextSecurer ;
48
+ private Registration registration ;
49
+ private OpenApiService openApiService ;
43
50
44
- private JaxRsOpenApi jaxRsOpenApi ;
45
-
46
- public OpenApiLighty (RestConfConfiguration restConfConfiguration ,
47
- LightyJettyServerProvider jettyServerBuilder , LightyServices lightyServices ) {
51
+ public OpenApiLighty (final RestConfConfiguration restConfConfiguration ,
52
+ final LightyJettyServerProvider jettyServerBuilder ,
53
+ final LightyServices lightyServices , final @ Nullable WebContextSecurer webContextSecurer ) {
48
54
this .restConfConfiguration = restConfConfiguration ;
49
55
this .jettyServerBuilder = jettyServerBuilder ;
50
56
this .lightyServices = lightyServices ;
57
+ this .registration = null ;
58
+ this .webContextSecurer = (webContextSecurer == null )
59
+ ? new CommunityRestConf .LightyWebContextSecurer () : webContextSecurer ;
51
60
}
52
61
53
62
@ Override
54
63
protected boolean initProcedure () {
55
64
LOG .info ("initializing openapi" );
56
-
57
- //replace all slash characters from the beginning of the string
58
- String basePathString = restConfConfiguration .getRestconfServletContextPath ().replaceAll ("^/+" , "" );
59
- LOG .info ("basePath: {}" , basePathString );
60
-
61
- final var openApiService = new OpenApiServiceImpl (lightyServices .getDOMSchemaService (),
65
+ this .openApiService = new OpenApiServiceImpl (lightyServices .getDOMSchemaService (),
62
66
lightyServices .getDOMMountPointService (), lightyServices .getJaxRsEndpoint ());
63
67
64
- this .jaxRsOpenApi = new JaxRsOpenApi (openApiService );
65
-
66
- final ServletContainer restServletContainer = new ServletContainer (ResourceConfig
67
- .forApplication ((new Application () {
68
+ final var webContextBuilder = WebContext .builder ()
69
+ .name ("OpenAPI" )
70
+ .contextPath (OPENAPI_PATH )
71
+ .supportsSessions (true )
72
+ .addServlet (ServletDetails .builder ()
73
+ .servlet (new JerseyServletSupport ().createHttpServletBuilder (new Application () {
68
74
@ Override
69
75
public Set <Object > getSingletons () {
70
76
return Set .of (new JaxRsOpenApi (openApiService ),
71
77
new OpenApiBodyWriter (new JsonFactoryBuilder ().build ()));
72
78
}
73
- })));
74
-
75
- ServletHolder restServletHolder = new ServletHolder (restServletContainer );
76
-
77
- ContextHandlerCollection contexts = new ContextHandlerCollection ();
78
- ServletContextHandler mainHandler = new ServletContextHandler (contexts , OPENAPI_PATH , true , false );
79
- mainHandler .addServlet (restServletHolder , "/api/v3/*" );
80
-
81
- addStaticResources (mainHandler , "/explorer" , "static-content" );
82
-
83
- LOG .info ("adding context handler ..." );
84
- jettyServerBuilder .addContextHandler (contexts );
79
+ }).build ())
80
+ .addUrlPattern ("/api/v3/*" )
81
+ .build ())
82
+ .addServlet (addStaticResources ("/explorer" , "OpenApiStaticServlet" ));
83
+
84
+ webContextSecurer .requireAuthentication (webContextBuilder , "/*" );
85
+
86
+ try {
87
+ registration = jettyServerBuilder .getServer ().registerWebContext (webContextBuilder .build ());
88
+ } catch (ServletException e ) {
89
+ LOG .error ("Failed to register OpenApi web context: {}!" , jettyServerBuilder .getClass (), e );
90
+ return false ;
91
+ }
85
92
return true ;
86
93
}
87
94
88
95
@ Override
89
96
protected boolean stopProcedure () {
90
97
LOG .info ("shutting down openapi ..." );
98
+ this .registration .close ();
91
99
return true ;
92
100
}
93
101
94
- private void addStaticResources (ServletContextHandler mainHandler , String path , String servletName ) {
95
- LOG .info ("initializing openapi UI at: http(s)://{hostname:port}{}{}/index.html" , OPENAPI_PATH , path );
96
- String externalResource = OpenApiLighty .class .getResource (path ).toExternalForm ();
102
+ private ServletDetails addStaticResources (String path , String servletName ) {
103
+ final String externalResource = OpenApiLighty .class .getResource ("/explorer" ).toExternalForm ();
97
104
LOG .info ("externalResource: {}" , externalResource );
98
- DefaultServlet defaultServlet = new DefaultServlet ();
99
- ServletHolder holderPwd = new ServletHolder (servletName , defaultServlet );
100
- holderPwd .setInitParameter ("resourceBase" , externalResource );
101
- holderPwd .setInitParameter ("dirAllowed" , TRUE );
102
- holderPwd .setInitParameter ("pathInfoOnly" , TRUE );
103
- mainHandler .addServlet (holderPwd , path + "/*" );
105
+
106
+ return ServletDetails .builder ()
107
+ .servlet (new DefaultServlet ())
108
+ .name (servletName )
109
+ .addUrlPattern (path + "/*" )
110
+ .putInitParam ("resourceBase" , externalResource )
111
+ .putInitParam ("dirAllowed" , TRUE )
112
+ .putInitParam ("pathInfoOnly" , TRUE )
113
+ .build ();
104
114
}
105
115
106
116
@ VisibleForTesting
107
- JaxRsOpenApi getJaxRsOpenApi () {
108
- return jaxRsOpenApi ;
117
+ OpenApiService getjaxRsOpenApi () {
118
+ return this . openApiService ;
109
119
}
110
120
}
0 commit comments