Java tutorial
/* * Copyright 2014 Andrew Tytula. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.kajj.tools.logviewer; import java.io.IOException; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.ResourceAccessException; /** * Presentation layer for the log viewer. * * @author Andrew Tytula */ @RestController public class LogViewerRestController { @Autowired private LogRepository logRepository; /** * Returns the last <code>numberOfLines</code> from the specified log file. The log file must * be in the containers log directory to be read. * * @param fileName the name of the log file. * @param numberOfLines the number of lines to return in the tail. * @return The last <code>numberOfLines</code> of the specified log file. */ @RequestMapping(value = "/logs/{fileName}/tail", method = RequestMethod.GET, produces = MediaType.TEXT_PLAIN_VALUE) public ResponseEntity<String> tailLog(@PathVariable final String fileName, @RequestParam(value = "lines", defaultValue = "100") final int numberOfLines) { try { final List<String> logs = logRepository.getTailLog(fileName, numberOfLines); final StringBuilder tail = new StringBuilder(); int i = 1; for (final String log : logs) { tail.append(i++).append(": ").append(log).append("\n"); } return new ResponseEntity(tail.toString(), HttpStatus.OK); } catch (final IOException ioe) { throw new ResourceAccessException("Unable to read log file", ioe); } } @RequestMapping(value = "/logs", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity<List<String>> getlogFileNames() { final List<String> logFileNames = logRepository.getLogFileNames(); return new ResponseEntity(logFileNames, HttpStatus.OK); } }