-
Notifications
You must be signed in to change notification settings - Fork 701
Redirect to the prefered console UI when the server root accessed #4499
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,131 @@ | ||||||||||||
| /* | ||||||||||||
| * Copyright (c) 2026, WSO2 LLC. (http://www.wso2.com). | ||||||||||||
| * | ||||||||||||
| * WSO2 LLC. licenses this file to you under the Apache License, | ||||||||||||
| * Version 2.0 (the "License"); you may not use this file except | ||||||||||||
| * in compliance with the License. | ||||||||||||
| * You may obtain a copy of the License at | ||||||||||||
| * | ||||||||||||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||||||||||||
| * | ||||||||||||
| * Unless required by applicable law or agreed to in writing, | ||||||||||||
| * software distributed under the License is distributed on an | ||||||||||||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||||||||||||
| * KIND, either express or implied. See the License for the | ||||||||||||
| * specific language governing permissions and limitations | ||||||||||||
| * under the License. | ||||||||||||
| */ | ||||||||||||
| package org.wso2.carbon.ui; | ||||||||||||
|
|
||||||||||||
| import org.apache.commons.lang.StringUtils; | ||||||||||||
| import org.apache.commons.logging.Log; | ||||||||||||
| import org.apache.commons.logging.LogFactory; | ||||||||||||
| import org.wso2.carbon.ui.deployment.beans.CarbonUIDefinitions; | ||||||||||||
| import org.wso2.carbon.ui.deployment.beans.Context; | ||||||||||||
|
|
||||||||||||
| import javax.servlet.ServletException; | ||||||||||||
| import javax.servlet.http.HttpServlet; | ||||||||||||
| import javax.servlet.http.HttpServletRequest; | ||||||||||||
| import javax.servlet.http.HttpServletResponse; | ||||||||||||
| import java.io.IOException; | ||||||||||||
|
|
||||||||||||
| import static org.wso2.carbon.ui.CarbonUIUtil.getDefaultManagementUIPath; | ||||||||||||
|
|
||||||||||||
| /** | ||||||||||||
| * A servlet that handles requests to the root path "/" and redirects to the | ||||||||||||
| * configured default context (e.g., /carbon/admin/index.jsp). | ||||||||||||
| * This servlet is necessary because when using OSGi HTTP Whiteboard, | ||||||||||||
| * the ServletContextHelper (CarbonSecuredHttpContext) registered at path "/" | ||||||||||||
| * doesn't receive requests for "/" directly unless there's a servlet | ||||||||||||
| * registered to handle that path. Without this servlet, requests to "/" | ||||||||||||
| * would result in a 404 error being forwarded to the error page. | ||||||||||||
| */ | ||||||||||||
| public class RootRedirectServlet extends HttpServlet { | ||||||||||||
|
|
||||||||||||
| private static final long serialVersionUID = 6080777136479623373L; | ||||||||||||
| private static final Log log = LogFactory.getLog(RootRedirectServlet.class); | ||||||||||||
|
|
||||||||||||
| private final CarbonUIDefinitions carbonUIDefinitions; | ||||||||||||
|
|
||||||||||||
| /** | ||||||||||||
| * Constructor with CarbonUIDefinitions for getting the default context. | ||||||||||||
| * | ||||||||||||
| * @param carbonUIDefinitions the UI definitions containing context configuration | ||||||||||||
| */ | ||||||||||||
| public RootRedirectServlet(CarbonUIDefinitions carbonUIDefinitions) { | ||||||||||||
|
|
||||||||||||
| this.carbonUIDefinitions = carbonUIDefinitions; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| /** | ||||||||||||
| * Default constructor when no CarbonUIDefinitions is available. | ||||||||||||
| */ | ||||||||||||
| public RootRedirectServlet() { | ||||||||||||
|
|
||||||||||||
| this.carbonUIDefinitions = null; | ||||||||||||
|
Comment on lines
+63
to
+65
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| @Override | ||||||||||||
| protected void doGet(HttpServletRequest request, HttpServletResponse response) | ||||||||||||
| throws ServletException, IOException { | ||||||||||||
|
|
||||||||||||
| handleRedirect(request, response); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| @Override | ||||||||||||
| protected void doPost(HttpServletRequest request, HttpServletResponse response) | ||||||||||||
| throws ServletException, IOException { | ||||||||||||
|
|
||||||||||||
| handleRedirect(request, response); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| /** | ||||||||||||
| * Handles the redirect from root path to the configured default context. | ||||||||||||
| */ | ||||||||||||
| private void handleRedirect(HttpServletRequest request, HttpServletResponse response) | ||||||||||||
| throws IOException { | ||||||||||||
|
|
||||||||||||
| String redirectUrl = getRedirectUrl(request); | ||||||||||||
|
|
||||||||||||
| if (log.isDebugEnabled()) { | ||||||||||||
| log.debug("Redirecting root request to: " + redirectUrl); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| response.sendRedirect(redirectUrl); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| /** | ||||||||||||
| * Determines the redirect URL based on configuration. | ||||||||||||
| * | ||||||||||||
| * @param request the HTTP request | ||||||||||||
| * @return the URL to redirect to | ||||||||||||
| */ | ||||||||||||
| private String getRedirectUrl(HttpServletRequest request) { | ||||||||||||
|
|
||||||||||||
| // Check for configured default context | ||||||||||||
| Context defaultContext = null; | ||||||||||||
| if (carbonUIDefinitions != null && | ||||||||||||
| carbonUIDefinitions.getContexts().containsKey("default-context")) { | ||||||||||||
| defaultContext = carbonUIDefinitions.getContexts().get("default-context"); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| if (defaultContext != null && !"".equals(defaultContext.getContextName()) | ||||||||||||
| && !"null".equals(defaultContext.getContextName())) { | ||||||||||||
| String adminConsoleURL = CarbonUIUtil.getAdminConsoleURL(request); | ||||||||||||
| if (adminConsoleURL != null) { | ||||||||||||
| int index = adminConsoleURL.lastIndexOf("carbon"); | ||||||||||||
| if (index >= 0) { | ||||||||||||
| return adminConsoleURL.substring(0, index) + defaultContext.getContextName() + "/"; | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| // Fall back to default management UI path | ||||||||||||
| String defaultManagementUIPath = getDefaultManagementUIPath(); | ||||||||||||
| if (StringUtils.isBlank(defaultManagementUIPath)) { | ||||||||||||
| defaultManagementUIPath = "/carbon/admin/index.jsp"; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| return defaultManagementUIPath; | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -56,6 +56,7 @@ | |||||||||||||||||||||||
| import org.wso2.carbon.ui.CarbonUIAuthenticator; | ||||||||||||||||||||||||
| import org.wso2.carbon.ui.CarbonUIUtil; | ||||||||||||||||||||||||
| import org.wso2.carbon.ui.DefaultCarbonAuthenticator; | ||||||||||||||||||||||||
| import org.wso2.carbon.ui.RootRedirectServlet; | ||||||||||||||||||||||||
| import org.wso2.carbon.ui.TenantAwareCsrfJsFilter; | ||||||||||||||||||||||||
| import org.wso2.carbon.ui.TextJavascriptHandler; | ||||||||||||||||||||||||
| import org.wso2.carbon.ui.TilesJspServlet; | ||||||||||||||||||||||||
|
|
@@ -84,6 +85,7 @@ | |||||||||||||||||||||||
| import javax.servlet.Servlet; | ||||||||||||||||||||||||
| import javax.servlet.ServletContextListener; | ||||||||||||||||||||||||
| import javax.servlet.Filter; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| @Component(name = "core.ui.dscomponent", immediate = true) | ||||||||||||||||||||||||
| public class CarbonUIServiceComponent { | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
|
|
@@ -252,6 +254,17 @@ public void start(BundleContext context) throws Exception { | |||||||||||||||||||||||
| contextProps.put("osgi.http.whiteboard.context.path", "/"); | ||||||||||||||||||||||||
| context.registerService(ServletContextHelper.class, (ServletContextHelper) commonContext, contextProps); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // Register RootRedirectServlet to handle requests to "/" path | ||||||||||||||||||||||||
| // This is necessary because without a servlet registered for "/", requests to the root path | ||||||||||||||||||||||||
| // would result in a 404 error being forwarded to the error page instead of being handled | ||||||||||||||||||||||||
| // by handleSecurity in CarbonSecuredHttpContext | ||||||||||||||||||||||||
|
Comment on lines
+257
to
+260
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shall we use javadoc multi line commenting style. |
||||||||||||||||||||||||
| Servlet rootRedirectServlet = new RootRedirectServlet(carbonUIDefinitions); | ||||||||||||||||||||||||
|
Comment on lines
+257
to
+261
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Log Improvement Suggestion No: 3
Suggested change
|
||||||||||||||||||||||||
| Dictionary<String, String> rootRedirectServletProps = new Hashtable<>(); | ||||||||||||||||||||||||
| rootRedirectServletProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/"); | ||||||||||||||||||||||||
| rootRedirectServletProps.put("osgi.http.whiteboard.context.select", | ||||||||||||||||||||||||
| "(osgi.http.whiteboard.context.name=carbonContext)"); | ||||||||||||||||||||||||
| context.registerService(Servlet.class, rootRedirectServlet, rootRedirectServletProps); | ||||||||||||||||||||||||
|
Comment on lines
+257
to
+266
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: fd -type f -name "RootRedirectServlet.java"Repository: wso2/carbon-kernel Length of output: 232 🏁 Script executed: fd -type f -name "CarbonUIServiceComponent.java"Repository: wso2/carbon-kernel Length of output: 232 🏁 Script executed: git ls-files | grep -i "root" | head -20Repository: wso2/carbon-kernel Length of output: 358 🏁 Script executed: cat -n "core/org.wso2.carbon.ui/src/main/java/org/wso2/carbon/ui/RootRedirectServlet.java"Repository: wso2/carbon-kernel Length of output: 5713 🏁 Script executed: cat -n "core/org.wso2.carbon.ui/src/main/java/org/wso2/carbon/ui/internal/CarbonUIServiceComponent.java" | sed -n '250,275p'Repository: wso2/carbon-kernel Length of output: 2246 🌐 Web query:
💡 Result: In OSGi HTTP Whiteboard,
So:
Citations:
🌐 Web query:
💡 Result: In the Servlet spec, a
If you want an exact match for the context root (i.e., only
Related: a pattern ending in Citations:
Pattern Either change the pattern to Recommended fix: add root path validation--- a/core/org.wso2.carbon.ui/src/main/java/org/wso2/carbon/ui/RootRedirectServlet.java
+++ b/core/org.wso2.carbon.ui/src/main/java/org/wso2/carbon/ui/RootRedirectServlet.java
@@ -81,6 +81,12 @@ public class RootRedirectServlet extends HttpServlet {
private void handleRedirect(HttpServletRequest request, HttpServletResponse response)
throws IOException {
+ // Only redirect if this is the root path, not other unmatched paths
+ String requestPath = request.getRequestURI().substring(request.getContextPath().length());
+ if (!"/".equals(requestPath)) {
+ response.sendError(HttpServletResponse.SC_NOT_FOUND);
+ return;
+ }
String redirectUrl = getRedirectUrl(request);
if (log.isDebugEnabled()) {Alternatively, change the servlet pattern from 🤖 Prompt for AI Agents |
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // Register file download servlet using HTTP Whiteboard pattern | ||||||||||||||||||||||||
| Servlet fileDownloadServlet = new FileDownloadServlet(context, getConfigurationContextService()); | ||||||||||||||||||||||||
| Dictionary<String, String> fileDownloadServletProperties = new Hashtable<>(); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Log Improvement Suggestion No: 1