public int deleteResource(WebDAVRequest webDavRequest) throws WebDAVException {

    try {
      Resource resource = getResource(webDavRequest);

      if (resource == null) {
        return HttpServletResponse.SC_NOT_FOUND;
      }

      Object model = resource.getModel();

      if (model instanceof IGFolder) {
        IGFolder folder = (IGFolder) model;

        IGFolderServiceUtil.deleteFolder(folder.getFolderId());
      } else {
        IGImage image = (IGImage) model;

        IGImageServiceUtil.deleteImage(image.getImageId());
      }

      return HttpServletResponse.SC_NO_CONTENT;
    } catch (PrincipalException pe) {
      return HttpServletResponse.SC_FORBIDDEN;
    } catch (Exception e) {
      throw new WebDAVException(e);
    }
  }
  public int moveSimpleResource(
      WebDAVRequest webDavRequest, Resource resource, String destination, boolean overwrite)
      throws WebDAVException {

    try {
      String[] destinationArray = WebDAVUtil.getPathArray(destination, true);

      IGImage image = (IGImage) resource.getModel();

      long companyId = webDavRequest.getCompanyId();
      long groupId = WebDAVUtil.getGroupId(companyId, destinationArray);
      long parentFolderId = getParentFolderId(companyId, destinationArray);
      String name = WebDAVUtil.getResourceName(destinationArray);
      String description = image.getDescription();
      File file = null;
      String contentType = null;

      ServiceContext serviceContext = new ServiceContext();

      int status = HttpServletResponse.SC_CREATED;

      if (overwrite) {
        if (deleteResource(groupId, parentFolderId, name)) {
          status = HttpServletResponse.SC_NO_CONTENT;
        }
      }

      IGImageServiceUtil.updateImage(
          image.getImageId(),
          groupId,
          parentFolderId,
          name,
          description,
          file,
          contentType,
          serviceContext);

      return status;
    } catch (PrincipalException pe) {
      return HttpServletResponse.SC_FORBIDDEN;
    } catch (DuplicateFileException dfe) {
      return HttpServletResponse.SC_PRECONDITION_FAILED;
    } catch (DuplicateFolderNameException dfne) {
      return HttpServletResponse.SC_PRECONDITION_FAILED;
    } catch (Exception e) {
      throw new WebDAVException(e);
    }
  }
  protected List<Resource> getImages(WebDAVRequest webDavRequest, long parentFolderId)
      throws Exception {

    long groupId = webDavRequest.getGroupId();

    List<Resource> resources = new ArrayList<Resource>();

    List<IGImage> images = IGImageServiceUtil.getImages(groupId, parentFolderId);

    for (IGImage image : images) {
      Resource resource = toResource(webDavRequest, image, true);

      resources.add(resource);
    }

    return resources;
  }
  public Resource getResource(WebDAVRequest webDavRequest) throws WebDAVException {

    try {
      String[] pathArray = webDavRequest.getPathArray();

      long companyId = webDavRequest.getCompanyId();
      long parentFolderId = getParentFolderId(companyId, pathArray);
      String name = WebDAVUtil.getResourceName(pathArray);

      if (Validator.isNull(name)) {
        String path = getRootPath() + webDavRequest.getPath();

        return new BaseResourceImpl(path, StringPool.BLANK, getToken());
      }

      try {
        IGFolder folder =
            IGFolderServiceUtil.getFolder(webDavRequest.getGroupId(), parentFolderId, name);

        if ((folder.getParentFolderId() != parentFolderId)
            || (webDavRequest.getGroupId() != folder.getGroupId())) {

          throw new NoSuchFolderException();
        }

        return toResource(webDavRequest, folder, false);
      } catch (NoSuchFolderException nsfe) {
        try {
          long groupId = webDavRequest.getGroupId();

          IGImage image =
              IGImageServiceUtil.getImageByFolderIdAndNameWithExtension(
                  groupId, parentFolderId, name);

          return toResource(webDavRequest, image, false);
        } catch (NoSuchImageException nsie) {
          return null;
        }
      }
    } catch (Exception e) {
      throw new WebDAVException(e);
    }
  }
  protected boolean deleteResource(long groupId, long parentFolderId, String name)
      throws PortalException, SystemException {

    try {
      IGFolder folder = IGFolderServiceUtil.getFolder(groupId, parentFolderId, name);

      IGFolderServiceUtil.deleteFolder(folder.getFolderId());

      return true;
    } catch (NoSuchFolderException nsfe) {
      if (name.indexOf(StringPool.PERIOD) == -1) {
        return false;
      }

      try {
        IGImageServiceUtil.deleteImageByFolderIdAndNameWithExtension(groupId, parentFolderId, name);

        return true;
      } catch (NoSuchImageException nsie) {
      }
    }

    return false;
  }
  public int putResource(WebDAVRequest webDavRequest) throws WebDAVException {
    File file = null;

    try {
      HttpServletRequest request = webDavRequest.getHttpServletRequest();

      String[] pathArray = webDavRequest.getPathArray();

      long companyId = webDavRequest.getCompanyId();
      long groupId = webDavRequest.getGroupId();
      long parentFolderId = getParentFolderId(companyId, pathArray);
      String name = WebDAVUtil.getResourceName(pathArray);
      String description = StringPool.BLANK;

      file = FileUtil.createTempFile(FileUtil.getExtension(name));

      FileUtil.write(file, request.getInputStream());

      String contentType = MimeTypesUtil.getContentType(name);

      ServiceContext serviceContext = new ServiceContext();

      serviceContext.setAddCommunityPermissions(isAddCommunityPermissions(groupId));
      serviceContext.setAddGuestPermissions(true);

      try {
        IGImage image =
            IGImageServiceUtil.getImageByFolderIdAndNameWithExtension(
                groupId, parentFolderId, name);

        long imageId = image.getImageId();

        description = image.getDescription();

        String[] assetTagNames =
            AssetTagLocalServiceUtil.getTagNames(IGImage.class.getName(), imageId);

        serviceContext.setAssetTagNames(assetTagNames);

        IGImageServiceUtil.updateImage(
            imageId, groupId, parentFolderId, name, description, file, contentType, serviceContext);
      } catch (NoSuchImageException nsie) {
        IGImageServiceUtil.addImage(
            groupId, parentFolderId, name, description, file, contentType, serviceContext);
      }

      return HttpServletResponse.SC_CREATED;
    } catch (PrincipalException pe) {
      return HttpServletResponse.SC_FORBIDDEN;
    } catch (PortalException pe) {
      if (_log.isWarnEnabled()) {
        _log.warn(pe, pe);
      }

      return HttpServletResponse.SC_CONFLICT;
    } catch (Exception e) {
      throw new WebDAVException(e);
    } finally {
      if (file != null) {
        file.delete();
      }
    }
  }
  public int copySimpleResource(
      WebDAVRequest webDavRequest, Resource resource, String destination, boolean overwrite)
      throws WebDAVException {

    File file = null;

    try {
      String[] destinationArray = WebDAVUtil.getPathArray(destination, true);

      long companyId = webDavRequest.getCompanyId();

      long parentFolderId = IGFolderConstants.DEFAULT_PARENT_FOLDER_ID;

      try {
        parentFolderId = getParentFolderId(companyId, destinationArray);
      } catch (NoSuchFolderException nsfe) {
        return HttpServletResponse.SC_CONFLICT;
      }

      IGImage image = (IGImage) resource.getModel();

      long groupId = WebDAVUtil.getGroupId(companyId, destination);
      String name = WebDAVUtil.getResourceName(destinationArray);
      String description = image.getDescription();
      String contentType = MimeTypesUtil.getContentType(image.getNameWithExtension());

      file = FileUtil.createTempFile(image.getImageType());

      InputStream is = resource.getContentAsStream();

      FileUtil.write(file, is);

      ServiceContext serviceContext = new ServiceContext();

      serviceContext.setAddCommunityPermissions(isAddCommunityPermissions(groupId));
      serviceContext.setAddGuestPermissions(true);

      int status = HttpServletResponse.SC_CREATED;

      if (overwrite) {
        if (deleteResource(groupId, parentFolderId, name)) {
          status = HttpServletResponse.SC_NO_CONTENT;
        }
      }

      IGImageServiceUtil.addImage(
          groupId, parentFolderId, name, description, file, contentType, serviceContext);

      return status;
    } catch (DuplicateFolderNameException dfne) {
      return HttpServletResponse.SC_PRECONDITION_FAILED;
    } catch (DuplicateFileException dfe) {
      return HttpServletResponse.SC_PRECONDITION_FAILED;
    } catch (PrincipalException pe) {
      return HttpServletResponse.SC_FORBIDDEN;
    } catch (Exception e) {
      throw new WebDAVException(e);
    } finally {
      if (file != null) {
        file.delete();
      }
    }
  }