PT 161583390 - Initial implementation to support context path
Added support for context path for boot 1.x and 2.x when property defined via command line args. Also added some relevant junits for request mappings with context paths.
This commit is contained in:
@@ -46,9 +46,10 @@ public class LiveAppURLSymbolProvider {
|
||||
try {
|
||||
String host = app.getHost();
|
||||
String port = app.getPort();
|
||||
String contextPath = app.getContextPath();
|
||||
Stream<String> urls = app.getRequestMappings().stream()
|
||||
.flatMap(rm -> Arrays.stream(rm.getSplitPath()))
|
||||
.map(path -> UrlUtil.createUrl(host, port, path));
|
||||
.map(path -> UrlUtil.createUrl(host, port, path, contextPath));
|
||||
urls.forEach(url -> result.add(new SymbolInformation(url, SymbolKind.Method, new Location(url, new Range(new Position(0, 0), new Position(0, 1))))));
|
||||
}
|
||||
catch (Exception e) {
|
||||
|
||||
@@ -179,6 +179,8 @@ public class RequestMappingHoverProvider implements HoverProvider {
|
||||
List<String> urls = new ArrayList<>();
|
||||
for (int i = 0; i < mappingMethods.size(); i++) {
|
||||
Tuple2<RequestMapping, SpringBootApp> mappingMethod = mappingMethods.get(i);
|
||||
SpringBootApp app = mappingMethod.getT2();
|
||||
String contextPath = app.getContextPath();
|
||||
|
||||
String port = mappingMethod.getT2().getPort();
|
||||
String host = mappingMethod.getT2().getHost();
|
||||
@@ -192,7 +194,7 @@ public class RequestMappingHoverProvider implements HoverProvider {
|
||||
paths = new String[] {""};
|
||||
}
|
||||
for (String path : paths) {
|
||||
String url = UrlUtil.createUrl(host, port, path);
|
||||
String url = UrlUtil.createUrl(host, port, path, contextPath);
|
||||
urls.add(url);
|
||||
}
|
||||
}
|
||||
@@ -215,8 +217,9 @@ public class RequestMappingHoverProvider implements HoverProvider {
|
||||
//So we'll pretend this is the same as path="" as that gives a working link.
|
||||
paths = new String[] {""};
|
||||
}
|
||||
String contextPath = app.getContextPath();
|
||||
List<Renderable> renderableUrls = Arrays.stream(paths).flatMap(path -> {
|
||||
String url = UrlUtil.createUrl(host, port, path);
|
||||
String url = UrlUtil.createUrl(host, port, path, contextPath);
|
||||
return Stream.of(Renderables.link(url, url), Renderables.lineBreak());
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2017 Pivotal, Inc.
|
||||
* Copyright (c) 2017, 2018 Pivotal, Inc.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
@@ -10,6 +10,8 @@
|
||||
*******************************************************************************/
|
||||
package org.springframework.ide.vscode.boot.java.requestmapping;
|
||||
|
||||
import org.springframework.ide.vscode.commons.util.StringUtil;
|
||||
|
||||
public class UrlUtil {
|
||||
|
||||
/**
|
||||
@@ -17,9 +19,10 @@ public class UrlUtil {
|
||||
* @param host
|
||||
* @param port
|
||||
* @param path
|
||||
* @param contextPath
|
||||
* @return the resultant URL
|
||||
*/
|
||||
public static String createUrl(String host, String port, String path) {
|
||||
public static String createUrl(String host, String port, String path, String contextPath) {
|
||||
if (path==null) {
|
||||
path = "";
|
||||
}
|
||||
@@ -28,6 +31,12 @@ public class UrlUtil {
|
||||
if (!path.startsWith("/")) {
|
||||
path = "/" +path;
|
||||
}
|
||||
if (StringUtil.hasText(contextPath)) {
|
||||
if (!contextPath.startsWith("/")) {
|
||||
contextPath = "/" + contextPath;
|
||||
}
|
||||
path = contextPath + path;
|
||||
}
|
||||
if (port.equals("80")) {
|
||||
return "http://"+host+path;
|
||||
} else {
|
||||
|
||||
@@ -326,6 +326,87 @@ public class RequestMappingLiveHoverTest {
|
||||
editor.assertNoHover("@PutMapping(\"/greetings\")");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLiveHoverHintWithContextPath() throws Exception {
|
||||
|
||||
File directory = new File(
|
||||
ProjectsHarness.class.getResource("/test-projects/test-request-mapping-live-hover/").toURI());
|
||||
String docUri = directory.toPath().resolve("src/main/java/example/HelloWorldController.java").toUri()
|
||||
.toString();
|
||||
|
||||
// Build a mock running boot app
|
||||
mockAppProvider.builder()
|
||||
.isSpringBootApp(true)
|
||||
.port("1111")
|
||||
.processId("22022")
|
||||
.host("cfapps.io")
|
||||
.contextPath("/adifferentpath")
|
||||
.processName("test-request-mapping-live-hover")
|
||||
// Ugly, but this is real JSON copied from a real live running app. We want the
|
||||
// mock app to return realistic results if possible
|
||||
.requestMappingsJson(
|
||||
"{\"/webjars/**\":{\"bean\":\"resourceHandlerMapping\"},\"/**\":{\"bean\":\"resourceHandlerMapping\"},\"/**/favicon.ico\":{\"bean\":\"faviconHandlerMapping\"},\"{[/hello-world],methods=[GET]}\":{\"bean\":\"requestMappingHandlerMapping\",\"method\":\"public example.Greeting example.HelloWorldController.sayHello(java.lang.String)\"},\"{[/goodbye]}\":{\"bean\":\"requestMappingHandlerMapping\",\"method\":\"public java.lang.String example.RestApi.goodbye()\"},\"{[/hello]}\":{\"bean\":\"requestMappingHandlerMapping\",\"method\":\"public java.lang.String example.RestApi.hello()\"},\"{[/error]}\":{\"bean\":\"requestMappingHandlerMapping\",\"method\":\"public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)\"},\"{[/error],produces=[text/html]}\":{\"bean\":\"requestMappingHandlerMapping\",\"method\":\"public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)\"}}")
|
||||
.build();
|
||||
|
||||
harness.intialize(directory);
|
||||
|
||||
Editor editor = harness.newEditorFromFileUri(docUri, LanguageId.JAVA);
|
||||
editor.assertHighlights("@RequestMapping(method=RequestMethod.GET)");
|
||||
editor.assertHoverContains("@RequestMapping(method=RequestMethod.GET)", "[http://cfapps.io:1111/adifferentpath/hello-world](http://cfapps.io:1111/adifferentpath/hello-world) \n" +
|
||||
"\n" +
|
||||
"Process [PID=22022, name=`test-request-mapping-live-hover`]");
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMultiPathMappingWithContextPath() throws Exception {
|
||||
|
||||
File directory = new File(
|
||||
ProjectsHarness.class.getResource("/test-projects/test-request-mapping-live-hover/").toURI());
|
||||
String docUri = directory.toPath().resolve("src/main/java/example/RestApi.java").toUri()
|
||||
.toString();
|
||||
|
||||
|
||||
// Build a mock running boot app
|
||||
mockAppProvider.builder()
|
||||
.isSpringBootApp(true)
|
||||
.port("999")
|
||||
.processId("76543")
|
||||
.host("cfapps.io")
|
||||
.contextPath("/differentPaath")
|
||||
.processName("test-request-mapping-live-hover")
|
||||
// Ugly, but this is real JSON copied from a real live running app. We want the
|
||||
// mock app to return realistic results if possible
|
||||
.requestMappingsJson(
|
||||
"{\"{[/greetings || /hello],methods=[GET]}\": {\"bean\": \"requestMappingHandlerMapping\", \"method\":\"public java.lang.String com.example.RestApi.greetings()\"}}")
|
||||
. build();
|
||||
|
||||
harness.intialize(directory);
|
||||
|
||||
Editor editor = harness.newEditor(LanguageId.JAVA,
|
||||
"package com.example;\n" +
|
||||
"\n" +
|
||||
"import org.springframework.stereotype.Controller;\n" +
|
||||
"import org.springframework.web.bind.annotation.RequestMapping;\n" +
|
||||
"import org.springframework.web.bind.annotation.RequestMethod.*;\n" +
|
||||
"\n" +
|
||||
"@Controller\n" +
|
||||
"public class RestApi {\n" +
|
||||
"\n" +
|
||||
"@RequestMapping(value={\"/greetings\", \"/hello\"}, method=GET)\n" +
|
||||
"public String greetings() {\n" +
|
||||
"}\n" +
|
||||
"\n" +
|
||||
"}",
|
||||
docUri);
|
||||
|
||||
editor.assertHoverContains("@RequestMapping(value={\"/greetings\", \"/hello\"}, method=GET)", "[http://cfapps.io:999/differentPaath/greetings](http://cfapps.io:999/differentPaath/greetings) \n" +
|
||||
"[http://cfapps.io:999/differentPaath/hello](http://cfapps.io:999/differentPaath/hello) \n" +
|
||||
"\n" +
|
||||
"Process [PID=76543, name=`test-request-mapping-live-hover`]");
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMultiPathMappingHoverHintMethod1() throws Exception {
|
||||
|
||||
|
||||
@@ -92,6 +92,11 @@ public class MockRunningAppProvider {
|
||||
return this;
|
||||
}
|
||||
|
||||
public MockAppBuilder contextPath(String contextPath) throws Exception {
|
||||
when(app.getContextPath()).thenReturn(contextPath);
|
||||
return this;
|
||||
}
|
||||
|
||||
public MockAppBuilder port(String port) throws Exception {
|
||||
when(app.getPort()).thenReturn(port);
|
||||
return this;
|
||||
|
||||
Reference in New Issue
Block a user