Skip to content

Commit

Permalink
More fixes to support jackson 2
Browse files Browse the repository at this point in the history
- adjust BrooklynJacksonSerializer to use jackson 2 api
- fix web.xml for rest-api and rest-client
  • Loading branch information
andreaturli committed Jan 28, 2016
1 parent 151ec2f commit 93d7535
Show file tree
Hide file tree
Showing 13 changed files with 138 additions and 146 deletions.
5 changes: 5 additions & 0 deletions brooklyn-server/parent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,11 @@
<artifactId>jackson-module-jaxb-annotations</artifactId>
<version>${fasterxml.jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-guava</artifactId>
<version>${fasterxml.jackson.version}</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.apache.brooklyn.rest.domain.ApplicationSpec;
import org.apache.brooklyn.rest.domain.ApplicationSummary;
import org.apache.brooklyn.rest.domain.EntitySummary;
import org.apache.brooklyn.rest.domain.EntityDetail;

import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
Expand All @@ -55,7 +56,7 @@ public interface ApplicationApi {
@ApiOperation(
value = "Fetch display details for all applications and optionally selected additional entities"
)
public List<EntitySummary> fetch(
public List<EntityDetail> fetch(
@ApiParam(value="Selected additional entity ID's to include, comma-separated", required=false)
@DefaultValue("")
@QueryParam("items") String items);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@
import java.util.Map;
import java.util.Objects;

import org.apache.brooklyn.core.entity.lifecycle.Lifecycle;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;

public class EntityDetail extends EntitySummary {

Expand All @@ -35,22 +36,25 @@ public class EntityDetail extends EntitySummary {
@JsonSerialize(include = JsonSerialize.Inclusion.NON_EMPTY)
private final List<EntitySummary> children;
private final List<String> groupIds;
private final Map<String, String> members;
private final List<Map<String, String>> members;

public EntityDetail(
@JsonProperty("id") String id,
@JsonProperty("parentId") String parentId,
@JsonProperty("name") String name,
@JsonProperty("type") String type,
@JsonProperty("serviceUp") Boolean serviceUp,
@JsonProperty("serviceState") Lifecycle serviceState,
@JsonProperty("iconUrl") String iconUrl,
@JsonProperty("catalogItemId") String catalogItemId,
@JsonProperty("children") List<EntitySummary> children,
@JsonProperty("groupIds") List<String> groupIds,
@JsonProperty("members") Map<String, String> members) {
@JsonProperty("members") List<Map<String, String>> members) {
super(id, name, type, catalogItemId, null);
this.parentId = parentId;
this.children = (children == null) ? ImmutableList.<EntitySummary>of() : ImmutableList.copyOf(children);
this.groupIds = (groupIds == null) ? ImmutableList.<String>of() : ImmutableList.copyOf(groupIds);
this.members = (members == null) ? ImmutableMap.<String,String> of() : ImmutableMap.copyOf(members);
this.members = (members == null) ? ImmutableList.<Map<String,String>>of() : ImmutableList.copyOf(members);
}

public static long getSerialVersionUID() {
Expand All @@ -69,34 +73,8 @@ public List<String> getGroupIds() {
return groupIds;
}

public Map<String, String> getMembers() {
public List<Map<String, String>> getMembers() {
return members;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof EntityDetail)) return false;
if (!super.equals(o)) return false;
EntityDetail that = (EntityDetail) o;
return Objects.equals(parentId, that.parentId) &&
Objects.equals(children, that.children) &&
Objects.equals(groupIds, that.groupIds) &&
Objects.equals(members, that.members);
}

@Override
public int hashCode() {
return Objects.hash(super.hashCode(), parentId, children, groupIds, members);
}

@Override
public String toString() {
return "EntityDetail{" +
"parentId='" + parentId + '\'' +
", children=" + children +
", groupIds=" + groupIds +
", members=" + members +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ public class TaskSummary implements HasId, Serializable {

private final String currentStatus;
private final Object result;
private final boolean isError;
private final boolean isCancelled;
private final boolean error;
private final boolean cancelled;

private final List<LinkWithMetadata> children;
private final LinkWithMetadata submittedByTask;
Expand Down Expand Up @@ -82,8 +82,8 @@ public TaskSummary(
@JsonProperty("endTimeUtc") Long endTimeUtc,
@JsonProperty("currentStatus") String currentStatus,
@JsonProperty("result") Object result,
@JsonProperty("isError") boolean isError,
@JsonProperty("isCancelled") boolean isCancelled,
@JsonProperty("error") boolean error,
@JsonProperty("cancelled") boolean cancelled,
@JsonProperty("children") List<LinkWithMetadata> children,
@JsonProperty("submittedByTask") LinkWithMetadata submittedByTask,
@JsonProperty("blockingTask") LinkWithMetadata blockingTask,
Expand All @@ -102,8 +102,8 @@ public TaskSummary(
this.endTimeUtc = endTimeUtc;
this.currentStatus = currentStatus;
this.result = result;
this.isError = isError;
this.isCancelled = isCancelled;
this.error = error;
this.cancelled = cancelled;
this.children = children;
this.blockingDetails = blockingDetails;
this.blockingTask = blockingTask;
Expand Down Expand Up @@ -168,26 +168,26 @@ public Object getResult() {
return result;
}

/** @deprecated since 0.7.0 use {@link #isError} instead. */
/** @deprecated since 0.7.0 use {@link #error} instead. */
@Deprecated
@JsonIgnore
public boolean getIsError() {
return isError;
return error;
}

/** @deprecated since 0.7.0 use {@link #isCancelled} instead. */
/** @deprecated since 0.7.0 use {@link #cancelled} instead. */
@Deprecated
@JsonIgnore
public boolean getIsCancelled() {
return isCancelled;
return cancelled;
}

public boolean isError() {
return isError;
return error;
}

public boolean isCancelled() {
return isCancelled;
return cancelled;
}

public List<LinkWithMetadata> getChildren() {
Expand Down Expand Up @@ -223,8 +223,8 @@ public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof TaskSummary)) return false;
TaskSummary that = (TaskSummary) o;
return isError == that.isError &&
isCancelled == that.isCancelled &&
return error == that.error &&
cancelled == that.cancelled &&
Objects.equals(id, that.id) &&
Objects.equals(displayName, that.displayName) &&
Objects.equals(entityId, that.entityId) &&
Expand All @@ -247,7 +247,7 @@ public boolean equals(Object o) {

@Override
public int hashCode() {
return Objects.hash(id, displayName, entityId, entityDisplayName, description, tags, submitTimeUtc, startTimeUtc, endTimeUtc, currentStatus, result, isError, isCancelled, children, submittedByTask, blockingTask, blockingDetails, detailedStatus, streams, links);
return Objects.hash(id, displayName, entityId, entityDisplayName, description, tags, submitTimeUtc, startTimeUtc, endTimeUtc, currentStatus, result, error, cancelled, children, submittedByTask, blockingTask, blockingDetails, detailedStatus, streams, links);
}

@Override
Expand All @@ -264,8 +264,8 @@ public String toString() {
", endTimeUtc=" + endTimeUtc +
", currentStatus='" + currentStatus + '\'' +
", result=" + result +
", isError=" + isError +
", isCancelled=" + isCancelled +
", error=" + error +
", cancelled=" + cancelled +
", children=" + children +
", submittedByTask=" + submittedByTask +
", blockingTask=" + blockingTask +
Expand Down
4 changes: 2 additions & 2 deletions brooklyn-server/rest/rest-api/src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,14 @@
<!-- load our REST API jersey resources -->
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>io.swagger.jaxrs.listing;org.codehaus.jackson.jaxrs;org.apache.brooklyn.rest.resources;org.apache.brooklyn.rest.util</param-value>
<param-value>io.swagger.jaxrs.listing;com.fasterxml.jackson.jaxrs;org.apache.brooklyn.rest.resources;org.apache.brooklyn.rest.util</param-value>
</init-param>

<!-- install Jackson and turn on pojo/json serialization (could add org.codehaus.jackson.jaxrs
above but seems cleaner to pull in just the class -->
<init-param>
<param-name>com.sun.jersey.config.property.classnames</param-name>
<param-value>org.codehaus.jackson.jaxrs.JacksonJsonProvider</param-value>
<param-value>com.fasterxml.jackson.jaxrs.JacksonJsonProvider</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
<param-value>
org.apache.brooklyn.rest.apidoc.ApidocHelpMessageBodyWriter;
org.apache.brooklyn.rest.util.FormMapProvider;
org.codehaus.jackson.jaxrs.JacksonJsonProvider;
com.fasterxml.jackson.jaxrs.JacksonJsonProvider;
org.apache.brooklyn.rest.resources.ActivityResource;
org.apache.brooklyn.rest.resources.ApidocResource;
org.apache.brooklyn.rest.resources.ApplicationResource;
Expand Down
5 changes: 4 additions & 1 deletion brooklyn-server/rest/rest-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-guava</artifactId>
<version>${fasterxml.jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import java.net.URI;
import java.net.URISyntaxException;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
Expand All @@ -38,6 +39,7 @@
import org.apache.brooklyn.api.entity.Application;
import org.apache.brooklyn.api.entity.Entity;
import org.apache.brooklyn.api.entity.EntitySpec;
import org.apache.brooklyn.api.entity.Group;
import org.apache.brooklyn.api.location.Location;
import org.apache.brooklyn.api.mgmt.Task;
import org.apache.brooklyn.api.objs.BrooklynObject;
Expand All @@ -58,9 +60,11 @@
import org.apache.brooklyn.core.sensor.Sensors;
import org.apache.brooklyn.core.typereg.RegisteredTypeLoadingContexts;
import org.apache.brooklyn.core.typereg.RegisteredTypes;
import org.apache.brooklyn.entity.group.AbstractGroup;
import org.apache.brooklyn.rest.api.ApplicationApi;
import org.apache.brooklyn.rest.domain.ApplicationSpec;
import org.apache.brooklyn.rest.domain.ApplicationSummary;
import org.apache.brooklyn.rest.domain.EntityDetail;
import org.apache.brooklyn.rest.domain.EntitySummary;
import org.apache.brooklyn.rest.domain.TaskSummary;
import org.apache.brooklyn.rest.filter.HaHotStateRequired;
Expand All @@ -79,11 +83,9 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.base.Throwables;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;

Expand All @@ -95,67 +97,88 @@ public class ApplicationResource extends AbstractBrooklynRestResource implements
@Context
private UriInfo uriInfo;

private ObjectNode entityBase(Entity entity) {
ObjectNode aRoot = mapper().createObjectNode();
aRoot.put("name", entity.getDisplayName());
aRoot.put("id", entity.getId());
aRoot.put("type", entity.getEntityType().getName());

private EntityDetail fromEntity(Entity entity) {
Boolean serviceUp = entity.getAttribute(Attributes.SERVICE_UP);
if (serviceUp!=null) aRoot.put("serviceUp", serviceUp);

Lifecycle serviceState = entity.getAttribute(Attributes.SERVICE_STATE_ACTUAL);
if (serviceState!=null) aRoot.put("serviceState", serviceState.toString());

String iconUrl = entity.getIconUrl();
if (iconUrl!=null) {
if (brooklyn().isUrlServerSideAndSafe(iconUrl))
// route to server if it is a server-side url
iconUrl = EntityTransformer.entityUri(entity)+"/icon";
aRoot.put("iconUrl", iconUrl);
}

return aRoot;
}
List<EntitySummary> children = Lists.newArrayList();
if (!entity.getChildren().isEmpty()) {
for (Entity child : entity.getChildren()) {
children.add(fromEntity(child));
}
}

private JsonNode recursiveTreeFromEntity(Entity entity) {
ObjectNode aRoot = entityBase(entity);
String parentId = null;
if (entity.getParent()!= null) {
parentId = entity.getParent().getId();
}

List<String> groupIds = Lists.newArrayList();
if (!entity.groups().isEmpty()) {
groupIds.addAll(entitiesIdAsArray(entity.groups()));
}

if (!entity.getChildren().isEmpty())
aRoot.put("children", childEntitiesRecursiveAsArray(entity));
List<Map<String, String>> members = Lists.newArrayList();
if (entity instanceof Group) {
// use attribute instead of method in case it is read-only
Collection<Entity> memberEntities = entity.getAttribute(AbstractGroup.GROUP_MEMBERS);
if (memberEntities != null && !memberEntities.isEmpty())
members.addAll(entitiesIdAndNameAsList(memberEntities));
}

return aRoot;
return new EntityDetail(entity.getId(), parentId, entity.getDisplayName(),
entity.getEntityType().getName(), serviceUp, serviceState, iconUrl, entity.getCatalogItemId(),
children, groupIds, members);
}

private ArrayNode childEntitiesRecursiveAsArray(Entity entity) {
ArrayNode node = mapper().createArrayNode();
for (Entity e : entity.getChildren()) {
private List<Map<String, String>> entitiesIdAndNameAsList(Collection<? extends Entity> entities) {
List<Map<String, String>> members = Lists.newArrayList();
for (Entity entity : entities) {
if (Entitlements.isEntitled(mgmt().getEntitlementManager(), Entitlements.SEE_ENTITY, entity)) {
node.add(recursiveTreeFromEntity(e));
members.add(ImmutableMap.of("id", entity.getId(), "name", entity.getDisplayName()));
}
}
return node;
return members;
}

private List<String> entitiesIdAsArray(Iterable<? extends Entity> entities) {
List<String> ids = Lists.newArrayList();
for (Entity entity : entities) {
if (Entitlements.isEntitled(mgmt().getEntitlementManager(), Entitlements.SEE_ENTITY, entity)) {
ids.add(entity.getId());
}
}
return ids;
}

@Override
public List<EntitySummary> fetch(String entityIds) {
List<EntitySummary> result = Lists.newArrayList();
for (Application application : mgmt().getApplications()) {
result.add(EntityTransformer.entitySummary(application));
public List<EntityDetail> fetch(String entityIds) {

List<EntityDetail> entitySummaries = Lists.newArrayList();
for (Entity application : mgmt().getApplications()) {
entitySummaries.add(fromEntity(application));
}

if (entityIds != null) {
for (String entityId: entityIds.split(",")) {
Entity entity = mgmt().getEntityManager().getEntity(entityId.trim());
while (entity != null && entity.getParent() != null) {
if (Entitlements.isEntitled(mgmt().getEntitlementManager(), Entitlements.SEE_ENTITY, entity)) {
result.add(EntityTransformer.entitySummary(entity));
entitySummaries.add(fromEntity(entity));
}
entity = entity.getParent();
}
}
}
return result;
return entitySummaries;
}

@Override
Expand Down
Loading

0 comments on commit 93d7535

Please sign in to comment.