com.artglorin.web.utils.ResourcesHelper.java Source code

Java tutorial

Introduction

Here is the source code for com.artglorin.web.utils.ResourcesHelper.java

Source

/**
 * Copyright (C) 2015 Verminsky V.V. (develop@artglorin.com)
 *
 * 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 com.artglorin.web.utils;

import org.apache.commons.io.FileUtils;

import javax.servlet.ServletContext;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;

/**
 * ?? ?  ? ?? ??,  ???  ?.
 *
 * @author Verminsky V.V. e-mail: develop@artglorin.com
 * @version 0.0.1
 *          Created on 15.07.15.
 */
public class ResourcesHelper {

    private String pathToResources;

    private String realPath;

    /**
     * ?  ?    ?  ??
     *
     * @param pathToResources   ? ?  ? ? ??,    null
     * @param servletContext  ?  ?,      ? ?? ??
     */
    public ResourcesHelper(String pathToResources, ServletContext servletContext) {
        if (pathToResources == null) {
            throw new IllegalArgumentException("Required argument pathToResources is null");
        }
        if (servletContext == null) {
            throw new IllegalArgumentException("Required argument servletContext is null");
        }
        this.pathToResources = pathToResources;
        realPath = servletContext.getRealPath(pathToResources);
    }

    /**
     * ?  ?     ,      ?    .
     *
     * @param path     
     * @throws IOException
     */
    public void deleteResourcesDirectory(String... path) throws IOException {
        FileUtils.deleteDirectory(this.createRealPath(path).toFile());
    }

    /**
     *      ??
     *
     * @param path    ?
     * @return  
     */
    private Path createRealPath(String... path) {
        return Paths.get(realPath, path);
    }

    /**
     *    ??  ? ?.
     *   ?, ?    ?  ?
     *
     * @return ?   ??  ? ?.
     */
    public String getPathToResources() {
        return pathToResources;
    }

    /**
     * ?   ??       .
     *   ? 
     *
     * @param data ?? ,   ?.
     * @param path       ? .    ??,   . ?  
     *               ?,   ?  ? ? ?. ? ?  ?? ?
     *             ["data","file",".txt"],    ?     "/pathToResources/data/file.txt".
     *             ?  ?? ["data","file.txt"]   ? "/data/file.txt",   
     *             ?     "/pathToResources/data/file.txt"
     * @throws IOException
     */
    public void saveData(byte[] data, String... path) throws IOException {
        if (path.length == 0) {
            throw new IOException("Path cannot be null");
        }
        Path file = createRealPath(path);
        String[] filePath = null;
        if (file.toFile().exists()) {
            FileUtils.writeByteArrayToFile(file.toFile(), data);
        } else {
            if (path.length > 1) {
                if (path[path.length - 1].startsWith(".")) {
                    filePath = Arrays.copyOf(path, path.length - 2);
                }
            }
        }
        if (filePath == null) {
            filePath = Arrays.copyOf(path, path.length - 1);
        }
        createResourcesDirectory(filePath);
        FileUtils.writeByteArrayToFile(file.toFile(), data);
    }

    /**
     * ?     
     *
     * @param path ?? ? ,   ?
     * @throws IOException
     */
    public void createResourcesDirectory(String... path) throws IOException {
        Files.createDirectories(this.createRealPath(path));
    }
}