This is an automated email from the ASF dual-hosted git repository. juanpablo pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/jspwiki.git
commit 3d0c11c972f04c93549f0cdfcd1870eb76fb7bd8 Author: Jürgen Weber <[email protected]> AuthorDate: Sun Sep 8 16:20:56 2024 +0200 AttachmentServlet to Commons FileUpload 2 --- jspwiki-main/pom.xml | 11 ++++----- .../apache/wiki/attachment/AttachmentServlet.java | 27 +++++++++++----------- pom.xml | 14 +++++------ 3 files changed, 25 insertions(+), 27 deletions(-) diff --git a/jspwiki-main/pom.xml b/jspwiki-main/pom.xml index aeb8a6fec..6e07a4905 100644 --- a/jspwiki-main/pom.xml +++ b/jspwiki-main/pom.xml @@ -65,16 +65,15 @@ <artifactId>gson</artifactId> </dependency> -<!-- - <dependency> - <groupId>commons-fileupload</groupId> - <artifactId>commons-fileupload</artifactId> - </dependency> ---> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-fileupload2-javax</artifactId> </dependency> + + <dependency> + <groupId>org.apache.commons</groupId> + <artifactId>commons-fileupload2-jakarta-servlet6</artifactId> + </dependency> <dependency> <groupId>net.sourceforge</groupId> diff --git a/jspwiki-main/src/main/java/org/apache/wiki/attachment/AttachmentServlet.java b/jspwiki-main/src/main/java/org/apache/wiki/attachment/AttachmentServlet.java index 6759087b1..5093fc367 100644 --- a/jspwiki-main/src/main/java/org/apache/wiki/attachment/AttachmentServlet.java +++ b/jspwiki-main/src/main/java/org/apache/wiki/attachment/AttachmentServlet.java @@ -36,6 +36,7 @@ import org.apache.commons.fileupload2.core.FileItem; import org.apache.commons.fileupload2.core.FileItemFactory; import org.apache.commons.fileupload2.core.FileUploadException; import org.apache.commons.fileupload2.core.ProgressListener; +import org.apache.commons.fileupload2.jakarta.servlet6.JakartaServletFileUpload; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.wiki.api.core.Attachment; @@ -385,14 +386,14 @@ public class AttachmentServlet extends HttpServlet { final String errorPage = m_engine.getURL( ContextEnum.WIKI_ERROR.getRequestContext(), "", null ); // If something bad happened, Upload should be able to take care of most stuff String nextPage = errorPage; final String progressId = req.getParameter( "progressid" ); -/* + // Check that we have a file upload request - if( !ServletFileUpload.isMultipartContent(req) ) { + if( !JakartaServletFileUpload.isMultipartContent(req) ) { throw new RedirectException( "Not a file upload", errorPage ); } try { - final FileItemFactory factory = new DiskFileItemFactory(); + final FileItemFactory factory = DiskFileItemFactory.builder().get(); // Create the context _before_ Multipart operations, otherwise strict servlet containers may fail when setting encoding. final Context context = Wiki.context().create( m_engine, req, ContextEnum.PAGE_ATTACH.getRequestContext() ); @@ -400,8 +401,8 @@ public class AttachmentServlet extends HttpServlet { m_engine.getManager( ProgressManager.class ).startProgress( pl, progressId ); - final ServletFileUpload upload = new ServletFileUpload( factory ); - upload.setHeaderEncoding( StandardCharsets.UTF_8.name() ); + final JakartaServletFileUpload upload = new JakartaServletFileUpload( factory ); + upload.setHeaderCharset(StandardCharsets.UTF_8); if( !context.hasAdminPermissions() ) { upload.setFileSizeMax( m_maxSize ); } @@ -418,20 +419,20 @@ public class AttachmentServlet extends HttpServlet { switch( item.getFieldName() ) { case "page": // FIXME: Kludge alert. We must end up with the parent page name, if this is an upload of a new revision - wikipage = item.getString( StandardCharsets.UTF_8.name() ); + wikipage = item.getString( StandardCharsets.UTF_8); final int x = wikipage.indexOf( "/" ); if( x != -1 ) { wikipage = wikipage.substring( 0, x ); } break; case "changenote": - changeNote = item.getString( StandardCharsets.UTF_8.name() ); + changeNote = item.getString( StandardCharsets.UTF_8 ); if( changeNote != null ) { changeNote = TextUtil.replaceEntities( changeNote ); } break; case "nextpage": - nextPage = validateNextPage( item.getString( StandardCharsets.UTF_8.name() ), errorPage ); + nextPage = validateNextPage( item.getString( StandardCharsets.UTF_8 ), errorPage ); break; } } else { @@ -457,26 +458,24 @@ public class AttachmentServlet extends HttpServlet { LOG.warn( msg + " (attachment: " + attName + ")", e ); throw new IOException( msg ); - } catch( final IOException e ) { + } catch( final FileUploadException e ) { // Show the submit page again, but with a bit more intimidating output. msg = "Upload failure: " + e.getMessage(); LOG.warn( msg + " (attachment: " + attName + ")", e ); - throw e; - } catch( final FileUploadException e ) { + throw new IOException( msg, e ); + } catch( final IOException e ) { // Show the submit page again, but with a bit more intimidating output. msg = "Upload failure: " + e.getMessage(); LOG.warn( msg + " (attachment: " + attName + ")", e ); - throw new IOException( msg, e ); + throw e; } finally { m_engine.getManager( ProgressManager.class ).stopProgress( progressId ); // FIXME: In case of exceptions should absolutely remove the uploaded file. } return nextPage; - */ - return null; } /** diff --git a/pom.xml b/pom.xml index b85093ff3..87d0be515 100644 --- a/pom.xml +++ b/pom.xml @@ -311,19 +311,19 @@ <artifactId>flexmark-ext-toc</artifactId> <version>${flexmark.version}</version> </dependency> -<!-- - <dependency> - <groupId>commons-fileupload</groupId> - <artifactId>commons-fileupload</artifactId> - <version>${commons-fileupload.version}</version> - </dependency> ---> + <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-fileupload2-javax</artifactId> <version>2.0.0-M2</version> </dependency> +<dependency> + <groupId>org.apache.commons</groupId> + <artifactId>commons-fileupload2-jakarta-servlet6</artifactId> + <version>2.0.0-M2</version> +</dependency> + <dependency> <groupId>commons-httpclient</groupId>
