/*
Copyright 2017 Integration Partners B.V.

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 nl.nn.adapterframework.http.rest;

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

import nl.nn.adapterframework.configuration.ConfigurationException;
import nl.nn.adapterframework.configuration.HasSpecialDefaultValues;
import nl.nn.adapterframework.core.HasPhysicalDestination;
import nl.nn.adapterframework.core.ListenerException;
import nl.nn.adapterframework.http.PushingListenerAdapter;

public class ApiListener extends PushingListenerAdapter implements HasPhysicalDestination, HasSpecialDefaultValues {

	private String uriPattern;
	private String method;
	private String authenticationMethod = null;
	private String consumes = "ANY";
	private String produces = "ANY";
	private List<String> mediaTypes = Arrays.asList("XML", "JSON", "TEXT");

	/**
	 * initialize listener and register <code>this</code> to the JNDI
	 */
	public void configure() throws ConfigurationException {
		ApiServiceDispatcher.getInstance().registerServiceClient(this, getCleanPattern(), getMethod());
	}

	public void close() {
		ApiServiceDispatcher.getInstance().unregisterServiceClient(getCleanPattern(), getMethod());
		super.close();
	}

	public String processRequest(String correlationId, String message, Map requestContext) throws ListenerException {
		String response = super.processRequest(correlationId, message, requestContext);

//		System.out.println("message: " + message);
//		System.out.println("response: " + response);

		return response;
	}

	public Object getSpecialDefaultValue(String attributeName, Object defaultValue, Map<String, String> attributes) {
		return defaultValue;
	}

	public String getPhysicalDestinationName() {
		return "uriPattern: "+getUriPattern()+"; method: "+getMethod();
	}

	public void setUriPattern(String uriPattern) {
		this.uriPattern = uriPattern;
	}
	public String getUriPattern() {
		return uriPattern;
	}
	public String getCleanPattern() {
		String pattern = uriPattern;
		if(pattern.startsWith("/"))
			pattern = pattern.substring(1);

		if(pattern.endsWith("/"))
			pattern = pattern.substring(0, pattern.length()-1);

		return pattern.replaceAll("\\{.*?}", "*");
	}

	public String getMethod() {
		return method;
	}
	public void setMethod(String method) {
		this.method = method;
	}

	//TODO add authenticationType

	public void setAuthenticationMethod(String method) {
		if(method.equalsIgnoreCase("token")) {
			this.authenticationMethod = "token";
		}
	}

	public String getAuthenticationMethod() {
		return this.authenticationMethod;
	}

	public void setConsumes(String consumes) throws ConfigurationException {
		if(mediaTypes.contains(consumes))
			this.consumes = consumes;
		else
			throw new ConfigurationException("Unknown mediatype ["+consumes+"]");
	}
	public String getConsumes() {
		return consumes;
	}

	public void setProduces(String produces) throws ConfigurationException {
		if(mediaTypes.contains(produces))
			this.produces = produces;
		else
			throw new ConfigurationException("Unknown mediatype ["+produces+"]");
	}
	public String getProduces() {
		return produces;
	}

	public boolean getGenerateEtag() {
		return true;
	}
}

@@ -989,16 +989,119 @@
 apter;%0A%0A
+/**%0A * The new and improved RESTful Listener now available for use!%0A * %0A * @author Niels Meijer%0A *%0A */%0A
 public c
@@ -1307,16 +1307,54 @@
 = null;%0A
+%09private boolean generateEtag = true;%0A
 %09private
@@ -1462,16 +1462,23 @@
 .asList(
+%22ANY%22, 
 %22XML%22, %22
@@ -2376,22 +2376,40 @@
 e() %7B%0A%09%09
-return
+String destinationName =
  %22uriPat
@@ -2454,24 +2454,244 @@
 etMethod();%0A
+%09%09if(!getConsumes().equalsIgnoreCase(%22ANY%22))%0A%09%09%09destinationName += %22; consumes: %22+getConsumes();%0A%09%09if(!getProduces().equalsIgnoreCase(%22ANY%22))%0A%09%09%09destinationName += %22; produces: %22+getProduces();%0A%09%09return destinationName;%0A
 %09%7D%0A%0A%09public 
@@ -3528,32 +3528,69 @@
 tionException %7B%0A
+%09%09consumes = consumes.toUpperCase();%0A
 %09%09if(mediaTypes.
@@ -3849,16 +3849,53 @@
 ption %7B%0A
+%09%09produces = produces.toUpperCase();%0A
 %09%09if(med
@@ -4080,32 +4080,124 @@
 n produces;%0A%09%7D%0A%0A
+%09public void setGenerateEtag(boolean generateEtag) %7B%0A%09%09this.generateEtag = generateEtag;%0A%09%7D%0A
 %09public boolean 
@@ -4225,19 +4225,27 @@
 %09return 
-true
+generateEtag
 ;%0A%09%7D%0A%7D%0A

Fix ApiListener destinationName, Add generateEtag property