]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/filter/servlet/basket/BasketItemAddedFilter.java
21fecbcaa3a03576e418680259e29390e0d30483
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / filter / servlet / basket / BasketItemAddedFilter.java
1 /*
2  * Copyright (C) 2015 Roland Haeder
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 package org.mxchange.pizzaapplication.filter.servlet.basket;
18
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;
34
35 /**
36  * A filter for handling added basket items
37  *
38  * @author Roland Haeder
39  */
40 public class BasketItemAddedFilter extends BaseServletFilter implements Filter {
41
42         @Override
43         public void doFilter (final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException {
44                 // Trace message
45                 this.getLogger().trace(MessageFormat.format("request={0},response={1},chain={2} - CALLED!", request, response, chain));
46
47                 // All must be set
48                 if (request == null) {
49                         // request is null
50                         throw new NullPointerException("request is null");
51                 } else if (response == null) {
52                         // response is null
53                         throw new NullPointerException("response is null");
54                 } else if (chain == null) {
55                         // chain is null
56                         throw new NullPointerException("chain is null");
57                 }
58
59                 // Call doFilter to move on
60                 chain.doFilter(request, response);
61
62                 // Get session instance
63                 HttpSession session = ((HttpServletRequest) request).getSession();
64
65                 // Debug message
66                 this.getLogger().debug(MessageFormat.format("session={0}", session));
67
68                 // Should not be null
69                 if (session == null) {
70                         // session is null
71                         throw new NullPointerException("session is null");
72                 }
73
74                 // Get item instance
75                 Object item = session.getAttribute("item");
76
77                 // Debug message
78                 this.getLogger().debug(MessageFormat.format("item={0}", item));
79
80                 // item should not be null
81                 if (item == null) {
82                         // item is null
83                         throw new NullPointerException("item is null");
84                 } else if (!(item instanceof AddableBasketItem)) {
85                         // Not right instance
86                         throw new IllegalArgumentException("item does not implement OrderableItem");
87                 }
88
89                 // Get basket instance
90                 Basket<AddableBasketItem> basket;
91                 try {
92                         basket = ItemBasket.getInstance(session);
93                 } catch (final UnsupportedDatabaseBackendException | SQLException ex) {
94                         // Continue to throw
95                         throw new ServletException(ex);
96                 }
97
98                 // Register item with it
99                 basket.addItem((AddableBasketItem) item);
100
101                 // Trace message
102                 this.getLogger().trace("EXIT!");
103         }
104
105 }