/*
 * Copyright 2012-2013 Continuuity,Inc. All Rights Reserved.
 */

package com.continuuity.passport.http.handlers;

import com.continuuity.passport.PassportConstants;
import com.continuuity.passport.core.service.DataManagementService;
import com.continuuity.passport.meta.VPC;
import com.google.inject.Inject;
import com.google.inject.Singleton;

import javax.ws.rs.*;
import javax.ws.rs.core.Response;
import java.util.List;

/**
 * Defines End point for vpc
 */

@Path("passport/v1/vpc")
@Singleton
public class VPCHandler extends PassportHandler {

  private final DataManagementService dataManagementService;

  @Inject
  public VPCHandler(DataManagementService dataManagementService) {
    this.dataManagementService = dataManagementService;
  }

  @GET
  @Produces("application/json")
  public Response getVPC(@HeaderParam(PassportConstants.CONTINUUITY_API_KEY_HEADER) String apiKey) {
    try {
      requestReceived();
      List<VPC> vpcList = dataManagementService.getVPC(apiKey);
      if (vpcList.isEmpty()) {
        return Response.ok("[]").build();
      } else {
        StringBuilder returnJson = new StringBuilder();
        returnJson.append("[");
        boolean first = true;
        for (VPC vpc : vpcList) {
          if (first) {
            first = false;
          } else {
            returnJson.append(",");
          }
          returnJson.append(vpc.toString());

        }
        returnJson.append("]");
        requestSuccess();
        return Response.ok(returnJson.toString()).build();
      }
    } catch (Exception e) {
      requestFailed();
      return Response.status(Response.Status.BAD_REQUEST)
        .entity(Utils.getJsonError(String.format("VPC get Failed. %s", e)))
        .build();
    }
  }

  @Path("valid/{vpcName}")
  @GET
  public Response isValidVPC(@PathParam("vpcName") String vpcName) {
    try {
      int count = dataManagementService.getVPCCount(vpcName);
      if (count == 0) {
        return Response.ok().entity(Utils.getJsonOK()).build();
      } else {
        return Response.ok().entity(Utils.getJsonError("VPC already exists")).build();
      }
    } catch (Exception e) {
      requestFailed();
      return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
        .entity(Utils.getJsonError("FAILED", e.getMessage()))
        .build();
    }

  }


}

@@ -1624,19 +1624,29 @@
 tus.
-BAD_REQUEST
+INTERNAL_SERVER_ERROR
 )%0A  

Fix return code for one calls