@Context Request request, @Context HttpHeaders headers,
@Context UriInfo uriInfo) {
MediaType mediaType = headers.getAcceptableMediaTypes().get(0);
MeasurementSchedule schedule;
Response.ResponseBuilder builder;
// Create a cache control
CacheControl cc = new CacheControl();
cc.setMaxAge(300); // Schedules are valid for 5 mins
cc.setPrivate(false); // Proxies may cache this
schedule = getFromCache(scheduleId, MeasurementSchedule.class);
if (schedule!=null) {
// If it is on cache, quickly return if match
long tim = schedule.getMtime() != null ? schedule.getMtime() : 0;
EntityTag eTag = new EntityTag(Long.toOctalString(schedule.hashCode()+tim)); // factor in mtime in etag
builder = request.evaluatePreconditions(new Date(tim),eTag);
if (builder!=null) {
builder.cacheControl(cc);
return builder.build();
}
}
if (schedule==null) {
schedule = scheduleManager.getScheduleById(caller, scheduleId);
if (schedule==null)
throw new StuffNotFoundException("Schedule with id " + scheduleId);
else
putToCache(scheduleId, MeasurementSchedule.class, schedule);
}
MeasurementDefinition definition = schedule.getDefinition();
// MetricSchedule metricSchedule = getMetricScheduleInternal(uriInfo,schedule,definition); todo
MetricSchedule metricSchedule = new MetricSchedule(schedule.getId(), definition.getName(),
definition.getDisplayName(),
schedule.isEnabled(), schedule.getInterval(), definition.getUnits().toString(),
definition.getDataType().toString());
metricSchedule.setDefinitionId(definition.getId());
if (schedule.getMtime()!=null)
metricSchedule.setMtime(schedule.getMtime());
// Check for conditional get again
// Interestingly computing the hashCode of the original schedule is slower, as it also
// pulls in data from the definition and the resource
long tim = schedule.getMtime() != null ? schedule.getMtime() : 0;
EntityTag eTag = new EntityTag(Long.toOctalString(schedule.hashCode()+tim));
builder = request.evaluatePreconditions(new Date(tim),eTag); // factor in mtime in etag
if (builder==null) {
// preconditions not met, we need to send the resource
UriBuilder uriBuilder;
URI uri;
Link link;
if (definition.getDataType()==DataType.MEASUREMENT) {
// create link to metrics
uriBuilder = uriInfo.getBaseUriBuilder();
uriBuilder.path("metric/data/" + scheduleId);
uri = uriBuilder.build();
link = new Link("metric",uri.toString());
metricSchedule.addLink(link);
}
// create link to the resource
uriBuilder = uriInfo.getBaseUriBuilder();
uriBuilder.path("resource/" + schedule.getResource().getId());
uri = uriBuilder.build();
link = new Link("resource",uri.toString());
metricSchedule.addLink(link);
// Link for updates
uriBuilder = uriInfo.getAbsolutePathBuilder();
uri = uriBuilder.build();
Link updateLink = new Link("edit",uri.toString());
metricSchedule.addLink(updateLink);
updateLink = new Link("self",uri.toString());
metricSchedule.addLink(updateLink);
metricSchedule.addLink(createUILink(uriInfo, UILinkTemplate.METRIC_SCHEDULE, schedule.getResource().getId()));
if (mediaType.equals(MediaType.TEXT_HTML_TYPE)) {
builder = Response.ok(renderTemplate("metricSchedule", metricSchedule), mediaType);
}
else {