org.shaf.server.controller.ActionFsController.java Source code

Java tutorial

Introduction

Here is the source code for org.shaf.server.controller.ActionFsController.java

Source

/**
 * Copyright 2014-2015 SHAF-WORK
 * 
 * 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.shaf.server.controller;

import org.apache.hadoop.fs.Path;
import org.shaf.core.util.Log;
import org.shaf.core.util.TextMatrix;
import org.shaf.core.util.UriUtils;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

/**
 * The controller for the "File System" dashboard.
 * 
 * @author Mykola Galushka
 */
@Controller
@Secured("ROLE_USER")
@RequestMapping("/fs/action")
public class ActionFsController extends GenericActionController {

    /**
     * Defines a logger.
     */
    private static final Log LOG = Log.forClass(ActionFsController.class);

    /**
     * Shows all directories and files defined under the root directory.
     * 
     * @return the view model.
     * @throws Exception
     *             is the view constructing has failed.
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ModelAndView onList() throws Exception {
        LOG.debug("CALL: /fs/action/list");

        return onDir(UriUtils.encodeArguments("/"));
    }

    /**
     * Shows all directories and files defined under the specified directory.
     * 
     * @param path
     *            the base directory for listing content.
     * @return the view model.
     * @throws Exception
     *             is the view constructing has failed.
     */
    @RequestMapping(value = "/dir/{path}", method = RequestMethod.GET)
    public ModelAndView onDir(@PathVariable String path) throws Exception {
        LOG.debug("CALL: /fs/action/dir/{" + UriUtils.decodeArguments(path) + "}");

        Path location = new Path(UriUtils.decodeArguments(path));
        String current = location.toString();
        String parent = location.isRoot() ? null : location.getParent().toString();

        TextMatrix content = OPER.getDirContent(current);

        return ViewFileSystem.getDirView().addCurrentPath(current).addParentPath(parent).addDirContent(content)
                .info(super.getListDescription(content, "entity"));
    }

    /**
     * Shows a content of the specified file.
     * 
     * @param path
     *            the path to the file which content needs to be shown.
     * @return the view model.
     * @throws Exception
     *             is the view constructing has failed.
     */
    @RequestMapping(value = "/file/{path}", method = RequestMethod.GET)
    public ModelAndView onFile(@PathVariable String path) throws Exception {
        LOG.debug("CALL: /fs/action/file/{" + UriUtils.decodeArguments(path) + "}");

        Path location = new Path(UriUtils.decodeArguments(path));
        String current = location.toString();
        String parent = location.isRoot() ? null : location.getParent().toString();

        return ViewFileSystem.getFileView().addCurrentPath(current).addParentPath(parent)
                .addFileContent(OPER.getFileContent(current)).info("The file content.");
    }
}