PT #155293325: Adopt Boot 2 mappings bean new JSON data

This commit is contained in:
BoykoAlex
2018-04-05 19:57:39 -04:00
parent 36512e3337
commit d57b4745dd
2 changed files with 108 additions and 45 deletions

View File

@@ -48,6 +48,11 @@
<groupId>org.springframework.ide.vscode</groupId> <groupId>org.springframework.ide.vscode</groupId>
<version>${project.version}</version> <version>${project.version}</version>
</dependency> </dependency>
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm</artifactId>
<version>6.1.1</version>
</dependency>
</dependencies> </dependencies>
</project> </project>

View File

@@ -10,54 +10,45 @@
*******************************************************************************/ *******************************************************************************/
package org.springframework.ide.vscode.commons.boot.app.cli.requestmappings; package org.springframework.ide.vscode.commons.boot.app.cli.requestmappings;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
import org.objectweb.asm.Type;
public class Boot20DispatcherServletMapping extends AbstractRequestMapping { public class Boot20DispatcherServletMapping implements RequestMapping {
/* /*
Some example entries: Example entry:
[ {
{ "handler": "public java.lang.String com.example.demo.RestApi.aha(java.lang.String)",
"handler":"ResourceHttpRequestHandler [locations=[class path resource [META-INF/resources/], class path resource [resources/], class path resource [static/], class path resource [public/], ServletContext resource [/], class path resource []], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver@7a63363d]]", "predicate": "{[/genericHello || /aha],methods=[GET]}",
"predicate":"/** /favicon.ico" "details": {
}, "requestMappingConditions": {
{ "headers": [],
"handler":"public java.lang.Object org.springframework.boot.actuate.endpoint.web.servlet.AbstractWebMvcEndpointHandlerMapping$OperationHandler.handle(javax.servlet.http.HttpServletRequest,java.util.Map<java.lang.String, java.lang.String>)", "methods": [
"predicate":"{[/actuator/health],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" "GET"
}, ],
{ "patterns": [
"handler":"public java.lang.Object org.springframework.boot.actuate.endpoint.web.servlet.AbstractWebMvcEndpointHandlerMapping$OperationHandler.handle(javax.servlet.http.HttpServletRequest,java.util.Map<java.lang.String, java.lang.String>)", "/genericHello",
"predicate":"{[/actuator/info],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" "/aha"
}, ],
{ "produces": [],
"handler":"protected java.util.Map<java.lang.String, java.util.Map<java.lang.String, org.springframework.boot.actuate.endpoint.web.Link>> org.springframework.boot.actuate.endpoint.web.servlet.WebMvcEndpointHandlerMapping.links(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)", "params": [],
"predicate":"{[/actuator],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" "consumes": []
}, },
{ "handlerMethod": {
"handler":"public com.example.SomeData com.example.ActuatorClientTestSubjectApplication.getMethodName(java.lang.String)", "name": "aha",
"predicate":"{[/path],methods=[GET]}" "className": "com.example.demo.RestApi",
}, "descriptor": "(Ljava/lang/String;)Ljava/lang/String;"
{ }
"handler":"public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)", }
"predicate":"{[/error],produces=[text/html]}" }
}, */
{
"handler":"public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)",
"predicate":"{[/error]}"
},
{
"handler":"ResourceHttpRequestHandler [locations=[class path resource [META-INF/resources/webjars/]], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver@78837bf]]",
"predicate":"/webjars/**"
},
{
"handler":"ResourceHttpRequestHandler [locations=[class path resource [META-INF/resources/], class path resource [resources/], class path resource [static/], class path resource [public/], ServletContext resource [/]], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver@64b93bad]]",
"predicate":"/**"
}
]
*/
private JSONObject data; private JSONObject data;
@@ -65,14 +56,81 @@ Some example entries:
this.data = data; this.data = data;
} }
private JSONObject getDetails() {
return data.optJSONObject("details");
}
private JSONObject getHandlerMethod() {
JSONObject details = getDetails();
return details == null ? null : details.getJSONObject("handlerMethod");
}
private JSONObject getRequestMappingConditions() {
JSONObject details = getDetails();
return details == null ? null : details.getJSONObject("requestMappingConditions");
}
@Override @Override
public String getMethodString() { public String getMethodString() {
return data.optString("handler"); return data.optString("handler");
} }
@Override @Override
protected String getPredicateString() { public String getFullyQualifiedClassName() {
return data.optString("predicate"); JSONObject handlerMethod = getHandlerMethod();
return handlerMethod == null ? null : handlerMethod.getString("className");
}
@Override
public String getMethodName() {
JSONObject handlerMethod = getHandlerMethod();
return handlerMethod == null ? null : handlerMethod.getString("name");
}
@Override
public String[] getMethodParameters() {
JSONObject handlerMethod = getHandlerMethod();
if (handlerMethod != null) {
Type type = Type.getMethodType(handlerMethod.getString("descriptor"));
Type[] argsTypes = type.getArgumentTypes();
String[] parameterTypes = new String[argsTypes.length];
for (int i = 0; i < argsTypes.length; i++) {
parameterTypes[i] = argsTypes[i].getClassName();
}
return parameterTypes;
}
return new String[0];
}
@Override
public String[] getSplitPath() {
JSONObject rmConditions = getRequestMappingConditions();
if (rmConditions == null) {
return new String[0];
} else {
JSONArray jsonArray = rmConditions.getJSONArray("patterns");
String[] paths = new String[jsonArray.length()];
for (int i = 0; i < jsonArray.length(); i++) {
paths[i] = jsonArray.getString(i);
}
return paths;
}
}
@Override
public Set<String> getRequestMethods() {
JSONObject rmConditions = getRequestMappingConditions();
if (rmConditions == null) {
return Collections.emptySet();
} else {
JSONArray jsonArray = rmConditions.getJSONArray("methods");
Set<String> methods = new HashSet<>();
for (int i = 0; i < jsonArray.length(); i++) {
methods.add(jsonArray.getString(i));
}
return methods;
}
} }
} }