001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements. See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache license, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License. You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the license for the specific language governing permissions and
015     * limitations under the license.
016     */
017    package org.apache.logging.log4j.web;
018    // Please note that if you move this class, make sure to update the Interpolator class (if still applicable) or remove
019    // this comment if no longer relevant
020    
021    import javax.servlet.ServletContext;
022    
023    import org.apache.logging.log4j.core.LogEvent;
024    import org.apache.logging.log4j.core.config.plugins.Plugin;
025    import org.apache.logging.log4j.core.lookup.AbstractLookup;
026    
027    @Plugin(name = "web", category = "Lookup")
028    public class WebLookup extends AbstractLookup {
029        private static final String ATTR_PREFIX = "attr.";
030        private static final String INIT_PARAM_PREFIX = "initParam.";
031    
032        /**
033         * @deprecated Use {@link WebLoggerContextUtils#getServletContext()}.
034         */
035        @Deprecated
036        protected ServletContext getServletContext() {
037            return WebLoggerContextUtils.getServletContext();
038        }
039    
040        @Override
041        public String lookup(final LogEvent event, final String key) {
042            final ServletContext ctx = WebLoggerContextUtils.getServletContext();
043            if (ctx == null) {
044                return null;
045            }
046    
047            if (key.startsWith(ATTR_PREFIX)) {
048                final String attrName = key.substring(ATTR_PREFIX.length());
049                final Object attrValue = ctx.getAttribute(attrName);
050                return attrValue == null ? null : attrValue.toString();
051            }
052    
053            if (key.startsWith(INIT_PARAM_PREFIX)) {
054                final String paramName = key.substring(INIT_PARAM_PREFIX.length());
055                return ctx.getInitParameter(paramName);
056            }
057    
058            if ("rootDir".equals(key)) {
059                final String root = ctx.getRealPath("/");
060                if (root == null) {
061                    final String msg = "Failed to resolve web:rootDir -- " +
062                            "servlet container unable to translate virtual path " +
063                            " to real path (probably not deployed as exploded";
064                    throw new IllegalStateException(msg);
065                }
066                return root;
067            }
068    
069            if ("contextPath".equals(key)) {
070                return ctx.getContextPath();
071            }
072    
073            if ("servletContextName".equals(key)) {
074                return ctx.getServletContextName();
075            }
076    
077            if ("serverInfo".equals(key)) {
078                return ctx.getServerInfo();
079            }
080    
081            if ("effectiveMajorVersion".equals(key)) {
082                return String.valueOf(ctx.getEffectiveMajorVersion());
083            }
084    
085            if ("effectiveMinorVersion".equals(key)) {
086                return String.valueOf(ctx.getEffectiveMinorVersion());
087            }
088    
089            if ("majorVersion".equals(key)) {
090                return String.valueOf(ctx.getMajorVersion());
091            }
092    
093            if ("minorVersion".equals(key)) {
094                return String.valueOf(ctx.getMinorVersion());
095            }
096    
097            if (ctx.getAttribute(key) != null) {
098                return ctx.getAttribute(key).toString();
099            }
100    
101            if (ctx.getInitParameter(key) != null) {
102                return ctx.getInitParameter(key);
103            }
104    
105            ctx.log(getClass().getName() + " unable to resolve key '" + key + '\'');
106            return null;
107        }
108    }