Simplified handling of embedded
This commit is contained in:
@@ -10,18 +10,19 @@
|
||||
*******************************************************************************/
|
||||
package org.springframework.ide.vscode.boot.validation.generations;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.ide.vscode.boot.validation.generations.json.Generations;
|
||||
import org.springframework.ide.vscode.boot.validation.generations.json.GenerationsEmbedded;
|
||||
import org.springframework.ide.vscode.boot.validation.generations.json.JsonHalEmbedded;
|
||||
import org.springframework.ide.vscode.boot.validation.generations.json.SpringProjects;
|
||||
import org.springframework.ide.vscode.boot.validation.generations.json.SpringProjectsEmbedded;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
public class SpringProjectsClient {
|
||||
|
||||
private final String url;
|
||||
@@ -31,23 +32,27 @@ public class SpringProjectsClient {
|
||||
}
|
||||
|
||||
public SpringProjects getSpringProjects() throws Exception {
|
||||
return getEmbedded(url, SpringProjectsEmbedded.class);
|
||||
return fromEmbedded(url, SpringProjects.class);
|
||||
}
|
||||
|
||||
public Generations getGenerations(String generationsUrl) throws Exception {
|
||||
return getEmbedded(generationsUrl, GenerationsEmbedded.class);
|
||||
return fromEmbedded(generationsUrl, Generations.class);
|
||||
}
|
||||
|
||||
private <T> T getEmbedded(String url, Class<? extends JsonHalEmbedded<T>> clazz) throws Exception {
|
||||
|
||||
private <T> T fromEmbedded(String url, Class<T> clazz) throws Exception {
|
||||
if (url != null) {
|
||||
JsonHalEmbedded<T> embedded = get(url, clazz);
|
||||
if (embedded != null) {
|
||||
return embedded.get_embedded();
|
||||
Map<?, ?> result = get(url, Map.class);
|
||||
if (result != null) {
|
||||
Object obj = result.get("_embedded");
|
||||
if (obj != null) {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
return mapper.convertValue(obj, clazz);
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
private <T> T get(String url, Class<T> clazz) throws Exception {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
|
||||
|
||||
@@ -19,8 +19,4 @@ public class Generations {
|
||||
public List<Generation> getGenerations() {
|
||||
return generations;
|
||||
}
|
||||
|
||||
public void setGenerations(List<Generation> generations) {
|
||||
this.generations = generations;
|
||||
}
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2020 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
|
||||
* https://www.eclipse.org/legal/epl-v10.html
|
||||
*
|
||||
* Contributors:
|
||||
* Pivotal, Inc. - initial API and implementation
|
||||
*******************************************************************************/
|
||||
package org.springframework.ide.vscode.boot.validation.generations.json;
|
||||
|
||||
public class GenerationsEmbedded extends JsonHalLinks implements JsonHalEmbedded<Generations> {
|
||||
|
||||
|
||||
private Generations _embedded;
|
||||
|
||||
public Generations get_embedded() {
|
||||
return _embedded;
|
||||
}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2020 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
|
||||
* https://www.eclipse.org/legal/epl-v10.html
|
||||
*
|
||||
* Contributors:
|
||||
* Pivotal, Inc. - initial API and implementation
|
||||
*******************************************************************************/
|
||||
package org.springframework.ide.vscode.boot.validation.generations.json;
|
||||
|
||||
public interface JsonHalEmbedded<T> {
|
||||
|
||||
T get_embedded();
|
||||
}
|
||||
@@ -17,8 +17,4 @@ public class JsonHalLinks {
|
||||
public Links get_links() {
|
||||
return _links;
|
||||
}
|
||||
|
||||
public void set_links(Links _links) {
|
||||
this._links = _links;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2020 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
|
||||
* https://www.eclipse.org/legal/epl-v10.html
|
||||
*
|
||||
* Contributors:
|
||||
* Pivotal, Inc. - initial API and implementation
|
||||
*******************************************************************************/
|
||||
package org.springframework.ide.vscode.boot.validation.generations.json;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
|
||||
public class JsonHalParser {
|
||||
|
||||
public <T> T getEmbedded(String json, Class<? extends JsonHalEmbedded<T>> clazz) throws Exception {
|
||||
if (json != null) {
|
||||
Gson gson = new Gson();
|
||||
JsonHalEmbedded<T> embedded = gson.fromJson(json, clazz);
|
||||
return embedded.get_embedded();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -14,10 +14,6 @@ public class Link {
|
||||
|
||||
private String href;
|
||||
|
||||
public void setHref(String href) {
|
||||
this.href = href;
|
||||
}
|
||||
|
||||
public String getHref() {
|
||||
return this.href;
|
||||
}
|
||||
|
||||
@@ -18,8 +18,4 @@ public class SpringProjects {
|
||||
public List<SpringProject> getProjects() {
|
||||
return projects;
|
||||
}
|
||||
|
||||
public void setProjects(List<SpringProject> projects) {
|
||||
this.projects = projects;
|
||||
}
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2020 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
|
||||
* https://www.eclipse.org/legal/epl-v10.html
|
||||
*
|
||||
* Contributors:
|
||||
* Pivotal, Inc. - initial API and implementation
|
||||
*******************************************************************************/
|
||||
package org.springframework.ide.vscode.boot.validation.generations.json;
|
||||
|
||||
public class SpringProjectsEmbedded extends JsonHalLinks implements JsonHalEmbedded<SpringProjects> {
|
||||
|
||||
private SpringProjects _embedded;
|
||||
|
||||
public SpringProjects get_embedded() {
|
||||
return _embedded;
|
||||
}
|
||||
}
|
||||
@@ -24,7 +24,6 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.ide.vscode.boot.bootiful.BootLanguageServerTest;
|
||||
import org.springframework.ide.vscode.boot.bootiful.HoverTestConf;
|
||||
import org.springframework.ide.vscode.boot.validation.generations.SampleProjectsProvider;
|
||||
import org.springframework.ide.vscode.boot.validation.generations.SpringIoProjectsProvider;
|
||||
import org.springframework.ide.vscode.boot.validation.generations.SpringProjectsClient;
|
||||
import org.springframework.ide.vscode.boot.validation.generations.SpringProjectsProvider;
|
||||
|
||||
@@ -8,16 +8,18 @@
|
||||
* Contributors:
|
||||
* Pivotal, Inc. - initial API and implementation
|
||||
*******************************************************************************/
|
||||
package org.springframework.ide.vscode.boot.validation.generations;
|
||||
package org.springframework.ide.vscode.boot.validation.test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.ide.vscode.boot.validation.generations.SpringProjectsProvider;
|
||||
import org.springframework.ide.vscode.boot.validation.generations.json.Generations;
|
||||
import org.springframework.ide.vscode.boot.validation.generations.json.GenerationsEmbedded;
|
||||
import org.springframework.ide.vscode.boot.validation.generations.json.JsonHalParser;
|
||||
import org.springframework.ide.vscode.boot.validation.generations.json.SpringProject;
|
||||
import org.springframework.ide.vscode.boot.validation.generations.json.SpringProjects;
|
||||
import org.springframework.ide.vscode.boot.validation.generations.json.SpringProjectsEmbedded;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
@@ -32,8 +34,7 @@ public class SampleProjectsProvider implements SpringProjectsProvider {
|
||||
@Override
|
||||
public SpringProject getProject(String projectSlug) throws Exception {
|
||||
if (this.projects == null) {
|
||||
JsonHalParser parser = new JsonHalParser();
|
||||
this.projects = parser.getEmbedded(SPRING_PROJECTS_JSON_SAMPLE, SpringProjectsEmbedded.class);
|
||||
this.projects = parse(SPRING_PROJECTS_JSON_SAMPLE, SpringProjects.class);
|
||||
}
|
||||
List<SpringProject> projectList = this.projects.getProjects();
|
||||
for (SpringProject springProject : projectList) {
|
||||
@@ -48,8 +49,20 @@ public class SampleProjectsProvider implements SpringProjectsProvider {
|
||||
public Generations getGenerations(String projectSlug) throws Exception {
|
||||
SpringProject project = getProject(projectSlug);
|
||||
if (project != null && project.getSlug().equals("spring-boot")) {
|
||||
JsonHalParser parser = new JsonHalParser();
|
||||
return parser.getEmbedded(SPRING_BOOT_PROJECT_GENERATIONS, GenerationsEmbedded.class);
|
||||
return parse(SPRING_BOOT_PROJECT_GENERATIONS, Generations.class);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private <T> T parse(String json, Class<T> clazz) throws Exception {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
Map<?, ?> result = mapper.readValue(json, Map.class);
|
||||
if (result != null) {
|
||||
Object obj = result.get("_embedded");
|
||||
if (obj != null) {
|
||||
return mapper.convertValue(obj, clazz);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
Reference in New Issue
Block a user