use of javax.servlet.MultipartConfigElement in project jetty.project by eclipse.
the class MultiPartInputStreamTest method testCharsetEncoding.
@Test
public void testCharsetEncoding() throws Exception {
String contentType = "multipart/form-data; boundary=TheBoundary; charset=ISO-8859-1";
String str = "--TheBoundary\r" + "content-disposition: form-data; name=\"field1\"\r" + "\r" + "\nJoe Blow\n" + "\r" + "--TheBoundary--\r";
MultipartConfigElement config = new MultipartConfigElement(_dirname, 1024, 3072, 50);
MultiPartInputStreamParser mpis = new MultiPartInputStreamParser(new ByteArrayInputStream(str.getBytes()), contentType, config, _tmpDir);
mpis.setDeleteOnExit(true);
Collection<Part> parts = mpis.getParts();
assertThat(parts.size(), is(1));
}
use of javax.servlet.MultipartConfigElement in project jetty.project by eclipse.
the class MultiPartConfigAnnotationHandler method doHandle.
/**
* @see org.eclipse.jetty.annotations.AnnotationIntrospector.AbstractIntrospectableAnnotationHandler#doHandle(java.lang.Class)
*/
public void doHandle(Class clazz) {
if (!Servlet.class.isAssignableFrom(clazz))
return;
MultipartConfig multi = (MultipartConfig) clazz.getAnnotation(MultipartConfig.class);
if (multi == null)
return;
MetaData metaData = _context.getMetaData();
//TODO: The MultipartConfigElement needs to be set on the ServletHolder's Registration.
//How to identify the correct Servlet? If the Servlet has no WebServlet annotation on it, does it mean that this MultipartConfig
//annotation applies to all declared instances in web.xml/programmatically?
//Assuming TRUE for now.
ServletHolder holder = getServletHolderForClass(clazz);
if (holder != null) {
Descriptor d = metaData.getOriginDescriptor(holder.getName() + ".servlet.multipart-config");
//let the annotation override it
if (d == null) {
metaData.setOrigin(holder.getName() + ".servlet.multipart-config", multi, clazz);
holder.getRegistration().setMultipartConfig(new MultipartConfigElement(multi));
}
}
}
use of javax.servlet.MultipartConfigElement in project jetty.project by eclipse.
the class MultiPartFilter method doFilter.
/* ------------------------------------------------------------------------------- */
/**
* @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest,
* javax.servlet.ServletResponse, javax.servlet.FilterChain)
*/
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest srequest = (HttpServletRequest) request;
if (srequest.getContentType() == null || !srequest.getContentType().startsWith("multipart/form-data")) {
chain.doFilter(request, response);
return;
}
String content_type = srequest.getContentType();
//Get current parameters so we can merge into them
MultiMap params = new MultiMap();
for (Map.Entry<String, String[]> entry : request.getParameterMap().entrySet()) {
Object value = entry.getValue();
if (value instanceof String[])
params.addValues(entry.getKey(), (String[]) value);
else
params.add(entry.getKey(), value);
}
MultipartConfigElement config = new MultipartConfigElement(tempdir.getCanonicalPath(), _maxFileSize, _maxRequestSize, _fileOutputBuffer);
MultiPartInputStreamParser mpis = new MultiPartInputStreamParser(request.getInputStream(), content_type, config, tempdir);
mpis.setDeleteOnExit(_deleteFiles);
mpis.setWriteFilesWithFilenames(_writeFilesWithFilenames);
request.setAttribute(MULTIPART, mpis);
try {
Collection<Part> parts = mpis.getParts();
if (parts != null) {
Iterator<Part> itor = parts.iterator();
while (itor.hasNext() && params.size() < _maxFormKeys) {
Part p = itor.next();
if (LOG.isDebugEnabled())
LOG.debug("{}", p);
MultiPartInputStreamParser.MultiPart mp = (MultiPartInputStreamParser.MultiPart) p;
if (mp.getFile() != null) {
request.setAttribute(mp.getName(), mp.getFile());
if (mp.getContentDispositionFilename() != null) {
params.add(mp.getName(), mp.getContentDispositionFilename());
if (mp.getContentType() != null)
params.add(mp.getName() + CONTENT_TYPE_SUFFIX, mp.getContentType());
}
} else {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
IO.copy(p.getInputStream(), bytes);
params.add(p.getName(), bytes.toByteArray());
if (p.getContentType() != null)
params.add(p.getName() + CONTENT_TYPE_SUFFIX, p.getContentType());
}
}
}
// handle request
chain.doFilter(new Wrapper(srequest, params), response);
} finally {
deleteFiles(request);
}
}
use of javax.servlet.MultipartConfigElement in project jetty.project by eclipse.
the class Request method getParts.
private Collection<Part> getParts(MultiMap<String> params) throws IOException, ServletException {
if (_multiPartInputStream == null)
_multiPartInputStream = (MultiPartInputStreamParser) getAttribute(__MULTIPART_INPUT_STREAM);
if (_multiPartInputStream == null) {
MultipartConfigElement config = (MultipartConfigElement) getAttribute(__MULTIPART_CONFIG_ELEMENT);
if (config == null)
throw new IllegalStateException("No multipart config for servlet");
_multiPartInputStream = new MultiPartInputStreamParser(getInputStream(), getContentType(), config, (_context != null ? (File) _context.getAttribute("javax.servlet.context.tempdir") : null));
setAttribute(__MULTIPART_INPUT_STREAM, _multiPartInputStream);
setAttribute(__MULTIPART_CONTEXT, _context);
//causes parsing
Collection<Part> parts = _multiPartInputStream.getParts();
ByteArrayOutputStream os = null;
for (Part p : parts) {
MultiPartInputStreamParser.MultiPart mp = (MultiPartInputStreamParser.MultiPart) p;
if (mp.getContentDispositionFilename() == null) {
// Servlet Spec 3.0 pg 23, parts without filename must be put into params.
String charset = null;
if (mp.getContentType() != null)
charset = MimeTypes.getCharsetFromContentType(mp.getContentType());
try (InputStream is = mp.getInputStream()) {
if (os == null)
os = new ByteArrayOutputStream();
IO.copy(is, os);
String content = new String(os.toByteArray(), charset == null ? StandardCharsets.UTF_8 : Charset.forName(charset));
if (_contentParameters == null)
_contentParameters = params == null ? new MultiMap<>() : params;
_contentParameters.add(mp.getName(), content);
}
os.reset();
}
}
}
return _multiPartInputStream.getParts();
}
use of javax.servlet.MultipartConfigElement in project jetty.project by eclipse.
the class ServletHolder method prepare.
/* ------------------------------------------------------------ */
/**
* Prepare to service a request.
*
* @param baseRequest the base request
* @param request the request
* @param response the response
* @throws ServletException if unable to prepare the servlet
* @throws UnavailableException if not available
*/
protected void prepare(Request baseRequest, ServletRequest request, ServletResponse response) throws ServletException, UnavailableException {
ensureInstance();
MultipartConfigElement mpce = ((Registration) getRegistration()).getMultipartConfig();
if (mpce != null)
baseRequest.setAttribute(Request.__MULTIPART_CONFIG_ELEMENT, mpce);
}
Aggregations