Commit fe84e3ac by Johannes Edmeier

Fix compatibility for older clients

parent accb52d1
......@@ -15,17 +15,22 @@
*/
package de.codecentric.boot.admin.model;
import java.io.IOException;
import java.io.Serializable;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
/**
* The domain model for all registered application at the spring boot admin application.
*/
@JsonDeserialize(using = Application.Deserializer.class)
public class Application implements Serializable {
private static final long serialVersionUID = 1L;
......@@ -48,24 +53,6 @@ public class Application implements Serializable {
this.statusInfo = builder.statusInfo;
}
@JsonCreator
public static Application fromJson(@JsonProperty("url") String url,
@JsonProperty("managementUrl") String managementUrl,
@JsonProperty("healthUrl") String healthUrl,
@JsonProperty("serviceUrl") String serviceUrl, @JsonProperty("name") String name) {
Builder builder = create(name);
if (StringUtils.hasText(url)) {
// old format
builder.withHealthUrl(url.replaceFirst("/+$", "") + "/health").withManagementUrl(url);
} else {
builder.withHealthUrl(healthUrl).withManagementUrl(managementUrl)
.withServiceUrl(serviceUrl);
}
return builder.build();
}
public static Builder create(String name) {
return new Builder(name);
}
......@@ -222,4 +209,36 @@ public class Application implements Serializable {
return true;
}
public static class Deserializer extends StdDeserializer<Application> {
private static final long serialVersionUID = 1L;
protected Deserializer() {
super(Application.class);
}
@Override
public Application deserialize(JsonParser p, DeserializationContext ctxt)
throws IOException, JsonProcessingException {
JsonNode node = p.readValueAsTree();
Builder builder = create(node.get("name").asText());
if (node.has("url")) {
String url = node.get("url").asText();
builder.withHealthUrl(url.replaceFirst("/+$", "") + "/health")
.withManagementUrl(url);
} else {
if (node.has("healthUrl")) {
builder.withHealthUrl(node.get("healthUrl").asText());
}
if (node.has("managementUrl")) {
builder.withManagementUrl(node.get("managementUrl").asText());
}
if (node.has("serviceUrl")) {
builder.withServiceUrl(node.get("serviceUrl").asText());
}
}
return builder.build();
}
}
}
......@@ -14,11 +14,10 @@ import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class ApplicationTest {
private ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json().build();
@Test
public void test_old_json_format() throws JsonProcessingException, IOException {
public void test_1_2_json_format() throws JsonProcessingException, IOException {
String json = "{ \"name\" : \"test\", \"url\" : \"http://test\" }";
Application value = objectMapper.readValue(json, Application.class);
......@@ -30,7 +29,19 @@ public class ApplicationTest {
}
@Test
public void test_new_json_format() throws JsonProcessingException, IOException {
public void test_1_4_json_format() throws JsonProcessingException, IOException {
String json = "{ \"name\" : \"test\", \"managementUrl\" : \"http://test\" , \"healthUrl\" : \"http://health\" , \"serviceUrl\" : \"http://service\", \"statusInfo\": {\"status\":\"UNKNOWN\"} }";
Application value = objectMapper.readValue(json, Application.class);
assertThat(value.getName(), is("test"));
assertThat(value.getManagementUrl(), is("http://test"));
assertThat(value.getHealthUrl(), is("http://health"));
assertThat(value.getServiceUrl(), is("http://service"));
}
@Test
public void test_1_5_json_format() throws JsonProcessingException, IOException {
String json = "{ \"name\" : \"test\", \"managementUrl\" : \"http://test\" , \"healthUrl\" : \"http://health\" , \"serviceUrl\" : \"http://service\"}";
Application value = objectMapper.readValue(json, Application.class);
......@@ -41,14 +52,27 @@ public class ApplicationTest {
assertThat(value.getServiceUrl(), is("http://service"));
}
@Test
public void test_onlyHealhUrl() throws JsonProcessingException, IOException {
String json = "{ \"name\" : \"test\", \"healthUrl\" : \"http://test\" }";
Application value = objectMapper.readValue(json, Application.class);
assertThat(value.getName(), is("test"));
assertThat(value.getHealthUrl(), is("http://test"));
assertThat(value.getManagementUrl(), nullValue());
assertThat(value.getServiceUrl(), nullValue());
}
@Test(expected = IllegalArgumentException.class)
public void test_name_expected() throws JsonProcessingException, IOException {
Application.fromJson("http://url", "", "", "", null);
String json = "{ \"name\" : \"\", \"managementUrl\" : \"http://test\" , \"healthUrl\" : \"http://health\" , \"serviceUrl\" : \"http://service\"}";
objectMapper.readValue(json, Application.class);
}
@Test(expected = IllegalArgumentException.class)
public void test_healthUrl_expected() throws JsonProcessingException, IOException {
Application.fromJson("", "", "", "name", null);
String json = "{ \"name\" : \"test\", \"managementUrl\" : \"http://test\" , \"healthUrl\" : \"\" , \"serviceUrl\" : \"http://service\"}";
objectMapper.readValue(json, Application.class);
}
@Test
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment