Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@
* @param mapping
* The action mapping to populate
*/
protected void parseNameAndNamespace(String uri, ActionMapping mapping, ConfigurationManager configManager) {

Check failure on line 337 in plugins/rest/src/main/java/org/apache/struts2/rest/RestActionMapper.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this method to reduce its Cognitive Complexity from 20 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=apache_struts&issues=AaAv__JhObA3UIzK44y_&open=AaAv__JhObA3UIzK44y_&pullRequest=1863
String namespace, name;
int lastSlash = uri.lastIndexOf("/");
if (lastSlash == -1) {
Expand All @@ -351,6 +351,7 @@
Configuration config = configManager.getConfiguration();
String prefix = uri.substring(0, lastSlash);
namespace = "";
boolean rootAvailable = false;
// Find the longest matching namespace, defaulting to the default
for (Object o : config.getPackageConfigs().values()) {
String ns = ((PackageConfig) o).getNamespace();
Expand All @@ -359,9 +360,19 @@
namespace = ns;
}
}
if ("/".equals(ns)) {
rootAvailable = true;
}
}

// must be read before the root namespace is selected below, as it is relative to ""
name = uri.substring(namespace.length() + 1);

// WW-5688, WW-2461: still none found, use the root namespace if it is declared, so that
// an id-bearing uri lands in the same namespace as the one without an id
if (rootAvailable && namespace.isEmpty()) {
namespace = "/";
}
}

mapping.setNamespace(cleanupNamespaceName(namespace));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF 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.apache.struts2.rest;

import com.opensymphony.xwork2.XWorkTestCase;
import org.apache.struts2.config.StrutsXmlConfigurationProvider;
import org.apache.struts2.dispatcher.mapper.ActionMapping;
import org.springframework.mock.web.MockHttpServletRequest;

/**
* WW-5688: an action declared in the root namespace has to stay reachable whether or not the
* request URI carries an id, so that {@code index} and {@code show} resolve to the same action.
*
* <p>These assertions go all the way to the {@code ActionConfig} rather than stopping at the
* mapping, because the reported symptom is a 404 - the mapper handing back a namespace that
* the configuration cannot resolve.</p>
*/
public class RestActionMapperRootNamespaceTest extends XWorkTestCase {

private RestActionMapper mapper;

@Override
protected void setUp() throws Exception {
super.setUp();
loadConfigurationProviders(new StrutsXmlConfigurationProvider("ww-5688.xml"));
mapper = new RestActionMapper();
}

private ActionMapping map(String servletPath, String httpMethod) {
MockHttpServletRequest request = new MockHttpServletRequest();
request.setContextPath("/myapp");
request.setMethod(httpMethod);
request.setRequestURI("/myapp" + servletPath);
request.setServletPath(servletPath);
return mapper.getMapping(request, configurationManager);
}

private void assertResolves(String servletPath, String httpMethod, String expectedMethod) {
ActionMapping mapping = map(servletPath, httpMethod);
assertNotNull(httpMethod + " " + servletPath + " produced no mapping", mapping);
assertEquals("dog", mapping.getName());
assertEquals(expectedMethod, mapping.getMethod());
assertNotNull(httpMethod + " " + servletPath + " must resolve to the action declared in the root namespace,"
+ " but namespace '" + mapping.getNamespace() + "' does not hold it",
configurationManager.getConfiguration().getRuntimeConfiguration()
.getActionConfig(mapping.getNamespace(), mapping.getName()));
}

public void testIndexResolvesInRootNamespace() {
assertResolves("/dog", "GET", "index");
}

public void testShowResolvesInRootNamespace() {
assertResolves("/dog/1", "GET", "show");
}

public void testUpdateResolvesInRootNamespace() {
assertResolves("/dog/1", "PUT", "update");
}

public void testDestroyResolvesInRootNamespace() {
assertResolves("/dog/1", "DELETE", "destroy");
}

public void testIdIsStillExtracted() {
ActionMapping mapping = map("/dog/1", "GET");
assertEquals("1", ((String[]) mapping.getParams().get("id"))[0]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,23 @@ public void testParseNameAndNamespaceWithEdit() {
tryUri("/my/foo/23;edit", "/my", "foo/23;edit");
}

/**
* WW-5688: when a package declares the root namespace, a uri that matches no more specific
* namespace belongs to that root rather than to the default namespace, so that "/foo" and
* "/foo/23" agree on where the action lives. Without a root package the default namespace
* still wins - see {@link #testParseNameAndNamespace()}.
*/
public void testParseNameAndNamespaceWithRootPackage() {
config.addPackageConfig("root", new PackageConfig.Builder("root").namespace("/").build());

tryUri("/foo/23", "/", "foo/23");
tryUri("/foo/", "/", "foo/");
tryUri("/", "/", "");

// a longer declared namespace still outranks the root
tryUri("/my/foo/23", "/my", "foo/23");
}

public void testShouldAllowExclamation() throws Exception {
req.setRequestURI("/myapp/animals/dog/fido!edit");
req.setServletPath("/animals/dog/fido!edit");
Expand Down
31 changes: 31 additions & 0 deletions plugins/rest/src/test/resources/ww-5688.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!--
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF 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.
*/
-->
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 6.0//EN"
"https://struts.apache.org/dtds/struts-6.0.dtd">

<!-- WW-5688: the only declared package sits at the root namespace -->
<struts>
<package name="ww-5688-root" namespace="/">
<action name="dog" class="org.apache.struts2.ActionSupport"/>
</package>
</struts>
Loading