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

Java tutorial

Introduction

Here is the source code for org.shaf.server.controller.ActionApplicationController.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.shaf.core.util.Log;
import org.shaf.core.util.TextMatrix;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

/**
 * The controller for the "Application" dashboard.
 * 
 * @author Mykola Galushka
 */
@Controller
@Secured("ROLE_ADMIN")
@RequestMapping("/app/action")
public class ActionApplicationController extends GenericActionController {

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

    /**
     * Shows a list of all available applications.
     * 
     * @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: /app/action/list");

        TextMatrix apps = OPER.getApplications();

        return ViewApplication.getListView().header("cloud", "All currently deployed applications.")
                .addApplicationList(apps).info(super.getListDescription(apps, "application"));
    }

    /**
     * Deploys an application to the server.
     * 
     * @param file
     *            the deploying package.
     * @return the view model.
     * @throws Exception
     *             is the view constructing has failed.
     */
    @RequestMapping(value = "/deploy", method = RequestMethod.POST)
    public ModelAndView onDeploy(@RequestParam("file") MultipartFile file) throws Exception {
        LOG.debug("CALL: /app/action/deploy (with attached multipart-file object)");

        ViewApplication view = ViewApplication.getListView().header("cloud",
                "All currently deployed applications.");

        if (!file.isEmpty()) {
            String app = file.getOriginalFilename();
            if (OPER.deployApplication(app, file.getBytes())) {
                view.info("The '" + app + "' application is deployed. ");
            } else {
                view.warn("The '" + app + "' application is not deployed yet. "
                        + "(Check the log to clarify a problem.)");
            }
        } else {
            view.warn("Select an application for deployment " + "(click on 'Browse' button).");
        }

        return view.addApplicationList(OPER.getApplications());
    }

    /**
     * Removes an application from the server.
     * 
     * @param app
     *            the application name.
     * @return the view model.
     * @throws Exception
     *             is the view constructing has failed.
     */
    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    public ModelAndView onRemove(@RequestParam(value = "app", required = false) String app) throws Exception {
        LOG.debug("CALL: /app/action/remove/{" + app + "}");

        ViewApplication view = ViewApplication.getListView().header("cloud",
                "All currently deployed applications.");

        if (app != null) {
            if (OPER.removeApplication(app)) {
                view.info("The '" + app + "' application is removed.");
            } else {
                view.warn("The '" + app + "' application is not removed yet. "
                        + "Check the log to clarify a problem.");
            }
        } else {
            view.warn("Select an application for removal " + "(tick a application radio-button).");
        }

        return view.addApplicationList(OPER.getApplications());
    }

    /**
     * Shows information about application.
     * 
     * @param app
     *            the application name.
     * @return the view model.
     * @throws Exception
     *             is the view constructing has failed.
     */
    @RequestMapping(value = "/info", method = RequestMethod.POST)
    public ModelAndView onInfo(@RequestParam(value = "app", required = false) String app) throws Exception {
        LOG.debug("CALL: /app/action/info/{" + app + "}");

        if (app == null) {
            return ViewApplication.getListView().header("cloud", "All currently deployed applications.")

                    .addApplicationList(OPER.getApplications())
                    .warn("Select an application for showing information " + "(tick a application radio-button).");
        } else {
            return ViewApplication.getInfoView().header("app", "The information about '" + app + "' application.")
                    .addApplicationInfo(OPER.getApplicationInfo(app))
                    .info("This information is obtained from the '/apps/" + app + "/app.info' file.");
        }
    }
}