1717package org .labkey .api .view ;
1818
1919import org .apache .commons .lang3 .StringUtils ;
20- import org .apache .logging .log4j .LogManager ;
2120import org .apache .logging .log4j .Logger ;
21+ import org .labkey .api .data .Container ;
22+ import org .labkey .api .data .ContainerManager ;
23+ import org .labkey .api .usageMetrics .SimpleMetricsService ;
2224import org .labkey .api .util .PageFlowUtil ;
2325import org .labkey .api .util .URLHelper ;
2426
2830import jakarta .servlet .http .HttpServlet ;
2931import jakarta .servlet .http .HttpServletRequest ;
3032import jakarta .servlet .http .HttpServletResponse ;
33+ import org .labkey .api .util .logging .LogHelper ;
34+
3135import java .io .IOException ;
3236
3337/**
3438 * Must keep ActionURL in sync.
3539 */
3640public class FileServlet extends HttpServlet
3741{
38- private static final Logger _log = LogManager .getLogger (FileServlet .class );
42+ private static final Logger _log = LogHelper .getLogger (FileServlet .class , "Forwards requests from /files to the FileContent module" );
3943
4044 @ Override
4145 protected void service (HttpServletRequest request , HttpServletResponse response )
4246 throws ServletException , IOException
4347 {
4448 String pathInfo = StringUtils .trimToEmpty (request .getPathInfo ());
45- int index = pathInfo .lastIndexOf ("/@" ); // new style URL's: /files/<container>/@files/<path>/<name>
49+ int index = pathInfo .lastIndexOf ("/@" ); // new style URL's: /files/<container>/@files/<path>/<name> or /files/<container>/@files/<path>/?fileName= <name>
4650 if (index < 0 )
47- index = pathInfo .lastIndexOf ('/' ); // legacy style: /files/<container>/<name>
51+ index = pathInfo .lastIndexOf ('/' ); // legacy style: /files/<container>/<name> or /files/<container>/?fileName= <name>
4852
4953 if (index < 0 )
5054 {
5155 response .setStatus (HttpServletResponse .SC_NOT_FOUND );
5256 return ;
5357 }
54- //pathInfo is /${extraPath}/${fileName}
58+ //pathInfo is /<container>>/<name>
5559 String fileNameParam = StringUtils .trimToNull (request .getParameter ("fileName" ));
5660 String fileName = pathInfo .substring (index + 1 );
57- String extraPath = pathInfo .substring (0 , index );
61+ String containerPath = pathInfo .substring (0 , index );
62+ Container c = ContainerManager .getForPath (containerPath );
63+ if (c == null )
64+ {
65+ response .setStatus (HttpServletResponse .SC_NOT_FOUND );
66+ return ;
67+ }
5868
5969 // Store the original URL in case we need to redirect for authentication
6070 if (request .getAttribute (ViewServlet .ORIGINAL_URL_STRING ) == null )
@@ -64,7 +74,9 @@ protected void service(HttpServletRequest request, HttpServletResponse response)
6474 request .setAttribute (ViewServlet .ORIGINAL_URL_URLHELPER , helper );
6575 }
6676
67- String dispatchUrl = extraPath + "/filecontent-sendFile.view?" + (null == fileNameParam ? "fileName=" + PageFlowUtil .encodeURIComponent (fileName ) : "" );
77+ SimpleMetricsService .get ().increment ("API" , "FileServlet" , "urlsDispatched" );
78+ String dispatchUrl = containerPath + "/filecontent-sendFile.view?" + (null == fileNameParam ? "fileName=" + PageFlowUtil .encodeURIComponent (fileName ) : "" );
79+ _log .info ("FileServlet dispatching " + request .getRequestURL () + " to " + dispatchUrl );
6880 // NOTE other parameters seem to get magically propagated...
6981 RequestDispatcher r = request .getRequestDispatcher (dispatchUrl );
7082 r .forward (request , response );
0 commit comments