// Path: src/main/java/com/balda/dialogflow/ApiAiPlugData.java
// public interface ApiAiPlugData {
// 	void toJson(Gson json, Map<String, JsonElement> map);
// }
// 
// Path: src/main/java/com/balda/keys/TokenException.java
// public class TokenException extends RuntimeException {
// 
// 	/**
// 	 * 
// 	 */
// 	private static final long serialVersionUID = -8833230552844748261L;
// 
// 	public TokenException() {
// 		super();
// 	}
// 
// 	public TokenException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
// 		super(message, cause, enableSuppression, writableStackTrace);
// 	}
// 
// 	public TokenException(String message, Throwable cause) {
// 		super(message, cause);
// 	}
// 
// 	public TokenException(String message) {
// 		super(message);
// 	}
// 
// 	public TokenException(Throwable cause) {
// 		super(cause);
// 	}
// }


import com.google.gson.Gson;
import ai.api.GsonFactory;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.balda.dialogflow.ApiAiPlugData;
import com.balda.keys.TokenException;
/*
 * Copyright 2017-2018 Marco Stornelli <playappassistance@gmail.com>
 * 
 * This file is part of Google Actions project
 *
 * Google Actions is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Google Actions is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Google Actions.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.balda.dialogflow.v1;





/**
 * Base class to be extended to create an dialog flow webhook
 *
 * @param <T>
 *            The class for custom data, RootResponse for Google, EmptyPlugData
 *            for none
 */
public abstract class DFWebhookServlet<T extends ApiAiPlugData> extends HttpServlet {
	private static final Logger log = Logger.getLogger(DFWebhookServlet.class.getName());
	private static final String RESPONSE_CONTENT_TYPE = "application/json";

	private static final String RESPONSE_CHARACTER_ENCODING = "utf-8";

	private static final long serialVersionUID = 1L;

	protected final Gson gson = GsonFactory.getDefaultFactory().getGson();

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		if (!verifyIdentity(request)) {
			response.sendError(HttpServletResponse.SC_FORBIDDEN);
			return;
		}
		DFWebhookResponse<T> output = new DFWebhookResponse<>();
		try {
			doWebhook(gson.fromJson(request.getReader(), DFWebhookRequest.class), output);
// Path: src/main/java/com/balda/dialogflow/ApiAiPlugData.java
// public interface ApiAiPlugData {
// 	void toJson(Gson json, Map<String, JsonElement> map);
// }
// 
// Path: src/main/java/com/balda/keys/TokenException.java
// public class TokenException extends RuntimeException {
// 
// 	/**
// 	 * 
// 	 */
// 	private static final long serialVersionUID = -8833230552844748261L;
// 
// 	public TokenException() {
// 		super();
// 	}
// 
// 	public TokenException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
// 		super(message, cause, enableSuppression, writableStackTrace);
// 	}
// 
// 	public TokenException(String message, Throwable cause) {
// 		super(message, cause);
// 	}
// 
// 	public TokenException(String message) {
// 		super(message);
// 	}
// 
// 	public TokenException(Throwable cause) {
// 		super(cause);
// 	}
// }


// Path: src/main/java/com/balda/dialogflow/v1/DFWebhookServlet.java
import com.google.gson.Gson;
import ai.api.GsonFactory;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.balda.dialogflow.ApiAiPlugData;
import com.balda.keys.TokenException;

/*
 * Copyright 2017-2018 Marco Stornelli <playappassistance@gmail.com>
 * 
 * This file is part of Google Actions project
 *
 * Google Actions is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Google Actions is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Google Actions.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.balda.dialogflow.v1;





/**
 * Base class to be extended to create an dialog flow webhook
 *
 * @param <T>
 *            The class for custom data, RootResponse for Google, EmptyPlugData
 *            for none
 */
public abstract class DFWebhookServlet<T extends ApiAiPlugData> extends HttpServlet {
	private static final Logger log = Logger.getLogger(DFWebhookServlet.class.getName());
	private static final String RESPONSE_CONTENT_TYPE = "application/json";

	private static final String RESPONSE_CHARACTER_ENCODING = "utf-8";

	private static final long serialVersionUID = 1L;

	protected final Gson gson = GsonFactory.getDefaultFactory().getGson();

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		if (!verifyIdentity(request)) {
			response.sendError(HttpServletResponse.SC_FORBIDDEN);
			return;
		}
		DFWebhookResponse<T> output = new DFWebhookResponse<>();
		try {
			doWebhook(gson.fromJson(request.getReader(), DFWebhookRequest.class), output);
		} catch (TokenException e) {