1 //
2 // ========================================================================
3 // Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4 // ------------------------------------------------------------------------
5 // All rights reserved. This program and the accompanying materials
6 // are made available under the terms of the Eclipse Public License v1.0
7 // and Apache License v2.0 which accompanies this distribution.
8 //
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
11 //
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
14 //
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
17 //
18
19
20 package org.eclipse.jetty.server;
21
22 import javax.servlet.ServletRequestEvent;
23 import javax.servlet.ServletRequestListener;
24
25 import org.eclipse.jetty.server.handler.ContextHandler;
26 import org.eclipse.jetty.util.MultiException;
27 import org.eclipse.jetty.util.MultiPartInputStreamParser;
28
29 public class MultiPartCleanerListener implements ServletRequestListener
30 {
31 public final static MultiPartCleanerListener INSTANCE = new MultiPartCleanerListener();
32
33 protected MultiPartCleanerListener()
34 {
35 }
36
37 @Override
38 public void requestDestroyed(ServletRequestEvent sre)
39 {
40 //Clean up any tmp files created by MultiPartInputStream
41 MultiPartInputStreamParser mpis = (MultiPartInputStreamParser)sre.getServletRequest().getAttribute(Request.__MULTIPART_INPUT_STREAM);
42 if (mpis != null)
43 {
44 ContextHandler.Context context = (ContextHandler.Context)sre.getServletRequest().getAttribute(Request.__MULTIPART_CONTEXT);
45
46 //Only do the cleanup if we are exiting from the context in which a servlet parsed the multipart files
47 if (context == sre.getServletContext())
48 {
49 try
50 {
51 mpis.deleteParts();
52 }
53 catch (MultiException e)
54 {
55 sre.getServletContext().log("Errors deleting multipart tmp files", e);
56 }
57 }
58 }
59 }
60
61 @Override
62 public void requestInitialized(ServletRequestEvent sre)
63 {
64 //nothing to do, multipart config set up by ServletHolder.handle()
65 }
66
67 }