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.sql.SQLException;
21 import java.text.MessageFormat;
22 import javax.servlet.Filter;
23 import javax.servlet.FilterChain;
24 import javax.servlet.ServletException;
25 import javax.servlet.ServletRequest;
26 import javax.servlet.ServletResponse;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpSession;
29 import org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException;
30 import org.mxchange.pizzaapplication.basket.Basket;
31 import org.mxchange.pizzaapplication.basket.item.ItemBasket;
32 import org.mxchange.pizzaapplication.filter.servlet.BaseServletFilter;
33 import org.mxchange.pizzaapplication.item.AddableBasketItem;
36 * A filter for handling added basket items
38 * @author Roland Haeder
40 public class BasketItemAddedFilter extends BaseServletFilter implements Filter {
43 public void doFilter (final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException {
45 this.getLogger().trace(MessageFormat.format("request={0},response={1},chain={2} - CALLED!", request, response, chain));
48 if (request == null) {
50 throw new NullPointerException("request is null");
51 } else if (response == null) {
53 throw new NullPointerException("response is null");
54 } else if (chain == null) {
56 throw new NullPointerException("chain is null");
59 // Call doFilter to move on
60 chain.doFilter(request, response);
62 // Get session instance
63 HttpSession session = ((HttpServletRequest) request).getSession();
66 this.getLogger().debug(MessageFormat.format("session={0}", session));
69 if (session == null) {
71 throw new NullPointerException("session is null");
75 Object item = session.getAttribute("item");
78 this.getLogger().debug(MessageFormat.format("item={0}", item));
80 // item should not be null
83 throw new NullPointerException("item is null");
84 } else if (!(item instanceof AddableBasketItem)) {
86 throw new IllegalArgumentException("item does not implement OrderableItem");
89 // Get basket instance
90 Basket<AddableBasketItem> basket;
92 basket = ItemBasket.getInstance(session);
93 } catch (final UnsupportedDatabaseBackendException | SQLException ex) {
95 throw new ServletException(ex);
98 // Register item with it
99 basket.addItem((AddableBasketItem) item);
102 this.getLogger().trace("EXIT!");