View Javadoc
1   /*
2    * Licensed under the GPL License. You may not use this file except in compliance with the License.
3    * You may obtain a copy of the License at
4    *
5    *   https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
6    *
7    * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
8    * WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
9    * PURPOSE.
10   */
11  package psiprobe.controllers.logs;
12  
13  import java.io.File;
14  import java.util.LinkedList;
15  
16  import javax.servlet.http.HttpServletRequest;
17  import javax.servlet.http.HttpServletResponse;
18  
19  import org.springframework.beans.factory.annotation.Value;
20  import org.springframework.stereotype.Controller;
21  import org.springframework.web.bind.ServletRequestUtils;
22  import org.springframework.web.bind.annotation.RequestMapping;
23  import org.springframework.web.servlet.ModelAndView;
24  
25  import psiprobe.tools.BackwardsFileStream;
26  import psiprobe.tools.BackwardsLineReader;
27  import psiprobe.tools.logging.LogDestination;
28  
29  /**
30   * The Class FollowController.
31   */
32  @Controller
33  public class FollowController extends AbstractLogHandlerController {
34  
35    @RequestMapping(path = "/follow.ajax")
36    @Override
37    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
38        throws Exception {
39      return super.handleRequest(request, response);
40    }
41  
42    @Override
43    protected ModelAndView handleLogFile(HttpServletRequest request, HttpServletResponse response,
44        LogDestination logDest) throws Exception {
45  
46      ModelAndView mv = new ModelAndView(getViewName());
47      File file = logDest.getFile();
48  
49      if (file.exists()) {
50        LinkedList<String> lines = new LinkedList<>();
51        long actualLength = file.length();
52        long lastKnownLength = ServletRequestUtils.getLongParameter(request, "lastKnownLength", 0);
53        long currentLength =
54            ServletRequestUtils.getLongParameter(request, "currentLength", actualLength);
55        long maxReadLines = ServletRequestUtils.getLongParameter(request, "maxReadLines", 0);
56  
57        if (lastKnownLength > currentLength || lastKnownLength > actualLength
58            || currentLength > actualLength) {
59  
60          // file length got reset
61          lastKnownLength = 0;
62          lines.add(" ------------- THE FILE HAS BEEN TRUNCATED --------------");
63        }
64  
65        try (BackwardsFileStream bfs = new BackwardsFileStream(file, currentLength)) {
66          BackwardsLineReader br;
67          if (logDest.getEncoding() != null) {
68            br = new BackwardsLineReader(bfs, logDest.getEncoding());
69          } else {
70            br = new BackwardsLineReader(bfs);
71          }
72          long readSize = 0;
73          long totalReadSize = currentLength - lastKnownLength;
74          String line;
75          while (readSize < totalReadSize && (line = br.readLine()) != null) {
76            if (!line.isEmpty()) {
77              lines.addFirst(line);
78              readSize += line.length();
79            } else {
80              readSize++;
81            }
82            if (maxReadLines != 0 && lines.size() >= maxReadLines) {
83              break;
84            }
85          }
86  
87          if (lastKnownLength != 0 && readSize > totalReadSize) {
88            lines.removeFirst();
89          }
90        }
91  
92        mv.addObject("lines", lines);
93      }
94      return mv;
95    }
96  
97    @Value("ajax/follow")
98    @Override
99    public void setViewName(String viewName) {
100     super.setViewName(viewName);
101   }
102 
103 }