/*
 * Copyright 2019 herd contributors
 *
 * Licensed 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.finra.herd.swaggergen;

import io.swagger.core.filter.AbstractSpecFilter;
import io.swagger.model.ApiDescription;
import io.swagger.models.Operation;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * Yaml filter to avoid all non GET operations for the resource
 * <p>
 * If allowedOperations list provided - only operations within this list will appear in final yaml spec
 **/
public class OperationsFilter extends AbstractSpecFilter {
    private List<String> allowedOperations;

    public OperationsFilter(String[] allowedOperations) {
        if (allowedOperations != null) {
            this.allowedOperations = Arrays.asList(allowedOperations);
        }
    }

    @Override
    public boolean isOperationAllowed(
            Operation operation,
            ApiDescription api,
            Map<String, List<String>> params,
            Map<String, String> cookies,
            Map<String, List<String>> headers) {
        return allowedOperations == null ||
                allowedOperations.size() == 0 ||
                allowedOperations.stream().anyMatch(op -> operation.getOperationId().equalsIgnoreCase(op))
                ;
    }
}

@@ -16,9 +16,9 @@
  201
-9
+5
  her
@@ -1068,17 +1068,17 @@
 ecFilter
- 
+%0A
 %7B%0A    pr
@@ -1116,16 +1116,179 @@
 tions;%0A%0A
+    /**%0A     * Constructs the model class finder.%0A     *%0A     * @param allowedOperations the list of operations to be included in the resulting yaml spec.%0A     */%0A
     publ
@@ -1334,16 +1334,20 @@
 rations)
+%0A   
  %7B%0A     
@@ -1379,16 +1379,24 @@
 != null)
+%0A       
  %7B%0A     
@@ -1729,16 +1729,20 @@
 headers)
+%0A   
  %7B%0A     

Use Swagger-UI and Herd-SDK that contain only R/O endpoints