Ok, I see how I can do it using both. All of the filter specific logic
stays in the filter, and I basically moved the purging part into a class
that extends ApiListingResource; I've included that code here below because
I'm having an issue with it.
The problem is that I'm having a compiler issue on the highlighted portion.
I'm receiving this error:
*The method process(Application, ServletContext, ServletConfig,
> HttpHeaders, UriInfo) from the type BaseApiListingResource is not visible*
I realize this might just be a Java question, rather than Swagger related,
but I figured I'd post it here anyway. The part I don't understand is why
I can reference getListingJsonResponse from within getListing just fine
when it has the exact same visibility as process. I tried accessing process
from
within getListing as well, but I get the same error. What am I missing
here?
I'm soooo close...
import static org.apache.commons.collections.CollectionUtils.isEmpty;
import static org.apache.commons.collections.CollectionUtils.isNotEmpty;
import static org.apache.commons.lang3.StringUtils.isNotBlank;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.ws.rs.GET;
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.MultivaluedMap;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.swagger.annotations.ApiOperation;
import io.swagger.jaxrs.listing.ApiListingResource;
import io.swagger.models.Swagger;
import io.swagger.models.Tag;
import io.swagger.models.Operation;
import io.swagger.models.Path;
import io.swagger.util.Json;
@javax.ws.rs.Path("/internal/swagger/{type:json|yaml}")
public class SwaggerTagFilterResourceExample extends ApiListingResource {
private static final Logger logger =
LoggerFactory.getLogger(SwaggerTagFilterResourceExample.class);
private static final String TAG_NAME_ADMIN1 = "Bob";
private static final String TAG_NAME_ADMIN2 = "Carl";
private static final String TAG_NAME_ADMIN3 = "Frank";
private static final String TAG_NAME_ADMIN4 = "Steve";
private static final Set<String> TAG_NAME_SET = new HashSet<String>();
static {
TAG_NAME_SET.add(TAG_NAME_ADMIN1);
TAG_NAME_SET.add(TAG_NAME_ADMIN2);
TAG_NAME_SET.add(TAG_NAME_ADMIN3);
TAG_NAME_SET.add(TAG_NAME_ADMIN4);
}
@Context
ServletContext context;
@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
ServletConfig servletConfig, @Context HttpHeaders headers,
@Context UriInfo uriInfo, @PathParam("type") String type) {
if (isNotBlank(type) && type.trim().equalsIgnoreCase("yaml")) {
logger.debug("Creating Swagger YAML...");
return getListingYamlResponse(app, context, servletConfig,
headers, uriInfo);
} else {
logger.debug("Creating Swagger JSON...");
return getListingJsonResponse(app, context, servletConfig,
headers, uriInfo);
}
}
protected Response getListingJsonResponse(
Application app,
ServletContext servletContext,
ServletConfig servletConfig,
HttpHeaders headers,
UriInfo uriInfo) {
Swagger swagger = process(app, servletContext, servletConfig,
headers, uriInfo);
MultivaluedMap<String, String> queryParamMultivaluedMap =
uriInfo.getQueryParameters();
List<Tag> tagList = swagger.getTags();
Map<String, Path> pathMap = swagger.getPaths();
List<Tag> newTagList = new ArrayList<Tag>();
List<String> newStringTagList = new ArrayList<String>();
for (MultivaluedMap.Entry<String, List<String>> entry :
queryParamMultivaluedMap.entrySet()) {
if (entry.getKey().equalsIgnoreCase("names") &&
isNotEmpty(entry.getValue())) {
// remove all name tags from the top level Swagger object that are not in
the names query parameter list
swagger.setTags(purgeUnrequestedNameTags(entry.getValue(), tagList));
// remove all name tags from each Operation object that are not in the
names query parameter list
for (Path path : pathMap.values()) {
List<Operation> operationList = path.getOperations();
for (Operation operation : operationList) {
newTagList = purgeUnrequestedNameTags(entry.getValue(), tagList);
for (Tag tag : newTagList) {
newStringTagList.add(tag.getName());
}
operation.setTags(newStringTagList);
}
}
}
}
if (swagger != null) {
return
Response.ok().entity(Json.mapper().writeValueAsString(swagger)).type(MediaType.APPLICATION_JSON_TYPE).build();
} else {
return Response.status(404).build();
}
}
/**
* Returns a purged list of name tags based upon the master set of name tags
while
* leaving non-name tags untouched.
*
* @param nameParamList
* @param tagValueList
* @return
*/
private List<Tag> purgeUnrequestedNameTags(List<String> nameParamList,
List<Tag> tagValueList) {
List<Tag> cleanNameTagValueList = new ArrayList<Tag>();
try {
if (isEmpty(nameParamList)) {
cleanNameTagValueList = tagValueList;
} else {
for (Tag tag : tagValueList) {
// keep the name if it was passed in as a parameter (in the nameParamList),
// but if it is a name tag and was NOT passed in then don't keep it; this
also
// makes sure that any non-name tags are also kept
if (nameParamList.contains(tag.getName()) ||
!TAG_NAME_SET.contains(tag.getName())) {
cleanNameTagValueList.add(tag);
}
}
}
} catch (Exception e) {
logger.error("Var - cleanNameTagValueList: {}", cleanNameTagValueList);
logger.error("Exception occurred while purging erroneous name tags: {}",
e);
logger.error("Continuing flow...");
}
logger.debug("--Exiting--");
return cleanNameTagValueList;
}
}
Thanks!
On Wednesday, June 21, 2017 at 4:35:21 PM UTC-4, Ron wrote:
>
> Possibly both, it depends on your logic.
>
--
You received this message because you are subscribed to the Google Groups
"Swagger" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.