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 */
017package 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
021import javax.servlet.ServletContext;
022
023import org.apache.logging.log4j.core.LogEvent;
024import org.apache.logging.log4j.core.config.plugins.Plugin;
025import org.apache.logging.log4j.core.lookup.AbstractLookup;
026import org.apache.logging.log4j.util.Strings;
027
028@Plugin(name = "web", category = "Lookup")
029public class WebLookup extends AbstractLookup {
030    private static final String ATTR_PREFIX = "attr.";
031    private static final String INIT_PARAM_PREFIX = "initParam.";
032
033    @Override
034    public String lookup(final LogEvent event, final String key) {
035        final ServletContext ctx = WebLoggerContextUtils.getServletContext();
036        if (ctx == null) {
037            return null;
038        }
039
040        if (key.startsWith(ATTR_PREFIX)) {
041            final String attrName = key.substring(ATTR_PREFIX.length());
042            final Object attrValue = ctx.getAttribute(attrName);
043            return attrValue == null ? null : attrValue.toString();
044        }
045
046        if (key.startsWith(INIT_PARAM_PREFIX)) {
047            final String paramName = key.substring(INIT_PARAM_PREFIX.length());
048            return ctx.getInitParameter(paramName);
049        }
050
051        if ("rootDir".equals(key)) {
052            final String root = ctx.getRealPath("/");
053            if (root == null) {
054                final String msg = "Failed to resolve web:rootDir -- " +
055                        "servlet container unable to translate virtual path " +
056                        " to real path (probably not deployed as exploded";
057                throw new IllegalStateException(msg);
058            }
059            return root;
060        }
061
062        if ("contextPath".equals(key)) {
063            return ctx.getContextPath();
064        }
065
066        if ("servletContextName".equals(key)) {
067            return ctx.getServletContextName();
068        }
069
070        if ("serverInfo".equals(key)) {
071            return ctx.getServerInfo();
072        }
073
074        if ("effectiveMajorVersion".equals(key)) {
075            return String.valueOf(ctx.getEffectiveMajorVersion());
076        }
077
078        if ("effectiveMinorVersion".equals(key)) {
079            return String.valueOf(ctx.getEffectiveMinorVersion());
080        }
081
082        if ("majorVersion".equals(key)) {
083            return String.valueOf(ctx.getMajorVersion());
084        }
085
086        if ("minorVersion".equals(key)) {
087            return String.valueOf(ctx.getMinorVersion());
088        }
089
090        if (ctx.getAttribute(key) != null) {
091            return ctx.getAttribute(key).toString();
092        }
093
094        if (ctx.getInitParameter(key) != null) {
095            return ctx.getInitParameter(key);
096        }
097
098        ctx.log(getClass().getName() + " unable to resolve key " + Strings.quote(key));
099        return null;
100    }
101}