// Path: log-policy/src/main/java/io/apiman/plugins/log_policy/beans/LogHeadersConfigBean.java
// public class LogHeadersConfigBean {
// 
//     @JsonProperty
//     private LogHeadersDirectionType direction;
// 
//     /**
//      * Whether to log the request/response headers.
//      * Defaults to {@code true} for backwards compatibility.
//      */
//     @JsonProperty
//     private boolean logHeaders = true;
// 
//     /**
//      * Whether to log the response status code.
//      */
//     @JsonProperty
//     private boolean logStatusCode;
// 
//     /**
//      * Constructor.
//      */
//     public LogHeadersConfigBean() {
//     }
// 
//     /**
//      * @return the direction
//      */
//     public LogHeadersDirectionType getDirection() {
//         return direction;
//     }
// 
//     /**
//      * @param direction the direction to set
//      */
//     public void setDirection(LogHeadersDirectionType direction) {
//         this.direction = direction;
//     }
// 
//     /**
//      * @return whether to log the headers
//      */
//     public boolean isLogHeaders() {
//         return logHeaders;
//     }
// 
//     /**
//      * @return whether to log the status code
//      */
//     public boolean isLogStatusCode() {
//         return logStatusCode;
//     }
// }
// 
// Path: log-policy/src/main/java/io/apiman/plugins/log_policy/beans/LogHeadersDirectionType.java
// public enum LogHeadersDirectionType {
// 
//     request, response, both
// 
// }


import io.apiman.common.logging.IApimanLogger;
import io.apiman.gateway.engine.beans.ApiRequest;
import io.apiman.gateway.engine.beans.ApiResponse;
import io.apiman.gateway.engine.beans.util.HeaderMap;
import io.apiman.gateway.engine.policies.AbstractMappedPolicy;
import io.apiman.gateway.engine.policy.IPolicyChain;
import io.apiman.gateway.engine.policy.IPolicyContext;
import io.apiman.plugins.log_policy.beans.LogHeadersConfigBean;
import io.apiman.plugins.log_policy.beans.LogHeadersDirectionType;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeSet;
/*
 * Copyright 2015 JBoss Inc
 *
 * 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 io.apiman.plugins.log_policy;



/**
 * A policy that logs the headers of the HTTP request and HTTP response at the current position in the chain.
 *
 * @author ton.swieb@finalist.nl
 */
public class LogHeadersPolicy extends AbstractMappedPolicy<LogHeadersConfigBean> {

	private static enum HttpDirection {
		REQUEST("HTTP Request"),  //$NON-NLS-1$
		RESPONSE("HTTP Response"); //$NON-NLS-1$

		private final String description;

		HttpDirection(String descr) {
			this.description = descr;
		}

		String getDescription() {
			return description;
		}
	};

	public static final String ENDPOINT_ATTRIBUTE = "LogHeadersPolicy_EndpointAttribute"; //$NON-NLS-1$

    /**
     * Constructor.
     */
    public LogHeadersPolicy() {
    }

    /**
     * @see io.apiman.gateway.engine.policies.AbstractMappedPolicy#getConfigurationClass()
     */
    @Override
    protected Class<LogHeadersConfigBean> getConfigurationClass() {
        return LogHeadersConfigBean.class;
    }

    /**
     * @see io.apiman.gateway.engine.policies.AbstractMappedPolicy#doApply(io.apiman.gateway.engine.beans.ApiRequest, io.apiman.gateway.engine.policy.IPolicyContext, java.lang.Object, io.apiman.gateway.engine.policy.IPolicyChain)
     */
    @Override
    protected void doApply(ApiRequest request, IPolicyContext context, LogHeadersConfigBean config,
            IPolicyChain<ApiRequest> chain) {
        IApimanLogger logger = context.getLogger(getClass());
        String endpoint = request.getApi().getEndpoint();
        context.setAttribute(ENDPOINT_ATTRIBUTE, endpoint);
// Path: log-policy/src/main/java/io/apiman/plugins/log_policy/beans/LogHeadersConfigBean.java
// public class LogHeadersConfigBean {
// 
//     @JsonProperty
//     private LogHeadersDirectionType direction;
// 
//     /**
//      * Whether to log the request/response headers.
//      * Defaults to {@code true} for backwards compatibility.
//      */
//     @JsonProperty
//     private boolean logHeaders = true;
// 
//     /**
//      * Whether to log the response status code.
//      */
//     @JsonProperty
//     private boolean logStatusCode;
// 
//     /**
//      * Constructor.
//      */
//     public LogHeadersConfigBean() {
//     }
// 
//     /**
//      * @return the direction
//      */
//     public LogHeadersDirectionType getDirection() {
//         return direction;
//     }
// 
//     /**
//      * @param direction the direction to set
//      */
//     public void setDirection(LogHeadersDirectionType direction) {
//         this.direction = direction;
//     }
// 
//     /**
//      * @return whether to log the headers
//      */
//     public boolean isLogHeaders() {
//         return logHeaders;
//     }
// 
//     /**
//      * @return whether to log the status code
//      */
//     public boolean isLogStatusCode() {
//         return logStatusCode;
//     }
// }
// 
// Path: log-policy/src/main/java/io/apiman/plugins/log_policy/beans/LogHeadersDirectionType.java
// public enum LogHeadersDirectionType {
// 
//     request, response, both
// 
// }


// Path: log-policy/src/main/java/io/apiman/plugins/log_policy/LogHeadersPolicy.java
import io.apiman.common.logging.IApimanLogger;
import io.apiman.gateway.engine.beans.ApiRequest;
import io.apiman.gateway.engine.beans.ApiResponse;
import io.apiman.gateway.engine.beans.util.HeaderMap;
import io.apiman.gateway.engine.policies.AbstractMappedPolicy;
import io.apiman.gateway.engine.policy.IPolicyChain;
import io.apiman.gateway.engine.policy.IPolicyContext;
import io.apiman.plugins.log_policy.beans.LogHeadersConfigBean;
import io.apiman.plugins.log_policy.beans.LogHeadersDirectionType;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeSet;

/*
 * Copyright 2015 JBoss Inc
 *
 * 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 io.apiman.plugins.log_policy;



/**
 * A policy that logs the headers of the HTTP request and HTTP response at the current position in the chain.
 *
 * @author ton.swieb@finalist.nl
 */
public class LogHeadersPolicy extends AbstractMappedPolicy<LogHeadersConfigBean> {

	private static enum HttpDirection {
		REQUEST("HTTP Request"),  //$NON-NLS-1$
		RESPONSE("HTTP Response"); //$NON-NLS-1$

		private final String description;

		HttpDirection(String descr) {
			this.description = descr;
		}

		String getDescription() {
			return description;
		}
	};

	public static final String ENDPOINT_ATTRIBUTE = "LogHeadersPolicy_EndpointAttribute"; //$NON-NLS-1$

    /**
     * Constructor.
     */
    public LogHeadersPolicy() {
    }

    /**
     * @see io.apiman.gateway.engine.policies.AbstractMappedPolicy#getConfigurationClass()
     */
    @Override
    protected Class<LogHeadersConfigBean> getConfigurationClass() {
        return LogHeadersConfigBean.class;
    }

    /**
     * @see io.apiman.gateway.engine.policies.AbstractMappedPolicy#doApply(io.apiman.gateway.engine.beans.ApiRequest, io.apiman.gateway.engine.policy.IPolicyContext, java.lang.Object, io.apiman.gateway.engine.policy.IPolicyChain)
     */
    @Override
    protected void doApply(ApiRequest request, IPolicyContext context, LogHeadersConfigBean config,
            IPolicyChain<ApiRequest> chain) {
        IApimanLogger logger = context.getLogger(getClass());
        String endpoint = request.getApi().getEndpoint();
        context.setAttribute(ENDPOINT_ATTRIBUTE, endpoint);
        if (config.isLogHeaders() && config.getDirection() != LogHeadersDirectionType.response) {