@RequestMapping(
      value = SHARE_REFERENCE_REQUEST,
      method = {RequestMethod.POST})
  public void shareReference(
      HttpServletRequest request,
      HttpServletResponse response,
      @RequestParam(value = "api", required = false) boolean api)
      throws IOException {

    if (apiRequested(response, api, SHARE_REFERENCE_REQUEST)) return;

    com.c9a.buffers.CatalogServiceRequestProtocalBuffer.ShareReferenceRequest
        shareReferenceRequest =
            com.c9a.buffers.CatalogServiceRequestProtocalBuffer.ShareReferenceRequest.parseFrom(
                GoogleProtoclBufferRequestContentUtils.getContentForRequest(request));

    try {
      catalogService.shareReference(
          shareReferenceRequest.getUserId(),
          shareReferenceRequest.getPartitionId(),
          shareReferenceRequest.getReferenceToShareUniqueId(),
          shareReferenceRequest.getApplicationName(),
          createNewMap(shareReferenceRequest.getUsersAndPartitionsToShareWithList()),
          createPermissionListFromProtoBufEnumList(shareReferenceRequest.getPermissionsList()));

      CatalogGPBUtils.generateResponseProtoBuf(
              com.c9a.buffers.CatalogServiceResponseProtocalBuffer.ResponseCode.SUCCESS, "")
          .build()
          .writeTo(response.getOutputStream());
      return;
    } catch (ReferenceNotFoundException e) {
      CatalogGPBUtils.generateCatalogCollectionResponseProtoBufError(
              com.c9a.buffers.CatalogServiceResponseProtocalBuffer.ResponseCode.REFERENCE_NOT_FOUND,
              "The reference can not be found with unique id : "
                  + shareReferenceRequest.getReferenceToShareUniqueId())
          .build()
          .writeTo(response.getOutputStream());
      return;
    } catch (ReferenceSharingException e) {
      CatalogGPBUtils.generateCatalogCollectionResponseProtoBufError(
              com.c9a.buffers.CatalogServiceResponseProtocalBuffer.ResponseCode
                  .REFERENCE_CAN_NOT_BE_SHARED,
              "The reference can not be shared.")
          .build()
          .writeTo(response.getOutputStream());
      return;
    } catch (InvalidCatalogNameException e) {
      CatalogGPBUtils.generateCatalogCollectionResponseProtoBufError(
              com.c9a.buffers.CatalogServiceResponseProtocalBuffer.ResponseCode
                  .CATALOG_NAME_INVALID,
              "Invalid catalog name.")
          .build()
          .writeTo(response.getOutputStream());
      return;
    }
  }
  @RequestMapping(
      value = ADD_DOCUMENT_REQUEST,
      method = {RequestMethod.POST})
  public void addDocument(
      HttpServletRequest request,
      HttpServletResponse response,
      @RequestParam(value = "api", required = false) boolean api)
      throws IOException {

    if (apiRequested(response, api, ADD_DOCUMENT_REQUEST)) return;

    com.c9a.buffers.CatalogServiceRequestProtocalBuffer.AddDocumentRequest addNewDocumentRequest =
        com.c9a.buffers.CatalogServiceRequestProtocalBuffer.AddDocumentRequest.parseFrom(
            GoogleProtoclBufferRequestContentUtils.getContentForRequest(request));

    try {

      Reference ref =
          catalogService.addDocument(
              addNewDocumentRequest.getUserId(),
              addNewDocumentRequest.getPartitionId(),
              addNewDocumentRequest.getDocument().getName(),
              addNewDocumentRequest.getParentCatalogCollectionId(),
              addNewDocumentRequest.getDocument().getContent().toByteArray(),
              createPermissionListFromProtoBufEnumList(addNewDocumentRequest.getPermissionsList()),
              createNewMap(addNewDocumentRequest.getAttributesList()));

      CatalogGPBUtils.generateReferenceResponseProtoBuf(
              ref, com.c9a.buffers.CatalogServiceResponseProtocalBuffer.ResponseCode.SUCCESS, "")
          .build()
          .writeTo(response.getOutputStream());
      return;

    } catch (CollectionNotFoundException e) {
      CatalogGPBUtils.generateCatalogCollectionResponseProtoBufError(
              com.c9a.buffers.CatalogServiceResponseProtocalBuffer.ResponseCode
                  .COLLECTION_NOT_FOUND,
              "The collection can not be found with unique id : "
                  + addNewDocumentRequest.getParentCatalogCollectionId())
          .build()
          .writeTo(response.getOutputStream());
      return;
    } catch (CollectionModificationException e) {
      CatalogGPBUtils.generateCatalogCollectionResponseProtoBufError(
              com.c9a.buffers.CatalogServiceResponseProtocalBuffer.ResponseCode
                  .COLLECTION_CAN_NOT_BE_MODIFIED,
              "The parent collection can not be modified : "
                  + addNewDocumentRequest.getParentCatalogCollectionId())
          .build()
          .writeTo(response.getOutputStream());
      return;
    }
  }
  @RequestMapping(
      value = ROOT_APPLICATION_COLLECTION_FOR_USER_REQUEST,
      method = {RequestMethod.POST})
  public void rootApplicationCollectionRequest(
      HttpServletRequest request,
      HttpServletResponse response,
      @RequestParam(value = "api", required = false) boolean api)
      throws IOException {

    if (apiRequested(response, api, ROOT_APPLICATION_COLLECTION_FOR_USER_REQUEST)) return;

    com.c9a.buffers.CatalogServiceRequestProtocalBuffer.GetRootApplicationCollectionForUserRequest
        rootApplicationCollectionForUserRequest =
            com.c9a.buffers.CatalogServiceRequestProtocalBuffer
                .GetRootApplicationCollectionForUserRequest.parseFrom(
                GoogleProtoclBufferRequestContentUtils.getContentForRequest(request));

    CatalogCollection rootAplicationCollection =
        catalogService.getRootApplicationCollectionForUser(
            rootApplicationCollectionForUserRequest.getUserId(),
            rootApplicationCollectionForUserRequest.getPartitionId(),
            rootApplicationCollectionForUserRequest.getApplicationName());

    CatalogGPBUtils.generateCatalogCollectionResponseProtoBuf(
            com.c9a.buffers.CatalogServiceResponseProtocalBuffer.ResponseCode.SUCCESS,
            "",
            rootAplicationCollection)
        .build()
        .writeTo(response.getOutputStream());
    return;
  }
  @RequestMapping(
      value = DELETE_REFERENCE_REQUEST,
      method = {RequestMethod.POST})
  public void deleteReference(
      HttpServletRequest request,
      HttpServletResponse response,
      @RequestParam(value = "api", required = false) boolean api)
      throws IOException {

    if (apiRequested(response, api, DELETE_REFERENCE_REQUEST)) return;

    com.c9a.buffers.CatalogServiceRequestProtocalBuffer.DeleteReferenceRequest
        deleteReferenceRequest =
            com.c9a.buffers.CatalogServiceRequestProtocalBuffer.DeleteReferenceRequest.parseFrom(
                GoogleProtoclBufferRequestContentUtils.getContentForRequest(request));

    try {
      catalogService.deleteReferenceFromCollection(
          deleteReferenceRequest.getUserId(),
          deleteReferenceRequest.getPartitionId(),
          deleteReferenceRequest.getReferenceUniqueId(),
          false);
      CatalogGPBUtils.generateResponseProtoBuf(
              com.c9a.buffers.CatalogServiceResponseProtocalBuffer.ResponseCode.SUCCESS, "")
          .build()
          .writeTo(response.getOutputStream());
      return;
    } catch (CollectionModificationException e) {
      CatalogGPBUtils.generateResponseProtoBuf(
              com.c9a.buffers.CatalogServiceResponseProtocalBuffer.ResponseCode
                  .COLLECTION_CAN_NOT_BE_MODIFIED,
              "You do not have the needed permissions to modify the collection that holds this reference.")
          .build()
          .writeTo(response.getOutputStream());
      return;
    } catch (ReferenceNotFoundException e) {
      CatalogGPBUtils.generateResponseProtoBuf(
              com.c9a.buffers.CatalogServiceResponseProtocalBuffer.ResponseCode.REFERENCE_NOT_FOUND,
              "The reference can not be found with unique id : "
                  + deleteReferenceRequest.getReferenceUniqueId())
          .build()
          .writeTo(response.getOutputStream());
      return;
    }
  }
  @RequestMapping(
      value = GET_COLLECTION_REQUEST,
      method = {RequestMethod.POST})
  public void getCollection(
      HttpServletRequest request,
      HttpServletResponse response,
      @RequestParam(value = "api", required = false) boolean api)
      throws IOException {

    if (apiRequested(response, api, GET_COLLECTION_REQUEST)) return;

    com.c9a.buffers.CatalogServiceRequestProtocalBuffer.GetCollectionRequest collectionRequest =
        com.c9a.buffers.CatalogServiceRequestProtocalBuffer.GetCollectionRequest.parseFrom(
            GoogleProtoclBufferRequestContentUtils.getContentForRequest(request));

    try {
      CatalogCollection catalogCollection =
          catalogService.getCollection(
              collectionRequest.getUserId(),
              collectionRequest.getPartitionId(),
              collectionRequest.getUniqueId());
      CatalogGPBUtils.generateCatalogCollectionResponseProtoBuf(
              com.c9a.buffers.CatalogServiceResponseProtocalBuffer.ResponseCode.SUCCESS,
              "",
              catalogCollection)
          .build()
          .writeTo(response.getOutputStream());
      return;
    } catch (CollectionNotFoundException e) {
      CatalogGPBUtils.generateCatalogCollectionResponseProtoBufError(
              com.c9a.buffers.CatalogServiceResponseProtocalBuffer.ResponseCode
                  .COLLECTION_NOT_FOUND,
              "The collection was not found with the give unique id : "
                  + collectionRequest.getUniqueId())
          .build()
          .writeTo(response.getOutputStream());
      return;
    }
  }
  @RequestMapping(
      value = GET_REFERENCE_REQUEST,
      method = {RequestMethod.POST})
  public void getReference(
      HttpServletRequest request,
      HttpServletResponse response,
      @RequestParam(value = "api", required = false) boolean api)
      throws IOException {

    if (apiRequested(response, api, GET_REFERENCE_REQUEST)) return;

    com.c9a.buffers.CatalogServiceRequestProtocalBuffer.GetReferenceRequest getReferenceRequest =
        com.c9a.buffers.CatalogServiceRequestProtocalBuffer.GetReferenceRequest.parseFrom(
            GoogleProtoclBufferRequestContentUtils.getContentForRequest(request));

    try {
      Reference ref =
          catalogService.getReference(
              getReferenceRequest.getUserId(),
              getReferenceRequest.getPartitionId(),
              getReferenceRequest.getUniqueId());
      CatalogGPBUtils.generateReferenceResponseProtoBuf(
              ref, com.c9a.buffers.CatalogServiceResponseProtocalBuffer.ResponseCode.SUCCESS, "")
          .build()
          .writeTo(response.getOutputStream());
      return;
    } catch (ReferenceNotFoundException e) {
      CatalogGPBUtils.generateCatalogCollectionResponseProtoBufError(
              com.c9a.buffers.CatalogServiceResponseProtocalBuffer.ResponseCode.REFERENCE_NOT_FOUND,
              "The reference can not be found with unique id : "
                  + getReferenceRequest.getUniqueId())
          .build()
          .writeTo(response.getOutputStream());
      return;
    }
  }
  @RequestMapping(
      value = MODIFY_REFERENCE_REQUEST,
      method = {RequestMethod.POST})
  public void modifyREFERENCE(
      HttpServletRequest request,
      HttpServletResponse response,
      @RequestParam(value = "api", required = false) boolean api)
      throws IOException {

    if (apiRequested(response, api, MODIFY_REFERENCE_REQUEST)) return;

    com.c9a.buffers.CatalogServiceRequestProtocalBuffer.ModifyReferenceRequest
        modifyReferenceRequest =
            com.c9a.buffers.CatalogServiceRequestProtocalBuffer.ModifyReferenceRequest.parseFrom(
                GoogleProtoclBufferRequestContentUtils.getContentForRequest(request));

    try {
      Reference ref =
          catalogService.modifyReference(
              modifyReferenceRequest.getUserId(),
              modifyReferenceRequest.getPartitionId(),
              modifyReferenceRequest.getReferenceUniqueId(),
              modifyReferenceRequest.getReferenceName(),
              createPermissionListFromProtoBufEnumList(modifyReferenceRequest.getPermissionsList()),
              createNewMap(modifyReferenceRequest.getAttributesList()));

      CatalogGPBUtils.generateReferenceResponseProtoBuf(
              ref, com.c9a.buffers.CatalogServiceResponseProtocalBuffer.ResponseCode.SUCCESS, "")
          .build()
          .writeTo(response.getOutputStream());
      return;
    } catch (CollectionNotFoundException e) {
      CatalogGPBUtils.generateResponseProtoBuf(
              com.c9a.buffers.CatalogServiceResponseProtocalBuffer.ResponseCode
                  .COLLECTION_NOT_FOUND,
              "The collection can not be found with unique id : "
                  + modifyReferenceRequest.getParentCollectionUniqueId())
          .build()
          .writeTo(response.getOutputStream());
      return;
    } catch (ReferenceModificationException e) {
      CatalogGPBUtils.generateResponseProtoBuf(
              com.c9a.buffers.CatalogServiceResponseProtocalBuffer.ResponseCode
                  .REFERENCE_CAN_NOT_BE_MODIFIED,
              "You do not have the needed permissions to modify this reference.")
          .build()
          .writeTo(response.getOutputStream());
      return;
    } catch (ReferenceNotFoundException e) {
      CatalogGPBUtils.generateResponseProtoBuf(
              com.c9a.buffers.CatalogServiceResponseProtocalBuffer.ResponseCode.REFERENCE_NOT_FOUND,
              "The reference can not be found with unique id : "
                  + modifyReferenceRequest.getReferenceUniqueId())
          .build()
          .writeTo(response.getOutputStream());
      return;
    } catch (InvalidCatalogNameException e) {
      CatalogGPBUtils.generateResponseProtoBuf(
              com.c9a.buffers.CatalogServiceResponseProtocalBuffer.ResponseCode
                  .CATALOG_NAME_INVALID,
              "You can not store the name \""
                  + modifyReferenceRequest.getReferenceName()
                  + "\" int he catalog.")
          .build()
          .writeTo(response.getOutputStream());
      return;
    }
  }
  @RequestMapping(
      value = SHARE_COLLECTION_REQUEST,
      method = {RequestMethod.POST})
  public void shareCollection(
      HttpServletRequest request,
      HttpServletResponse response,
      @RequestParam(value = "api", required = false) boolean api)
      throws IOException {

    if (apiRequested(response, api, SHARE_COLLECTION_REQUEST)) return;

    com.c9a.buffers.CatalogServiceRequestProtocalBuffer.ShareCollectionRequest
        shareCollectionForUserRequest =
            com.c9a.buffers.CatalogServiceRequestProtocalBuffer.ShareCollectionRequest.parseFrom(
                GoogleProtoclBufferRequestContentUtils.getContentForRequest(request));

    try {
      Map<String, String> userAndPartitionsToShareWith =
          createNewMap(shareCollectionForUserRequest.getUsersAndPartitionsToShareWithList());
      Set<Long> sharePermissionIds =
          createPermissionListFromProtoBufEnumList(
              shareCollectionForUserRequest.getPermissionsList());
      catalogService.shareCollection(
          shareCollectionForUserRequest.getUserId(),
          shareCollectionForUserRequest.getPartitionId(),
          shareCollectionForUserRequest.getApplicationName(),
          shareCollectionForUserRequest.getCollectionUniqueId(),
          userAndPartitionsToShareWith,
          sharePermissionIds);
      CatalogGPBUtils.generateResponseProtoBuf(
              com.c9a.buffers.CatalogServiceResponseProtocalBuffer.ResponseCode.SUCCESS, "")
          .build()
          .writeTo(response.getOutputStream());
      return;
    } catch (CollectionNotFoundException e) {
      CatalogGPBUtils.generateResponseProtoBuf(
              com.c9a.buffers.CatalogServiceResponseProtocalBuffer.ResponseCode
                  .COLLECTION_NOT_FOUND,
              "The collection was not found with the give unique id : "
                  + shareCollectionForUserRequest.getCollectionUniqueId())
          .build()
          .writeTo(response.getOutputStream());
      return;
    } catch (CollectionSharingException e) {
      CatalogGPBUtils.generateResponseProtoBuf(
              com.c9a.buffers.CatalogServiceResponseProtocalBuffer.ResponseCode
                  .COLLECTION_CAN_NOT_BE_SHARED,
              "The collection does not have the needed permissions to be shared.")
          .build()
          .writeTo(response.getOutputStream());
      return;
    } catch (InvalidCatalogNameException e) {
      CatalogGPBUtils.generateResponseProtoBuf(
              com.c9a.buffers.CatalogServiceResponseProtocalBuffer.ResponseCode
                  .CATALOG_NAME_INVALID,
              "Invalid catalog name.")
          .build()
          .writeTo(response.getOutputStream());
      return;
    }
  }
  @RequestMapping(
      value = ADD_COLLECTION_REQUEST,
      method = {RequestMethod.POST})
  public void addCollection(
      HttpServletRequest request,
      HttpServletResponse response,
      @RequestParam(value = "api", required = false) boolean api)
      throws IOException {

    if (apiRequested(response, api, ADD_COLLECTION_REQUEST)) return;

    com.c9a.buffers.CatalogServiceRequestProtocalBuffer.AddCollectionRequest
        addNewCollectionRequest =
            com.c9a.buffers.CatalogServiceRequestProtocalBuffer.AddCollectionRequest.parseFrom(
                GoogleProtoclBufferRequestContentUtils.getContentForRequest(request));

    try {
      Set<Long> permissions =
          createPermissionListFromProtoBufEnumList(addNewCollectionRequest.getPermissionsList());
      Map<String, String> attributes = createNewMap(addNewCollectionRequest.getAttributesList());
      CatalogCollection catalogCollection =
          catalogService.addCollection(
              addNewCollectionRequest.getUserId(),
              addNewCollectionRequest.getPartitionId(),
              addNewCollectionRequest.getCollectionName(),
              addNewCollectionRequest.getParentCatalogCollectionId(),
              permissions,
              attributes);
      CatalogGPBUtils.generateCatalogCollectionResponseProtoBuf(
              com.c9a.buffers.CatalogServiceResponseProtocalBuffer.ResponseCode.SUCCESS,
              "",
              catalogCollection)
          .build()
          .writeTo(response.getOutputStream());
      return;
    } catch (CollectionNotFoundException e) {
      CatalogGPBUtils.generateCatalogCollectionResponseProtoBufError(
              com.c9a.buffers.CatalogServiceResponseProtocalBuffer.ResponseCode
                  .COLLECTION_NOT_FOUND,
              "The parent collection was not found with the give unique id : "
                  + addNewCollectionRequest.getParentCatalogCollectionId())
          .build()
          .writeTo(response.getOutputStream());
      return;
    } catch (CollectionModificationException e) {
      CatalogGPBUtils.generateCatalogCollectionResponseProtoBufError(
              com.c9a.buffers.CatalogServiceResponseProtocalBuffer.ResponseCode
                  .COLLECTION_CAN_NOT_BE_MODIFIED,
              "The parent collection with unique id : "
                  + addNewCollectionRequest.getParentCatalogCollectionId()
                  + " can not be modified : ")
          .build()
          .writeTo(response.getOutputStream());
      return;
    } catch (InvalidCatalogNameException e) {
      CatalogGPBUtils.generateCatalogCollectionResponseProtoBufError(
              com.c9a.buffers.CatalogServiceResponseProtocalBuffer.ResponseCode
                  .CATALOG_NAME_INVALID,
              "The supplied name is not valid for the catalog")
          .build()
          .writeTo(response.getOutputStream());
      return;
    }
  }