/**
   * Retrieves project header information. Returns the project header or null if it does not exist
   * (for old projects) as JSON - OK status. Returns INTERNAL_SERVER_ERROR status and no response
   * body for serious errors
   *
   * @param name - The project name to get information for
   * @throws StorageException
   * @throws ProjectDoesNotExist
   */
  @RvdAuth
  @GET
  @Path("{name}/info")
  public Response projectInfo(@PathParam("name") String name)
      throws StorageException, ProjectDoesNotExist {
    assertProjectAvailable(name);

    StateHeader header = activeProject.getHeader();
    return Response.status(Status.OK)
        .entity(marshaler.getGson().toJson(header))
        .type(MediaType.APPLICATION_JSON)
        .build();
  }
 @RvdAuth
 @GET
 @Path("{name}/settings")
 public Response getProjectSettings(@PathParam("name") String name) {
   try {
     ProjectSettings projectSettings =
         FsProjectStorage.loadProjectSettings(name, workspaceStorage);
     return Response.ok(marshaler.toData(projectSettings)).build();
   } catch (StorageEntityNotFound e) {
     return Response.status(Status.NOT_FOUND).build();
   } catch (StorageException e) {
     logger.error(e, e);
     return Response.status(Status.INTERNAL_SERVER_ERROR).build();
   }
 }
 @RvdAuth
 @GET
 @Path("{name}/cc")
 public Response getCcInfo(@PathParam("name") String projectName) {
   try {
     CallControlInfo ccInfo = FsCallControlInfoStorage.loadInfo(projectName, workspaceStorage);
     return Response.ok(marshaler.toData(ccInfo), MediaType.APPLICATION_JSON).build();
     // return buildOkResponse(ccInfo);
   } catch (StorageEntityNotFound e) {
     return Response.status(Status.NOT_FOUND).build();
   } catch (StorageException e) {
     logger.error(e, e);
     return Response.status(Status.INTERNAL_SERVER_ERROR).build();
   }
 }
 @RvdAuth
 @GET
 @Path("{name}")
 @Produces(MediaType.APPLICATION_JSON)
 public Response openProject(@PathParam("name") String name, @Context HttpServletRequest request)
     throws StorageException, ProjectDoesNotExist {
   assertProjectAvailable(name);
   return Response.ok().entity(marshaler.toData(activeProject)).build();
   /*
    * try { String projectState = projectService.openProject(name); return Response.ok().entity(projectState).build(); }
    * catch (StorageException e) { logger.error("Error loading project '" + name + "'", e); return
    * Response.status(Status.INTERNAL_SERVER_ERROR).build(); } catch (ProjectDoesNotExist e) { return
    * Response.status(Status.NOT_FOUND).entity(e.asJson()).type(MediaType.APPLICATION_JSON).build(); } catch
    * (IncompatibleProjectVersion e) { logger.error(e.getMessage(), e); return
    * Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.asJson()).type(MediaType.APPLICATION_JSON).build(); }
    */
 }
 @RvdAuth
 @POST
 @Path("{name}/settings")
 public Response saveProjectSettings(@PathParam("name") String name) {
   logger.info("saving project settings for " + name);
   String data;
   try {
     data = IOUtils.toString(request.getInputStream(), Charset.forName("UTF-8"));
     ProjectSettings projectSettings = marshaler.toModel(data, ProjectSettings.class);
     FsProjectStorage.storeProjectSettings(projectSettings, name, workspaceStorage);
     return Response.ok().build();
   } catch (StorageException e) {
     logger.error(e, e);
     return Response.status(Status.INTERNAL_SERVER_ERROR).build();
   } catch (IOException e) {
     logger.error(e, e);
     return Response.status(Status.INTERNAL_SERVER_ERROR).build();
   }
 }
  /** Store Call Control project information */
  @RvdAuth
  @POST
  @Path("{name}/cc")
  public Response storeCcInfo(
      @PathParam("name") String projectName, @Context HttpServletRequest request) {
    try {
      String data = IOUtils.toString(request.getInputStream(), Charset.forName("UTF-8"));
      CallControlInfo ccInfo = marshaler.toModel(data, CallControlInfo.class);
      if (ccInfo != null) FsCallControlInfoStorage.storeInfo(ccInfo, projectName, workspaceStorage);
      else FsCallControlInfoStorage.clearInfo(projectName, workspaceStorage);

      return Response.ok().build();
    } catch (IOException e) {
      logger.error(e, e);
      return Response.status(Status.INTERNAL_SERVER_ERROR).build();
    } catch (StorageException e) {
      logger.error(e, e);
      return Response.status(Status.INTERNAL_SERVER_ERROR).build();
    }
  }
  @RvdAuth
  @POST
  // @Path("{name}/archive")
  public Response importProjectArchive(
      @Context HttpServletRequest request, @QueryParam("ticket") String ticket) {
    logger.info("Importing project from raw archive");
    ProjectApplicationsApi applicationsApi = null;
    String projectName = null;

    try {
      if (request.getHeader("Content-Type") != null
          && request.getHeader("Content-Type").startsWith("multipart/form-data")) {
        Gson gson = new Gson();
        ServletFileUpload upload = new ServletFileUpload();
        FileItemIterator iterator = upload.getItemIterator(request);

        JsonArray fileinfos = new JsonArray();

        while (iterator.hasNext()) {
          FileItemStream item = iterator.next();
          JsonObject fileinfo = new JsonObject();
          fileinfo.addProperty("fieldName", item.getFieldName());

          // is this a file part (talking about multipart requests, there might be parts that are
          // not actual files).
          // They will be ignored
          if (item.getName() != null) {
            String effectiveProjectName =
                projectService.importProjectFromArchive(item.openStream(), item.getName());
            // buildService.buildProject(effectiveProjectName);

            // Load project kind
            String projectString =
                FsProjectStorage.loadProjectString(effectiveProjectName, workspaceStorage);
            ProjectState state = marshaler.toModel(projectString, ProjectState.class);
            String projectKind = state.getHeader().getProjectKind();
            projectName = effectiveProjectName;

            // Create application
            applicationsApi =
                new ProjectApplicationsApi(servletContext, workspaceStorage, marshaler);
            applicationsApi.createApplication(ticket, effectiveProjectName, projectKind);

            fileinfo.addProperty("name", item.getName());
            fileinfo.addProperty("projectName", effectiveProjectName);
          }
          if (item.getName() == null) {
            logger.warn("non-file part found in upload");
            fileinfo.addProperty("value", read(item.openStream()));
          }
          fileinfos.add(fileinfo);
        }
        return Response.ok(gson.toJson(fileinfos), MediaType.APPLICATION_JSON).build();
      } else {
        String json_response = "{\"result\":[{\"size\":" + size(request.getInputStream()) + "}]}";
        return Response.ok(json_response, MediaType.APPLICATION_JSON).build();
      }
    } catch (StorageException e) {
      logger.warn(e, e);
      logger.debug(e, e);
      return buildErrorResponse(Status.BAD_REQUEST, RvdResponse.Status.ERROR, e);
    } catch (ApplicationAlreadyExists e) {
      logger.warn(e, e);
      logger.debug(e, e);
      try {
        applicationsApi.rollbackCreateApplication(ticket, projectName);
      } catch (ApplicationsApiSyncException e1) {
        logger.error(e1.getMessage(), e1);
        return buildErrorResponse(Status.INTERNAL_SERVER_ERROR, RvdResponse.Status.ERROR, e);
      }
      return buildErrorResponse(Status.CONFLICT, RvdResponse.Status.ERROR, e);
    } catch (Exception e /* TODO - use a more specific type !!! */) {
      logger.error(e.getMessage(), e);
      return Response.status(Status.INTERNAL_SERVER_ERROR).build();
    }
  }