forked from apache/pinot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix the ServletConfig loading issue with swagger. (apache#13122)
- Loading branch information
Showing
9 changed files
with
207 additions
and
192 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
pinot-common/src/main/java/org/apache/pinot/common/swagger/SwaggerApiListingResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.apache.pinot.common.swagger; | ||
|
||
import io.swagger.annotations.ApiOperation; | ||
import io.swagger.jaxrs.listing.BaseApiListingResource; | ||
import javax.servlet.ServletConfig; | ||
import javax.servlet.ServletContext; | ||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.PathParam; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.core.Application; | ||
import javax.ws.rs.core.Context; | ||
import javax.ws.rs.core.HttpHeaders; | ||
import javax.ws.rs.core.MediaType; | ||
import javax.ws.rs.core.Response; | ||
import javax.ws.rs.core.UriInfo; | ||
import org.apache.commons.lang3.StringUtils; | ||
|
||
|
||
/* | ||
This class is required to avoid the jersey2 warning messages regarding servlet config constructor missing while | ||
injecting the config. Please refer to Pinot issue 13047 & 5306 for more context. | ||
In this implementation, we added the ServletConfig as the class level member instead of injecting it. | ||
*/ | ||
@Path("/swagger.{type:json|yaml}") | ||
public class SwaggerApiListingResource extends BaseApiListingResource { | ||
@Context | ||
ServletContext _context; | ||
|
||
@Context | ||
ServletConfig _servletConfig; | ||
|
||
@GET | ||
@Produces({MediaType.APPLICATION_JSON, "application/yaml"}) | ||
@ApiOperation(value = "The swagger definition in either JSON or YAML", hidden = true) | ||
public Response getListing(@Context Application app, @Context HttpHeaders headers, @Context UriInfo uriInfo, | ||
@PathParam("type") String type) { | ||
if (StringUtils.isNotBlank(type) && type.trim().equalsIgnoreCase("yaml")) { | ||
return getListingYamlResponse(app, _context, _servletConfig, headers, uriInfo); | ||
} else { | ||
return getListingJsonResponse(app, _context, _servletConfig, headers, uriInfo); | ||
} | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
pinot-common/src/main/java/org/apache/pinot/common/swagger/SwaggerSetupUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.apache.pinot.common.swagger; | ||
|
||
import io.swagger.jaxrs.config.BeanConfig; | ||
import java.net.InetAddress; | ||
import java.net.URL; | ||
import java.net.URLClassLoader; | ||
import java.net.UnknownHostException; | ||
import org.apache.pinot.common.utils.PinotStaticHttpHandler; | ||
import org.apache.pinot.spi.utils.CommonConstants; | ||
import org.glassfish.grizzly.http.server.CLStaticHttpHandler; | ||
import org.glassfish.grizzly.http.server.HttpServer; | ||
|
||
|
||
public class SwaggerSetupUtils { | ||
private SwaggerSetupUtils() { | ||
} | ||
|
||
public static void setupSwagger(String componentType, String resourcePackage, boolean useHttps, String basePath, | ||
HttpServer httpServer) { | ||
BeanConfig beanConfig = new BeanConfig(); | ||
beanConfig.setTitle(String.format("Pinot %s API", componentType)); | ||
beanConfig.setDescription(String.format("APIs for accessing Pinot %s information", componentType)); | ||
beanConfig.setContact("https://github.com/apache/pinot"); | ||
beanConfig.setVersion("1.0"); | ||
beanConfig.setExpandSuperTypes(false); | ||
if (useHttps) { | ||
beanConfig.setSchemes(new String[]{CommonConstants.HTTPS_PROTOCOL}); | ||
} else { | ||
beanConfig.setSchemes(new String[]{CommonConstants.HTTP_PROTOCOL, CommonConstants.HTTPS_PROTOCOL}); | ||
} | ||
beanConfig.setBasePath(basePath); | ||
beanConfig.setResourcePackage(resourcePackage); | ||
beanConfig.setScan(true); | ||
|
||
try { | ||
beanConfig.setHost(InetAddress.getLocalHost().getHostName()); | ||
} catch (UnknownHostException e) { | ||
throw new RuntimeException("Cannot get localhost name"); | ||
} | ||
|
||
ClassLoader classLoader = SwaggerSetupUtils.class.getClassLoader(); | ||
CLStaticHttpHandler staticHttpHandler = new CLStaticHttpHandler(classLoader, "/api/"); | ||
// map both /api and /help to swagger docs. /api because it looks nice. /help for backward compatibility | ||
httpServer.getServerConfiguration().addHttpHandler(staticHttpHandler, "/api/", "/help/"); | ||
|
||
URL swaggerDistLocation = classLoader.getResource(CommonConstants.CONFIG_OF_SWAGGER_RESOURCES_PATH); | ||
CLStaticHttpHandler swaggerDist = new PinotStaticHttpHandler(new URLClassLoader(new URL[]{swaggerDistLocation})); | ||
httpServer.getServerConfiguration().addHttpHandler(swaggerDist, "/swaggerui-dist/"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.