001/*
002 * (C) Copyright 2006-2008 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     bstefanescu
018 */
019package org.nuxeo.ecm.webengine.base;
020
021import java.util.ArrayList;
022import java.util.Collections;
023import java.util.Comparator;
024import java.util.List;
025
026import javax.ws.rs.GET;
027import javax.ws.rs.Path;
028import javax.ws.rs.Produces;
029import javax.ws.rs.WebApplicationException;
030import javax.ws.rs.core.Response;
031
032import org.nuxeo.ecm.webengine.model.WebObject;
033import org.nuxeo.ecm.webengine.model.exceptions.WebResourceNotFoundException;
034import org.nuxeo.ecm.webengine.model.exceptions.WebSecurityException;
035import org.nuxeo.ecm.webengine.model.impl.ModuleConfiguration;
036import org.nuxeo.ecm.webengine.model.impl.ModuleRoot;
037import org.nuxeo.ecm.webengine.model.impl.ModuleShortcut;
038
039/**
040 * The web entry point of WebEngine.
041 * <p>
042 * This is a mix between an webengine module and a JAX-RS root resource
043 *
044 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
045 */
046@Path("/")
047@Produces("text/html; charset=UTF-8")
048@WebObject(type = "base")
049public class Main extends ModuleRoot {
050
051    @GET
052    public Object doGet() {
053        List<ModuleShortcut> list = new ArrayList<ModuleShortcut>();
054        for (ModuleConfiguration mc : ctx.getEngine().getModuleManager().getModules()) {
055            List<ModuleShortcut> items = mc.getShortcuts();
056            if (items != null && !items.isEmpty()) {
057                for (ModuleShortcut item : items) {
058                    if (item.title == null) {
059                        item.title = mc.name;
060                    }
061                }
062                list.addAll(items);
063            } else if (!mc.isHeadless) {
064                if (mc.roots != null && mc.roots.length > 0) {
065                    Path path = mc.roots[0].getAnnotation(Path.class);
066                    if (path != null) {
067                        list.add(new ModuleShortcut(path.value(), mc.name));
068                    }
069                }
070            }
071        }
072        Collections.sort(list, new Comparator<ModuleShortcut>() {
073            public int compare(ModuleShortcut o1, ModuleShortcut o2) {
074                return o1.title.compareTo(o2.title);
075            }
076        });
077        return getView("index").arg("moduleLinks", list);
078    }
079
080    // handle errors
081    @Override
082    public Object handleError(WebApplicationException e) {
083        if (e instanceof WebSecurityException) {
084            return Response.status(401).entity(getTemplate("error/error_401.ftl")).type("text/html").build();
085        } else if (e instanceof WebResourceNotFoundException) {
086            return Response.status(404).entity(getTemplate("error/error_404.ftl")).type("text/html").build();
087        } else {
088            return super.handleError(e);
089        }
090    }
091
092}