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.text.StringEscapeUtils;
032import org.nuxeo.runtime.api.Framework;
033import org.apache.commons.logging.Log;
034import org.apache.commons.logging.LogFactory;
035
036/**
037 * Resource representing a facelet that is not found in this application.
038 * <p>
039 * This is used to avoid crashing triggering an exception when a facelet resource is missing.
040 * <p>
041 * Instead, a message referencing the missing resource is displayed in red and in bold where the facelet would have been
042 * included.
043 *
044 * @since 6.0
045 */
046public class NuxeoUnknownResource extends ViewResource {
047
048    public static final String MARKER = NuxeoUnknownResource.class.getName();
049
050    public static final String PLACEHOLDER = "/facelet_not_found.xhtml";
051
052    private static final Log log = LogFactory.getLog(NuxeoUnknownResource.class);
053
054    protected final String path;
055
056    public NuxeoUnknownResource(String path) {
057        super();
058        this.path = path;
059    }
060
061    @Override
062    public URL getURL() {
063        try {
064            String urlPath = 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("facelet not found: " + path);
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 message = "ERROR: facelet not found";
096                // NXP-25746
097                if (Framework.isDevModeSet() && !path.contains("$") && !path.contains("#")) {
098                    message += " at '" + path + "'";
099                }
100                String msg = "<span><span style=\"color:red;font-weight:bold;\">"
101                        + StringEscapeUtils.escapeHtml4(message) + "</span><br/></span>";
102                return new ByteArrayInputStream(msg.getBytes());
103            }
104        }
105    }
106}