Search in sources :

Example 1 with LogFileNotFoundException

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();
}
Also used : LogFileNotFoundException(com.haulmont.cuba.core.sys.logging.LogFileNotFoundException) RandomAccessFile(java.io.RandomAccessFile) LogControlException(com.haulmont.cuba.core.sys.logging.LogControlException) IOException(java.io.IOException) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Example 2 with LogFileNotFoundException

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);
    }
}
Also used : LogFileNotFoundException(com.haulmont.cuba.core.sys.logging.LogFileNotFoundException) UserSession(com.haulmont.cuba.security.global.UserSession) OutputStream(java.io.OutputStream) IOException(java.io.IOException) File(java.io.File) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with LogFileNotFoundException

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);
    }
}
Also used : LogFileNotFoundException(com.haulmont.cuba.core.sys.logging.LogFileNotFoundException) UserSession(com.haulmont.cuba.security.global.UserSession) OutputStream(java.io.OutputStream) IOException(java.io.IOException) File(java.io.File) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with LogFileNotFoundException

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);
    }
}
Also used : LogFileNotFoundException(com.haulmont.cuba.core.sys.logging.LogFileNotFoundException) UserSession(com.haulmont.cuba.security.global.UserSession) OutputStream(java.io.OutputStream) IOException(java.io.IOException) File(java.io.File) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

LogFileNotFoundException (com.haulmont.cuba.core.sys.logging.LogFileNotFoundException)4 File (java.io.File)4 IOException (java.io.IOException)4 UserSession (com.haulmont.cuba.security.global.UserSession)3 OutputStream (java.io.OutputStream)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 LogControlException (com.haulmont.cuba.core.sys.logging.LogControlException)1 RandomAccessFile (java.io.RandomAccessFile)1