-
Notifications
You must be signed in to change notification settings - Fork 38.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce LookupPath in WebFlux request routing
This commit adds the `LookupPath` class that contains the full request path relative to the web context; the application can get from it various information, including the file extension and path parameters (if any). Since that operation is done multiple times for each request, this object is stored as an attribute at the `ServerWebExchange` level. Issue: SPR-15397
- Loading branch information
Showing
18 changed files
with
335 additions
and
183 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
spring-web/src/main/java/org/springframework/web/server/support/LookupPath.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* Copyright 2002-2017 the original author or authors. | ||
* | ||
* Licensed 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.springframework.web.server.support; | ||
|
||
import org.springframework.lang.Nullable; | ||
import org.springframework.web.server.ServerWebExchange; | ||
|
||
/** | ||
* Lookup path information of an incoming HTTP request. | ||
* | ||
* @author Brian Clozel | ||
* @since 5.0 | ||
* @see HttpRequestPathHelper | ||
*/ | ||
public final class LookupPath { | ||
|
||
public static final String LOOKUP_PATH_ATTRIBUTE = LookupPath.class.getName(); | ||
|
||
private final String path; | ||
|
||
private final int fileExtensionIndex; | ||
|
||
private final int pathParametersIndex; | ||
|
||
public LookupPath(String path, int fileExtensionIndex, int pathParametersIndex) { | ||
this.path = path; | ||
this.fileExtensionIndex = fileExtensionIndex; | ||
this.pathParametersIndex = pathParametersIndex; | ||
} | ||
|
||
public String getPath() { | ||
if (this.pathParametersIndex != -1) { | ||
// TODO: variant without the path parameter information? | ||
//return this.path.substring(0, this.pathParametersIndex); | ||
return this.path; | ||
} | ||
else { | ||
return this.path; | ||
} | ||
} | ||
|
||
public String getPathWithoutExtension() { | ||
if (this.fileExtensionIndex != -1) { | ||
return this.path.substring(0, this.fileExtensionIndex); | ||
} | ||
else { | ||
return this.path; | ||
} | ||
} | ||
|
||
@Nullable | ||
public String getFileExtension() { | ||
if (this.fileExtensionIndex == -1) { | ||
return null; | ||
} | ||
else if (this.pathParametersIndex == -1) { | ||
return this.path.substring(this.fileExtensionIndex); | ||
} | ||
else { | ||
return this.path.substring(this.fileExtensionIndex, this.pathParametersIndex); | ||
} | ||
} | ||
|
||
@Nullable | ||
public String getPathParameters() { | ||
return this.pathParametersIndex == -1 ? | ||
null : this.path.substring(this.pathParametersIndex + 1); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
spring-web/src/test/java/org/springframework/web/server/support/LookupPathTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright 2002-2017 the original author or authors. | ||
* | ||
* Licensed 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.springframework.web.server.support; | ||
|
||
import org.junit.Test; | ||
|
||
import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest; | ||
import org.springframework.web.server.ServerWebExchange; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
/** | ||
* Unit tests for {@link LookupPath} | ||
* @author Brian Clozel | ||
*/ | ||
public class LookupPathTests { | ||
|
||
@Test | ||
public void parsePath() { | ||
LookupPath path = create("/foo"); | ||
assertEquals("/foo", path.getPath()); | ||
assertEquals("/foo", path.getPathWithoutExtension()); | ||
} | ||
|
||
@Test | ||
public void parsePathWithExtension() { | ||
LookupPath path = create("/foo.txt"); | ||
assertEquals("/foo.txt", path.getPath()); | ||
assertEquals("/foo", path.getPathWithoutExtension()); | ||
assertEquals(".txt", path.getFileExtension()); | ||
} | ||
|
||
@Test | ||
public void parsePathWithParams() { | ||
LookupPath path = create("/test/foo.txt;foo=bar?framework=spring"); | ||
assertEquals("/test/foo.txt;foo=bar", path.getPath()); | ||
assertEquals("/test/foo", path.getPathWithoutExtension()); | ||
assertEquals(".txt", path.getFileExtension()); | ||
assertEquals("foo=bar", path.getPathParameters()); | ||
} | ||
|
||
private LookupPath create(String path) { | ||
HttpRequestPathHelper helper = new HttpRequestPathHelper(); | ||
ServerWebExchange exchange = MockServerHttpRequest.get(path).build().toExchange(); | ||
return helper.getLookupPathForRequest(exchange); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.