Esempio n. 1
0
  public static Namespace createNamespace(String prefix, String uri) {
    Namespace namespace = null;

    if (uri.equals(WebDAVUtil.DAV_URI.getURI())) {
      namespace = WebDAVUtil.DAV_URI;
    } else if (Validator.isNull(prefix)) {
      namespace = SAXReaderUtil.createNamespace(uri);
    } else {
      namespace = SAXReaderUtil.createNamespace(prefix, uri);
    }

    return namespace;
  }
Esempio n. 2
0
/**
 * @author Brian Wing Shun Chan
 * @author Alexander Chow
 * @author Raymond Augé
 */
public class WebDAVUtil {

  public static final Namespace DAV_URI = SAXReaderUtil.createNamespace("D", "DAV:");

  public static final int SC_LOCKED = 423;

  public static final int SC_MULTI_STATUS = 207;

  public static final String TOKEN_PREFIX = "opaquelocktoken:";

  public static void addStorage(WebDAVStorage storage) {
    getInstance()._addStorage(storage);
  }

  public static Namespace createNamespace(String prefix, String uri) {
    Namespace namespace = null;

    if (uri.equals(WebDAVUtil.DAV_URI.getURI())) {
      namespace = WebDAVUtil.DAV_URI;
    } else if (Validator.isNull(prefix)) {
      namespace = SAXReaderUtil.createNamespace(uri);
    } else {
      namespace = SAXReaderUtil.createNamespace(prefix, uri);
    }

    return namespace;
  }

  public static void deleteStorage(WebDAVStorage storage) {
    getInstance()._deleteStorage(storage);
  }

  public static long getDepth(HttpServletRequest request) {
    String value = GetterUtil.getString(request.getHeader("Depth"));

    if (_log.isDebugEnabled()) {
      _log.debug("\"Depth\" header is " + value);
    }

    if (value.equals("0")) {
      return 0;
    } else {
      return -1;
    }
  }

  public static String getDestination(HttpServletRequest request, String rootPath) {

    String headerDestination = request.getHeader("Destination");
    String[] pathSegments = StringUtil.split(headerDestination, rootPath);

    String destination = pathSegments[pathSegments.length - 1];

    destination = HttpUtil.decodePath(destination);

    if (_log.isDebugEnabled()) {
      _log.debug("Destination " + destination);
    }

    return destination;
  }

  public static long getGroupId(long companyId, String path) throws WebDAVException {

    String[] pathArray = getPathArray(path);

    return getGroupId(companyId, pathArray);
  }

  public static long getGroupId(long companyId, String[] pathArray) throws WebDAVException {

    try {
      if (pathArray.length == 0) {
        return 0;
      }

      String name = pathArray[0];

      Group group = GroupLocalServiceUtil.fetchFriendlyURLGroup(companyId, StringPool.SLASH + name);

      if (group != null) {
        return group.getGroupId();
      }

      User user = UserLocalServiceUtil.fetchUserByScreenName(companyId, name);

      if (user != null) {
        group = user.getGroup();

        return group.getGroupId();
      }
    } catch (Exception e) {
      throw new WebDAVException(e);
    }

    return 0;
  }

  public static List<Group> getGroups(long userId) throws Exception {
    User user = UserLocalServiceUtil.getUser(userId);

    return getGroups(user);
  }

  public static List<Group> getGroups(User user) throws Exception {

    // Guest

    if (user.isDefaultUser()) {
      List<Group> groups = new ArrayList<>();

      Group group = GroupLocalServiceUtil.getGroup(user.getCompanyId(), GroupConstants.GUEST);

      groups.add(group);

      return groups;
    }

    // Communities

    Set<Group> groups = new HashSet<>();

    LinkedHashMap<String, Object> params = new LinkedHashMap<>();

    params.put("usersGroups", user.getUserId());

    OrderByComparator<Group> orderByComparator = new GroupFriendlyURLComparator(true);

    groups.addAll(
        GroupLocalServiceUtil.search(
            user.getCompanyId(),
            null,
            null,
            params,
            QueryUtil.ALL_POS,
            QueryUtil.ALL_POS,
            orderByComparator));

    // Organizations

    groups.addAll(
        GroupLocalServiceUtil.getUserOrganizationsGroups(
            user.getUserId(), QueryUtil.ALL_POS, QueryUtil.ALL_POS));

    // User

    if (!user.isDefaultUser()) {
      groups.add(user.getGroup());
    }

    List<Group> groupsList = new ArrayList<>(groups);

    Collections.sort(groupsList, orderByComparator);

    return groupsList;
  }

  public static WebDAVUtil getInstance() {
    PortalRuntimePermission.checkGetBeanProperty(WebDAVUtil.class);

    return _instance;
  }

  public static String getLockUuid(HttpServletRequest request) throws WebDAVException {

    String token = StringPool.BLANK;

    String value = GetterUtil.getString(request.getHeader("If"));

    if (_log.isDebugEnabled()) {
      _log.debug("\"If\" header is " + value);
    }

    if (value.contains("(<DAV:no-lock>)")) {
      if (_log.isWarnEnabled()) {
        _log.warn("Lock tokens can never be <DAV:no-lock>");
      }

      throw new WebDAVException();
    }

    int beg = value.indexOf(TOKEN_PREFIX);

    if (beg >= 0) {
      beg += TOKEN_PREFIX.length();

      if (beg < value.length()) {
        int end = value.indexOf(CharPool.GREATER_THAN, beg);

        token = GetterUtil.getString(value.substring(beg, end));
      }
    }

    return token;
  }

  public static String[] getPathArray(String path) {
    return getPathArray(path, false);
  }

  public static String[] getPathArray(String path, boolean fixTrailing) {
    path = HttpUtil.fixPath(path, true, fixTrailing);

    return StringUtil.split(path, CharPool.SLASH);
  }

  public static String getResourceName(String[] pathArray) {
    if (pathArray.length <= 2) {
      return StringPool.BLANK;
    } else {
      return HttpUtil.decodeURL(pathArray[pathArray.length - 1]);
    }
  }

  public static WebDAVStorage getStorage(String token) {
    return getInstance()._getStorage(token);
  }

  public static Collection<String> getStorageTokens() {
    return getInstance()._getStorageTokens();
  }

  public static long getTimeout(HttpServletRequest request) {
    final String TIME_PREFIX = "Second-";

    long timeout = 0;

    String value = GetterUtil.getString(request.getHeader("Timeout"));

    if (_log.isDebugEnabled()) {
      _log.debug("\"Timeout\" header is " + value);
    }

    int index = value.indexOf(TIME_PREFIX);

    if (index >= 0) {
      index += TIME_PREFIX.length();

      if (index < value.length()) {
        timeout = GetterUtil.getLong(value.substring(index));
      }
    }

    return timeout * Time.SECOND;
  }

  public static boolean isOverwrite(HttpServletRequest request) {
    return getInstance()._isOverwrite(request);
  }

  public static String stripManualCheckInRequiredPath(String url) {
    return stripToken(url, DL.MANUAL_CHECK_IN_REQUIRED_PATH);
  }

  public static String stripOfficeExtension(String url) {
    String strippedUrl = stripToken(url, DL.OFFICE_EXTENSION_PATH);

    if (strippedUrl.length() != url.length()) {
      strippedUrl = FileUtil.stripExtension(strippedUrl);
    }

    return strippedUrl;
  }

  public static String stripToken(String url, String token) {
    if (Validator.isNull(url)) {
      return StringPool.BLANK;
    }

    int index = url.indexOf(token);

    if (index >= 0) {
      url = url.substring(0, index) + url.substring(index + token.length());
    }

    return url;
  }

  private WebDAVUtil() {
    Registry registry = RegistryUtil.getRegistry();

    _serviceTracker =
        registry.trackServices(WebDAVStorage.class, new WebDAVStorageServiceTrackerCustomizer());

    _serviceTracker.open();
  }

  private void _addStorage(WebDAVStorage storage) {
    Registry registry = RegistryUtil.getRegistry();

    ServiceRegistration<WebDAVStorage> serviceRegistration =
        registry.registerService(WebDAVStorage.class, storage);

    _serviceRegistrations.put(storage, serviceRegistration);
  }

  private void _deleteStorage(WebDAVStorage storage) {
    ServiceRegistration<WebDAVStorage> serviceRegistration = _serviceRegistrations.remove(storage);

    if (serviceRegistration != null) {
      serviceRegistration.unregister();
    }
  }

  private WebDAVStorage _getStorage(String token) {
    return _storageMap.get(token);
  }

  private Collection<String> _getStorageTokens() {
    return _storageMap.keySet();
  }

  private boolean _isOverwrite(HttpServletRequest request) {
    String value = GetterUtil.getString(request.getHeader("Overwrite"));

    if (StringUtil.equalsIgnoreCase(value, "F") || !GetterUtil.getBoolean(value)) {

      return false;
    } else {
      return true;
    }
  }

  private static final Log _log = LogFactoryUtil.getLog(WebDAVUtil.class);

  private static final WebDAVUtil _instance = new WebDAVUtil();

  private final ServiceRegistrationMap<WebDAVStorage> _serviceRegistrations =
      new ServiceRegistrationMap<>();
  private final ServiceTracker<WebDAVStorage, WebDAVStorage> _serviceTracker;
  private final Map<String, WebDAVStorage> _storageMap = new ConcurrentSkipListMap<>();

  private class WebDAVStorageServiceTrackerCustomizer
      implements ServiceTrackerCustomizer<WebDAVStorage, WebDAVStorage> {

    @Override
    public WebDAVStorage addingService(ServiceReference<WebDAVStorage> serviceReference) {

      Registry registry = RegistryUtil.getRegistry();

      WebDAVStorage webDAVStorage = registry.getService(serviceReference);

      if (webDAVStorage.getToken() == null) {
        return null;
      }

      _storageMap.put(webDAVStorage.getToken(), webDAVStorage);

      return webDAVStorage;
    }

    @Override
    public void modifiedService(
        ServiceReference<WebDAVStorage> serviceReference, WebDAVStorage webDAVStorage) {}

    @Override
    public void removedService(
        ServiceReference<WebDAVStorage> serviceReference, WebDAVStorage webDAVStorage) {

      Registry registry = RegistryUtil.getRegistry();

      registry.ungetService(serviceReference);

      _storageMap.remove(webDAVStorage.getToken());
    }
  }
}
Esempio n. 3
0
/**
 * @author Brian Wing Shun Chan
 * @author Alexander Chow
 */
public class WebDAVUtil {

  public static final Namespace DAV_URI = SAXReaderUtil.createNamespace("D", "DAV:");

  public static final int SC_MULTI_STATUS = 207;

  public static final int SC_LOCKED = 423;

  public static final String TOKEN_PREFIX = "opaquelocktoken:";

  public static void addStorage(WebDAVStorage storage) {
    _instance._addStorage(storage);
  }

  public static void deleteStorage(WebDAVStorage storage) {
    _instance._deleteStorage(storage);
  }

  public static long getDepth(HttpServletRequest request) {
    String value = GetterUtil.getString(request.getHeader("Depth"));

    if (_log.isDebugEnabled()) {
      _log.debug("\"Depth\" header is " + value);
    }

    if (value.equals("0")) {
      return 0;
    } else {
      return -1;
    }
  }

  public static String getDestination(HttpServletRequest request, String rootPath) {

    String headerDestination = request.getHeader("Destination");
    String[] pathSegments = StringUtil.split(headerDestination, rootPath);

    String destination = pathSegments[pathSegments.length - 1];

    destination = HttpUtil.decodePath(destination);

    if (_log.isDebugEnabled()) {
      _log.debug("Destination " + destination);
    }

    return destination;
  }

  public static long getGroupId(long companyId, String path) throws WebDAVException {

    String[] pathArray = getPathArray(path);

    return getGroupId(companyId, pathArray);
  }

  public static long getGroupId(long companyId, String[] pathArray) throws WebDAVException {

    try {
      if (pathArray.length == 0) {
        return 0;
      }

      String name = pathArray[0];

      try {
        Group group = GroupLocalServiceUtil.getGroup(companyId, name);

        return group.getGroupId();
      } catch (NoSuchGroupException nsge) {
      }

      try {
        Group group = GroupLocalServiceUtil.getFriendlyURLGroup(companyId, StringPool.SLASH + name);

        return group.getGroupId();
      } catch (NoSuchGroupException nsge) {
      }

      try {
        User user = UserLocalServiceUtil.getUserByScreenName(companyId, name);

        Group group = user.getGroup();

        return group.getGroupId();
      } catch (NoSuchUserException nsue) {
      }
    } catch (Exception e) {
      throw new WebDAVException(e);
    }

    return 0;
  }

  public static List<Group> getGroups(long userId) throws Exception {
    User user = UserLocalServiceUtil.getUser(userId);

    return getGroups(user);
  }

  public static List<Group> getGroups(User user) throws Exception {

    // Guest

    if (user.isDefaultUser()) {
      List<Group> groups = new ArrayList<Group>();

      Group group = GroupLocalServiceUtil.getGroup(user.getCompanyId(), GroupConstants.GUEST);

      groups.add(group);

      return groups;
    }

    // Communities

    LinkedHashMap<String, Object> params = new LinkedHashMap<String, Object>();

    params.put("usersGroups", user.getUserId());

    OrderByComparator orderByComparator = new GroupFriendlyURLComparator(true);

    List<Group> groups =
        GroupLocalServiceUtil.search(
            user.getCompanyId(),
            null,
            null,
            params,
            QueryUtil.ALL_POS,
            QueryUtil.ALL_POS,
            orderByComparator);

    // Organizations

    groups.addAll(
        GroupLocalServiceUtil.getUserOrganizationsGroups(
            user.getUserId(), QueryUtil.ALL_POS, QueryUtil.ALL_POS));

    // User

    if (!user.isDefaultUser()) {
      groups.add(user.getGroup());
    }

    Collections.sort(groups, orderByComparator);

    return groups;
  }

  public static String getLockUuid(HttpServletRequest request) throws WebDAVException {

    String token = StringPool.BLANK;

    String value = GetterUtil.getString(request.getHeader("If"));

    if (_log.isDebugEnabled()) {
      _log.debug("\"If\" header is " + value);
    }

    if (value.contains("(<DAV:no-lock>)")) {
      if (_log.isWarnEnabled()) {
        _log.warn("Lock tokens can never be <DAV:no-lock>");
      }

      throw new WebDAVException();
    }

    int beg = value.indexOf(TOKEN_PREFIX);

    if (beg >= 0) {
      beg += TOKEN_PREFIX.length();

      if (beg < value.length()) {
        int end = value.indexOf(">", beg);

        token = GetterUtil.getString(value.substring(beg, end));
      }
    }

    return token;
  }

  public static String[] getPathArray(String path) {
    return getPathArray(path, false);
  }

  public static String[] getPathArray(String path, boolean fixTrailing) {
    path = HttpUtil.fixPath(path, true, fixTrailing);

    return StringUtil.split(path, StringPool.SLASH);
  }

  public static String getResourceName(String[] pathArray) {
    if (pathArray.length <= 2) {
      return StringPool.BLANK;
    } else {
      return pathArray[pathArray.length - 1];
    }
  }

  public static WebDAVStorage getStorage(String token) {
    return _instance._getStorage(token);
  }

  public static Collection<String> getStorageTokens() {
    return _instance._getStorageTokens();
  }

  public static long getTimeout(HttpServletRequest request) {
    final String TIME_PREFIX = "Second-";

    long timeout = 0;

    String value = GetterUtil.getString(request.getHeader("Timeout"));

    if (_log.isDebugEnabled()) {
      _log.debug("\"Timeout\" header is " + value);
    }

    int index = value.indexOf(TIME_PREFIX);

    if (index >= 0) {
      index += TIME_PREFIX.length();

      if (index < value.length()) {
        timeout = GetterUtil.getLong(value.substring(index));
      }
    }

    return timeout * Time.SECOND;
  }

  public static boolean isOverwrite(HttpServletRequest request) {
    return _instance._isOverwrite(request);
  }

  private WebDAVUtil() {
    _storageMap = new TreeMap<String, WebDAVStorage>();
  }

  private void _addStorage(WebDAVStorage storage) {
    _storageMap.put(storage.getToken(), storage);
  }

  private void _deleteStorage(WebDAVStorage storage) {
    if (storage != null) {
      _storageMap.remove(storage.getToken());
    }
  }

  private WebDAVStorage _getStorage(String token) {
    return _storageMap.get(token);
  }

  private Collection<String> _getStorageTokens() {
    return _storageMap.keySet();
  }

  private boolean _isOverwrite(HttpServletRequest request) {
    String value = GetterUtil.getString(request.getHeader("Overwrite"));

    if (value.equalsIgnoreCase("F") || !GetterUtil.getBoolean(value)) {
      return false;
    } else {
      return true;
    }
  }

  private static Log _log = LogFactoryUtil.getLog(WebDAVUtil.class);

  private static WebDAVUtil _instance = new WebDAVUtil();

  private Map<String, WebDAVStorage> _storageMap;
}