001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.juneau.microservice.examples;
018
019import static org.apache.juneau.commons.utils.Shorts.*;
020import static org.apache.juneau.commons.utils.StringUtils.*;
021
022import java.io.*;
023import java.nio.charset.*;
024import java.nio.file.*;
025import java.time.*;
026import java.util.*;
027import java.util.stream.*;
028
029import org.apache.juneau.bean.*;
030import org.apache.juneau.commons.*;
031import org.apache.juneau.commons.bean.*;
032import org.apache.juneau.commons.time.*;
033import org.apache.juneau.commons.utils.*;
034import org.apache.juneau.config.*;
035import org.apache.juneau.http.*;
036import org.apache.juneau.http.Path;
037import org.apache.juneau.http.classic.response.*;
038import org.apache.juneau.marshall.html.*;
039import org.apache.juneau.microservice.logging.*;
040import org.apache.juneau.rest.server.*;
041import org.apache.juneau.rest.server.beans.*;
042import org.apache.juneau.rest.server.converter.*;
043import org.apache.juneau.rest.server.servlet.*;
044
045/**
046 * REST resource for viewing and accessing log files.
047 *
048 * <h2 class='section'>Routes</h2>
049 * <table class='styled'>
050 *    <caption>Log routes</caption>
051 *    <tr><th>Request</th><th>Does</th></tr>
052 *    <tr><td>{@code GET /logs/{path}}</td><td>Information on a file, or a directory listing</td></tr>
053 *    <tr><td>{@code GET /logs/view/{path}}</td><td>The file's contents as text, or as HTML with {@code ?highlight=true}</td></tr>
054 *    <tr><td>{@code GET /logs/parse/{path}}</td><td>The file's parsed log entries</td></tr>
055 *    <tr><td>{@code GET /logs/download/{path}}</td><td>The file's contents as {@code application/octet-stream}</td></tr>
056 *    <tr><td>{@code DELETE /logs/{path}}</td><td>Deletes the file or directory, when {@code Logging/allowDeletes} is on</td></tr>
057 * </table>
058 *
059 * <p>
060 * The {@code view}, {@code parse} and {@code download} prefixes are reserved: they always name an action, never a
061 * directory.  A log directory with one of those names cannot be listed through {@code GET /logs/view} and so on, but
062 * its files are still reachable through the action routes, for example {@code GET /logs/view/view/app.log}.  The
063 * listing's link to such a directory routes to the action and is answered with {@code 404}, as is any action on a
064 * directory.  {@code DELETE} reserves no prefix: {@code DELETE /logs/view/app.log} deletes {@code app.log} in the
065 * {@code view} directory.
066 *
067 * <p>
068 * Every read is a plain {@code GET}, and the {@code method} query parameter is not accepted, so no {@code GET} link
069 * can delete anything.  A delete must be a real {@code DELETE}, sent by a REST client or from the command line; the
070 * HTML listing has no delete link, because a browser link or form cannot carry the header-borne token a CSRF
071 * boundary requires.  A {@code DELETE} cannot be sent cross-site without a CORS preflight, so a boundary in front of
072 * this resource (see {@link AdminBoundary}) admits it without a token:
073 * <p class='bcode'>
074 *    curl -X DELETE http://localhost:10000/logs/old.log
075 * </p>
076 *
077 * <p>
078 * This resource has no authentication of its own.  Do not expose it on an untrusted network.
079 *
080 * <h2 class='section'>See Also:</h2><ul>
081 *    <li class='jc'>{@link AdminBoundary}
082 * </ul>
083 */
084@Rest(
085   path="/logs",
086   title="Log files",
087   description="Log files from this service"
088)
089@HtmlConfig(uriAnchorText="PROPERTY_NAME")
090@SuppressWarnings({
091   "resource" // Streams and parsers returned to servlet container or caller; lifecycle managed externally
092})
093public class LogsResource extends BasicRestServlet {
094
095   /** File action link. */
096   @Response(schema = @Schema(description = "File action"))
097   public static class Action extends LinkString {
098
099      /**
100       * Constructor.
101       *
102       * @param name Action name.
103       * @param uri Action URI.
104       * @param uriArgs URI format arguments.
105       */
106      public Action(String name, String uri, Object...uriArgs) {
107         super(name, uri, uriArgs);
108      }
109
110      /** {@inheritDoc} */
111      @Override /* Overridden from LinkString */
112      public Action setName(String value) {
113         super.setName(value);
114         return this;
115      }
116
117      /** {@inheritDoc} */
118      @Override /* Overridden from LinkString */
119      public Action setUri(java.net.URI value) {
120         super.setUri(value);
121         return this;
122      }
123
124      /** {@inheritDoc} */
125      @Override /* Overridden from LinkString */
126      public Action setUri(String value) {
127         super.setUri(value);
128         return this;
129      }
130
131      /** {@inheritDoc} */
132      @Override /* Overridden from LinkString */
133      public Action setUri(String value, Object...args) {
134         super.setUri(value, args);
135         return this;
136      }
137   }
138
139   /** File or directory details for REST response. */
140   @Response(schema = @Schema(description = "File or directory details"))
141   @BeanType(properties = "type,name,size,lastModified,actions,files")
142   public static class FileResource {
143      static final FileFilter FILE_FILTER = f2 -> f2.isDirectory() || f2.getName().endsWith(".log");
144      static final Comparator<FileResource> FILE_COMPARATOR = (o1, o2) -> {
145         int c = o1.getType().compareTo(o2.getType());
146         return c == 0 ? o1.getName().compareTo(o2.getName()) : c;
147      };
148      private final File f;
149      private final String path;
150
151      private final String uri;
152
153      private final boolean includeChildren;
154
155      /**
156       * Constructor.
157       *
158       * @param f The file or directory.
159       * @param path The path.
160       * @param includeChildren Whether to include child files.
161       */
162      public FileResource(File f, String path, boolean includeChildren) {
163         this.f = f;
164         this.path = path;
165         this.uri = "servlet:/" + or(path, "");
166         this.includeChildren = includeChildren;
167      }
168
169      /**
170       * Returns the read actions for this file: view, highlighted, parsed and download.
171       *
172       * <p>
173       * Every action is a plain {@code GET} link.  There is no delete action: a delete is a {@code DELETE} request,
174       * which a link cannot send.
175       *
176       * @return The list of actions.
177       */
178      @Html(format = HtmlFormat.HTML_CDC)
179      public List<Action> getActions() {
180         var l = new ArrayList<Action>();
181         if (f.canRead() && ! f.isDirectory()) {
182            var p = or(path, "");
183            l.add(new Action("view", "servlet:/view/" + p));
184            l.add(new Action("highlighted", "servlet:/view/" + p + "?highlight=true"));
185            l.add(new Action("parsed", "servlet:/parse/" + p));
186            l.add(new Action("download", "servlet:/download/" + p));
187         }
188         return l;
189      }
190
191      /**
192       * Returns child files when this is a directory and includeChildren is <jk>true</jk>.
193       *
194       * @return Child files, or <jk>null</jk> when this is a file or includeChildren is false.
195       */
196      @SuppressWarnings({
197         "java:S1168"     // Intentional null when file or !includeChildren.
198      })
199      public Set<FileResource> getFiles() {
200         if (f.isFile() || ! includeChildren)
201            return null;
202         return Arrays.stream(f.listFiles(FILE_FILTER))
203            .map(fc -> new FileResource(fc, (nn(path) ? (path + '/') : "") + urlEncode(fc.getName()), false))
204            .collect(Collectors.toCollection(() -> new TreeSet<>(FILE_COMPARATOR)));
205      }
206
207      /**
208       * Returns the last modified date of the file.
209       *
210       * @return The last modified date as an {@link Instant}.
211       */
212      public Instant getLastModified() { return Instant.ofEpochMilli(f.lastModified()); }
213
214      /**
215       * Returns the file name as a link.
216       *
217       * @return The name as a link.
218       */
219      public LinkString getName() { return new LinkString(f.getName(), uri); }
220
221      /**
222       * Returns the file size, or number of items for directories.
223       *
224       * @return The size.
225       */
226      public long getSize() { return f.isDirectory() ? f.listFiles().length : f.length(); }
227
228      /**
229       * Returns the type: "dir" or "file".
230       *
231       * @return The type.
232       */
233      public String getType() { return (f.isDirectory() ? "dir" : "file"); }
234   }
235
236   @Response(schema = @Schema(type = "string", format = "binary", description = "Contents of file"))
237   static class FileContents extends FileInputStream {
238
239      /**
240       * Constructor.
241       *
242       * @param file The file to read.
243       * @throws FileNotFoundException If the file is not found.
244       */
245      public FileContents(File file) throws FileNotFoundException {
246         super(file);
247      }
248   }
249
250   @Response(schema = @Schema(description = "Redirect to root page on success"))
251   static class RedirectToRoot extends SeeOtherRoot {}
252
253   private static final long serialVersionUID = 1L;
254
255   private static BufferedReader getReader(File f) throws IOException {
256      return new BufferedReader(new InputStreamReader(new FileInputStream(f), Charset.defaultCharset()));
257   }
258
259   /** Log directory path. Set once in @RestInit, shared by all requests. */
260   private static File logDir;
261
262   /** Log entry formatter. Set once in @RestInit, shared by all requests. */
263   private static LogEntryFormatter leFormatter;
264
265   /** Whether deletes are allowed. Set once in @RestInit, shared by all requests. */
266   private static boolean allowDeletes;
267
268   /**
269    * Deletes a log file on the file system.
270    *
271    * @param path The file path.
272    * @return Redirect to root on success.
273    * @throws MethodNotAllowed If deletes are not enabled.
274    */
275   @Mutating("Deletes log files")
276   @RestDelete(
277      path="/*",
278      summary="Delete log file",
279      description="Delete a log file on the file system."
280   )
281   public RedirectToRoot deleteFile(@Path("/*") String path) throws MethodNotAllowed {
282      deleteFile(getFile(path));
283      return new RedirectToRoot();
284   }
285   /**
286    * Downloads the contents of a file.
287    *
288    * @param res The response.
289    * @param path The file path.
290    * @return The file contents stream.
291    * @throws NotFound If the file is not found, or is a directory.
292    */
293   @RestGet(
294      path="/download/*",
295      summary="Download file",
296      description="Download the contents of a file.\nContent-Type is set to 'application/octet-stream'."
297   )
298   public FileContents downloadFile(RestResponse res, @Path("/*") String path) throws NotFound {
299      var f = getRegularFile(path);
300      res.setContentType("application/octet-stream");
301      try {
302         return new FileContents(f);
303      } catch (@SuppressWarnings("unused") FileNotFoundException e) {
304         throw new NotFound("File not found");
305      }
306   }
307
308   /**
309    * Returns information about the specified file or directory.
310    *
311    * @param req The request.
312    * @param path The file path.
313    * @return The file or directory details.
314    * @throws NotFound If the file is not found.
315    * @throws Exception If an error occurs.
316    */
317   @RestGet(
318      path="/*",
319      summary="View information on file or directory",
320      description="Returns information about the specified file or directory."
321   )
322   @HtmlDocConfig(
323      nav={"<h5>Folder:  ${RA:fullPath}</h5>"}
324   )
325   @SuppressWarnings({
326      "java:S112" // throws Exception intentional - callback/lifecycle method
327   })
328   public FileResource getFile(RestRequest req, @Path("/*") String path) throws NotFound, Exception {
329
330      var dir = getFile(path);
331      req.setAttribute("fullPath", dir.getAbsolutePath());
332
333      return new FileResource(dir, path, true);
334   }
335
336   /**
337    * Initializes log directory and formatter from config.
338    *
339    * @param config The microservice configuration.
340    */
341   @RestInit
342   @SuppressWarnings({
343      "java:S2696" // RestInit must be instance method; static fields shared across requests by design
344   })
345   public void init(Config config) {
346      logDir = new File(config.get("Logging/logDir").asString().orElse("logs"));
347      allowDeletes = config.get("Logging/allowDeletes").asBoolean().orElse(true);
348      leFormatter = new LogEntryFormatter(
349         config.get("Logging/format").asString().orElse("[{date} {level}] {msg}%n"),
350         config.get("Logging/dateFormat").asString().orElse("yyyy.MM.dd hh:mm:ss"),
351         config.get("Logging/useStackTraceHashes").asBoolean().orElse(true)
352      );
353   }
354
355   /**
356    * Views the contents of a log file with optional filtering.
357    *
358    * @param res The response.
359    * @param path The file path.
360    * @param highlight Add severity color highlighting.
361    * @param start Start timestamp filter.
362    * @param end End timestamp filter.
363    * @param thread Thread name filter.
364    * @param loggers Logger filter.
365    * @param severity Severity filter.
366    * @throws NotFound If the file is not found, or is a directory.
367    * @throws IOException If an I/O error occurs.
368    */
369   @RestGet(
370      path="/view/*",
371      summary="View contents of log file",
372      description="View the contents of a log file."
373   )
374   @SuppressWarnings({
375      "java:S107", // Method has many parameters; acceptable for builder/configuration methods
376      "java:S3776" // Cognitive complexity acceptable for this specific logic
377   })
378   public void viewFile(
379         RestResponse res,
380         @Path("/*") String path,
381         @Query(name="highlight", schema=@Schema(d="Add severity color highlighting.")) boolean highlight,
382         @Query(name="start", schema=@Schema(d="Start timestamp (ISO8601, full or partial).\nDon't print lines logged before the specified timestamp.\nUse any of the following formats: yyyy, yyyy-MM, yyyy-MM-dd, yyyy-MM-ddThh, yyyy-MM-ddThh:mm, yyyy-MM-ddThh:mm:ss, yyyy-MM-ddThh:mm:ss.SSS")) String start,
383         @Query(name="end", schema=@Schema(d="End timestamp (ISO8601, full or partial).\nDon't print lines logged after the specified timestamp.\nUse any of the following formats: yyyy, yyyy-MM, yyyy-MM-dd, yyyy-MM-ddThh, yyyy-MM-ddThh:mm, yyyy-MM-ddThh:mm:ss, yyyy-MM-ddThh:mm:ss.SSS")) String end,
384         @Query(name="thread", schema=@Schema(d="Thread name filter.\nOnly show log entries with the specified thread name.")) String thread,
385         @Query(name="loggers", schema=@Schema(d="Logger filter (simple class name).\nOnly show log entries if they were produced by one of the specified loggers.")) String[] loggers,
386         @Query(name="severity",schema=@Schema( d="Severity filter.\nOnly show log entries with the specified severity.")) String[] severity
387      ) throws NotFound, IOException {
388
389      var f = getRegularFile(path);
390
391      var startDate = o(start).filter(x1 -> ! isBlank(x1)).map(x2 -> GranularZonedDateTime.of(start).getZonedDateTime()).map(GregorianCalendar::from).map(Calendar::getTime).orElse(null);
392      var endDate = o(end).filter(x11 -> ! isBlank(x11)).map(x4 -> GranularZonedDateTime.of(end).getZonedDateTime()).map(GregorianCalendar::from).map(Calendar::getTime).orElse(null);
393
394      if (! highlight) {
395         var o = getReader(f, startDate, endDate, thread, loggers, severity);
396         res.setContentType("text/plain");
397         if (o instanceof Reader)
398            res.setContent(o);
399         else {
400            try (var p = (LogParser)o; Writer w = res.getNegotiatedWriter()) {
401               p.writeTo(w);
402            }
403         }
404         return;
405      }
406
407      res.setContentType("text/html");
408      try (var w = res.getNegotiatedWriter()) {
409         w.println("<html><body style='font-family:monospace;font-size:8pt;white-space:pre;'>");
410         try (var lp = getLogParser(f, startDate, endDate, thread, loggers, severity)) {
411            if (! lp.hasNext())
412               w.append("<span style='color:gray'>[EMPTY]</span>");
413            else
414               for (var le : lp) {
415                  var s = ie(le.severity) ? ' ' : le.severity.charAt(0);
416                  String color = "black";
417                  //SEVERE|WARNING|INFO|CONFIG|FINE|FINER|FINEST
418                  if (s == 'I')
419                     color = "#006400";
420                  else if (s == 'W')
421                     color = "#CC8400";
422                  else if (s == 'E' || s == 'S')
423                     color = "#DD0000";
424                  else if (s == 'D' || s == 'F' || s == 'T')
425                     color = "#000064";
426                  w.append("<span style='color:").append(color).append("'>");
427                  le.appendHtml(w).append("</span>");
428               }
429            w.append("</body></html>");
430         }
431      }
432   }
433   /**
434    * Views the parsed contents of a log file with filtering.
435    *
436    * @param req The request.
437    * @param path The file path.
438    * @param start Start timestamp filter.
439    * @param end End timestamp filter.
440    * @param thread Thread name filter.
441    * @param loggers Logger filter.
442    * @param severity Severity filter.
443    * @return The log parser iterable.
444    * @throws NotFound If the file is not found.
445    * @throws IOException If an I/O error occurs.
446    */
447   @RestGet(
448      path="/parse/*",
449      converters=Queryable.class,
450      summary="View parsed contents of file",
451      description="View the parsed contents of a file.",
452      swagger=@OpSwagger(
453         parameters={
454             Queryable.SWAGGER_PARAMS
455         }
456      )
457   )
458   @HtmlDocConfig(
459      nav={"<h5>Folder:  ${RA:fullPath}</h5>"}
460   )
461   public LogParser viewParsedEntries(
462         RestRequest req,
463         @Path("/*") String path,
464         @Query(name="start", schema=@Schema(d="Start timestamp (ISO8601, full or partial).\nDon't print lines logged before the specified timestamp.\nUse any of the following formats: yyyy, yyyy-MM, yyyy-MM-dd, yyyy-MM-ddThh, yyyy-MM-ddThh:mm, yyyy-MM-ddThh:mm:ss, yyyy-MM-ddThh:mm:ss.SSS")) String start,
465         @Query(name="end", schema=@Schema(d="End timestamp (ISO8601, full or partial).\nDon't print lines logged after the specified timestamp.\nUse any of the following formats: yyyy, yyyy-MM, yyyy-MM-dd, yyyy-MM-ddThh, yyyy-MM-ddThh:mm, yyyy-MM-ddThh:mm:ss, yyyy-MM-ddThh:mm:ss.SSS")) String end,
466         @Query(name="thread", schema=@Schema(d="Thread name filter.\nOnly show log entries with the specified thread name.")) String thread,
467         @Query(name="loggers", schema=@Schema(d="Logger filter (simple class name).\nOnly show log entries if they were produced by one of the specified loggers.")) String[] loggers,
468         @Query(name="severity", schema=@Schema(d="Severity filter.\nOnly show log entries with the specified severity.")) String[] severity
469      ) throws NotFound, IOException {
470
471      var f = getRegularFile(path);
472      req.setAttribute("fullPath", f.getAbsolutePath());
473
474      var startDate = o(start).filter(x1 -> ! isBlank(x1)).map(x2 -> GranularZonedDateTime.of(start).getZonedDateTime()).map(GregorianCalendar::from).map(Calendar::getTime).orElse(null);
475      var endDate = o(end).filter(x11 -> ! isBlank(x11)).map(x4 -> GranularZonedDateTime.of(end).getZonedDateTime()).map(GregorianCalendar::from).map(Calendar::getTime).orElse(null);
476
477      return getLogParser(f, startDate, endDate, thread, loggers, severity);
478   }
479
480   private void deleteFile(File f) {
481      if (! allowDeletes)
482         throw new MethodNotAllowed("DELETE not enabled");
483      if (f.isDirectory()) {
484         var files = f.listFiles();
485         if (nn(files)) {
486            for (var fc : files)
487               deleteFile(fc);
488         }
489      }
490      try {
491         Files.delete(f.toPath());
492      } catch (@SuppressWarnings("unused") IOException e) {
493         throw new Forbidden("Could not delete file %s", f.getAbsolutePath());
494      }
495   }
496
497   /**
498    * Resolves a request path against {@link #logDir} with a canonical-path boundary check
499    * so {@code ..} segments, absolute paths, and symlink-out cannot escape the configured root.
500    *
501    * <p>
502    * The check is intentionally applied at this single funnel because every public operation on
503    * this resource (view, parse, download, delete, info) resolves the user-supplied path through
504    * this method. Delegates to {@link FileUtils#resolveSafely(File, String)} — the canonical
505    * shared implementation; the caller-layer mapping is: boundary violation → 403 (Forbidden),
506    * non-existent target → 404 (NotFound).
507    */
508   private static File getFile(String path) throws NotFound {
509      try {
510         return FileUtils.resolveSafely(logDir, path)
511            .orElseThrow(() -> new NotFound("File not found."));
512      } catch (IllegalArgumentException e) {
513         throw new Forbidden(e.getMessage());
514      }
515   }
516
517   /**
518    * Resolves a request path as {@link #getFile(String)} does, and requires a file: the read actions have nothing to
519    * show for a directory.
520    */
521   private static File getRegularFile(String path) throws NotFound {
522      var f = getFile(path);
523      if (f.isDirectory())
524         throw new NotFound("File not found.");
525      return f;
526   }
527
528   private static LogParser getLogParser(File f, Date start, Date end, String thread, String[] loggers, String[] severity) throws IOException {
529      return new LogParser(leFormatter, f, start, end, thread, loggers, severity);
530   }
531
532   private static Object getReader(File f, Date start, Date end, String thread, String[] loggers, String[] severity) throws IOException {
533      if (start == null && end == null && thread == null && loggers == null)
534         return getReader(f);
535      return getLogParser(f, start, end, thread, loggers, severity);
536   }
537}