001/* 002 * (C) Copyright 2014 Nuxeo SA (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-2.1.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 * Anahide Tchertchian 016 */ 017package org.nuxeo.ecm.platform.ui.web.application; 018 019import java.io.ByteArrayInputStream; 020import java.io.IOException; 021import java.io.InputStream; 022import java.net.MalformedURLException; 023import java.net.URL; 024import java.net.URLConnection; 025import java.net.URLStreamHandler; 026 027import javax.faces.application.ViewResource; 028 029import org.apache.commons.lang.StringEscapeUtils; 030import org.apache.commons.logging.Log; 031import org.apache.commons.logging.LogFactory; 032 033/** 034 * Resource representing a facelet that is not found in this application. 035 * <p> 036 * This is used to avoid crashing triggering an exception when a facelet resource is missing. 037 * <p> 038 * Instead, a message referencing the missing resource is displayed in red and in bold where the facelet would have been 039 * included. 040 * 041 * @since 6.0 042 */ 043public class NuxeoUnknownResource extends ViewResource { 044 045 public static final String MARKER = NuxeoUnknownResource.class.getName(); 046 047 public static final String PLACEHOLDER = "/facelet_not_found.xhtml"; 048 049 private static final Log log = LogFactory.getLog(NuxeoUnknownResource.class); 050 051 protected final String path; 052 053 protected final String errorMessage; 054 055 public NuxeoUnknownResource(String path) { 056 super(); 057 this.path = path; 058 errorMessage = String.format("ERROR: facelet not found at '%s'", path); 059 } 060 061 @Override 062 public URL getURL() { 063 try { 064 String urlPath = String.format("%s%s", MARKER, path); 065 return new URL("", "", -1, urlPath, new NuxeoNotFoundResourceHandler()); 066 } catch (MalformedURLException e) { 067 return null; 068 } 069 } 070 071 class NuxeoNotFoundResourceHandler extends URLStreamHandler { 072 073 public NuxeoNotFoundResourceHandler() { 074 super(); 075 } 076 077 @Override 078 protected URLConnection openConnection(URL url) throws IOException { 079 log.error(errorMessage); 080 return new Connection(url); 081 } 082 083 class Connection extends URLConnection { 084 085 public Connection(URL url) { 086 super(url); 087 } 088 089 @Override 090 public void connect() throws IOException { 091 } 092 093 @Override 094 public InputStream getInputStream() throws IOException { 095 String msg = String.format("<span><span style=\"color:red;font-weight:bold;\">%s</span><br/></span>", 096 StringEscapeUtils.escapeHtml(errorMessage)); 097 return new ByteArrayInputStream(msg.getBytes()); 098 } 099 } 100 } 101}