Skip to content

Commit c1786c5

Browse files
committed
Use injection for RollerSessionManager.
1 parent 170937d commit c1786c5

File tree

11 files changed

+156
-92
lines changed

11 files changed

+156
-92
lines changed

app/src/main/java/org/apache/roller/weblogger/ui/core/RollerSession.java

+26-25
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import javax.servlet.http.HttpSessionActivationListener;
2727
import javax.servlet.http.HttpSessionEvent;
2828
import javax.servlet.http.HttpSessionListener;
29+
30+
import com.opensymphony.xwork2.inject.Inject;
2931
import org.apache.commons.logging.Log;
3032
import org.apache.commons.logging.LogFactory;
3133
import org.apache.roller.weblogger.WebloggerException;
@@ -43,37 +45,39 @@ public class RollerSession
4345
implements HttpSessionListener, HttpSessionActivationListener, Serializable {
4446

4547
private static final long serialVersionUID = 5890132909166913727L;
48+
private static final Log log;
4649

4750
// the id of the user represented by this session
4851
private String userName = null;
49-
50-
private static final Log log;
51-
52+
private final SessionManager sessionManager;
53+
5254
public static final String ROLLER_SESSION = "org.apache.roller.weblogger.rollersession";
5355

5456
static{
5557
WebloggerConfig.init(); // must be called before calls to logging APIs
5658
log = LogFactory.getLog(RollerSession.class);
5759
}
58-
59-
/**
60-
* Get RollerSession from request (and add user if not already present).
61-
*/
62-
public static RollerSession getRollerSession(HttpServletRequest request) {
63-
RollerSession rollerSession = null;
60+
61+
62+
@Inject
63+
public RollerSession(SessionManager sessionManager) {
64+
this.sessionManager = sessionManager;
65+
}
66+
67+
@Inject
68+
public RollerSession(SessionManager sessionManager, HttpServletRequest request) {
69+
this.sessionManager = sessionManager;
70+
6471
HttpSession session = request.getSession(false);
6572
if (session != null) {
66-
rollerSession = (RollerSession)session.getAttribute(ROLLER_SESSION);
67-
68-
if (rollerSession == null) {
69-
rollerSession = new RollerSession();
70-
session.setAttribute(ROLLER_SESSION, rollerSession);
71-
} else if (rollerSession.getAuthenticatedUser() != null) {
72-
RollerSessionManager sessionManager = RollerSessionManager.getInstance();
73-
if (sessionManager.get(rollerSession.getAuthenticatedUser().getUserName()) == null) {
74-
// session not present in cache means that it is invalid
75-
rollerSession = new RollerSession();
76-
session.setAttribute(ROLLER_SESSION, rollerSession);
73+
RollerSession storedSession = (RollerSession)session.getAttribute(ROLLER_SESSION);
74+
75+
if (storedSession == null) {
76+
session.setAttribute(ROLLER_SESSION, this);
77+
} else if (storedSession.getAuthenticatedUser() != null) {
78+
if (sessionManager.get(storedSession.getAuthenticatedUser().getUserName()) == null) {
79+
// override it with the new session
80+
session.setAttribute(ROLLER_SESSION, this);
7781
}
7882
}
7983

@@ -83,7 +87,7 @@ public static RollerSession getRollerSession(HttpServletRequest request) {
8387
// user object from user manager but *only* do this if we have been
8488
// bootstrapped because under an SSO scenario we may have a
8589
// principal even before we have been bootstrapped.
86-
if (rollerSession.getAuthenticatedUser() == null && principal != null && WebloggerFactory.isBootstrapped()) {
90+
if (getAuthenticatedUser() == null && principal != null && WebloggerFactory.isBootstrapped()) {
8791
try {
8892

8993
UserManager umgr = WebloggerFactory.getWeblogger().getUserManager();
@@ -114,16 +118,14 @@ public static RollerSession getRollerSession(HttpServletRequest request) {
114118
}
115119
// only set authenticated user if user is enabled
116120
if (user != null && user.getEnabled()) {
117-
rollerSession.setAuthenticatedUser(user);
121+
setAuthenticatedUser(user);
118122
}
119123

120124
} catch (WebloggerException e) {
121125
log.error("ERROR: getting user object",e);
122126
}
123127
}
124128
}
125-
126-
return rollerSession;
127129
}
128130

129131
/**
@@ -149,7 +151,6 @@ public User getAuthenticatedUser() {
149151
*/
150152
public void setAuthenticatedUser(User authenticatedUser) {
151153
this.userName = authenticatedUser.getUserName();
152-
RollerSessionManager sessionManager = RollerSessionManager.getInstance();
153154
sessionManager.register(authenticatedUser.getUserName(), this);
154155
}
155156

app/src/main/java/org/apache/roller/weblogger/ui/core/RollerSessionManager.java

+20-10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. The ASF licenses this file to You
4+
* under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License. For additional information regarding
15+
* copyright in this work, please see the NOTICE file in the top level
16+
* directory of this distribution.
17+
*/
18+
119
package org.apache.roller.weblogger.ui.core;
220

321
import org.apache.commons.logging.Log;
@@ -10,20 +28,12 @@
1028
import java.util.HashMap;
1129
import java.util.Map;
1230

13-
public class RollerSessionManager {
31+
public class RollerSessionManager implements SessionManager {
1432
private static final Log log = LogFactory.getLog(RollerSessionManager.class);
1533
private static final String CACHE_ID = "roller.session.cache";
1634

1735
private final Cache sessionCache;
1836

19-
public static RollerSessionManager getInstance() {
20-
return RollerSessionManager.SingletonHolder.INSTANCE;
21-
}
22-
23-
private static class SingletonHolder {
24-
private static final RollerSessionManager INSTANCE = new RollerSessionManager();
25-
}
26-
2737
private class SessionCacheHandler extends CacheHandlerAdapter {
2838
public void invalidateUser(User user) {
2939
if (user != null && user.getUserName() != null) {
@@ -32,7 +42,7 @@ public void invalidateUser(User user) {
3242
}
3343
}
3444

35-
private RollerSessionManager() {
45+
public RollerSessionManager() {
3646
Map<String, String> cacheProps = new HashMap<>();
3747
cacheProps.put("id", CACHE_ID);
3848
this.sessionCache = CacheManager.constructCache(null, cacheProps);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. The ASF licenses this file to You
4+
* under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License. For additional information regarding
15+
* copyright in this work, please see the NOTICE file in the top level
16+
* directory of this distribution.
17+
*/
18+
19+
package org.apache.roller.weblogger.ui.core;
20+
21+
public interface SessionManager {
22+
void register(String userName, RollerSession session);
23+
RollerSession get(String userName);
24+
void invalidate(String userName);
25+
}
26+

app/src/main/java/org/apache/roller/weblogger/ui/core/filters/LoadSaltFilter.java

+16-12
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,31 @@
2323
import javax.servlet.*;
2424
import javax.servlet.http.HttpServletRequest;
2525
import org.apache.commons.lang3.RandomStringUtils;
26+
import org.apache.commons.logging.Log;
27+
import org.apache.commons.logging.LogFactory;
2628
import org.apache.roller.weblogger.ui.core.RollerSession;
2729
import org.apache.roller.weblogger.ui.rendering.util.cache.SaltCache;
30+
import org.apache.roller.weblogger.ui.struts2.util.UIBeanFactory;
2831

29-
/**
30-
* Filter generates a unique salt value for use in any HTTP form generated by
31-
* Roller. See also: ValidateSalt filter.
32-
*/
3332
public class LoadSaltFilter implements Filter {
3433

34+
private static final Log log = LogFactory.getLog(LoadSaltFilter.class);
35+
private RollerSession rollerSession;
36+
37+
@Override
38+
public void init(FilterConfig filterConfig) throws ServletException {
39+
rollerSession = UIBeanFactory.getBean(RollerSession.class);
40+
}
41+
3542
@Override
3643
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
37-
throws IOException, ServletException {
44+
throws IOException, ServletException {
3845

3946
HttpServletRequest httpReq = (HttpServletRequest) request;
40-
RollerSession rollerSession = RollerSession.getRollerSession(httpReq);
47+
4148
if (rollerSession != null) {
42-
String userId = rollerSession.getAuthenticatedUser() != null ? rollerSession.getAuthenticatedUser().getId() : "";
49+
String userId = rollerSession.getAuthenticatedUser() != null ?
50+
rollerSession.getAuthenticatedUser().getId() : "";
4351
SaltCache saltCache = SaltCache.getInstance();
4452
String salt = RandomStringUtils.random(20, 0, 0, true, true, null, new SecureRandom());
4553
saltCache.put(salt, userId);
@@ -48,11 +56,7 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
4856

4957
chain.doFilter(request, response);
5058
}
51-
52-
@Override
53-
public void init(FilterConfig filterConfig) throws ServletException {
54-
}
55-
59+
5660
@Override
5761
public void destroy() {
5862
}

app/src/main/java/org/apache/roller/weblogger/ui/core/filters/ValidateSaltFilter.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.apache.roller.weblogger.config.WebloggerConfig;
3838
import org.apache.roller.weblogger.ui.rendering.util.cache.SaltCache;
3939
import org.apache.roller.weblogger.ui.core.RollerSession;
40+
import org.apache.roller.weblogger.ui.struts2.util.UIBeanFactory;
4041

4142
/**
4243
* Filter checks all POST request for presence of valid salt value and rejects those without
@@ -45,6 +46,7 @@
4546
public class ValidateSaltFilter implements Filter {
4647
private static final Log log = LogFactory.getLog(ValidateSaltFilter.class);
4748
private Set<String> ignored = Collections.emptySet();
49+
private RollerSession rollerSession;
4850

4951
@Override
5052
public void doFilter(ServletRequest request, ServletResponse response,
@@ -58,9 +60,9 @@ public void doFilter(ServletRequest request, ServletResponse response,
5860
}
5961

6062
if ("POST".equals(httpReq.getMethod()) && !isIgnoredURL(requestURL)) {
61-
RollerSession rollerSession = RollerSession.getRollerSession(httpReq);
6263
if (rollerSession != null) {
63-
String userId = rollerSession.getAuthenticatedUser() != null ? rollerSession.getAuthenticatedUser().getId() : "";
64+
String userId = rollerSession.getAuthenticatedUser() != null ?
65+
rollerSession.getAuthenticatedUser().getId() : "";
6466

6567
String salt = httpReq.getParameter("salt");
6668
SaltCache saltCache = SaltCache.getInstance();
@@ -71,7 +73,6 @@ public void doFilter(ServletRequest request, ServletResponse response,
7173
throw new ServletException("Security Violation");
7274
}
7375

74-
// Remove salt from cache after successful validation
7576
saltCache.remove(salt);
7677
if (log.isDebugEnabled()) {
7778
log.debug("Salt used and invalidated: " + salt);
@@ -86,6 +87,7 @@ public void doFilter(ServletRequest request, ServletResponse response,
8687
public void init(FilterConfig filterConfig) throws ServletException {
8788
String urls = WebloggerConfig.getProperty("salt.ignored.urls");
8889
ignored = Set.of(StringUtils.stripAll(StringUtils.split(urls, ",")));
90+
rollerSession = UIBeanFactory.getBean(RollerSession.class);
8991
}
9092

9193
@Override

app/src/main/java/org/apache/roller/weblogger/ui/struts2/ajax/CommentDataServlet.java

+12-5
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
import javax.servlet.http.HttpServlet;
2424
import javax.servlet.http.HttpServletRequest;
2525
import javax.servlet.http.HttpServletResponse;
26+
27+
import org.apache.commons.logging.Log;
28+
import org.apache.commons.logging.LogFactory;
2629
import org.apache.commons.text.StringEscapeUtils;
2730
import org.apache.commons.text.WordUtils;
2831
import org.apache.roller.weblogger.business.Weblogger;
@@ -32,6 +35,7 @@
3235
import org.apache.roller.weblogger.pojos.WeblogEntryComment;
3336
import org.apache.roller.weblogger.pojos.WeblogPermission;
3437
import org.apache.roller.weblogger.ui.core.RollerSession;
38+
import org.apache.roller.weblogger.ui.struts2.util.UIBeanFactory;
3539
import org.apache.roller.weblogger.util.Utilities;
3640

3741

@@ -40,7 +44,12 @@
4044
*/
4145
public class CommentDataServlet extends HttpServlet {
4246

43-
public void checkAuth(HttpServletRequest request, Weblog weblog) {
47+
private static final Log log = LogFactory.getLog(CommentDataServlet.class);
48+
private RollerSession rollerSession;
49+
50+
@Override
51+
public void init() throws ServletException {
52+
rollerSession = UIBeanFactory.getBean(RollerSession.class);
4453
}
4554

4655
/**
@@ -62,9 +71,8 @@ public void doGet(HttpServletRequest request,
6271
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
6372
} else {
6473
// need post permission to view comments
65-
RollerSession rses = RollerSession.getRollerSession(request);
6674
Weblog weblog = c.getWeblogEntry().getWebsite();
67-
if (weblog.hasUserPermission(rses.getAuthenticatedUser(), WeblogPermission.POST)) {
75+
if (weblog.hasUserPermission(rollerSession.getAuthenticatedUser(), WeblogPermission.POST)) {
6876
String content = Utilities.escapeHTML(c.getContent());
6977
content = StringEscapeUtils.escapeEcmaScript(content);
7078
String json = "{ id: \"" + c.getId() + "\"," + "content: \"" + content + "\" }";
@@ -101,9 +109,8 @@ public void doPut(HttpServletRequest request,
101109
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
102110
} else {
103111
// need post permission to edit comments
104-
RollerSession rses = RollerSession.getRollerSession(request);
105112
Weblog weblog = c.getWeblogEntry().getWebsite();
106-
if (weblog.hasUserPermission(rses.getAuthenticatedUser(), WeblogPermission.POST)) {
113+
if (weblog.hasUserPermission(rollerSession.getAuthenticatedUser(), WeblogPermission.POST)) {
107114
String content = Utilities.streamToString(request.getInputStream());
108115
c.setContent(content);
109116
// don't update the posttime when updating the comment

app/src/main/java/org/apache/roller/weblogger/ui/struts2/util/UIActionInterceptor.java

+14-5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
package org.apache.roller.weblogger.ui.struts2.util;
2020

21+
import javax.servlet.ServletException;
2122
import javax.servlet.http.HttpServletRequest;
2223

2324
import org.apache.commons.lang3.StringUtils;
@@ -40,6 +41,17 @@ public class UIActionInterceptor extends MethodFilterInterceptor implements
4041

4142
private static final long serialVersionUID = -6452966127207525616L;
4243
private static Log log = LogFactory.getLog(UIActionInterceptor.class);
44+
private RollerSession rollerSession;
45+
46+
@Override
47+
public void init() {
48+
try {
49+
rollerSession = UIBeanFactory.getBean(RollerSession.class);
50+
} catch (ServletException e) {
51+
log.error("Failed to initialize UIActionInterceptor", e);
52+
throw new RuntimeException("Failed to initialize UIActionInterceptor", e);
53+
}
54+
}
4355

4456
@Override
4557
public String doIntercept(ActionInvocation invocation) throws Exception {
@@ -63,10 +75,8 @@ public String doIntercept(ActionInvocation invocation) throws Exception {
6375

6476
UIAction theAction = (UIAction) action;
6577

66-
// extract the authenticated user and set it
67-
RollerSession rses = RollerSession.getRollerSession(request);
68-
if (rses != null) {
69-
theAction.setAuthenticatedUser(rses.getAuthenticatedUser());
78+
if (rollerSession != null) {
79+
theAction.setAuthenticatedUser(rollerSession.getAuthenticatedUser());
7080
}
7181

7282
// extract the work weblog and set it
@@ -88,5 +98,4 @@ public String doIntercept(ActionInvocation invocation) throws Exception {
8898

8999
return invocation.invoke();
90100
}
91-
92101
}

app/src/main/resources/struts.xml

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
"http://struts.apache.org/dtds/struts-2.5.dtd">
2222
<struts>
2323

24+
<constant name="struts.objectFactory" value="org.apache.roller.weblogger.ui.struts2.util.UICustomObjectFactory"/>
25+
2426
<!-- Weblogger default package -->
2527
<package name="weblogger" namespace="/roller-ui" extends="struts-default">
2628

0 commit comments

Comments
 (0)