2 * Copyright (C) 2015 Roland Haeder
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package org.mxchange.pizzaapplication.filter.servlet.basket;
19 import java.io.IOException;
20 import java.lang.reflect.InvocationTargetException;
21 import java.sql.SQLException;
22 import java.text.MessageFormat;
23 import javax.servlet.Filter;
24 import javax.servlet.FilterChain;
25 import javax.servlet.ServletException;
26 import javax.servlet.ServletRequest;
27 import javax.servlet.ServletResponse;
28 import javax.servlet.http.HttpServletRequest;
29 import javax.servlet.http.HttpSession;
30 import org.mxchange.jcore.exceptions.BadTokenException;
31 import org.mxchange.jcore.exceptions.CorruptedDatabaseFileException;
32 import org.mxchange.jshop.beans.basket.BasketBean;
33 import org.mxchange.jshop.item.AddableBasketItem;
34 import org.mxchange.pizzaapplication.beans.controller.PizzaBean;
35 import org.mxchange.pizzaapplication.filter.servlet.BaseServletFilter;
38 * A filter for handling added basket items
40 * @author Roland Haeder
42 public class BasketItemAddedFilter extends BaseServletFilter implements Filter {
45 @SuppressWarnings ("unchecked")
46 public void doFilter (final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException {
48 this.getLogger().trace(MessageFormat.format("request={0},response={1},chain={2} - CALLED!", request, response, chain)); //NOI18N
51 if (null == request) {
53 throw new NullPointerException("request is null"); //NOI18N
54 } else if (null == response) {
56 throw new NullPointerException("response is null"); //NOI18N
57 } else if (null == chain) {
59 throw new NullPointerException("chain is null"); //NOI18N
62 // Call doFilter to move on
63 chain.doFilter(request, response);
65 // Get item instance from request
66 Object object = request.getAttribute("item"); //NOI18N
69 this.getLogger().debug(MessageFormat.format("object={0}", object)); //NOI18N
71 // item should not be null
74 throw new NullPointerException("item is null"); //NOI18N
75 } else if (!(object instanceof AddableBasketItem)) {
77 throw new IllegalArgumentException("item does not implement AddableBasketItem"); //NOI18N
80 // Now it is secure to cast
81 AddableBasketItem item = (AddableBasketItem) object;
84 this.getLogger().debug(MessageFormat.format("item.id={0},item.itemId={1},item.itemType={2},item.amount={3}", item.getId(), item.getItemId(), item.getItemType(), item.getAmount())); //NOI18N
89 // Get session instance
90 HttpSession session = ((HttpServletRequest) request).getSession();
93 this.getLogger().debug(MessageFormat.format("session={0}", session)); //NOI18N
96 if (null == session) {
98 throw new NullPointerException("session is null"); //NOI18N
101 // Get man controller
102 PizzaBean bean = (PizzaBean) session.getAttribute("controller"); //NOI18N
105 this.getLogger().debug("bean=" + bean);
107 // Get basket instance
108 basket = bean.getBasket();
111 this.getLogger().debug("basket=" + basket);
113 // Is the item already added?
114 if (item.getItemId() == null) {
115 // Item id is not set
116 throw new NullPointerException(MessageFormat.format("item id of item={0} is null", item)); //NOI18N
117 } else if (item.getItemType() == null) {
118 // Item type is not set
119 throw new NullPointerException(MessageFormat.format("item type of item={0} is null", item)); //NOI18N
120 } else if ((item.getAmount() == null) || (item.getAmount() == 0)) {
122 this.getLogger().debug(MessageFormat.format("Amount for item {0} is null", item)); //NOI18N
124 // Amount is not entered
126 } else if (basket.isItemAdded(item)) {
127 // Yes, then throw exception here
128 throw new ServletException(MessageFormat.format("item id={0} has already been added.", item.getItemId())); //NOI18N
131 // Register item with it
132 basket.addItem(item);
133 } catch (final SQLException | BadTokenException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
135 throw new ServletException(ex);
139 this.getLogger().trace("EXIT!"); //NOI18N