Fix for Penetration test _ Session and cookie management
[vid.git] / vid-app-common / src / main / java / org / onap / vid / controller / LogfilePathCreator.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.vid.controller;
22
23 import ch.qos.logback.classic.LoggerContext;
24 import ch.qos.logback.core.Appender;
25 import ch.qos.logback.core.FileAppender;
26 import ch.qos.logback.core.spi.AppenderAttachable;
27 import com.att.eelf.configuration.Configuration;
28 import java.util.stream.Stream;
29 import javax.ws.rs.InternalServerErrorException;
30 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
31 import org.onap.vid.utils.Streams;
32 import org.slf4j.LoggerFactory;
33 import org.springframework.stereotype.Component;
34
35 @Component
36 public final class LogfilePathCreator {
37
38     private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(LogfilePathCreator.class);
39
40     String getLogfilePath(String loggerName) {
41         /*
42         Find the requested logger, and pull all of it's appenders.
43         Find the first of the appenders that is a FileAppender, and return it's
44         write-out filename.
45          */
46         LOGGER.debug("Searching for logfile path with loggerName: ", loggerName);
47         LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();;
48         return context.getLoggerList().stream()
49             .filter(logger -> logger.getName().equals(Configuration.GENERAL_LOGGER_NAME + "." + loggerName))
50             .flatMap(this::pullSubAppenders)
51             .flatMap(appender -> {
52                 // Appender might be "attachable", if so - roll-up its sub-appenders
53                 return (appender instanceof AppenderAttachable) ?
54                     pullSubAppenders((AppenderAttachable<?>) appender) : Stream.of(appender);
55             })
56             .filter(appender -> appender instanceof FileAppender)
57             .map(appender -> (FileAppender<?>) appender)
58             .map(FileAppender::getFile)
59             .findFirst()
60             .orElseThrow(() -> new InternalServerErrorException("logfile for " + loggerName + " not found"));
61     }
62
63     private <T> Stream<Appender<T>> pullSubAppenders(AppenderAttachable<T> logger) {
64         return Streams.fromIterator(logger.iteratorForAppenders());
65     }
66 }