1+ /*
2+ * Licensed to the Apache Software Foundation (ASF) under one
3+ * or more contributor license agreements. See the NOTICE file
4+ * distributed with this work for additional information
5+ * regarding copyright ownership. The ASF licenses this file
6+ * to you under the Apache License, Version 2.0 (the
7+ * "License"); you may not use this file except in compliance
8+ * with the License. You may obtain a copy of the License at
9+ *
10+ * http://www.apache.org/licenses/LICENSE-2.0
11+ *
12+ * Unless required by applicable law or agreed to in writing,
13+ * software distributed under the License is distributed on an
14+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+ * KIND, either express or implied. See the License for the
16+ * specific language governing permissions and limitations
17+ * under the License.
18+ */
19+
20+ package org .apache .axis2 .openapi ;
21+
22+ import org .apache .axis2 .AxisFault ;
23+ import org .apache .axis2 .context .ConfigurationContext ;
24+ import org .apache .axis2 .description .AxisDescription ;
25+ import org .apache .axis2 .description .AxisModule ;
26+ import org .apache .axis2 .modules .Module ;
27+ import org .apache .commons .logging .Log ;
28+ import org .apache .commons .logging .LogFactory ;
29+ import org .apache .neethi .Assertion ;
30+ import org .apache .neethi .Policy ;
31+
32+ /**
33+ * Apache Axis2 OpenAPI/Swagger integration module.
34+ *
35+ * This module provides automatic OpenAPI specification generation and Swagger UI support
36+ * for Axis2 REST services. It integrates with the Axis2 transport layer to serve
37+ * OpenAPI documentation at standard endpoints.
38+ *
39+ * Key features:
40+ * - Automatic OpenAPI 3.0.1 specification generation from service metadata
41+ * - Swagger UI integration for interactive API documentation
42+ * - Support for REST service introspection and annotation processing
43+ * - Integration with Axis2's existing metadata query mechanisms
44+ */
45+ public class OpenApiModule implements Module {
46+
47+ private static final Log log = LogFactory .getLog (OpenApiModule .class );
48+
49+ /**
50+ * Initialize the OpenAPI module.
51+ *
52+ * This method is called when the module is loaded and initializes the OpenAPI
53+ * integration components including specification generation and UI serving.
54+ */
55+ @ Override
56+ public void init (ConfigurationContext configContext , AxisModule module ) throws AxisFault {
57+ log .info ("Initializing Apache Axis2 OpenAPI module" );
58+
59+ try {
60+ // Register OpenAPI specification generator
61+ OpenApiSpecGenerator specGenerator = new OpenApiSpecGenerator (configContext );
62+ configContext .setProperty ("axis2.openapi.generator" , specGenerator );
63+
64+ // Register Swagger UI handler
65+ SwaggerUIHandler uiHandler = new SwaggerUIHandler (configContext );
66+ configContext .setProperty ("axis2.openapi.ui" , uiHandler );
67+
68+ // Initialize OpenAPI service introspector
69+ ServiceIntrospector introspector = new ServiceIntrospector (configContext );
70+ configContext .setProperty ("axis2.openapi.introspector" , introspector );
71+
72+ log .info ("OpenAPI module initialization completed successfully" );
73+
74+ } catch (Exception e ) {
75+ log .error ("Failed to initialize OpenAPI module" , e );
76+ throw new AxisFault ("OpenAPI module initialization failed: " + e .getMessage (), e );
77+ }
78+ }
79+
80+ /**
81+ * Called when this module is engaged to a service or operation.
82+ *
83+ * This allows the module to customize behavior per service and validate
84+ * that the service is compatible with OpenAPI documentation generation.
85+ */
86+ @ Override
87+ public void engageNotify (AxisDescription axisDescription ) throws AxisFault {
88+ String serviceName = axisDescription .getClass ().getSimpleName ();
89+ if (axisDescription instanceof org .apache .axis2 .description .AxisService ) {
90+ serviceName = ((org .apache .axis2 .description .AxisService ) axisDescription ).getName ();
91+ }
92+
93+ log .debug ("OpenAPI module engaged to: " + serviceName );
94+
95+ // Validate that the service supports REST operations for OpenAPI generation
96+ if (axisDescription .getParameter ("enableREST" ) == null ) {
97+ log .warn ("Service " + serviceName +
98+ " does not have REST enabled - OpenAPI documentation may be limited" );
99+ }
100+ }
101+
102+ /**
103+ * Shutdown the OpenAPI module and clean up resources.
104+ */
105+ @ Override
106+ public void shutdown (ConfigurationContext configurationContext ) throws AxisFault {
107+ log .info ("Shutting down Apache Axis2 OpenAPI module" );
108+
109+ try {
110+ // Clean up registered components
111+ configurationContext .removeProperty ("axis2.openapi.generator" );
112+ configurationContext .removeProperty ("axis2.openapi.ui" );
113+ configurationContext .removeProperty ("axis2.openapi.introspector" );
114+
115+ } catch (Exception e ) {
116+ log .warn ("Error during OpenAPI module shutdown" , e );
117+ }
118+ }
119+
120+ /**
121+ * Policy assertion support - currently not implemented for OpenAPI.
122+ */
123+ @ Override
124+ public boolean canSupportAssertion (Assertion assertion ) {
125+ return false ;
126+ }
127+
128+ /**
129+ * Policy processing - currently not implemented for OpenAPI.
130+ */
131+ @ Override
132+ public void applyPolicy (Policy policy , AxisDescription axisDescription ) throws AxisFault {
133+ // OpenAPI module does not currently support WS-Policy integration
134+ }
135+ }
0 commit comments