001/*
002 * (C) Copyright 2006-2008 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     bstefanescu
016 */
017package org.nuxeo.ecm.webengine.base;
018
019import java.util.ArrayList;
020import java.util.Collections;
021import java.util.Comparator;
022import java.util.List;
023
024import javax.ws.rs.GET;
025import javax.ws.rs.Path;
026import javax.ws.rs.Produces;
027import javax.ws.rs.WebApplicationException;
028import javax.ws.rs.core.Response;
029
030import org.nuxeo.ecm.webengine.model.WebObject;
031import org.nuxeo.ecm.webengine.model.exceptions.WebResourceNotFoundException;
032import org.nuxeo.ecm.webengine.model.exceptions.WebSecurityException;
033import org.nuxeo.ecm.webengine.model.impl.ModuleConfiguration;
034import org.nuxeo.ecm.webengine.model.impl.ModuleRoot;
035import org.nuxeo.ecm.webengine.model.impl.ModuleShortcut;
036
037/**
038 * The web entry point of WebEngine.
039 * <p>
040 * This is a mix between an webengine module and a JAX-RS root resource
041 *
042 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
043 */
044@Path("/")
045@Produces("text/html; charset=UTF-8")
046@WebObject(type = "base")
047public class Main extends ModuleRoot {
048
049    @GET
050    public Object doGet() {
051        List<ModuleShortcut> list = new ArrayList<ModuleShortcut>();
052        for (ModuleConfiguration mc : ctx.getEngine().getModuleManager().getModules()) {
053            List<ModuleShortcut> items = mc.getShortcuts();
054            if (items != null && !items.isEmpty()) {
055                for (ModuleShortcut item : items) {
056                    if (item.title == null) {
057                        item.title = mc.name;
058                    }
059                }
060                list.addAll(items);
061            } else if (!mc.isHeadless) {
062                if (mc.roots != null && mc.roots.length > 0) {
063                    Path path = mc.roots[0].getAnnotation(Path.class);
064                    if (path != null) {
065                        list.add(new ModuleShortcut(path.value(), mc.name));
066                    }
067                }
068            }
069        }
070        Collections.sort(list, new Comparator<ModuleShortcut>() {
071            public int compare(ModuleShortcut o1, ModuleShortcut o2) {
072                return o1.title.compareTo(o2.title);
073            }
074        });
075        return getView("index").arg("moduleLinks", list);
076    }
077
078    // handle errors
079    @Override
080    public Object handleError(WebApplicationException e) {
081        if (e instanceof WebSecurityException) {
082            return Response.status(401).entity(getTemplate("error/error_401.ftl")).type("text/html").build();
083        } else if (e instanceof WebResourceNotFoundException) {
084            return Response.status(404).entity(getTemplate("error/error_404.ftl")).type("text/html").build();
085        } else {
086            return super.handleError(e);
087        }
088    }
089
090}