Thanks for that. Before diving in too much, I see you have debugging logs. When 
you start the app and issue requests to get the definition, do you see those 
log lines for each call or only for the first?

 

 

 

From: <[email protected]> on behalf of Bryan Nelson 
<[email protected]>
Reply-To: "[email protected]" 
<[email protected]>
Date: Tuesday, 20 June 2017 at 12:09
To: Swagger <[email protected]>
Subject: Re: Swagger-Core - Caching of Tag Data in Operations?

 

Sure.  I can share all of the codez. 

 

import static org.apache.commons.collections.CollectionUtils.isEmpty;

import static org.apache.commons.collections.CollectionUtils.isNotEmpty;

import static org.apache.commons.collections.ListUtils.intersection;

import static org.apache.commons.collections.MapUtils.isEmpty;

import static org.apache.commons.lang3.StringUtils.isNotBlank;

 

import java.util.ArrayList;

import java.util.Arrays;

import java.util.HashSet;

import java.util.List;

import java.util.Map;

import java.util.Set;

 

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

 

import io.swagger.core.filter.AbstractSpecFilter;

import io.swagger.model.ApiDescription;

import io.swagger.models.Operation;

 

public class SwaggerTagFilterExample extends AbstractSpecFilter {

 

 private static final Logger logger = 
LoggerFactory.getLogger(SwaggerTagFilterExample.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);

 }

 

 @Override

 public boolean isOperationAllowed (Operation operation, ApiDescription api, 
Map<String, List<String>> paramMap, 
              Map<String, String> cookies,  Map<String, List<String>> headers) {

 

 logger.debug("--Entering--");

 

 boolean showOperation = false;

 

 List<String> nameParamList = null;

 List<String> tagValueList = null;

 

 try {

 

 // get all the tags from the json

 tagValueList = operation.getTags();

 

 logger.debug("The tag values are: {}", tagValueList);

 

 nameParamList = getParamList(paramMap, "names");

 

 // apply name filtering logic

 showOperation = isEmpty(nameParamList) ? true : 
applyNameFiltering(nameParamList, tagValueList);

 // remove all name tags for this operation that are not in the nameParamList

 if (showOperation) {

 operation.setTags(purgeUnrequestedNameTags(nameParamList, tagValueList));

 }

 

 } catch (Exception e) {

 

 logger.error("Var - nameParamList: {}", nameParamList);

 logger.error("Var - tagValueList: {}", tagValueList);

 

 logger.error("Exception occurred when verifying allowed operations - No 
swagger operation displayed for you!!");

 }

 

 logger.debug("--Exiting--");

 

 return showOperation;

 }

 

 

@Override

 public boolean isRemovingUnreferencedDefinitions() {

     return true;

 }

 

 

/** 

  * Returns the values from the paramMap that are associated with the paramName 
key as a List<String>.  

  * 

  * @param paramMap

  * @param paramName

  * @return

  */

 private List<String> getParamList(Map<String, List<String>> paramMap, String 
paramName) {

 logger.debug("--Entering--");

 logger.debug("Var - paramMap: " + (isEmpty(paramMap) ? null : 
paramMap.toString()));

 logger.debug("Var - paramName: " + paramName);

 List<String> paramList = null;

 try {

 if (paramMap.containsKey(paramName)) {  

 logger.debug("paramMap.get(paramName).isEmpty() is: {}", 
paramMap.get(paramName).isEmpty());

 

 if (isNotBlank(paramMap.get(paramName).get(0))) { 

 

 // get specific tags from the query param

 paramList = Arrays.asList(paramMap.get(paramName).get(0).split(","));

 }

 

 logger.debug("The {} params are: {}", paramName, paramList);

 

 } 

 } catch (Exception e) {

 logger.error("Var - paramList: {}", paramList);

 logger.error("Exception occurred when checking the filter values against the 
operation tags: {}", e);

 logger.error("Continuing flow...");

 }

 logger.debug("--Exiting--");

 return paramList;

 }

 

private final boolean applyNameFiltering(List<String> nameParamList, 
List<String> tagValueList) {

   logger.debug("--Entering--");

   logger.debug("Var - nameParamList: " + (isEmpty(nameParamList) ? null : 
nameParamList.toString()));

   logger.debug("Var - tagValueList: " + (isEmpty(tagValueList) ? null : 
tagValueList.toString()));

boolean matchFound = false;

try {

if (isNotEmpty(nameParamList) && isNotEmpty(tagValueList)) {

matchFound = isNotEmpty(intersection(nameParamList, tagValueList));

}

   } catch (Exception e) {

   logger.error("Var - matchFound: {}", matchFound);

   logger.error("Exception occurred when finding matching name tags: {}", e);

   logger.error("Continuing flow...");

   }

logger.debug("--Exiting--");

return matchFound;

}

/**

* 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<String> purgeUnrequestedNameTags(List<String> nameParamList, 
List<String> tagValueList) {

 

List<String> cleanNameTagValueList = new ArrayList<String>();

try {

if (isEmpty(nameParamList)) {

cleanNameTagValueList = tagValueList;

} else { 

for (String 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) || !TAG_NAME_SET.contains(tag)) {

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!

-- 
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.


-- 
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.

Reply via email to