use of com.haulmont.cuba.core.sys.logging.LogFileNotFoundException in project cuba by cuba-platform.
the class LogControlImpl method getTail.
@Override
public String getTail(String fileName) throws LogControlException {
// security check, supported only valid file names
fileName = FilenameUtils.getName(fileName);
StringBuilder sb = new StringBuilder();
RandomAccessFile randomAccessFile = null;
try {
File logFile = new File(logDir, fileName);
if (!logFile.exists())
throw new LogFileNotFoundException(fileName);
randomAccessFile = new RandomAccessFile(logFile, "r");
long lengthFile = randomAccessFile.length();
if (lengthFile >= LOG_TAIL_AMOUNT_BYTES) {
randomAccessFile.seek(lengthFile - LOG_TAIL_AMOUNT_BYTES);
skipFirstLine(randomAccessFile);
}
while (randomAccessFile.read() != -1) {
randomAccessFile.seek(randomAccessFile.getFilePointer() - 1);
String line = readUtf8Line(randomAccessFile);
if (line != null) {
sb.append(line).append("\n");
}
}
} catch (IOException e) {
log.error("Error reading log file", e);
throw new LogControlException("Error reading log file: " + fileName);
} finally {
if (randomAccessFile != null) {
try {
randomAccessFile.close();
} catch (IOException ignored) {
}
}
}
return sb.toString();
}
use of com.haulmont.cuba.core.sys.logging.LogFileNotFoundException in project cuba by cuba-platform.
the class LogDownloadController method getLogFile.
@RequestMapping(value = "/log/{file:[a-zA-Z0-9\\.\\-_]+}", method = RequestMethod.GET)
public void getLogFile(HttpServletResponse response, @RequestParam(value = "s") String sessionId, @RequestParam(value = "full", required = false) Boolean downloadFull, @PathVariable(value = "file") String logFileName) throws IOException {
UserSession userSession = getSession(sessionId, response);
if (userSession == null)
return;
if (!userSession.isSpecificPermitted("cuba.gui.administration.downloadlogs")) {
response.sendError(HttpServletResponse.SC_FORBIDDEN);
return;
}
// security check, handle only valid file name
String filename = FilenameUtils.getName(logFileName);
try {
File logFile = logControl.getLogFile(filename);
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Pragma", "no-cache");
response.setDateHeader("Expires", 0);
response.setHeader("Content-Type", "application/zip");
response.setHeader("Pragma", "no-cache");
response.setHeader("Content-Disposition", "attachment; filename=" + filename);
OutputStream outputStream = null;
try {
outputStream = response.getOutputStream();
if (BooleanUtils.isTrue(downloadFull)) {
LogArchiver.writeArchivedLogToStream(logFile, outputStream);
} else {
LogArchiver.writeArchivedLogTailToStream(logFile, outputStream);
}
} catch (RuntimeException | IOException ex) {
log.error("Unable to download file", ex);
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
} finally {
IOUtils.closeQuietly(outputStream);
}
} catch (LogFileNotFoundException e) {
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
}
}
use of com.haulmont.cuba.core.sys.logging.LogFileNotFoundException in project cuba by cuba-platform.
the class LogDownloadController method getLogFile.
@RequestMapping(value = "/log/{file:[a-zA-Z0-9\\.\\-_]+}", method = RequestMethod.GET)
public void getLogFile(HttpServletResponse response, @RequestParam(value = "s") String sessionId, @RequestParam(value = "full", required = false) Boolean downloadFull, @PathVariable(value = "file") String logFileName) throws IOException {
UserSession userSession = getSession(sessionId, response);
if (userSession == null)
return;
if (!userSession.isSpecificPermitted("cuba.gui.administration.downloadlogs")) {
response.sendError(HttpServletResponse.SC_FORBIDDEN);
return;
}
// security check, handle only valid file name
String filename = FilenameUtils.getName(logFileName);
try {
File logFile = logControl.getLogFile(filename);
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Pragma", "no-cache");
response.setDateHeader("Expires", 0);
response.setHeader("Content-Type", "application/zip");
response.setHeader("Pragma", "no-cache");
response.setHeader("Content-Disposition", "attachment; filename=" + filename + ".zip");
OutputStream outputStream = null;
try {
outputStream = response.getOutputStream();
if (BooleanUtils.isTrue(downloadFull)) {
LogArchiver.writeArchivedLogToStream(logFile, outputStream);
} else {
LogArchiver.writeArchivedLogTailToStream(logFile, outputStream);
}
} catch (RuntimeException | IOException ex) {
log.error("Unable to download file", ex);
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
} finally {
IOUtils.closeQuietly(outputStream);
}
} catch (LogFileNotFoundException e) {
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
}
}
use of com.haulmont.cuba.core.sys.logging.LogFileNotFoundException in project cuba by cuba-platform.
the class LogDownloadController method getLogFile.
@RequestMapping(value = "/log/{file:[a-zA-Z0-9\\.\\-_]+}", method = RequestMethod.GET)
public void getLogFile(HttpServletResponse response, @RequestParam(value = "s") String sessionId, @RequestParam(value = "full", required = false) Boolean downloadFull, @PathVariable(value = "file") String logFileName) throws IOException {
UserSession userSession = getSession(sessionId, response);
if (userSession == null)
return;
if (!userSession.isSpecificPermitted("cuba.gui.administration.downloadlogs")) {
response.sendError(HttpServletResponse.SC_FORBIDDEN);
return;
}
// security check, handle only valid file name
String filename = FilenameUtils.getName(logFileName);
try {
File logFile = logControl.getLogFile(filename);
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Pragma", "no-cache");
response.setDateHeader("Expires", 0);
response.setHeader("Content-Type", "application/zip");
response.setHeader("Pragma", "no-cache");
response.setHeader("Content-Disposition", "attachment; filename=" + filename);
OutputStream outputStream = null;
try {
outputStream = response.getOutputStream();
if (BooleanUtils.isTrue(downloadFull)) {
LogArchiver.writeArchivedLogToStream(logFile, outputStream);
} else {
LogArchiver.writeArchivedLogTailToStream(logFile, outputStream);
}
} catch (RuntimeException | IOException ex) {
log.error("Unable to assemble zipped log file", ex);
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
} finally {
IOUtils.closeQuietly(outputStream);
}
} catch (LogFileNotFoundException e) {
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
}
}
Aggregations