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,75 @@ 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 Registration registration ;
48
+ private OpenApiService openApiService ;
49
+ private WebContextSecurer webContextSecurer ;
43
50
44
- private JaxRsOpenApi jaxRsOpenApi ;
45
-
46
- public OpenApiLighty (RestConfConfiguration restConfConfiguration ,
47
- LightyJettyServerProvider jettyServerBuilder , LightyServices lightyServices ) {
51
+ public OpenApiLighty (RestConfConfiguration restConfConfiguration , LightyJettyServerProvider jettyServerBuilder ,
52
+ LightyServices lightyServices , @ Nullable WebContextSecurer webContextSecurer ) {
48
53
this .restConfConfiguration = restConfConfiguration ;
49
54
this .jettyServerBuilder = jettyServerBuilder ;
50
55
this .lightyServices = lightyServices ;
56
+ this .registration = null ;
57
+ this .webContextSecurer = (webContextSecurer == null )
58
+ ? new CommunityRestConf .LightyWebContextSecurer () : webContextSecurer ;
51
59
}
52
60
53
61
@ Override
54
62
protected boolean initProcedure () {
55
63
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 (),
64
+ this .openApiService = new OpenApiServiceImpl (lightyServices .getDOMSchemaService (),
62
65
lightyServices .getDOMMountPointService (), lightyServices .getJaxRsEndpoint ());
63
66
64
- this .jaxRsOpenApi = new JaxRsOpenApi (openApiService );
65
-
66
- final ServletContainer restServletContainer = new ServletContainer (ResourceConfig
67
- .forApplication ((new Application () {
67
+ final var webContextBuilder = WebContext .builder ()
68
+ .name ("OpenAPI" )
69
+ .contextPath (OPENAPI_PATH )
70
+ .supportsSessions (true )
71
+ .addServlet (ServletDetails .builder ()
72
+ .servlet (new JerseyServletSupport ().createHttpServletBuilder (new Application () {
68
73
@ Override
69
74
public Set <Object > getSingletons () {
70
75
return Set .of (new JaxRsOpenApi (openApiService ),
71
76
new OpenApiBodyWriter (new JsonFactoryBuilder ().build ()));
72
77
}
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 );
78
+ }). build ())
79
+ . addUrlPattern ( "/api/v3/*" )
80
+ . build ())
81
+ . addServlet ( addStaticResources ( "/explorer" , "OpenApiStaticServlet" ));
82
+
83
+ webContextSecurer . requireAuthentication ( webContextBuilder , "/*" );
84
+
85
+ try {
86
+ registration = jettyServerBuilder . build (). registerWebContext ( webContextBuilder . build () );
87
+ } catch ( ServletException e ) {
88
+ LOG .error ( "Failed to register OpenApi web context: {}!" , jettyServerBuilder . getClass (), e );
89
+ }
85
90
return true ;
86
91
}
87
92
88
93
@ Override
89
94
protected boolean stopProcedure () {
90
95
LOG .info ("shutting down openapi ..." );
96
+ this .registration .close ();
91
97
return true ;
92
98
}
93
99
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 ();
100
+ private ServletDetails addStaticResources (String path , String servletName ) {
101
+ final String externalResource = OpenApiLighty .class .getResource ("/explorer" ).toExternalForm ();
97
102
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 + "/*" );
103
+
104
+ return ServletDetails .builder ()
105
+ .servlet (new DefaultServlet ())
106
+ .name (servletName )
107
+ .addUrlPattern (path + "/*" )
108
+ .putInitParam ("resourceBase" , externalResource )
109
+ .putInitParam ("dirAllowed" , TRUE )
110
+ .putInitParam ("pathInfoOnly" , TRUE )
111
+ .build ();
104
112
}
105
113
106
114
@ VisibleForTesting
107
- JaxRsOpenApi getJaxRsOpenApi () {
108
- return jaxRsOpenApi ;
115
+ OpenApiService getjaxRsOpenApi () {
116
+ return this . openApiService ;
109
117
}
110
118
}
0 commit comments