2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.vid.controller;
23 import static java.nio.charset.StandardCharsets.UTF_8;
26 import java.io.IOException;
27 import java.util.List;
28 import java.util.Objects;
29 import java.util.function.Supplier;
30 import java.util.stream.Collectors;
31 import java.util.stream.Stream;
32 import javax.servlet.http.HttpServletRequest;
33 import javax.ws.rs.InternalServerErrorException;
34 import javax.ws.rs.NotAuthorizedException;
35 import org.apache.commons.io.input.ReversedLinesFileReader;
36 import org.apache.commons.lang3.StringUtils;
37 import org.onap.portalsdk.core.controller.RestrictedBaseController;
38 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
39 import org.onap.vid.model.ExceptionResponse;
40 import org.onap.vid.roles.Role;
41 import org.onap.vid.roles.RoleProvider;
42 import org.onap.vid.utils.Streams;
43 import org.springframework.beans.factory.annotation.Autowired;
44 import org.springframework.http.HttpStatus;
45 import org.springframework.web.bind.annotation.ExceptionHandler;
46 import org.springframework.web.bind.annotation.GetMapping;
47 import org.springframework.web.bind.annotation.PathVariable;
48 import org.springframework.web.bind.annotation.RequestMapping;
49 import org.springframework.web.bind.annotation.RequestParam;
50 import org.springframework.web.bind.annotation.ResponseStatus;
51 import org.springframework.web.bind.annotation.RestController;
55 @RequestMapping("logger")
56 public class LoggerController extends RestrictedBaseController {
58 private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(LoggerController.class);
59 public static final String VID_IS_STARTED = "VID is started";
60 private RoleProvider roleProvider;
61 private LogfilePathCreator logfilePathCreator;
64 public LoggerController(RoleProvider roleProvider, LogfilePathCreator logfilePathCreator) {
65 this.roleProvider = roleProvider;
66 this.logfilePathCreator = logfilePathCreator;
69 @GetMapping(value = "/{loggerName:audit|audit2019|error|metrics|metrics2019}")
70 public String getLog(@PathVariable String loggerName, HttpServletRequest request,
71 @RequestParam(value="limit", defaultValue = "5000") Integer limit) throws IOException {
73 List<Role> roles = roleProvider.getUserRoles(request);
74 boolean userPermitted = roleProvider.userPermissionIsReadLogs(roles);
76 throw new NotAuthorizedException("User not authorized to get logs");
79 String logfilePath = logfilePathCreator.getLogfilePath(loggerName);
80 try (final ReversedLinesFileReader reader = new ReversedLinesFileReader(new File(logfilePath), UTF_8)) {
81 Supplier<String> reverseLinesSupplier = () -> {
83 return reader.readLine();
84 } catch (NullPointerException e) {
87 } catch (IOException e) {
88 throw new InternalServerErrorException("error while reading " + logfilePath, e);
92 return Streams.takeWhile(
93 Stream.generate(reverseLinesSupplier),
94 line -> !StringUtils.contains(line, "Logging is started") &&
95 !StringUtils.contains(line, VID_IS_STARTED))
98 .filter(Objects::nonNull)
99 .collect(Collectors.joining("\n"));
103 @ExceptionHandler({ NotAuthorizedException.class })
104 @ResponseStatus(HttpStatus.UNAUTHORIZED)
105 public String notAuthorizedHandler(NotAuthorizedException e) {
106 return "UNAUTHORIZED";
109 @ExceptionHandler({ IOException.class, InternalServerErrorException.class })
110 @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
111 public ExceptionResponse ioExceptionHandler(Exception e) {
112 return ControllersUtils.handleException(e, LOGGER);