/*
* @(#)ConfigurationAction.java
*
* Copyright 2009 Instituto Superior Tecnico
* Founding Authors: João Figueiredo, Luis Cruz, Paulo Abrantes, Susana Fernandes
*
* https://fenix-ashes.ist.utl.pt/
*
* This file is part of the Bennu Web Application Infrastructure.
*
* The Bennu Web Application Infrastructure is free software: you can
* redistribute it and/or modify it under the terms of the GNU Lesser General
* Public License as published by the Free Software Foundation, either version
* 3 of the License, or (at your option) any later version.
*
* Bennu is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Bennu. If not, see .
*
*/
package pt.ist.bennu.manager.presentation.actions;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.EmptyFileFilter;
import org.apache.commons.io.filefilter.RegexFileFilter;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import pt.ist.bennu.core.annotation.Functionality;
import pt.ist.bennu.core.domain.MyOrg;
import pt.ist.bennu.core.domain.RoleType;
import pt.ist.bennu.core.domain.User;
import pt.ist.bennu.core.domain.VirtualHost;
import pt.ist.bennu.core.domain.groups.People;
import pt.ist.bennu.core.domain.groups.PersistentGroup;
import pt.ist.bennu.core.presentation.Context;
import pt.ist.bennu.core.presentation.LayoutContext;
import pt.ist.bennu.core.presentation.actions.ContextBaseAction;
import pt.ist.bennu.core.security.UserView;
import pt.ist.renderers.utils.RenderUtils;
import pt.ist.struts.dispatch.annotation.Mapping;
import pt.utl.ist.fenix.tools.util.ByteArray;
/**
*
* @author Pedro Santos
* @author Nuno Diegues
* @author Paulo Abrantes
* @author Luis Cruz
*/
@Mapping(path = "/configuration")
public class ConfigurationAction extends ContextBaseAction {
// ActionNode.createActionNode(virtualHost, systemSettings, "/scheduler",
// "viewScheduler", "resources.BennuManagerResources",
// "label.configuration.tasks.scheduleing", Role.getRole(RoleType.MANAGER));
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
throws Exception {
final User user = UserView.getUser();
if (user == null || !user.hasRoleType(RoleType.MANAGER)) {
throw new Error("unauthorized.access");
}
return super.execute(mapping, form, request, response);
}
@Functionality(bundle = "resources/BennuManagerResources", groupKey = "label.application", key = "label.application.configuration")
public ActionForward applicationConfiguration(final ActionMapping mapping, final ActionForm form,
final HttpServletRequest request, final HttpServletResponse response) throws Exception {
return forward(request, "/bennu-manager/applicationConfiguration.jsp");
}
public ActionForward basicApplicationConfiguration(final ActionMapping mapping, final ActionForm form,
final HttpServletRequest request, final HttpServletResponse response, final String forward) throws Exception {
VirtualHostBean virtualHostToConfigure = RenderUtils.getRenderedObject("virtualHostToConfigure");
if (virtualHostToConfigure == null) {
final VirtualHost virtualHost = getDomainObject(request, "virtualHostId");
virtualHostToConfigure = virtualHost != null ? new VirtualHostBean(virtualHost, request) : new VirtualHostBean(
request);
}
request.setAttribute("virtualHostToConfigure", virtualHostToConfigure);
return forward(request, forward);
}
public ActionForward basicApplicationConfiguration(final ActionMapping mapping, final ActionForm form,
final HttpServletRequest request, final HttpServletResponse response) throws Exception {
return basicApplicationConfiguration(mapping, form, request, response, "/bennu-manager/basicApplicationConfiguration.jsp");
}
public ActionForward themeApplicationConfiguration(final ActionMapping mapping, final ActionForm form,
final HttpServletRequest request, final HttpServletResponse response) throws Exception {
return basicApplicationConfiguration(mapping, form, request, response, "/bennu-manager/themeApplicationConfiguration.jsp");
}
public ActionForward languageApplicationConfiguration(final ActionMapping mapping, final ActionForm form,
final HttpServletRequest request, final HttpServletResponse response) throws Exception {
return basicApplicationConfiguration(mapping, form, request, response,
"/bennu-manager/languageApplicationConfiguration.jsp");
}
public ActionForward editBasicApplicationConfiguration(final ActionMapping mapping, final ActionForm form,
final HttpServletRequest request, final HttpServletResponse response) throws Exception {
final VirtualHostBean bean = RenderUtils.getRenderedObject("virtualHostToConfigure");
bean.save();
((LayoutContext) getContext(request)).switchLayoutAndTheme(bean.getLayout(), bean.getTheme());
return applicationConfiguration(mapping, form, request, response);
}
public ActionForward editBasicApplicationConfigurationLogo(final ActionMapping mapping, final ActionForm form,
final HttpServletRequest request, final HttpServletResponse response) throws Exception {
final VirtualHostBean bean = RenderUtils.getRenderedObject("virtualHostToConfigureLogo");
final byte[] logo = consumeInputStream(bean.getLogoInputStream());
if (logo != null) {
final VirtualHost virtualHost = getDomainObject(request, "virtualHostId");
virtualHost.setLogo(new ByteArray(logo));
}
return applicationConfiguration(mapping, form, request, response);
}
public ActionForward editBasicApplicationConfigurationFavico(final ActionMapping mapping, final ActionForm form,
final HttpServletRequest request, final HttpServletResponse response) throws Exception {
final VirtualHostBean bean = RenderUtils.getRenderedObject("virtualHostToConfigureFavico");
final byte[] favico = consumeInputStream(bean.getFaviconInputStream());
if (favico != null) {
final VirtualHost virtualHost = getDomainObject(request, "virtualHostId");
virtualHost.setFavicon(new ByteArray(favico));
}
return applicationConfiguration(mapping, form, request, response);
}
public ActionForward editLanguageApplicationConfiguration(final ActionMapping mapping, final ActionForm form,
final HttpServletRequest request, final HttpServletResponse response) throws Exception {
final VirtualHostBean bean = RenderUtils.getRenderedObject("virtualHostToConfigure");
bean.save();
return applicationConfiguration(mapping, form, request, response);
}
@Functionality(bundle = "resources/BennuManagerResources", groupKey = "label.application", key = "label.configuration.manage.system.groups")
public ActionForward manageSystemGroups(final ActionMapping mapping, final ActionForm form, final HttpServletRequest request,
final HttpServletResponse response) throws Exception {
final Context context = getContext(request);
final Set persistentGroups = MyOrg.getInstance().getSystemGroupsSet();
request.setAttribute("persistentGroups", persistentGroups);
return context.forward("/bennu-manager/manageGroups.jsp");
}
public ActionForward viewPersistentGroup(final ActionMapping mapping, final ActionForm form,
final HttpServletRequest request, final HttpServletResponse response) throws Exception {
final PersistentGroup persistentGroup = getDomainObject(request, "persistentGroupId");
return viewPersistentGroup(request, persistentGroup);
}
private ActionForward viewPersistentGroup(final HttpServletRequest request, final PersistentGroup persistentGroup) {
final Context context = getContext(request);
request.setAttribute("persistentGroup", persistentGroup);
return context.forward("/bennu-manager/viewPersistentGroup.jsp");
}
public ActionForward prepareAddVirtualHost(final ActionMapping mapping, final ActionForm form,
final HttpServletRequest request, final HttpServletResponse response) throws Exception {
final Context context = getContext(request);
VirtualHostBean virtualHostBean = RenderUtils.getRenderedObject();
if (virtualHostBean == null) {
virtualHostBean = new VirtualHostBean(request);
}
request.setAttribute("virtualHostBean", virtualHostBean);
return context.forward("/bennu-manager/addVirtualHost.jsp");
}
public ActionForward addVirtualHost(final ActionMapping mapping, final ActionForm form, final HttpServletRequest request,
final HttpServletResponse response) throws Exception {
final VirtualHostBean virtualHostBean = RenderUtils.getRenderedObject();
virtualHostBean.save();
return applicationConfiguration(mapping, form, request, response);
}
public ActionForward deleteVirtualHost(final ActionMapping mapping, final ActionForm form, final HttpServletRequest request,
final HttpServletResponse response) throws Exception {
final VirtualHost virtualHost = getDomainObject(request, "virtualHostToManageId");
virtualHost.delete();
return applicationConfiguration(mapping, form, request, response);
}
@Functionality(bundle = "resources/BennuManagerResources", groupKey = "label.application", key = "label.configuration.viewProperties")
public ActionForward viewSystemConfig(final ActionMapping mapping, final ActionForm form, final HttpServletRequest request,
final HttpServletResponse response) {
Map manifests = new HashMap();
String libPath = request.getServletContext().getRealPath("/WEB-INF/lib");
Collection jars = FileUtils
.listFiles(new File(libPath), new RegexFileFilter(".*\\.jar"), EmptyFileFilter.NOT_EMPTY);
for (File file : jars) {
try {
URL manifest = new URL("jar:file:" + file.getAbsolutePath() + "!/META-INF/MANIFEST.MF");
Properties properties = new Properties();
try (InputStream stream = manifest.openStream()) {
properties.load(stream);
manifests.put(file.getName(), properties);
} catch (IOException e) {
}
} catch (MalformedURLException e1) {
}
}
request.setAttribute("manifests", manifests.entrySet());
return forward(request, "/bennu-manager/systemInfo.jsp");
}
public ActionForward removeUser(final ActionMapping mapping, final ActionForm form, final HttpServletRequest request,
final HttpServletResponse response) throws Exception {
final People people = getDomainObject(request, "persistentGroupId");
final User user = getDomainObject(request, "userId");
people.removeMember(user);
return viewPersistentGroup(request, people);
}
}