diff --git a/src/main/java/org/valuereporter/activity/ActivitiesResource.java b/src/main/java/org/valuereporter/activity/ActivitiesResource.java new file mode 100644 index 0000000..e935029 --- /dev/null +++ b/src/main/java/org/valuereporter/activity/ActivitiesResource.java @@ -0,0 +1,67 @@ +package org.valuereporter.activity; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.slf4j.Logger; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import java.io.IOException; +import java.io.StringWriter; +import java.io.Writer; +import java.util.ArrayList; +import java.util.List; + +import static org.slf4j.LoggerFactory.getLogger; + +/** + * Created by baardl on 02.03.16. + */ +@Component +@Path("/activities") +public class ActivitiesResource { + private static final Logger log = getLogger(ActivitiesResource.class); + + private final ObjectMapper mapper; + + @Autowired + public ActivitiesResource(ObjectMapper mapper) { + this.mapper = mapper; + } + + @POST + @Path("/{prefix}") + @Produces(MediaType.APPLICATION_JSON) + public Response addObservationActivity(@PathParam("prefix") String prefix, String jsonBody){ + log.trace("addObservationMethod prefix {} , jsonBody {}.", prefix, jsonBody); + List observedActivities = null; + try { + observedActivities = mapper.readValue(jsonBody, new TypeReference>(){ }); + if (observedActivities != null) { + for (ObservedActivity observedActivity : observedActivities) { + observedActivity.setPrefix(prefix); + } + } + } catch (IOException e) { + log.warn("Unexpected error trying to produce list of ObservedActivity from \n prefix {} \n json {}, \n Reason {}",prefix, jsonBody, e.getMessage()); + return Response.status(Response.Status.NOT_ACCEPTABLE).entity("Error converting to requested format.").build(); + } + + long updatedCount = observedActivities.size(); //writeOperations.addObservations(prefix,observedMethods); + String message = "added " + updatedCount + " observedMethods."; + Writer strWriter = new StringWriter(); + try { + mapper.writeValue(strWriter, message); + } catch (IOException e) { + log.error("Could not convert {} to JSON.", updatedCount, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("Error converting to requested format.").build(); + } + return Response.ok(strWriter.toString()).build(); + } +} diff --git a/src/main/java/org/valuereporter/activity/ObservedActivity.java b/src/main/java/org/valuereporter/activity/ObservedActivity.java new file mode 100755 index 0000000..3b049c4 --- /dev/null +++ b/src/main/java/org/valuereporter/activity/ObservedActivity.java @@ -0,0 +1,58 @@ +package org.valuereporter.activity; + +import java.util.Map; + +/** + * This class will represent an activity. This activity will have some main characteristics: + * - name: Unique identifier for a type of activity + * - startTime: When the activity occured + * - data: map of parameters, aka payload + * + * These methods will be forwarded to the Valuereporter analyzer, as an activity that has occured. + * + * @author Bard Lind + */ +public class ObservedActivity { + //The name is the identification of a method. Typically the name is the full name, including class, and package. + private String prefix = ""; + + public String getPrefix() { + return prefix; + } + + public void setPrefix(String prefix) { + this.prefix = prefix; + } + + private final String name; + private final long startTime; + private final Map data; + + public ObservedActivity(String name, long startTime, Map data) { + this.name = name; + this.startTime = startTime; + this.data = data; + } + + public String getName() { + return name; + } + + public long getStartTime() { + return startTime; + } + + + public Map getData() { + return data; + } + + @Override + public String toString() { + return "ObservedActivity{" + + "name='" + name + '\'' + + ", startTime=" + startTime + + ", data=" + data + + '}'; + } +} diff --git a/src/main/java/org/valuereporter/activity/ObservedActivityJson.java b/src/main/java/org/valuereporter/activity/ObservedActivityJson.java new file mode 100755 index 0000000..dac555a --- /dev/null +++ b/src/main/java/org/valuereporter/activity/ObservedActivityJson.java @@ -0,0 +1,17 @@ +package org.valuereporter.activity; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonProperty; + +import java.util.Map; + +/** + * @author Bard Lind + */ +public class ObservedActivityJson extends ObservedActivity { + + @JsonCreator + public ObservedActivityJson(@JsonProperty("name") String name, @JsonProperty("startTime") long startTime, @JsonProperty("data") Map data) { + super(name, startTime, data); + } +}