Beispiel #1
0
  public static Resource getResource(
      WebDAVRequest webDAVRequest, String rootPath, String token, long classNameId)
      throws WebDAVException {

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

      if (pathArray.length == 2) {
        String path = rootPath + webDAVRequest.getPath();

        return new BaseResourceImpl(path, StringPool.BLANK, token);
      } else if (pathArray.length == 3) {
        String type = pathArray[2];

        return toResource(webDAVRequest, type, rootPath, false);
      } else if (pathArray.length == 4) {
        String type = pathArray[2];
        String typeId = pathArray[3];

        if (type.equals(TYPE_STRUCTURES)) {
          try {
            DDMStructure structure = null;

            try {
              structure = DDMStructureLocalServiceUtil.getStructure(GetterUtil.getLong(typeId));
            } catch (NumberFormatException nfe) {
              structure =
                  DDMStructureLocalServiceUtil.getStructure(
                      webDAVRequest.getGroupId(), classNameId, typeId);
            }

            return DDMWebDavUtil.toResource(webDAVRequest, structure, rootPath, false);
          } catch (NoSuchStructureException nsse) {
            return null;
          }
        } else if (type.equals(TYPE_TEMPLATES)) {
          try {
            DDMTemplate template = null;

            try {
              template = DDMTemplateLocalServiceUtil.getTemplate(GetterUtil.getLong(typeId));
            } catch (NumberFormatException nfe) {
              template =
                  DDMTemplateLocalServiceUtil.getTemplate(
                      webDAVRequest.getGroupId(), classNameId, typeId);
            }

            return DDMWebDavUtil.toResource(webDAVRequest, template, rootPath, false);
          } catch (NoSuchTemplateException nste) {
            return null;
          }
        }
      }

      return null;
    } catch (Exception e) {
      throw new WebDAVException(e);
    }
  }
  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);
    }
  }
  public static int addResource(WebDAVRequest webDavRequest, long classNameId) throws Exception {

    String[] pathArray = webDavRequest.getPathArray();

    if (pathArray.length != 4) {
      return HttpServletResponse.SC_FORBIDDEN;
    }

    String type = pathArray[2];
    String typeId = pathArray[3];

    if (type.equals(TYPE_STRUCTURES)) {
      HttpServletRequest request = webDavRequest.getHttpServletRequest();

      String definition = StringUtil.read(request.getInputStream());

      DDMForm ddmForm = getDDMForm(definition);

      DDMFormLayout ddmFormLayout = DDMUtil.getDefaultDDMFormLayout(ddmForm);

      Map<Locale, String> nameMap = new HashMap<>();

      Locale defaultLocale = ddmForm.getDefaultLocale();

      nameMap.put(defaultLocale, typeId);

      ServiceContext serviceContext = new ServiceContext();

      serviceContext.setAddGroupPermissions(true);
      serviceContext.setAddGuestPermissions(true);

      DDMStructureLocalServiceUtil.addStructure(
          webDavRequest.getUserId(),
          webDavRequest.getGroupId(),
          classNameId,
          nameMap,
          null,
          ddmForm,
          ddmFormLayout,
          StorageType.JSON.toString(),
          serviceContext);

      return HttpServletResponse.SC_CREATED;
    } else if (type.equals(TYPE_TEMPLATES)) {

      // DDM templates can not be added via WebDAV because there is no way
      // to know the associated class name or class PK

      return HttpServletResponse.SC_FORBIDDEN;
    }

    return HttpServletResponse.SC_FORBIDDEN;
  }
  public Status makeCollection(WebDAVRequest webDavRequest) throws WebDAVException {

    try {
      HttpServletRequest request = webDavRequest.getHttpServletRequest();

      if (request.getContentLength() > 0) {
        return new Status(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE);
      }

      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;

      ServiceContext serviceContext = new ServiceContext();

      serviceContext.setAddCommunityPermissions(isAddCommunityPermissions(groupId));
      serviceContext.setAddGuestPermissions(true);
      serviceContext.setPlid(getPlid(webDavRequest.getGroupId()));
      serviceContext.setScopeGroupId(webDavRequest.getGroupId());

      IGFolderServiceUtil.addFolder(parentFolderId, name, description, serviceContext);

      String location = StringUtil.merge(pathArray, StringPool.SLASH);

      return new Status(location, HttpServletResponse.SC_CREATED);
    } catch (DuplicateFolderNameException dfne) {
      return new Status(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
    } catch (NoSuchFolderException nsfe) {
      return new Status(HttpServletResponse.SC_CONFLICT);
    } catch (PrincipalException pe) {
      return new Status(HttpServletResponse.SC_FORBIDDEN);
    } catch (Exception e) {
      throw new WebDAVException(e);
    }
  }
  protected List<Resource> getStructures(WebDAVRequest webDAVRequest) throws Exception {

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

    List<DDMStructure> ddmStructures =
        DDMStructureLocalServiceUtil.getStructures(
            webDAVRequest.getGroupId(), PortalUtil.getClassNameId(JournalArticle.class));

    for (DDMStructure ddmStructure : ddmStructures) {
      Resource resource =
          DDMWebDavUtil.toResource(webDAVRequest, ddmStructure, getRootPath(), true);

      resources.add(resource);
    }

    return resources;
  }
  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;
  }
  protected List<Resource> getFolders(WebDAVRequest webDavRequest, long parentFolderId)
      throws Exception {

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

    long groupId = webDavRequest.getGroupId();

    List<IGFolder> folders = IGFolderServiceUtil.getFolders(groupId, parentFolderId);

    for (IGFolder folder : folders) {
      Resource resource = toResource(webDavRequest, folder, true);

      resources.add(resource);
    }

    return resources;
  }
  protected List<Resource> getTemplates(WebDAVRequest webDAVRequest) throws Exception {

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

    List<DDMTemplate> ddmTemplates =
        DDMTemplateLocalServiceUtil.getTemplatesByStructureClassNameId(
            webDAVRequest.getGroupId(),
            PortalUtil.getClassNameId(JournalArticle.class),
            QueryUtil.ALL_POS,
            QueryUtil.ALL_POS,
            null);

    for (DDMTemplate ddmTemplate : ddmTemplates) {
      Resource resource = DDMWebDavUtil.toResource(webDAVRequest, ddmTemplate, getRootPath(), true);

      resources.add(resource);
    }

    return resources;
  }
  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();
      }
    }
  }