All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.anotheria.moskito.webui.shared.action.BaseAJAXMoskitoUIAction Maven / Gradle / Ivy

There is a newer version: 4.0.3
Show newest version
package net.anotheria.moskito.webui.shared.action;

import net.anotheria.maf.action.AbortExecutionException;
import net.anotheria.maf.action.Action;
import net.anotheria.maf.action.ActionCommand;
import net.anotheria.maf.action.ActionMapping;
import net.anotheria.maf.bean.FormBean;
import net.anotheria.maf.json.JSONResponse;
import org.json.JSONException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Abstract AJAX action.
 *
 * @author Vasyl Zarva
 */
public abstract class BaseAJAXMoskitoUIAction implements Action {

	/**
	 * {@code Log4j} {@link Logger} instance.
	 */
	private static final Logger LOGGER = LoggerFactory.getLogger(BaseAJAXMoskitoUIAction.class);

	/**
	 * Encoding.
	 */
	private static final String UTF_8 = "UTF-8";

	/**
	 * Content type.
	 */
	private static final String TEXT_X_JSON = "application/json";

	/**
	 * Internal server error message.
	 */
	public static final String ERROR_MSG_INTERNAL_SERVER_ERROR = "Internal server error";


	@Override
	public final ActionCommand execute(ActionMapping mapping, FormBean bean, HttpServletRequest req, HttpServletResponse res) throws AbortExecutionException {
		JSONResponse response = new JSONResponse();

		try {

			invokeExecute(mapping, bean, req, res, response);
			writeTextToResponse(res, response);
			return null;
		} catch (Exception e) {
			LOGGER.error("execute(mapping, bean, req, res) fail.", e);
			JSONResponse result = new JSONResponse();
			result.addError(ERROR_MSG_INTERNAL_SERVER_ERROR);
			try {
				writeTextToResponse(res, result);
				return null;
			} catch (final IOException ioe) {
				LOGGER.error("execute(mapping,bean,req,res) fail.", ioe);
				throw new AbortExecutionException();
			}
		}
	}

	/**
	 * Override this method for invoking main action code.
	 *
	 * @param mapping
	 * 		- action mapping
	 * @param bean
	 * 		- bean
	 * @param req
	 * 		- request
	 * @param res
	 * 		- response
	 * @param jsonResponse
	 * 		- JSON Response
	 * @throws AbortExecutionException
	 * @throws JSONException
	 * @throws IOException
	 */
	protected void invokeExecute(final ActionMapping mapping, final FormBean bean, final HttpServletRequest req, final HttpServletResponse res, final JSONResponse jsonResponse)
			throws AbortExecutionException, IOException, JSONException {

	}

	/**
	 * Writes specified text to response and flushes the stream.
	 *
	 * @param res
	 * 		{@link HttpServletRequest}
	 * @param jsonResponse
	 * 		{@link JSONResponse}
	 * @throws java.io.IOException
	 * 		if an input or output exception occurred
	 */
	private static void writeTextToResponse(final HttpServletResponse res, final JSONResponse jsonResponse) throws IOException {
		writeTextToResponse(res, jsonResponse.toString());
	}

	/**
	 * Writes specified text to response and flushes the stream.
	 *
	 * @param res
	 * 		{@link HttpServletRequest}
	 * @param text
	 * 		{@link String}
	 * @throws java.io.IOException
	 * 		if an input or output exception occurred
	 */
	private static void writeTextToResponse(final HttpServletResponse res, final String text) throws IOException {
		res.setCharacterEncoding(UTF_8);
		res.setContentType(TEXT_X_JSON);
		PrintWriter writer = res.getWriter();
		writer.write(text);
		writer.flush();
	}

	@Override
	public void preProcess(ActionMapping mapping, HttpServletRequest req, HttpServletResponse res) throws Exception {

	}

	@Override
	public void postProcess(ActionMapping mapping, HttpServletRequest req, HttpServletResponse res) throws Exception {

	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy