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.HttpServletResponse;
30 import javax.servlet.http.HttpSession;
31 import org.mxchange.jcore.exceptions.BadTokenException;
32 import org.mxchange.jcore.exceptions.CorruptedDatabaseFileException;
33 import org.mxchange.jshop.beans.basket.BasketBean;
34 import org.mxchange.jshop.item.AddableBasketItem;
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("object is null"); //NOI18N
75 } else if (!(object instanceof AddableBasketItem)) {
77 throw new IllegalArgumentException("object 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
87 // Cast to servlet request
88 HttpServletRequest servletRequest = (HttpServletRequest) request;
90 // Get session instance
91 HttpSession session = servletRequest.getSession();
94 this.getLogger().debug(MessageFormat.format("session={0}", session)); //NOI18N
97 if (null == session) {
99 throw new NullPointerException("session is null"); //NOI18N
102 // Get basket instance
103 BasketBean basket = (BasketBean) session.getAttribute("basket"); //NOI18N
106 this.getLogger().debug(MessageFormat.format("basket={0}", basket)); //NOI18N
108 // Is the item already added?
109 if (item.getItemId() == null) {
110 // Item id is not set
111 throw new NullPointerException(MessageFormat.format("item id of item={0} is null", item)); //NOI18N
112 } else if (item.getItemType() == null) {
113 // Item type is not set
114 throw new NullPointerException(MessageFormat.format("item type of item={0} is null", item)); //NOI18N
115 } else if ((item.getAmount() == null) || (item.getAmount() == 0)) {
117 this.getLogger().debug(MessageFormat.format("Amount for item {0} is null - EXIT!", item)); //NOI18N
119 // Amount is not entered
121 } else if (basket.isItemAdded(item)) {
122 // Yes, then throw exception here
123 throw new ServletException(MessageFormat.format("item id={0} has already been added.", item.getItemId())); //NOI18N
126 // Register item with it
127 basket.addItem(item);
129 // Is amount null or zero?
130 if ((item.getAmount() == null) || (item.getAmount() == 0)) {
131 // Then redirect to added=0
132 ((HttpServletResponse) response).sendRedirect(servletRequest.getContextPath() + "/?add=0"); //NOI18N
134 // Redirect to proper URL
135 ((HttpServletResponse) response).sendRedirect(servletRequest.getContextPath() + "/?add=1"); //NOI18N
137 } catch (final SQLException | BadTokenException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
139 throw new ServletException(ex);
143 this.getLogger().trace("EXIT!"); //NOI18N