1 package org.apache.turbine.pipeline;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.Enumeration;
27 import java.util.List;
28
29 import javax.servlet.http.HttpSession;
30
31 import org.apache.turbine.TurbineConstants;
32 import org.apache.turbine.annotation.TurbineConfiguration;
33 import org.apache.turbine.annotation.TurbineLoader;
34 import org.apache.turbine.modules.Action;
35 import org.apache.turbine.modules.ActionLoader;
36 import org.apache.turbine.services.velocity.VelocityService;
37 import org.apache.turbine.util.RunData;
38 import org.apache.turbine.util.TurbineException;
39 import org.apache.turbine.util.template.TemplateInfo;
40
41
42
43
44
45
46
47
48
49
50 public class DefaultLoginValve
51 implements Valve
52 {
53
54 @TurbineLoader( Action.class )
55 private ActionLoader actionLoader;
56
57 @TurbineConfiguration( TurbineConstants.ACTION_LOGIN_KEY )
58 private String actionLogin;
59
60 @TurbineConfiguration( TurbineConstants.ACTION_LOGOUT_KEY )
61 private String actionLogout;
62
63
64
65
66 @Override
67 public void invoke(PipelineData pipelineData, ValveContext context)
68 throws IOException, TurbineException
69 {
70 try
71 {
72 process(pipelineData);
73 }
74 catch (Exception e)
75 {
76 throw new TurbineException(e);
77 }
78
79
80 context.invokeNext(pipelineData);
81 }
82
83
84
85
86
87
88
89
90
91 protected void process(PipelineData pipelineData)
92 throws Exception
93 {
94 RunData data = pipelineData.getRunData();
95
96
97
98
99
100 String actionName = data.getAction();
101 if (data.hasAction() &&
102 actionName.equalsIgnoreCase(actionLogin) ||
103 actionName.equalsIgnoreCase(actionLogout))
104 {
105
106
107
108
109
110
111
112
113
114
115
116 if (actionName.equalsIgnoreCase(actionLogin))
117 {
118 Enumeration<String> names = data.getSession().getAttributeNames();
119 if (names != null)
120 {
121
122
123 List<String> nameList = new ArrayList<>();
124 while (names.hasMoreElements())
125 {
126 nameList.add(names.nextElement());
127 }
128
129 HttpSession session = data.getSession();
130 for (String name : nameList)
131 {
132 try
133 {
134 session.removeAttribute(name);
135 }
136 catch (IllegalStateException invalidatedSession)
137 {
138 break;
139 }
140 }
141 }
142 }
143
144 actionLoader.exec(pipelineData, data.getAction());
145 cleanupTemplateContext(data);
146 data.setAction(null);
147 }
148 }
149
150
151
152
153
154
155 private void cleanupTemplateContext(RunData data)
156 {
157
158
159
160 TemplateInfo ti = data.getTemplateInfo();
161 if (ti != null)
162 {
163 ti.removeTemp(VelocityService.CONTEXT);
164 }
165 }
166 }