From c140cee5953f4518a75918ac6994ac1d13ce11cc Mon Sep 17 00:00:00 2001 From: Roland Haeder Date: Wed, 2 Sep 2015 08:34:35 +0200 Subject: [PATCH] Added from pizza-service as they are generic enough Signed-off-by:Roland Haeder --- .../filter/servlet/BaseServletFilter.java | 80 +++++++++++++++++++ .../servlet/utf8/Utf8ServletFilter.java | 58 ++++++++++++++ 2 files changed, 138 insertions(+) create mode 100644 src/org/mxchange/jsfcore/filter/servlet/BaseServletFilter.java create mode 100644 src/org/mxchange/jsfcore/filter/servlet/utf8/Utf8ServletFilter.java diff --git a/src/org/mxchange/jsfcore/filter/servlet/BaseServletFilter.java b/src/org/mxchange/jsfcore/filter/servlet/BaseServletFilter.java new file mode 100644 index 0000000..24bf76e --- /dev/null +++ b/src/org/mxchange/jsfcore/filter/servlet/BaseServletFilter.java @@ -0,0 +1,80 @@ +/* + * Copyright (C) 2015 Roland Haeder + * + * This program 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. + * + * This program 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 this program. If not, see . + */ +package org.mxchange.jsfcore.filter.servlet; + +import java.text.MessageFormat; +import javax.servlet.Filter; +import javax.servlet.FilterConfig; +import javax.servlet.ServletException; +import org.mxchange.jcore.BaseFrameworkSystem; + +/** + * A general servlet filter class. If you need to override init() or + * destroy() please always call the super method first and handle over all + * parameter. + * + * @author Roland Haeder + */ +public abstract class BaseServletFilter extends BaseFrameworkSystem implements Filter { + /** + * Configuration instance + */ + private FilterConfig config; + + /** + * Destroys this filter + */ + @Override + public void destroy () { + // Unset config instance + this.setConfig(null); + } + + /** + * Initializes this filter + * + * @param filterConfig Filter configuration + * @throws ServletException + */ + @Override + public void init (final FilterConfig filterConfig) throws ServletException { + // Trace message + this.getLogger().trace(MessageFormat.format("filterConfig={0} - CALLED!", filterConfig)); //NOI18N + + // Set config instance + this.setConfig(filterConfig); + + // Trace message + this.getLogger().trace("EXIT!"); //NOI18N + } + + /** + * Configuration instance + * @return the config + */ + protected final FilterConfig getConfig () { + return this.config; + } + + /** + * Configuration instance + * @param config the config to set + */ + private void setConfig (final FilterConfig config) { + this.config = config; + } +} diff --git a/src/org/mxchange/jsfcore/filter/servlet/utf8/Utf8ServletFilter.java b/src/org/mxchange/jsfcore/filter/servlet/utf8/Utf8ServletFilter.java new file mode 100644 index 0000000..949c08e --- /dev/null +++ b/src/org/mxchange/jsfcore/filter/servlet/utf8/Utf8ServletFilter.java @@ -0,0 +1,58 @@ +/* + * Copyright (C) 2015 Roland Haeder + * + * This program 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. + * + * This program 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 this program. If not, see . + */ +package org.mxchange.jsfcore.filter.servlet.utf8; + +import java.io.IOException; +import java.text.MessageFormat; +import javax.servlet.Filter; +import javax.servlet.FilterChain; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import org.mxchange.jsfcore.filter.servlet.BaseServletFilter; + +/** + * A HTTP filter for setting UTF-8 character encoding. + * + * @author Roland Haeder + */ +public class Utf8ServletFilter extends BaseServletFilter implements Filter { + /** + * Filter to set UTF-8 encoding + * + * @param request ServletRequest instance + * @param response ServletResponse instance + * @param chain FilterChain instance + * @throws IOException + * @throws ServletException + */ + @Override + public void doFilter (final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException { + // Trace message + this.getLogger().trace(MessageFormat.format("request={0},response={1},chain={2} - CALLED!", request, response, chain)); //NOI18N + + // Call super method + chain.doFilter(request, response); + + // Set response/request both to UTF-8 + request.setCharacterEncoding("UTF-8"); //NOI18N + response.setCharacterEncoding("UTF-8"); //NOI18N + + // Trace message + this.getLogger().trace("EXIT!"); //NOI18N + } +} -- 2.39.5