PathValidator.java

/*
 * Copyright 2018 Global Crop Diversity Trust
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.genesys.filerepository.service.impl;

import java.nio.file.Path;

import org.apache.commons.lang3.StringUtils;
import org.genesys.filerepository.InvalidRepositoryPathException;

import lombok.extern.slf4j.Slf4j;

/**
 * Simple path validator.
 */
@Slf4j
public class PathValidator {

	/**
	 * Checks if path is valid.
	 *
	 * @param path the path
	 * @return true, if path is valid
	 */
	protected static boolean isValidPath(final String path) {
		return StringUtils.isNotBlank(path) && path.startsWith("/") && ("/".equals(path) || !path.endsWith("/")) && !path.contains("//") && !path.contains(" /") && !path.contains(
			"/ ") && !path.contains("?") && !path.contains("&") && !path.contains("\\") && !path.endsWith(" ");
	}

	/**
	 * Checks if folder name is valid.
	 *
	 * @param name the name
	 * @return true, if path is valid
	 */
	protected static boolean isValidFolderName(final String name) {
		return StringUtils.isNotBlank(name) && !("..".equals(name) || name.contains("/") || name.contains("?") || name.contains("&") || name.contains("\\"));
	}

	/**
	 * Checks if path is valid and throws InvalidPathException when path is not
	 * valid.
	 *
	 * @param path the path
	 * @throws InvalidRepositoryPathException when path is not valid.
	 */
	public static void checkValidPath(final String path) throws InvalidRepositoryPathException {
		if (!isValidPath(path)) {
			log.warn("Invalid repository path {}", path);
			throw new InvalidRepositoryPathException(path);
		}
	}

	/**
	 * Check valid folder name.
	 *
	 * @param name the name
	 * @throws InvalidRepositoryPathException the invalid repository path exception
	 */
	public static void checkValidFolderName(String name) throws InvalidRepositoryPathException {
		if (!isValidFolderName(name)) {
			log.warn("Invalid repository name {}", name);
			throw new InvalidRepositoryPathException(name);
		}
	}

	/**
	 * Check valid path.
	 *
	 * @param repositoryPath the repository path
	 * @throws InvalidRepositoryPathException the invalid repository path exception
	 */
	public static void checkValidPath(Path repositoryPath) throws InvalidRepositoryPathException {
		if (repositoryPath == null) {
			throw new InvalidRepositoryPathException("null path not allowed");
		} else if (! repositoryPath.isAbsolute()) {
			throw new InvalidRepositoryPathException("Path must be absolute. Path was " + repositoryPath);
		}
		checkValidPath(repositoryPath.normalize().toAbsolutePath().toString());
	}
}