From 8e7f3f38af176d01d0cff04ff98b6ba84022ef4a Mon Sep 17 00:00:00 2001 From: Roland Haeder Date: Wed, 12 Aug 2015 12:28:35 +0200 Subject: [PATCH] =?utf8?q?Continued=20with=20project:=20-=20Moved=20some?= =?utf8?q?=20classes=20to=20proper=20location=20-=20Removed=20no=20longer?= =?utf8?q?=20glassfish-specific=20character=20encoding,=20because=20..=20-?= =?utf8?q?=20Added=20filter=20for=20setting=20character=20encoding=20to=20?= =?utf8?q?UTF-8=20-=20Added=20"data"=20directory=20+=20ignored=20all=20fil?= =?utf8?q?es=20in=20it=20-=20Rewrote=20initialization=20of=20properties=20?= =?utf8?q?so=20the=20context=20parameter=20from=20web.xml=20can=20be=20use?= =?utf8?q?d=20to=20set=20needed=20properties=20for=20jcore=20database=20ba?= =?utf8?q?ckend.=20Still=20this=20is=20not=20so=20satifying,=20maybe=20the?= =?utf8?q?=20method=20needs=20to=20be=20moved=20to=20jcore=3F=20Signed-off?= =?utf8?q?-by:Roland=20H=C3=A4der=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + .../BasePizzaServiceSystem.java | 29 +++++++- .../application/PizzaServiceApplication.java | 71 +++++++++++++------ .../PizzaProductDatabaseFrontend.java | 53 ++++++++++++-- .../frontend/product}/ProductFrontend.java | 11 ++- .../PizzaProductDatabaseConstants.java | 34 +++++++++ .../filter/http/BaseServletFilter.java | 67 +++++++++++++++++ .../filter/http/utf8/Utf8HttpFilter.java | 51 +++++++++++++ .../pizzaapplication/product/Product.java | 4 +- web/WEB-INF/glassfish-web.xml | 1 - web/WEB-INF/web.xml | 38 ++++++++++ 11 files changed, 326 insertions(+), 34 deletions(-) rename src/java/org/mxchange/{pizzaapplicationk/database/frontend/contact => pizzaapplication/database/frontend/product}/PizzaProductDatabaseFrontend.java (61%) rename src/java/org/mxchange/{pizzaapplicationk/database/frontend/contact => pizzaapplication/database/frontend/product}/ProductFrontend.java (76%) create mode 100644 src/java/org/mxchange/pizzaapplication/database/product/PizzaProductDatabaseConstants.java create mode 100644 src/java/org/mxchange/pizzaapplication/filter/http/BaseServletFilter.java create mode 100644 src/java/org/mxchange/pizzaapplication/filter/http/utf8/Utf8HttpFilter.java diff --git a/.gitignore b/.gitignore index d73b7d0f..4bb68df3 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ /build/ /dist/ *.properties +/data/*.* diff --git a/src/java/org/mxchange/pizzaapplication/BasePizzaServiceSystem.java b/src/java/org/mxchange/pizzaapplication/BasePizzaServiceSystem.java index 84f1e1c5..0e0e7541 100644 --- a/src/java/org/mxchange/pizzaapplication/BasePizzaServiceSystem.java +++ b/src/java/org/mxchange/pizzaapplication/BasePizzaServiceSystem.java @@ -16,6 +16,8 @@ */ package org.mxchange.pizzaapplication; +import java.text.MessageFormat; +import javax.servlet.ServletContext; import org.mxchange.jcore.BaseFrameworkSystem; /** @@ -24,5 +26,30 @@ import org.mxchange.jcore.BaseFrameworkSystem; * @author Roland Haeder */ public class BasePizzaServiceSystem extends BaseFrameworkSystem { - + /** + * Initializes properties from given servlet configuration + * @param context Servlet context instance + */ + protected void initProperties (final ServletContext context) { + // Trace message + this.getLogger().trace(MessageFormat.format("context={0} - CALLED!", context)); + + // We need some properties that needs to be set + for (final String name : this.getPropertyNames()) { + // Debug log + this.getLogger().debug(MessageFormat.format("name={0}", name)); + + // Get value + String value = context.getInitParameter(name).trim(); + + // Debug log + this.getLogger().debug(MessageFormat.format("{0}={1}", name, value)); + + // Set property + this.setProperty(name, value); + } + + // Trace message + this.getLogger().trace("EXIT!"); + } } diff --git a/src/java/org/mxchange/pizzaapplication/application/PizzaServiceApplication.java b/src/java/org/mxchange/pizzaapplication/application/PizzaServiceApplication.java index b3199752..9161e1ab 100644 --- a/src/java/org/mxchange/pizzaapplication/application/PizzaServiceApplication.java +++ b/src/java/org/mxchange/pizzaapplication/application/PizzaServiceApplication.java @@ -30,7 +30,8 @@ import org.mxchange.jcore.contact.Gender; import org.mxchange.pizzaapplication.BasePizzaServiceSystem; import org.mxchange.pizzaapplication.customer.Customer; import org.mxchange.pizzaapplication.customer.PizzaServiceCustomer; -import org.mxchange.pizzaapplication.product.PizzaProduct; +import org.mxchange.pizzaapplication.database.frontend.product.PizzaProductDatabaseFrontend; +import org.mxchange.pizzaapplication.database.frontend.product.ProductFrontend; import org.mxchange.pizzaapplication.product.Product; /** @@ -47,13 +48,13 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P * Some singleton getter for this instance. If the instance is not set in * given application, it will be created. * - * @param application Servlet context + * @param context Servlet context * @return This instance * @throws javax.servlet.ServletException If object is not set correctly */ - public static final PizzaApplication getInstance (final ServletContext application) throws ServletException { + public static final PizzaApplication getInstance (final ServletContext context) throws ServletException { // Check application instance - if (application == null) { + if (context == null) { // Not set throw new NullPointerException("application is null"); //NOI18N } @@ -62,7 +63,7 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P PizzaApplication instance = null; // Get instance from servlet application (aka. "application scope") - Object object = application.getAttribute("app"); //NOI18N + Object object = context.getAttribute("app"); //NOI18N // Is it set? if (object instanceof PizzaApplication) { @@ -73,10 +74,10 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P throw new ServletException("app is not set correctly"); //NOI18N } else { // "service" is null, so initialize it - instance = new PizzaServiceApplication(); + instance = new PizzaServiceApplication(context); // And set it here - application.setAttribute("app", instance); //NOI18N + context.setAttribute("app", instance); //NOI18N } // Trace message @@ -107,11 +108,20 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P private PizzaServiceApplication () { // Init products instance this.products = new TreeMap<>(); + } - // Init bundle - this.initBundle(); + /** + * Constructor with servet configuration + * @param context Servlet context + */ + private PizzaServiceApplication (final ServletContext context) { + // Call other constructor first + this(); - // Fill products list + // Initialize properties from config + this.initProperties(context); + + // Load available products this.fillProductsList(); } @@ -828,13 +838,14 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P /** * Adds given product to list or throws an exception if name is already found * - * @param name Internal name of product - * @param title Product's title - * @param price Price + * @param product Product instance to add */ - private void addProduct (final String name, final String title, final float price) { + private void addProduct (final Product product) { // Trace message - this.getLogger().trace(MessageFormat.format("name={0},title={1},price={2} - CALLED!", name, title, price)); //NOI18N + this.getLogger().trace(MessageFormat.format("product={0} - CALLED!", product)); //NOI18N + + // Get all data from it + String name = product.getName(); // Is the name already used? if (this.isProductNameUsed(name)) { @@ -842,9 +853,6 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P throw new IllegalArgumentException(MessageFormat.format("product {0} is already used.", name)); //NOI18N } - // Instance product - Product product = new PizzaProduct(name, title, price); - // Debug message this.getLogger().debug(MessageFormat.format("Adding product={0} ...", product)); //NOI18N @@ -882,10 +890,20 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P // Trace message this.getLogger().trace("CALLED!"); //NOI18N - // Add products - this.addProduct("italia", "Pizza Italia", 5.50f); //NOI18N - this.addProduct("diablo", "Pizza Diablo", 7.80f); //NOI18N - this.addProduct("bolognese", "Spagetti Bolognese", 11.95f); //NOI18N + // Get a product frontend + ProductFrontend frontend = new PizzaProductDatabaseFrontend(); + + // Get iterator + Iterator iterator = frontend.getProducts(); + + // Get all products + while (iterator.hasNext()) { + // Get it + Product product = iterator.next(); + + // Add it + this.addProduct(product); + } // Trace message this.getLogger().trace("EXIT!"); //NOI18N @@ -1063,6 +1081,15 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P * Application starter */ private void start () { + // Init bundle + this.initBundle(); + + // Init properties + this.initProperties(); + + // Fill products list + this.fillProductsList(); + // "Walk" over all products for (final Product product : this.getProducts()) { // Output data diff --git a/src/java/org/mxchange/pizzaapplicationk/database/frontend/contact/PizzaProductDatabaseFrontend.java b/src/java/org/mxchange/pizzaapplication/database/frontend/product/PizzaProductDatabaseFrontend.java similarity index 61% rename from src/java/org/mxchange/pizzaapplicationk/database/frontend/contact/PizzaProductDatabaseFrontend.java rename to src/java/org/mxchange/pizzaapplication/database/frontend/product/PizzaProductDatabaseFrontend.java index 97b19cf0..f0a63a78 100644 --- a/src/java/org/mxchange/pizzaapplicationk/database/frontend/contact/PizzaProductDatabaseFrontend.java +++ b/src/java/org/mxchange/pizzaapplication/database/frontend/product/PizzaProductDatabaseFrontend.java @@ -14,14 +14,19 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -package org.mxchange.pizzaapplicationk.database.frontend.contact; +package org.mxchange.pizzaapplication.database.frontend.product; import java.sql.SQLException; import java.text.MessageFormat; +import java.util.Iterator; +import org.mxchange.jcore.criteria.searchable.SearchCriteria; +import org.mxchange.jcore.criteria.searchable.SearchableCritera; import org.mxchange.jcore.database.frontend.BaseDatabaseFrontend; +import org.mxchange.jcore.database.result.Result; import org.mxchange.jcore.database.storage.Storeable; import org.mxchange.jcore.exceptions.BadTokenException; import org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException; +import org.mxchange.pizzaapplication.database.product.PizzaProductDatabaseConstants; import org.mxchange.pizzaapplication.product.Product; /** @@ -32,14 +37,14 @@ import org.mxchange.pizzaapplication.product.Product; public class PizzaProductDatabaseFrontend extends BaseDatabaseFrontend implements ProductFrontend { /** - * Basic constrcutor + * Default constrcutor */ - protected PizzaProductDatabaseFrontend () { + public PizzaProductDatabaseFrontend () { // Trace message this.getLogger().trace("CALLED!"); //NOI18N // Set "table" name - this.setTableName("contacts"); //NOI18N + this.setTableName("products"); //NOI18N try { // Initalize backend @@ -65,6 +70,39 @@ public class PizzaProductDatabaseFrontend extends BaseDatabaseFrontend implement this.getLogger().trace("EXIT!"); //NOI18N } + /** + * An iterator on all products + * + * @return Iterator on all products + */ + @Override + @SuppressWarnings ("unchecked") + public Iterator getProducts () { + // Trace message + this.getLogger().trace("CALLED!"); //NOI18N + + // Instance search criteria + SearchableCritera critera = new SearchCriteria(); + + // Add criteria + critera.addCriteria(PizzaProductDatabaseConstants.COLUMN_AVAILABLE, true); + + // Run the query + Result result = this.getBackend().doSelectByCriteria(critera); + + // Debug message + this.getLogger().debug(MessageFormat.format("result={0}", result)); //NOI18N + + // Get iterator + Iterator iterator = result.iterator(); + + // Trace message + this.getLogger().trace(MessageFormat.format("iterator={0} - EXIT!", iterator)); //NOI18N + + // Get iterator and return it + return (Iterator) iterator; + } + /** * Parses given line from database backend into a Storeable instance. Please * note that not all backends need this. @@ -81,18 +119,19 @@ public class PizzaProductDatabaseFrontend extends BaseDatabaseFrontend implement Product product = this.parseLineToProduct(line); // Debug message - this.getLogger().debug(MessageFormat.format("product={0}", product)); + this.getLogger().trace(MessageFormat.format("product={0} - EXIT!", product)); //NOI18N // Return it - return (Storeable) product; + return product; } /** * Parses given line to a Product instance + * * @param line * @return A Product instance from given line */ private Product parseLineToProduct (final String line) { - throw new UnsupportedOperationException("Not supported yet."); + throw new UnsupportedOperationException(MessageFormat.format("Not supported yet: line={0}", line)); //NOI18N } } diff --git a/src/java/org/mxchange/pizzaapplicationk/database/frontend/contact/ProductFrontend.java b/src/java/org/mxchange/pizzaapplication/database/frontend/product/ProductFrontend.java similarity index 76% rename from src/java/org/mxchange/pizzaapplicationk/database/frontend/contact/ProductFrontend.java rename to src/java/org/mxchange/pizzaapplication/database/frontend/product/ProductFrontend.java index a04499ec..b94d5502 100644 --- a/src/java/org/mxchange/pizzaapplicationk/database/frontend/contact/ProductFrontend.java +++ b/src/java/org/mxchange/pizzaapplication/database/frontend/product/ProductFrontend.java @@ -14,9 +14,11 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -package org.mxchange.pizzaapplicationk.database.frontend.contact; +package org.mxchange.pizzaapplication.database.frontend.product; +import java.util.Iterator; import org.mxchange.jcore.database.frontend.DatabaseFrontend; +import org.mxchange.pizzaapplication.product.Product; /** * An interface for product database frontends @@ -24,4 +26,11 @@ import org.mxchange.jcore.database.frontend.DatabaseFrontend; * @author Roland Häder */ public interface ProductFrontend extends DatabaseFrontend { + + /** + * An iterator on all products + * + * @return Iterator on all products + */ + public Iterator getProducts (); } diff --git a/src/java/org/mxchange/pizzaapplication/database/product/PizzaProductDatabaseConstants.java b/src/java/org/mxchange/pizzaapplication/database/product/PizzaProductDatabaseConstants.java new file mode 100644 index 00000000..16d08d70 --- /dev/null +++ b/src/java/org/mxchange/pizzaapplication/database/product/PizzaProductDatabaseConstants.java @@ -0,0 +1,34 @@ +/* + * 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.product; + +/** + * + * @author Roland Haeder + */ +public final class PizzaProductDatabaseConstants { + /** + * Column name for "available" + */ + public static final String COLUMN_AVAILABLE = "available"; + + /** + * No instance from this class + */ + private PizzaProductDatabaseConstants () { + } +} diff --git a/src/java/org/mxchange/pizzaapplication/filter/http/BaseServletFilter.java b/src/java/org/mxchange/pizzaapplication/filter/http/BaseServletFilter.java new file mode 100644 index 00000000..0e40f85f --- /dev/null +++ b/src/java/org/mxchange/pizzaapplication/filter/http/BaseServletFilter.java @@ -0,0 +1,67 @@ +/* + * 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; + + /** + * 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; + } + + /** + * 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); + } +} diff --git a/src/java/org/mxchange/pizzaapplication/filter/http/utf8/Utf8HttpFilter.java b/src/java/org/mxchange/pizzaapplication/filter/http/utf8/Utf8HttpFilter.java new file mode 100644 index 00000000..51585e3a --- /dev/null +++ b/src/java/org/mxchange/pizzaapplication/filter/http/utf8/Utf8HttpFilter.java @@ -0,0 +1,51 @@ +/* + * 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"); + response.setCharacterEncoding("UTF-8"); + } +} diff --git a/src/java/org/mxchange/pizzaapplication/product/Product.java b/src/java/org/mxchange/pizzaapplication/product/Product.java index a81b9163..bdeea227 100644 --- a/src/java/org/mxchange/pizzaapplication/product/Product.java +++ b/src/java/org/mxchange/pizzaapplication/product/Product.java @@ -16,13 +16,13 @@ */ package org.mxchange.pizzaapplication.product; -import org.mxchange.jcore.FrameworkInterface; +import org.mxchange.jcore.database.storage.Storeable; /** * * @author Roland Haeder */ -public interface Product extends FrameworkInterface { +public interface Product extends Storeable { /** * Getter for name, suitable for form fields. * diff --git a/web/WEB-INF/glassfish-web.xml b/web/WEB-INF/glassfish-web.xml index 0c186a9a..13e0059f 100644 --- a/web/WEB-INF/glassfish-web.xml +++ b/web/WEB-INF/glassfish-web.xml @@ -7,5 +7,4 @@ Keep a copy of the generated servlet class' java code. - diff --git a/web/WEB-INF/web.xml b/web/WEB-INF/web.xml index 764689af..2676cd9a 100644 --- a/web/WEB-INF/web.xml +++ b/web/WEB-INF/web.xml @@ -1,5 +1,39 @@ + + Full-qualified back class name, must implement DatabaseBackend interface + database.backend.class + org.mxchange.jcore.database.backend.base64.Base64CsvDatabaseBackend + + + Login name for MySQL database, mostly not root + database.mysql.login + root + + + Password for above login, an empty password can be archived by setting a space + database.mysql.password + root + + + Hostname or IP address for MySQL server + database.mysql.host + localhost + + + Name of MySQL catalog + database.mysql.dbname + test + + + A filter for setting character encoding to UTF-8 + Utf8HttpFilter + org.mxchange.pizzaapplication.filter.http.utf8.Utf8HttpFilter + + + Utf8HttpFilter + /* + 30 @@ -8,4 +42,8 @@ index.jsp + + Exception + /errorHandler.jsp + -- 2.39.5