package com.hubspot.baragon.service.resources;

import javax.ws.rs.DELETE;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;

import com.codahale.metrics.annotation.Timed;
import com.google.common.base.Optional;
import com.google.common.net.HttpHeaders;
import com.google.inject.Inject;
import com.hubspot.baragon.auth.NoAuth;
import com.hubspot.baragon.cache.BaragonStateCache;
import com.hubspot.baragon.cache.CachedBaragonState;
import com.hubspot.baragon.models.BaragonResponse;
import com.hubspot.baragon.models.BaragonServiceState;
import com.hubspot.baragon.service.managers.ServiceManager;

@Path("/state")
@Produces(MediaType.APPLICATION_JSON)
public class StateResource {
  private final ServiceManager serviceManager;
  private final BaragonStateCache stateCache;

  @HeaderParam(HttpHeaders.ACCEPT_ENCODING)
  private String acceptEncoding;

  @HeaderParam(HttpHeaders.IF_NONE_MATCH)
  private String ifNoneMatch;

  @Inject
  public StateResource(ServiceManager serviceManager, BaragonStateCache stateCache) {
    this.serviceManager = serviceManager;
    this.stateCache = stateCache;
  }

  @GET
  @NoAuth
  @Timed
  public Response getAllServices() {
    CachedBaragonState state = stateCache.getState();
    String verisonString = Integer.toString(state.getVersion());

    if (ifNoneMatch != null && ifNoneMatch.equals(verisonString)) {
      return Response.notModified(verisonString).build();
    }

    ResponseBuilder builder = Response.ok();

    final byte[] entity;
    if (acceptEncoding != null && acceptEncoding.contains("gzip")) {
      builder.header(HttpHeaders.CONTENT_ENCODING, "gzip");
      entity = state.getGzip();
    } else {
      entity = state.getUncompressed();
    }

    return builder
        .entity(entity)
        .header(HttpHeaders.ETAG, state.getVersion())
        .build();
  }

  @GET
  @NoAuth
  @Path("/{serviceId}")
  public Optional<BaragonServiceState> getService(@PathParam("serviceId") String serviceId) {
    return serviceManager.getService(serviceId);
  }

  @POST
  @Path("/{serviceId}/reload")
  public BaragonResponse reloadConfigs(@PathParam("serviceId") String serviceId, @DefaultValue("false") @QueryParam("noValidate") boolean noValidate) {
    return serviceManager.enqueueReloadServiceConfigs(serviceId, noValidate);
  }

  @DELETE
  @Path("/{serviceId}")
  public BaragonResponse removeService(@PathParam("serviceId") String serviceId, @DefaultValue("false") @QueryParam("noValidate") boolean noValidate, @DefaultValue("false") @QueryParam("noReload") boolean noReload) {
    return serviceManager.enqueueRemoveService(serviceId, noValidate, noReload);
  }
}

@@ -1071,159 +1071,8 @@
 e;%0A%0A
-  @HeaderParam(HttpHeaders.ACCEPT_ENCODING)%0A  private String acceptEncoding;%0A%0A  @HeaderParam(HttpHeaders.IF_NONE_MATCH)%0A  private String ifNoneMatch;%0A%0A
   @I
@@ -1303,16 +1303,172 @@
 ervices(
+@HeaderParam(HttpHeaders.IF_NONE_MATCH) String ifNoneMatch,%0A                                 @HeaderParam(HttpHeaders.ACCEPT_ENCODING) String acceptEncoding
 ) %7B%0A    
@@ -1579,24 +1579,62 @@
 ersion());%0A%0A
+    System.out.println(ifNoneMatch);%0A%0A
     if (ifNo
@@ -1684,16 +1684,23 @@
 onString
+.trim()
 )) %7B%0A   
@@ -1734,22 +1734,76 @@
 ied(
-verisonString)
+)%0A          .header(HttpHeaders.ETAG, state.getVersion())%0A          
 .bui

Fix header injection