Mercurial Hosting > luan
comparison src/org/eclipse/jetty/server/Request.java @ 859:3dcc52e17535
simplify multipart
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Wed, 21 Sep 2016 16:15:19 -0600 |
parents | fa6158f29c45 |
children | df84a1741687 |
comparison
equal
deleted
inserted
replaced
858:dbbd9c3f14f8 | 859:3dcc52e17535 |
---|---|
44 | 44 |
45 import javax.servlet.AsyncContext; | 45 import javax.servlet.AsyncContext; |
46 import javax.servlet.AsyncEvent; | 46 import javax.servlet.AsyncEvent; |
47 import javax.servlet.AsyncListener; | 47 import javax.servlet.AsyncListener; |
48 import javax.servlet.DispatcherType; | 48 import javax.servlet.DispatcherType; |
49 import javax.servlet.MultipartConfigElement; | |
50 import javax.servlet.RequestDispatcher; | 49 import javax.servlet.RequestDispatcher; |
51 import javax.servlet.ServletContext; | 50 import javax.servlet.ServletContext; |
52 import javax.servlet.ServletException; | 51 import javax.servlet.ServletException; |
53 import javax.servlet.ServletInputStream; | 52 import javax.servlet.ServletInputStream; |
54 import javax.servlet.ServletRequest; | 53 import javax.servlet.ServletRequest; |
87 import org.eclipse.jetty.util.AttributesMap; | 86 import org.eclipse.jetty.util.AttributesMap; |
88 import org.eclipse.jetty.util.IO; | 87 import org.eclipse.jetty.util.IO; |
89 import org.eclipse.jetty.util.LazyList; | 88 import org.eclipse.jetty.util.LazyList; |
90 import org.eclipse.jetty.util.MultiException; | 89 import org.eclipse.jetty.util.MultiException; |
91 import org.eclipse.jetty.util.MultiMap; | 90 import org.eclipse.jetty.util.MultiMap; |
92 import org.eclipse.jetty.util.MultiPartInputStream; | |
93 import org.eclipse.jetty.util.StringUtil; | 91 import org.eclipse.jetty.util.StringUtil; |
94 import org.eclipse.jetty.util.URIUtil; | 92 import org.eclipse.jetty.util.URIUtil; |
95 import org.eclipse.jetty.util.UrlEncoded; | 93 import org.eclipse.jetty.util.UrlEncoded; |
96 import org.slf4j.Logger; | 94 import org.slf4j.Logger; |
97 import org.slf4j.LoggerFactory; | 95 import org.slf4j.LoggerFactory; |
134 | 132 |
135 private static final String __ASYNC_FWD = "org.eclipse.asyncfwd"; | 133 private static final String __ASYNC_FWD = "org.eclipse.asyncfwd"; |
136 private static final Collection __defaultLocale = Collections.singleton(Locale.getDefault()); | 134 private static final Collection __defaultLocale = Collections.singleton(Locale.getDefault()); |
137 private static final int __NONE = 0, _STREAM = 1, __READER = 2; | 135 private static final int __NONE = 0, _STREAM = 1, __READER = 2; |
138 | 136 |
139 public static class MultiPartCleanerListener implements ServletRequestListener | |
140 { | |
141 | |
142 @Override | |
143 public void requestDestroyed(ServletRequestEvent sre) | |
144 { | |
145 //Clean up any tmp files created by MultiPartInputStream | |
146 MultiPartInputStream mpis = (MultiPartInputStream)sre.getServletRequest().getAttribute(__MULTIPART_INPUT_STREAM); | |
147 if (mpis != null) | |
148 { | |
149 ContextHandler.Context context = (ContextHandler.Context)sre.getServletRequest().getAttribute(__MULTIPART_CONTEXT); | |
150 | |
151 //Only do the cleanup if we are exiting from the context in which a servlet parsed the multipart files | |
152 if (context == sre.getServletContext()) | |
153 { | |
154 try | |
155 { | |
156 mpis.deleteParts(); | |
157 } | |
158 catch (MultiException e) | |
159 { | |
160 sre.getServletContext().log("Errors deleting multipart tmp files", e); | |
161 } | |
162 } | |
163 } | |
164 } | |
165 | |
166 @Override | |
167 public void requestInitialized(ServletRequestEvent sre) | |
168 { | |
169 //nothing to do, multipart config set up by ServletHolder.handle() | |
170 } | |
171 | |
172 } | |
173 | |
174 | 137 |
175 /* ------------------------------------------------------------ */ | 138 /* ------------------------------------------------------------ */ |
176 public static Request getRequest(HttpServletRequest request) | 139 public static Request getRequest(HttpServletRequest request) |
177 { | 140 { |
178 if (request instanceof Request) | 141 if (request instanceof Request) |
216 private long _timeStamp; | 179 private long _timeStamp; |
217 private long _dispatchTime; | 180 private long _dispatchTime; |
218 | 181 |
219 private Buffer _timeStampBuffer; | 182 private Buffer _timeStampBuffer; |
220 private HttpURI _uri; | 183 private HttpURI _uri; |
221 | |
222 private MultiPartInputStream _multiPartInputStream; //if the request is a multi-part mime | |
223 | 184 |
224 /* ------------------------------------------------------------ */ | 185 /* ------------------------------------------------------------ */ |
225 public Request() | 186 public Request() |
226 { | 187 { |
227 } | 188 } |
1387 if (_baseParameters != null) | 1348 if (_baseParameters != null) |
1388 _baseParameters.clear(); | 1349 _baseParameters.clear(); |
1389 _parameters = null; | 1350 _parameters = null; |
1390 _paramsExtracted = false; | 1351 _paramsExtracted = false; |
1391 _inputState = __NONE; | 1352 _inputState = __NONE; |
1392 | |
1393 _multiPartInputStream = null; | |
1394 } | 1353 } |
1395 | 1354 |
1396 /* ------------------------------------------------------------ */ | 1355 /* ------------------------------------------------------------ */ |
1397 /* | 1356 /* |
1398 * @see javax.servlet.ServletRequest#removeAttribute(java.lang.String) | 1357 * @see javax.servlet.ServletRequest#removeAttribute(java.lang.String) |
1832 } | 1791 } |
1833 | 1792 |
1834 /* ------------------------------------------------------------ */ | 1793 /* ------------------------------------------------------------ */ |
1835 public Part getPart(String name) throws IOException, ServletException | 1794 public Part getPart(String name) throws IOException, ServletException |
1836 { | 1795 { |
1837 getParts(); | 1796 throw new UnsupportedOperationException(); |
1838 return _multiPartInputStream.getPart(name); | |
1839 } | 1797 } |
1840 | 1798 |
1841 /* ------------------------------------------------------------ */ | 1799 /* ------------------------------------------------------------ */ |
1842 public Collection<Part> getParts() throws IOException, ServletException | 1800 public Collection<Part> getParts() throws IOException, ServletException |
1843 { | 1801 { |
1844 if (getContentType() == null || !getContentType().startsWith("multipart/form-data")) | 1802 throw new UnsupportedOperationException(); |
1845 throw new ServletException("Content-Type != multipart/form-data"); | |
1846 | |
1847 if (_multiPartInputStream == null) | |
1848 _multiPartInputStream = (MultiPartInputStream)getAttribute(__MULTIPART_INPUT_STREAM); | |
1849 | |
1850 if (_multiPartInputStream == null) | |
1851 { | |
1852 MultipartConfigElement config = (MultipartConfigElement)getAttribute(__MULTIPART_CONFIG_ELEMENT); | |
1853 | |
1854 if (config == null) | |
1855 throw new IllegalStateException("No multipart config for servlet"); | |
1856 | |
1857 _multiPartInputStream = new MultiPartInputStream(getInputStream(), | |
1858 getContentType(), config, | |
1859 (_context != null?(File)_context.getAttribute("javax.servlet.context.tempdir"):null)); | |
1860 | |
1861 setAttribute(__MULTIPART_INPUT_STREAM, _multiPartInputStream); | |
1862 setAttribute(__MULTIPART_CONTEXT, _context); | |
1863 Collection<Part> parts = _multiPartInputStream.getParts(); //causes parsing | |
1864 for (Part p:parts) | |
1865 { | |
1866 MultiPartInputStream.MultiPart mp = (MultiPartInputStream.MultiPart)p; | |
1867 if (mp.getContentDispositionFilename() == null) | |
1868 { | |
1869 //Servlet Spec 3.0 pg 23, parts without filenames must be put into init params | |
1870 String charset = null; | |
1871 if (mp.getContentType() != null) | |
1872 charset = MimeTypes.getCharsetFromContentType(new ByteArrayBuffer(mp.getContentType())); | |
1873 | |
1874 ByteArrayOutputStream os = null; | |
1875 InputStream is = mp.getInputStream(); //get the bytes regardless of being in memory or in temp file | |
1876 try | |
1877 { | |
1878 os = new ByteArrayOutputStream(); | |
1879 IO.copy(is, os); | |
1880 String content=new String(os.toByteArray(),charset==null?StringUtil.__UTF8:charset); | |
1881 getParameter(""); //cause params to be evaluated | |
1882 getParameters().add(mp.getName(), content); | |
1883 } | |
1884 finally | |
1885 { | |
1886 IO.close(os); | |
1887 IO.close(is); | |
1888 } | |
1889 } | |
1890 } | |
1891 } | |
1892 | |
1893 return _multiPartInputStream.getParts(); | |
1894 } | 1803 } |
1895 | 1804 |
1896 /* ------------------------------------------------------------ */ | 1805 /* ------------------------------------------------------------ */ |
1897 public void login(String username, String password) throws ServletException | 1806 public void login(String username, String password) throws ServletException |
1898 { | 1807 { |