1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.apache.struts.webapp.example;
24
25
26 import java.lang.reflect.InvocationTargetException;
27 import javax.servlet.ServletException;
28 import javax.servlet.http.HttpServletRequest;
29 import javax.servlet.http.HttpSession;
30 import javax.servlet.http.HttpServletResponse;
31 import org.apache.commons.beanutils.PropertyUtils;
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34 import org.apache.struts.action.Action;
35 import org.apache.struts.action.ActionForm;
36 import org.apache.struts.action.ActionForward;
37 import org.apache.struts.action.ActionMapping;
38
39
40
41
42
43
44
45
46
47
48 public final class EditSubscriptionAction extends Action {
49
50
51
52
53
54
55
56
57 private Log log =
58 LogFactory.getLog("org.apache.struts.webapp.Example");
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79 public ActionForward execute(ActionMapping mapping,
80 ActionForm form,
81 HttpServletRequest request,
82 HttpServletResponse response)
83 throws Exception {
84
85
86 HttpSession session = request.getSession();
87 String action = request.getParameter("action");
88 if (action == null) {
89 action = "Create";
90 }
91 String host = request.getParameter("host");
92 if (log.isDebugEnabled()) {
93 log.debug("EditSubscriptionAction: Processing " + action +
94 " action");
95 }
96
97
98 User user = (User) session.getAttribute(Constants.USER_KEY);
99 if (user == null) {
100 if (log.isTraceEnabled()) {
101 log.trace(" User is not logged on in session "
102 + session.getId());
103 }
104 return (mapping.findForward("logon"));
105 }
106
107
108 Subscription subscription =
109 user.findSubscription(request.getParameter("host"));
110 if ((subscription == null) && !action.equals("Create")) {
111 if (log.isTraceEnabled()) {
112 log.trace(" No subscription for user " +
113 user.getUsername() + " and host " + host);
114 }
115 return (mapping.findForward("failure"));
116 }
117 if (subscription != null) {
118 session.setAttribute(Constants.SUBSCRIPTION_KEY, subscription);
119 }
120
121
122 if (form == null) {
123 if (log.isTraceEnabled()) {
124 log.trace(" Creating new SubscriptionForm bean under key "
125 + mapping.getAttribute());
126 }
127 form = new SubscriptionForm();
128 if ("request".equals(mapping.getScope())) {
129 request.setAttribute(mapping.getAttribute(), form);
130 } else {
131 session.setAttribute(mapping.getAttribute(), form);
132 }
133 }
134 SubscriptionForm subform = (SubscriptionForm) form;
135 subform.setAction(action);
136 if (!action.equals("Create")) {
137 if (log.isTraceEnabled()) {
138 log.trace(" Populating form from " + subscription);
139 }
140 try {
141 PropertyUtils.copyProperties(subform, subscription);
142 subform.setAction(action);
143 } catch (InvocationTargetException e) {
144 Throwable t = e.getTargetException();
145 if (t == null)
146 t = e;
147 log.error("SubscriptionForm.populate", t);
148 throw new ServletException("SubscriptionForm.populate", t);
149 } catch (Throwable t) {
150 log.error("SubscriptionForm.populate", t);
151 throw new ServletException("SubscriptionForm.populate", t);
152 }
153 }
154
155
156 if (log.isTraceEnabled()) {
157 log.trace(" Forwarding to 'success' page");
158 }
159 return (mapping.findForward("success"));
160
161 }
162
163
164 }