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