package com.hubspot.singularity.resources;

import java.util.List;

import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriInfo;

import com.google.inject.Inject;
import com.hubspot.singularity.SingularityCreateResult;
import com.hubspot.singularity.SingularityDeleteResult;
import com.hubspot.singularity.SingularityDeployWebhook;
import com.hubspot.singularity.SingularityRequestHistory;
import com.hubspot.singularity.SingularityService;
import com.hubspot.singularity.SingularityTaskHistoryUpdate;
import com.hubspot.singularity.SingularityWebhook;
import com.hubspot.singularity.data.WebhookManager;

@Path(SingularityService.API_BASE_PATH + "/webhooks")
@Produces({ MediaType.APPLICATION_JSON })
public class WebhookResource {

  private final WebhookManager webhookManager;
  private final UriInfo uriInfo;

  @Inject
  public WebhookResource(WebhookManager webhookManager, UriInfo uriInfo) {
    this.webhookManager = webhookManager;
    this.uriInfo = uriInfo;
  }

  @GET
  public List<SingularityWebhook> getActiveWebhooks() {
    return webhookManager.getActiveWebhooks();
  }

  @POST
  public SingularityCreateResult addWebhook(SingularityWebhook webhook) {
    return webhookManager.addWebhook(webhook);
  }

  private String getWebhookId() {
    return uriInfo.getPathParameters(false).getFirst("webhookId");
  }

  @DELETE
  @Path("/{webhookId}")
  public SingularityDeleteResult deleteWebhook() {
    return webhookManager.deleteWebhook(getWebhookId());
  }

  @GET
  @Path("/deploy/{webhookId}")
  public List<SingularityDeployWebhook> getQueuedDeployUpdates() {
    return webhookManager.getQueuedDeployUpdatesForHook(getWebhookId());
  }

  @GET
  @Path("/request/{webhookId}")
  public List<SingularityRequestHistory> getQueuedRequestUpdates() {
    return webhookManager.getQueuedRequestHistoryForHook(getWebhookId());
  }

  @GET
  @Path("/task/{webhookId}")
  public List<SingularityTaskHistoryUpdate> getQueuedTaskUpdates() {
    return webhookManager.getQueuedTaskUpdatesForHook(getWebhookId());
  }

}

@@ -162,16 +162,46 @@
 s.Path;%0A
+import javax.ws.rs.PathParam;%0A
 import j
@@ -259,41 +259,8 @@
 ype;
-%0Aimport javax.ws.rs.core.UriInfo;
 %0A%0Aim
@@ -290,16 +290,52 @@
 Inject;%0A
+import com.hubspot.mesos.JavaUtils;%0A
 import c
@@ -947,41 +947,8 @@
 ger;
-%0A  private final UriInfo uriInfo;
 %0A%0A  
@@ -1013,25 +1013,8 @@
 ager
-, UriInfo uriInfo
 ) %7B%0A
@@ -1059,36 +1059,8 @@
 er;%0A
-    this.uriInfo = uriInfo;%0A
   %7D%0A
@@ -1313,114 +1313,8 @@
  %7D%0A%0A
-  private String getWebhookId() %7B%0A    return uriInfo.getPathParameters(false).getFirst(%22webhookId%22);%0A  %7D%0A%0A
   @D
@@ -1390,16 +1390,56 @@
 Webhook(
+@PathParam(%22webhookId%22) String webhookId
 ) %7B%0A    
@@ -1470,36 +1470,53 @@
 leteWebhook(
-getW
+JavaUtils.urlEncode(w
 ebhookId
 ());%0A  %7D%0A%0A  
@@ -1495,33 +1495,32 @@
 Encode(webhookId
-(
 ));%0A  %7D%0A%0A  @GET%0A
@@ -1605,32 +1605,72 @@
 edDeployUpdates(
+@PathParam(%22webhookId%22) String webhookId
 ) %7B%0A    return w
@@ -1709,36 +1709,53 @@
 atesForHook(
-getW
+JavaUtils.urlEncode(w
 ebhookId
 ());%0A  %7D%0A%0A  
@@ -1734,33 +1734,32 @@
 Encode(webhookId
-(
 ));%0A  %7D%0A%0A  @GET%0A
@@ -1847,32 +1847,72 @@
 dRequestUpdates(
+@PathParam(%22webhookId%22) String webhookId
 ) %7B%0A    return w
@@ -1952,36 +1952,53 @@
 toryForHook(
-getW
+JavaUtils.urlEncode(w
 ebhookId
 ());%0A  %7D%0A%0A  
@@ -1977,33 +1977,32 @@
 Encode(webhookId
-(
 ));%0A  %7D%0A%0A  @GET%0A
@@ -2095,16 +2095,56 @@
 Updates(
+@PathParam(%22webhookId%22) String webhookId
 ) %7B%0A    
@@ -2197,20 +2197,37 @@
 ook(
-getW
+JavaUtils.urlEncode(w
 ebhookId
 ());
@@ -2222,17 +2222,16 @@
 ebhookId
-(
 ));%0A  %7D%0A

Use PathParameters for webhook resource