From: Roland Haeder Date: Tue, 18 Aug 2015 13:22:35 +0000 (+0200) Subject: Continued with project: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=f3b3ea942ca73068a00f1d85b9e4ed47080208a5;p=pizzaservice-war.git Continued with project: - added filter for handling added basket items - added frontend class for baskets (not their items) as they may be stored in database for later reusage - Renamed "FooHttpFilter" to "FooServletFilter" as they actually are servlet filters - Other minor improvements - BaseServletFilter is now abstract and implements Filter, so all filters extendending (and re-implementing Filter) must implement at least doFilter(). Please call chain.doFilter() in your own filter Signed-off-by:Roland Häder --- diff --git a/src/java/org/mxchange/pizzaapplication/basket/BaseBasket.java b/src/java/org/mxchange/pizzaapplication/basket/BaseBasket.java new file mode 100644 index 00000000..fa81756f --- /dev/null +++ b/src/java/org/mxchange/pizzaapplication/basket/BaseBasket.java @@ -0,0 +1,68 @@ +/* + * 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.pizzaapplication.basket; + +import java.sql.SQLException; +import org.mxchange.pizzaapplication.database.frontend.basket.BasketDatabaseFrontend; +import java.text.MessageFormat; +import java.util.LinkedHashMap; +import java.util.Map; +import org.mxchange.jcore.BaseFrameworkSystem; +import org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException; +import org.mxchange.pizzaapplication.database.frontend.basket.BasketFrontend; +import org.mxchange.pizzaapplication.item.AddableBasketItem; + +/** + * A general basket class + * + * @author Roland Haeder + */ +public class BaseBasket extends BaseFrameworkSystem implements Basket { + /** + * Item map + */ + private final Map items; + + /** + * Protected constructor with session instance + * + * @throws org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException If an unsupported backend has been configured + * @throws java.sql.SQLException If an SQL error occurs + */ + protected BaseBasket () throws UnsupportedDatabaseBackendException, SQLException { + // Trace message + this.getLogger().trace("CALLED!"); + + // Init item map instance + this.items = new LinkedHashMap<>(); + + // Create frontend instance + BasketFrontend frontend = new BasketDatabaseFrontend(); + + // Debug message + this.getLogger().debug(MessageFormat.format("frontend={0} being set ...", frontend)); + + // Instance it here + this.setFrontend(frontend); + } + + @Override + public void addItem (final AddableBasketItem item) { + // Add it to map + this.items.put(item.getId(), item); + } +} diff --git a/src/java/org/mxchange/pizzaapplication/basket/Basket.java b/src/java/org/mxchange/pizzaapplication/basket/Basket.java new file mode 100644 index 00000000..182ea4d1 --- /dev/null +++ b/src/java/org/mxchange/pizzaapplication/basket/Basket.java @@ -0,0 +1,36 @@ +/* + * 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.pizzaapplication.basket; + +import org.mxchange.jcore.FrameworkInterface; +import org.mxchange.pizzaapplication.item.AddableBasketItem; + +/** + * An interface for baskets + * + * @author Roland Haeder + * @param Any addable basket items + */ +public interface Basket extends FrameworkInterface { + + /** + * Adds given item instance to this basket + * @param item Item instance to add + */ + public void addItem (final AddableBasketItem item); + +} diff --git a/src/java/org/mxchange/pizzaapplication/basket/item/ItemBasket.java b/src/java/org/mxchange/pizzaapplication/basket/item/ItemBasket.java new file mode 100644 index 00000000..53e71e84 --- /dev/null +++ b/src/java/org/mxchange/pizzaapplication/basket/item/ItemBasket.java @@ -0,0 +1,64 @@ +/* + * 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.pizzaapplication.basket.item; + +import java.sql.SQLException; +import javax.servlet.http.HttpSession; +import org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException; +import org.mxchange.pizzaapplication.basket.BaseBasket; +import org.mxchange.pizzaapplication.basket.Basket; +import org.mxchange.pizzaapplication.item.AddableBasketItem; + +/** + * A basket for orderable items + * + * @author Roland Haeder + */ +public class ItemBasket extends BaseBasket implements Basket { + + /** + * Creates a singleton instance from given session's id + * + * @param session + * @return A session-id based singleton instance of this basket + * @throws org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException If an unsupported backend was configured + * @throws java.sql.SQLException If an SQL error occurs + */ + @SuppressWarnings ("unchecked") + public final static Basket getInstance (final HttpSession session) throws UnsupportedDatabaseBackendException, SQLException { + // Get instance + Basket basket = (Basket) session.getAttribute("basket"); + + // Is the basket already created? + if (!(basket instanceof BaseBasket)) { + // Not yet, so create it + basket = new ItemBasket(); + + // Add it in session + session.setAttribute("basket", basket); + } + + // Return it casted + return basket; + } + + /** + * Default constructor to be able to throw exceptions from super constructor + */ + private ItemBasket () throws UnsupportedDatabaseBackendException, SQLException { + } +} diff --git a/src/java/org/mxchange/pizzaapplication/database/frontend/basket/BasketDatabaseFrontend.java b/src/java/org/mxchange/pizzaapplication/database/frontend/basket/BasketDatabaseFrontend.java new file mode 100644 index 00000000..b98caccf --- /dev/null +++ b/src/java/org/mxchange/pizzaapplication/database/frontend/basket/BasketDatabaseFrontend.java @@ -0,0 +1,108 @@ +/* + * 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.pizzaapplication.database.frontend.basket; + +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.sql.SQLException; +import java.text.MessageFormat; +import java.util.Map; +import org.mxchange.jcore.database.frontend.BaseDatabaseFrontend; +import org.mxchange.jcore.database.storage.Storeable; +import org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException; + +/** + * A database frontend for baskets + * + * @author Roland Haeder + */ +public class BasketDatabaseFrontend extends BaseDatabaseFrontend implements BasketFrontend { + /** + * General constructor + * + * @throws org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException If an unsupported backend was configured + * @throws java.sql.SQLException If any SQL error occurs + */ + public BasketDatabaseFrontend () throws UnsupportedDatabaseBackendException, SQLException { + // Trace message + this.getLogger().trace("CALLED!"); //NOI18N + + // Set "table" name + this.setTableName("basket"); //NOI18N + + // Initalize backend + this.initBackend(); + } + + /** + * Shuts down the database layer + * + * @throws java.sql.SQLException If an SQL error occurs + * @throws java.io.IOException If any IO error occurs + */ + @Override + public void doShutdown () throws SQLException, IOException { + // Trace message + this.getLogger().trace("CALLED!"); //NOI18N + + // Shutdown backend + this.getBackend().doShutdown(); + + // Trace message + this.getLogger().trace("EXIT!"); //NOI18N + } + + /** + * Depending on column, an empty value may be converted to null + * + * @param key Key to check + * @param value Value to check + * @return Possible previous value or null + * @todo Nothing will be changed now + */ + @Override + public Object emptyStringToNull (final String key, final Object value) { + // Trace message + this.getLogger().trace(MessageFormat.format("key={0},value={1} - CALLED!", key, value)); //NOI18N + + // Copy value + Object v = value; + + // Is the value empty? + if ((value instanceof String) && ("".equals(value))) { //NOI18N + // This value may need to be changed + switch (key) { + } + } + + // Trace message + this.getLogger().trace(MessageFormat.format("v={0} - EXIT!", v)); //NOI18N + + // Return it + return v; + } + + @Override + public String getIdName () { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public Storeable toStoreable (final Map map) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { + throw new UnsupportedOperationException("Not supported yet: map=" + map); + } +} diff --git a/src/java/org/mxchange/pizzaapplication/database/frontend/basket/BasketFrontend.java b/src/java/org/mxchange/pizzaapplication/database/frontend/basket/BasketFrontend.java new file mode 100644 index 00000000..e1efd84f --- /dev/null +++ b/src/java/org/mxchange/pizzaapplication/database/frontend/basket/BasketFrontend.java @@ -0,0 +1,27 @@ +/* + * 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.pizzaapplication.database.frontend.basket; + +import org.mxchange.jcore.database.frontend.DatabaseFrontend; + +/** + * An interface for basket database frontends + * + * @author Roland Haeder + */ +public interface BasketFrontend extends DatabaseFrontend { +} diff --git a/src/java/org/mxchange/pizzaapplication/database/frontend/category/PizzaCategoryDatabaseFrontend.java b/src/java/org/mxchange/pizzaapplication/database/frontend/category/PizzaCategoryDatabaseFrontend.java index 4b358b42..d3b2c7cb 100644 --- a/src/java/org/mxchange/pizzaapplication/database/frontend/category/PizzaCategoryDatabaseFrontend.java +++ b/src/java/org/mxchange/pizzaapplication/database/frontend/category/PizzaCategoryDatabaseFrontend.java @@ -68,12 +68,12 @@ public class PizzaCategoryDatabaseFrontend extends BaseDatabaseFrontend implemen @Override public void addCategory (final String title, final Integer parent) throws SQLException, IOException { // Trace message - this.getLogger().trace(MessageFormat.format("title={0},parent={1} - CALLED!", title, parent)); + this.getLogger().trace(MessageFormat.format("title={0},parent={1} - CALLED!", title, parent)); //NOI18N // Title should not be null if (title == null) { // Abort here - throw new NullPointerException("title is null"); + throw new NullPointerException("title is null"); //NOI18N } // Clear dataset from previous usage @@ -87,10 +87,10 @@ public class PizzaCategoryDatabaseFrontend extends BaseDatabaseFrontend implemen Result result = this.doInsertDataSet(); // Debug message - this.getLogger().debug(MessageFormat.format("result={0}", result)); + this.getLogger().debug(MessageFormat.format("result={0}", result)); //NOI18N // Trace message - this.getLogger().trace("EXIT!"); + this.getLogger().trace("EXIT!"); //NOI18N } /** @@ -120,13 +120,13 @@ public class PizzaCategoryDatabaseFrontend extends BaseDatabaseFrontend implemen @Override public Object emptyStringToNull (final String key, final Object value) { // Trace message - this.getLogger().trace(MessageFormat.format("key={0},value={1} - CALLED!", key, value)); + this.getLogger().trace(MessageFormat.format("key={0},value={1} - CALLED!", key, value)); //NOI18N // Copy value Object v = value; // Is the value empty? - if ((value instanceof String) && ("".equals(value))) { + if ((value instanceof String) && ("".equals(value))) { //NOI18N // This value may need to be changed switch (key) { case PizzaCategoryDatabaseConstants.COLUMN_PARENT: // Convert this @@ -136,7 +136,7 @@ public class PizzaCategoryDatabaseFrontend extends BaseDatabaseFrontend implemen } // Trace message - this.getLogger().trace(MessageFormat.format("v={0} - EXIT!", v)); + this.getLogger().trace(MessageFormat.format("v={0} - EXIT!", v)); //NOI18N // Return it return v; @@ -170,22 +170,22 @@ public class PizzaCategoryDatabaseFrontend extends BaseDatabaseFrontend implemen @Override public Category getCategory (final Product product) throws IOException, BadTokenException, CorruptedDatabaseFileException, SQLException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { // Trace message - this.getLogger().trace(MessageFormat.format("product={0} - CALLED!", product)); + this.getLogger().trace(MessageFormat.format("product={0} - CALLED!", product)); //NOI18N // product must not be null if (product == null) { // Abort here - throw new NullPointerException("product is null"); + throw new NullPointerException("product is null"); //NOI18N } // Get category id from it Long id = product.getCategory(); // Debug message - this.getLogger().debug(MessageFormat.format("id={0}", id)); + this.getLogger().debug(MessageFormat.format("id={0}", id)); //NOI18N // It should be >0 here - assert(id > 0) : MessageFormat.format("id={0} must be larger zero", id); + assert(id > 0) : MessageFormat.format("id={0} must be larger zero", id); //NOI18N // Then construct a search instance SearchableCriteria criteria = new SearchCriteria(); @@ -200,7 +200,7 @@ public class PizzaCategoryDatabaseFrontend extends BaseDatabaseFrontend implemen Result result = this.getBackend().doSelectByCriteria(criteria); // Debug log - this.getLogger().debug(MessageFormat.format("result({0})={1}", result, result.size())); + this.getLogger().debug(MessageFormat.format("result({0})={1}", result, result.size())); //NOI18N // Init category instance Category category = null; @@ -211,7 +211,7 @@ public class PizzaCategoryDatabaseFrontend extends BaseDatabaseFrontend implemen Storeable storeable = result.next(); // Debug message - this.getLogger().debug(MessageFormat.format("storeable={0}", storeable)); + this.getLogger().debug(MessageFormat.format("storeable={0}", storeable)); //NOI18N // Is it instance of Category? if (storeable instanceof Category) { @@ -221,7 +221,7 @@ public class PizzaCategoryDatabaseFrontend extends BaseDatabaseFrontend implemen } // Trace message - this.getLogger().trace(MessageFormat.format("category={0} - EXIT!", category)); + this.getLogger().trace(MessageFormat.format("category={0} - EXIT!", category)); //NOI18N // Return it return category; @@ -243,7 +243,7 @@ public class PizzaCategoryDatabaseFrontend extends BaseDatabaseFrontend implemen @Override public Result getResultFromSet (final ResultSet resultSet) throws SQLException { // Trace message - this.getLogger().trace(MessageFormat.format("resultSet={0} - CALLED!", resultSet)); + this.getLogger().trace(MessageFormat.format("resultSet={0} - CALLED!", resultSet)); //NOI18N // Init result instance Result result = new DatabaseResult(); @@ -259,20 +259,20 @@ public class PizzaCategoryDatabaseFrontend extends BaseDatabaseFrontend implemen Long parent = resultSet.getLong(PizzaCategoryDatabaseConstants.COLUMN_PARENT); // Debug message - this.getLogger().debug(MessageFormat.format("id={0},title={1},parent={2}", id, title, parent)); + this.getLogger().debug(MessageFormat.format("id={0},title={1},parent={2}", id, title, parent)); //NOI18N // Instance new object Category category = new ProductCategory(id, title, parent); // Debug log - this.getLogger().debug(MessageFormat.format("category={0}", category)); + this.getLogger().debug(MessageFormat.format("category={0}", category)); //NOI18N // Add it to result result.add(category); } // Trace message - this.getLogger().trace(MessageFormat.format("result({0})={1} - EXIT!", result.size(), result)); + this.getLogger().trace(MessageFormat.format("result({0})={1} - EXIT!", result.size(), result)); //NOI18N // Return result return result; @@ -287,7 +287,7 @@ public class PizzaCategoryDatabaseFrontend extends BaseDatabaseFrontend implemen @Override public boolean isCategoryTitleUsed (final String title) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { // Trace message - this.getLogger().trace(MessageFormat.format("title={0} - CALLED!", title)); + this.getLogger().trace(MessageFormat.format("title={0} - CALLED!", title)); //NOI18N // Get search criteria SearchableCriteria criteria = new SearchCriteria(); @@ -302,13 +302,13 @@ public class PizzaCategoryDatabaseFrontend extends BaseDatabaseFrontend implemen Result result = this.getBackend().doSelectByCriteria(criteria); // Debug log - this.getLogger().debug(MessageFormat.format("result({0})={1}", result, result.size())); + this.getLogger().debug(MessageFormat.format("result({0})={1}", result, result.size())); //NOI18N // Now check size of the result boolean isFound = (result.size() == 1); // Trace message - this.getLogger().trace(MessageFormat.format("isFound={0} - EXIT!", isFound)); + this.getLogger().trace(MessageFormat.format("isFound={0} - EXIT!", isFound)); //NOI18N // Return it return isFound; @@ -325,19 +325,19 @@ public class PizzaCategoryDatabaseFrontend extends BaseDatabaseFrontend implemen @Override public Storeable toStoreable (final Map map) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { // Trace message - this.getLogger().trace(MessageFormat.format("map={0} - CALLED!", map)); + this.getLogger().trace(MessageFormat.format("map={0} - CALLED!", map)); //NOI18N // Is map null? if (map == null) { // Is null - throw new NullPointerException("map is null"); + throw new NullPointerException("map is null"); //NOI18N } else if (map.isEmpty()) { // Map is empty - throw new IllegalArgumentException("map is empty."); + throw new IllegalArgumentException("map is empty."); //NOI18N } // Debug message - this.getLogger().debug(MessageFormat.format("Has to handle {0} entries", map.size())); + this.getLogger().debug(MessageFormat.format("Has to handle {0} entries", map.size())); //NOI18N // Get iterator on all entries Iterator> iterator = map.entrySet().iterator(); @@ -351,14 +351,14 @@ public class PizzaCategoryDatabaseFrontend extends BaseDatabaseFrontend implemen Map.Entry entry = iterator.next(); // Debug message - this.getLogger().debug(MessageFormat.format("entry:{0}={1}", entry.getKey(), entry.getValue())); + this.getLogger().debug(MessageFormat.format("entry:{0}={1}", entry.getKey(), entry.getValue())); //NOI18N // Try to set value instance.setValueFromColumn(entry.getKey(), entry.getValue()); } // Trace message - this.getLogger().trace(MessageFormat.format("instance={0} - EXIT!", instance)); + this.getLogger().trace(MessageFormat.format("instance={0} - EXIT!", instance)); //NOI18N // Return it return instance; diff --git a/src/java/org/mxchange/pizzaapplication/filter/http/BaseServletFilter.java b/src/java/org/mxchange/pizzaapplication/filter/http/BaseServletFilter.java deleted file mode 100644 index ba8f30b2..00000000 --- a/src/java/org/mxchange/pizzaapplication/filter/http/BaseServletFilter.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * 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.pizzaapplication.filter.http; - -import javax.servlet.FilterConfig; -import javax.servlet.ServletException; -import org.mxchange.jcore.BaseFrameworkSystem; - -/** - * A general HTTP servlet filter class - * @author Roland Haeder - */ -public class BaseServletFilter extends BaseFrameworkSystem { - /** - * Configuration instance - */ - private FilterConfig config; - - /** - * Destroys this filter - */ - public void destroy () { - // Unset config instance - this.setConfig(null); - } - - /** - * Initializes this filter - * - * @param filterConfig Filter configuration - * @throws ServletException - */ - public void init (final FilterConfig filterConfig) throws ServletException { - // Set config instance - this.setConfig(filterConfig); - } - - /** - * Configuration instance - * @return the config - */ - protected FilterConfig getConfig () { - return this.config; - } - - /** - * Configuration instance - * @param config the config to set - */ - protected void setConfig (final FilterConfig config) { - this.config = config; - } -} diff --git a/src/java/org/mxchange/pizzaapplication/filter/http/utf8/Utf8HttpFilter.java b/src/java/org/mxchange/pizzaapplication/filter/http/utf8/Utf8HttpFilter.java deleted file mode 100644 index 9bdc862c..00000000 --- a/src/java/org/mxchange/pizzaapplication/filter/http/utf8/Utf8HttpFilter.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * 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.pizzaapplication.filter.http.utf8; - -import java.io.IOException; -import javax.servlet.Filter; -import javax.servlet.FilterChain; -import javax.servlet.ServletException; -import javax.servlet.ServletRequest; -import javax.servlet.ServletResponse; -import org.mxchange.pizzaapplication.filter.http.BaseServletFilter; - -/** - * A HTTP filter for setting UTF-8 character encoding. - * - * @author Roland Haeder - */ -public class Utf8HttpFilter 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 { - // Call super method - chain.doFilter(request, response); - - // Set response/request both to UTF-8 - request.setCharacterEncoding("UTF-8"); //NOI18N - response.setCharacterEncoding("UTF-8"); //NOI18N - } -} diff --git a/src/java/org/mxchange/pizzaapplication/filter/servlet/BaseServletFilter.java b/src/java/org/mxchange/pizzaapplication/filter/servlet/BaseServletFilter.java new file mode 100644 index 00000000..382efbf3 --- /dev/null +++ b/src/java/org/mxchange/pizzaapplication/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.pizzaapplication.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)); + + // Set config instance + this.setConfig(filterConfig); + + // Trace message + this.getLogger().trace("EXIT!"); + } + + /** + * 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/java/org/mxchange/pizzaapplication/filter/servlet/basket/BasketItemAddedFilter.java b/src/java/org/mxchange/pizzaapplication/filter/servlet/basket/BasketItemAddedFilter.java new file mode 100644 index 00000000..21fecbca --- /dev/null +++ b/src/java/org/mxchange/pizzaapplication/filter/servlet/basket/BasketItemAddedFilter.java @@ -0,0 +1,105 @@ +/* + * 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.pizzaapplication.filter.servlet.basket; + +import java.io.IOException; +import java.sql.SQLException; +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 javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpSession; +import org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException; +import org.mxchange.pizzaapplication.basket.Basket; +import org.mxchange.pizzaapplication.basket.item.ItemBasket; +import org.mxchange.pizzaapplication.filter.servlet.BaseServletFilter; +import org.mxchange.pizzaapplication.item.AddableBasketItem; + +/** + * A filter for handling added basket items + * + * @author Roland Haeder + */ +public class BasketItemAddedFilter extends BaseServletFilter implements Filter { + + @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)); + + // All must be set + if (request == null) { + // request is null + throw new NullPointerException("request is null"); + } else if (response == null) { + // response is null + throw new NullPointerException("response is null"); + } else if (chain == null) { + // chain is null + throw new NullPointerException("chain is null"); + } + + // Call doFilter to move on + chain.doFilter(request, response); + + // Get session instance + HttpSession session = ((HttpServletRequest) request).getSession(); + + // Debug message + this.getLogger().debug(MessageFormat.format("session={0}", session)); + + // Should not be null + if (session == null) { + // session is null + throw new NullPointerException("session is null"); + } + + // Get item instance + Object item = session.getAttribute("item"); + + // Debug message + this.getLogger().debug(MessageFormat.format("item={0}", item)); + + // item should not be null + if (item == null) { + // item is null + throw new NullPointerException("item is null"); + } else if (!(item instanceof AddableBasketItem)) { + // Not right instance + throw new IllegalArgumentException("item does not implement OrderableItem"); + } + + // Get basket instance + Basket basket; + try { + basket = ItemBasket.getInstance(session); + } catch (final UnsupportedDatabaseBackendException | SQLException ex) { + // Continue to throw + throw new ServletException(ex); + } + + // Register item with it + basket.addItem((AddableBasketItem) item); + + // Trace message + this.getLogger().trace("EXIT!"); + } + +} diff --git a/src/java/org/mxchange/pizzaapplication/filter/servlet/utf8/Utf8ServletFilter.java b/src/java/org/mxchange/pizzaapplication/filter/servlet/utf8/Utf8ServletFilter.java new file mode 100644 index 00000000..627b1a1b --- /dev/null +++ b/src/java/org/mxchange/pizzaapplication/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.pizzaapplication.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.pizzaapplication.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)); + + // 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!"); + } +} diff --git a/src/java/org/mxchange/pizzaapplication/item/AddableBasketItem.java b/src/java/org/mxchange/pizzaapplication/item/AddableBasketItem.java new file mode 100644 index 00000000..34acdc08 --- /dev/null +++ b/src/java/org/mxchange/pizzaapplication/item/AddableBasketItem.java @@ -0,0 +1,47 @@ +/* + * 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.pizzaapplication.item; + +import org.mxchange.jcore.FrameworkInterface; + +/** + * An interface for addable basket items + * + * @author Roland Haeder + */ +public interface AddableBasketItem extends FrameworkInterface { + + /** + * @return the id + */ + public Long getId (); + + /** + * @param id the id to set + */ + public void setId (final Long id); + + /** + * @return the type + */ + public String getType (); + + /** + * @param type the type to set + */ + public void setType (final String type); +} diff --git a/src/java/org/mxchange/pizzaapplication/item/BaseItem.java b/src/java/org/mxchange/pizzaapplication/item/BaseItem.java index 262ed53a..c72ba9ab 100644 --- a/src/java/org/mxchange/pizzaapplication/item/BaseItem.java +++ b/src/java/org/mxchange/pizzaapplication/item/BaseItem.java @@ -22,7 +22,7 @@ import org.mxchange.jcore.BaseFrameworkSystem; * A general item cl * @author Roland Haeder */ -public class BaseItem extends BaseFrameworkSystem implements OrderableItem { +public class BaseItem extends BaseFrameworkSystem implements AddableBasketItem { /** * Item id number */ diff --git a/src/java/org/mxchange/pizzaapplication/item/OrderableItem.java b/src/java/org/mxchange/pizzaapplication/item/OrderableItem.java deleted file mode 100644 index 871e2eb0..00000000 --- a/src/java/org/mxchange/pizzaapplication/item/OrderableItem.java +++ /dev/null @@ -1,26 +0,0 @@ -/* - * 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.pizzaapplication.item; - -import org.mxchange.jcore.FrameworkInterface; - -/** - * -* @author Roland Haeder - */ -public interface OrderableItem extends FrameworkInterface { -} diff --git a/web/WEB-INF/web.xml b/web/WEB-INF/web.xml index 70dcf7ac..78c62db0 100644 --- a/web/WEB-INF/web.xml +++ b/web/WEB-INF/web.xml @@ -31,12 +31,21 @@ data - A filter for setting character encoding to UTF-8 - Utf8HttpFilter - org.mxchange.pizzaapplication.filter.http.utf8.Utf8HttpFilter + A servlet filter for setting character encoding to UTF-8 + Utf8ServletFilter + org.mxchange.pizzaapplication.filter.servlet.utf8.Utf8ServletFilter + + A filter for handling added basket items + BasketItemAddedFilter + org.mxchange.pizzaapplication.filter.servlet.basket.BasketItemAddedFilter + + + BasketItemAddedFilter + /form_handler/add_item.jsp + - Utf8HttpFilter + Utf8ServletFilter /* diff --git a/web/form_handler/add_item.jsp b/web/form_handler/add_item.jsp index 2b13c70b..6b09543f 100644 --- a/web/form_handler/add_item.jsp +++ b/web/form_handler/add_item.jsp @@ -10,6 +10,9 @@ <%@page import="org.mxchange.pizzaapplication.application.PizzaServiceApplication"%> <%@page import="org.mxchange.pizzaapplication.item.OrderableItem"%> + + + <% // Init application instance PizzaApplication app = PizzaServiceApplication.getInstance(application);