diff --git a/TODO.md b/TODO.md index 7e3dcc65363e..3e497eaacf9f 100644 --- a/TODO.md +++ b/TODO.md @@ -987,16 +987,6 @@ These are bugs, correctness issues, or missing functionality that may affect pro --- -### 93. Manager Session Detail TODO (1 item) - -| # | File:Line | Description | Fix Idea | Effort | Difficulty | -|---|-----------|-------------|----------|--------|------------| -| 93.1 | `webapps/manager/WEB-INF/jsp/sessionDetail.jsp:140` | Max Inactive Interval not settable from session detail page | Add a form field and API call to update session max inactive interval. | 1-2 days | Medium | - -**Total estimated effort: 1-2 days, Medium difficulty** - ---- - ## Auto-Generated Stub TODOs These are IDE-generated stub methods with `// TODO Auto-generated method stub` comments. They are low-priority but should eventually be properly implemented. diff --git a/java/org/apache/catalina/manager/HTMLManagerServlet.java b/java/org/apache/catalina/manager/HTMLManagerServlet.java index 6cbb793fa625..c1867555fb47 100644 --- a/java/org/apache/catalina/manager/HTMLManagerServlet.java +++ b/java/org/apache/catalina/manager/HTMLManagerServlet.java @@ -865,6 +865,24 @@ protected void doSessions(ContextName cn, HttpServletRequest req, HttpServletRes req.setAttribute(APPLICATION_MESSAGE, outMessage); displaySessionDetailPage(req, resp, cn, sessionId, smClient); return; + } else if ("setMaxInactiveInterval".equals(action)) { + String sessionId = req.getParameter("sessionId"); + String intervalParam = req.getParameter("maxInactiveInterval"); + try { + int interval = Integer.parseInt(intervalParam); + if (setSessionMaxInactiveInterval(cn, sessionId, interval, smClient)) { + req.setAttribute(APPLICATION_MESSAGE, + smClient.getString("htmlManagerServlet.sessionTimeoutUpdated", sessionId, intervalParam)); + } else { + req.setAttribute(APPLICATION_ERROR, + smClient.getString("htmlManagerServlet.sessionTimeoutNotUpdated", sessionId)); + } + } catch (NumberFormatException nfe) { + req.setAttribute(APPLICATION_ERROR, + smClient.getString("htmlManagerServlet.invalidSessionTimeout", intervalParam)); + } + displaySessionDetailPage(req, resp, cn, sessionId, smClient); + return; } // else displaySessionsListPage(cn, req, resp, smClient); } @@ -1090,6 +1108,42 @@ protected boolean removeSessionAttribute(ContextName cn, String sessionId, Strin return wasPresent; } + /** + * Set the maximum inactive interval for an HTTP session. + * + * @param cn Name of the application hosting the session + * @param sessionId The session id + * @param maxInactiveInterval The maximum inactive interval in seconds + * @param smClient StringManager for the client's locale + * + * @return {@code true} if the interval was updated, otherwise {@code false} + */ + protected boolean setSessionMaxInactiveInterval(ContextName cn, String sessionId, int maxInactiveInterval, + StringManager smClient) { + Session session = getSessionForNameAndId(cn, sessionId, smClient); + if (session == null) { + if (debug >= 1) { + log("Cannot update maximum inactive interval for null session " + sessionId); + } + return false; + } + if (session.getSession() == null) { + if (debug >= 1) { + log("Cannot update maximum inactive interval for session with null HTTP session " + sessionId); + } + return false; + } + try { + session.setMaxInactiveInterval(maxInactiveInterval); + return true; + } catch (IllegalStateException ise) { + if (debug >= 1) { + log("Cannot update maximum inactive interval for invalidated session id " + sessionId); + } + return false; + } + } + /** * Get a comparator for sessions based on the specified sort criteria. * diff --git a/java/org/apache/catalina/manager/LocalStrings.properties b/java/org/apache/catalina/manager/LocalStrings.properties index 7f71ba4215e4..dca21ecd7943 100644 --- a/java/org/apache/catalina/manager/LocalStrings.properties +++ b/java/org/apache/catalina/manager/LocalStrings.properties @@ -85,6 +85,7 @@ htmlManagerServlet.helpHtmlManager=HTML Manager Help htmlManagerServlet.helpHtmlManagerFile=../docs/html-manager-howto.html htmlManagerServlet.helpManager=Manager Help htmlManagerServlet.helpManagerFile=../docs/manager-howto.html +htmlManagerServlet.invalidSessionTimeout=Invalid maximum inactive interval [{0}] htmlManagerServlet.jvmFreeMemory=Free Memory: htmlManagerServlet.jvmMaxMemory=Max Memory: htmlManagerServlet.jvmTableTitleInitial=Initial @@ -116,6 +117,8 @@ htmlManagerServlet.serverOSVersion=OS Version htmlManagerServlet.serverTitle=Server Information htmlManagerServlet.serverVersion=Tomcat Version htmlManagerServlet.sessionNotFound=Session not found [{0}] +htmlManagerServlet.sessionTimeoutNotUpdated=Unable to update maximum inactive interval for session [{0}] +htmlManagerServlet.sessionTimeoutUpdated=Maximum inactive interval for session [{0}] set to [{1}] seconds htmlManagerServlet.title=Tomcat Web Application Manager jmxProxyServlet.noBeanFound=Cannot find MBean with object name [{0}] diff --git a/test/org/apache/catalina/manager/TestManagerWebapp.java b/test/org/apache/catalina/manager/TestManagerWebapp.java index bfad7e0d6356..262a4f1f9a58 100644 --- a/test/org/apache/catalina/manager/TestManagerWebapp.java +++ b/test/org/apache/catalina/manager/TestManagerWebapp.java @@ -729,6 +729,10 @@ public void testJsps() throws Exception { tomcat.start(); + ManagerBase manager = (ManagerBase) ctx.getManager(); + Session session = manager.createSession(null); + manager.add(session); + SimpleHttpClient client = new SimpleHttpClient() { @Override public boolean isResponseBodyOK() { @@ -789,6 +793,40 @@ public boolean isResponseBodyOK() { Assert.assertTrue(client.getResponseBody().contains("Sessions Administration")); Assert.assertTrue(client.getResponseBody().contains("active Sessions")); + m = noncePattern.matcher(client.getResponseBody()); + Assert.assertTrue("CSRF nonce not found in sessions response", m.find()); + nonce = m.group(1); + + String requestBody = "action=setMaxInactiveInterval&sessionId=" + session.getId() + + "&sessionType=Primary&maxInactiveInterval=123"; + + // Update the maximum inactive interval from the session detail page + // Authentication may have changed the session ID, so use the latest cookie for the POST. + // @formatter:off + client.setRequestPause(0); + client.setRequest(new String[] { + "POST /manager/html/sessions?path=/testapp&" + + Constants.CSRF_NONCE_REQUEST_PARAM + "=" + nonce + + " HTTP/1.1" + CRLF, + "Host: localhost" + CRLF, + "Authorization: " + basicHeader + CRLF, + "Cookie: JSESSIONID=" + client.getSessionId() + CRLF, + "Content-Type: application/x-www-form-urlencoded" + CRLF, + "Content-Length: " + requestBody.getBytes(StandardCharsets.UTF_8).length + CRLF, + "Connection: Close" + CRLF, + CRLF, + requestBody + }); + // @formatter:on + client.connect(); + client.processRequest(true); + Assert.assertEquals(HttpServletResponse.SC_OK, client.getStatusCode()); + Assert.assertEquals(123, session.getMaxInactiveInterval()); + Assert.assertTrue(client.getResponseBody().contains("name=\"maxInactiveInterval\"")); + Assert.assertTrue(client.getResponseBody().contains("value=\"123\"")); + Assert.assertTrue(client.getResponseBody().contains( + "Maximum inactive interval for session [" + session.getId() + "] set to [123] seconds")); + tomcat.stop(); } diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml index af6936534a3e..ded5849637cf 100644 --- a/webapps/docs/changelog.xml +++ b/webapps/docs/changelog.xml @@ -609,6 +609,10 @@ + + Add support for updating a primary session's maximum inactive interval + from the session detail page in the HTML Manager. (sainadh777) + Add Manager 2.0 webapp with a modern reactive design. Provided as a module in the modules/manager2 folder in the source diff --git a/webapps/manager/WEB-INF/jsp/sessionDetail.jsp b/webapps/manager/WEB-INF/jsp/sessionDetail.jsp index 4e86fe2471c6..54e153e20783 100644 --- a/webapps/manager/WEB-INF/jsp/sessionDetail.jsp +++ b/webapps/manager/WEB-INF/jsp/sessionDetail.jsp @@ -135,13 +135,31 @@ Attribute value - <%--tfoot> + - TODO: set Max Inactive Interval on sessions + <% + if ("Primary".equals(request.getParameter("sessionType"))) { + %> +
+
+ + + + + + +
+
+ <% + } else { + out.print("Primary sessions only"); + } + %> - + <% attributeNamesEnumeration = currentHttpSession.getAttributeNames(); while (attributeNamesEnumeration.hasMoreElements()) {