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.core.Response; 030 031import org.nuxeo.ecm.webengine.model.WebObject; 032import org.nuxeo.ecm.webengine.model.exceptions.WebResourceNotFoundException; 033import org.nuxeo.ecm.webengine.model.exceptions.WebSecurityException; 034import org.nuxeo.ecm.webengine.model.impl.ModuleConfiguration; 035import org.nuxeo.ecm.webengine.model.impl.ModuleRoot; 036import org.nuxeo.ecm.webengine.model.impl.ModuleShortcut; 037 038/** 039 * The web entry point of WebEngine. 040 * <p> 041 * This is a mix between an webengine module and a JAX-RS root resource 042 * 043 * @author <a href="mailto:[email protected]">Bogdan Stefanescu</a> 044 */ 045@Path("/") 046@Produces("text/html; charset=UTF-8") 047@WebObject(type = "base") 048public class Main extends ModuleRoot { 049 050 @GET 051 public Object doGet() { 052 List<ModuleShortcut> list = new ArrayList<ModuleShortcut>(); 053 for (ModuleConfiguration mc : ctx.getEngine().getModuleManager().getModules()) { 054 List<ModuleShortcut> items = mc.getShortcuts(); 055 if (items != null && !items.isEmpty()) { 056 for (ModuleShortcut item : items) { 057 if (item.title == null) { 058 item.title = mc.name; 059 } 060 } 061 list.addAll(items); 062 } else if (!mc.isHeadless) { 063 if (mc.roots != null && mc.roots.length > 0) { 064 Path path = mc.roots[0].getAnnotation(Path.class); 065 if (path != null) { 066 list.add(new ModuleShortcut(path.value(), mc.name)); 067 } 068 } 069 } 070 } 071 Collections.sort(list, new Comparator<ModuleShortcut>() { 072 public int compare(ModuleShortcut o1, ModuleShortcut o2) { 073 return o1.title.compareTo(o2.title); 074 } 075 }); 076 return getView("index").arg("moduleLinks", list); 077 } 078 079 // handle errors 080 081 @Override 082 public Object handleError(Throwable t) { 083 if (t instanceof WebSecurityException) { 084 return Response.status(401).entity(getTemplate("error/error_401.ftl")).type("text/html").build(); 085 } else if (t instanceof WebResourceNotFoundException) { 086 return Response.status(404).entity(getTemplate("error/error_404.ftl")).type("text/html").build(); 087 } else { 088 return super.handleError(t); 089 } 090 } 091}