001/**
002 * Copyright (C) 2006-2020 Talend Inc. - www.talend.com
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 */
016package org.talend.sdk.component.server.configuration;
017
018import java.util.EnumSet;
019import java.util.Set;
020
021import javax.servlet.DispatcherType;
022import javax.servlet.FilterRegistration;
023import javax.servlet.ServletContainerInitializer;
024import javax.servlet.ServletContext;
025import javax.servlet.ServletException;
026
027import org.talend.sdk.component.server.mdc.MdcRequestBinder;
028
029import lombok.extern.slf4j.Slf4j;
030
031@Slf4j
032public class MdcInitializer implements ServletContainerInitializer {
033
034    @Override
035    public void onStartup(final Set<Class<?>> c, final ServletContext ctx) throws ServletException {
036        try {
037            ctx.getClassLoader().loadClass("org.apache.logging.log4j.web.ServletRequestThreadContext");
038        } catch (final ClassNotFoundException e) {
039            log.debug("log4j-web not available, skipping MDC setup");
040            return;
041        }
042        final FilterRegistration.Dynamic filter = ctx.addFilter("mdc-request-binder", MdcRequestBinder.class);
043        filter.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), false, "/*");
044        filter.setAsyncSupported(true);
045    }
046}