Skip to content

Commit

Permalink
Upgrade Commons FileUpload from 1.5 to 2.0.0-M2
Browse files Browse the repository at this point in the history
  • Loading branch information
basil committed May 10, 2024
1 parent 2610e43 commit 681658b
Show file tree
Hide file tree
Showing 10 changed files with 163 additions and 79 deletions.
44 changes: 38 additions & 6 deletions bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ THE SOFTWARE.
<description>The module contains dependencies that are used by a specific Jenkins version</description>

<properties>
<commons-fileupload2.version>2.0.0-M2</commons-fileupload2.version>
<slf4jVersion>2.0.13</slf4jVersion>
<stapler.version>1860.vb_89682cf8d96</stapler.version>
<!-- TODO https://github.com/jenkinsci/stapler/pull/542 -->
<stapler.version>1861.v1e935e64198a_</stapler.version>
<groovy.version>2.4.21</groovy.version>
</properties>

Expand Down Expand Up @@ -119,11 +121,6 @@ THE SOFTWARE.
<artifactId>commons-collections</artifactId>
<version>3.2.2</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.5</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
Expand Down Expand Up @@ -189,6 +186,41 @@ THE SOFTWARE.
<artifactId>commons-compress</artifactId>
<version>1.26.1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-fileupload2</artifactId>
<version>${commons-fileupload2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-fileupload2-core</artifactId>
<version>${commons-fileupload2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-fileupload2-distribution</artifactId>
<version>${commons-fileupload2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-fileupload2-jakarta-servlet5</artifactId>
<version>${commons-fileupload2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-fileupload2-jakarta-servlet6</artifactId>
<version>${commons-fileupload2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-fileupload2-javax</artifactId>
<version>${commons-fileupload2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-fileupload2-portlet</artifactId>
<version>${commons-fileupload2.version}</version>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
Expand Down
12 changes: 8 additions & 4 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,6 @@ THE SOFTWARE.
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
Expand Down Expand Up @@ -284,6 +280,14 @@ THE SOFTWARE.
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-fileupload2-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-fileupload2-javax</artifactId>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
Expand Down
12 changes: 10 additions & 2 deletions core/src/main/java/hudson/FilePath.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@
import jenkins.util.VirtualFile;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload2.core.FileItem;
import org.apache.commons.io.input.CountingInputStream;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DirectoryScanner;
Expand Down Expand Up @@ -1166,7 +1166,7 @@ public void copyFrom(FilePath src) throws IOException, InterruptedException {
public void copyFrom(FileItem file) throws IOException, InterruptedException {
if (channel == null) {
try {
file.write(new File(remote));
file.write(Paths.get(remote));
} catch (IOException e) {
throw e;
} catch (Exception e) {
Expand All @@ -1180,6 +1180,14 @@ public void copyFrom(FileItem file) throws IOException, InterruptedException {
}
}

/**
* @deprecated use {@link #copyFrom(FileItem)}
*/
@Deprecated
public void copyFrom(org.apache.commons.fileupload.FileItem file) throws IOException, InterruptedException {
copyFrom(file.toFileUpload2FileItem());
}

/**
* Code that gets executed on the machine where the {@link FilePath} is local.
* Used to act on {@link FilePath}.
Expand Down
24 changes: 15 additions & 9 deletions core/src/main/java/hudson/PluginManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,12 @@
import jenkins.util.xml.RestrictiveEntityResolver;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload2.core.DiskFileItem;
import org.apache.commons.fileupload2.core.DiskFileItemFactory;
import org.apache.commons.fileupload2.core.FileItem;
import org.apache.commons.fileupload2.core.FileUploadException;
import org.apache.commons.fileupload2.javax.JavaxServletDiskFileUpload;
import org.apache.commons.fileupload2.javax.JavaxServletFileUpload;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
Expand Down Expand Up @@ -1832,13 +1834,17 @@ static class FileUploadPluginCopier implements PluginCopier {
}

@Override
public void copy(File target) throws Exception {
fileItem.write(target);
public void copy(File target) throws IOException {
fileItem.write(Util.fileToPath(target));
}

@Override
public void cleanup() {
fileItem.delete();
try {
fileItem.delete();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
}

Expand Down Expand Up @@ -1873,8 +1879,8 @@ public HttpResponse doUploadPlugin(StaplerRequest req) throws IOException, Servl
String fileName = "";
PluginCopier copier;
File tmpDir = Files.createTempDirectory("uploadDir").toFile();
ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory(DiskFileItemFactory.DEFAULT_SIZE_THRESHOLD, tmpDir));
List<FileItem> items = upload.parseRequest(req);
JavaxServletFileUpload<DiskFileItem, DiskFileItemFactory> upload = new JavaxServletDiskFileUpload(DiskFileItemFactory.builder().setFile(tmpDir).get());
List<DiskFileItem> items = upload.parseRequest(req);
String string = items.get(1).getString();
if (string != null && !string.isBlank()) {
// this is a URL deployment
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import java.util.Objects;
import javax.servlet.ServletException;
import net.sf.json.JSONObject;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload2.core.FileItem;
import org.apache.commons.io.FileUtils;
import org.jenkinsci.Symbol;
import org.kohsuke.stapler.DataBoundConstructor;
Expand Down
54 changes: 35 additions & 19 deletions core/src/main/java/hudson/model/FileParameterValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

package hudson.model;

import com.infradna.tool.bridge_method_injector.WithBridgeMethods;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import hudson.EnvVars;
import hudson.FilePath;
Expand All @@ -36,15 +37,16 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UncheckedIOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.regex.Pattern;
import jenkins.util.SystemProperties;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemHeaders;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.apache.commons.fileupload.util.FileItemHeadersImpl;
import org.apache.commons.fileupload2.core.DiskFileItem;
import org.apache.commons.fileupload2.core.FileItem;
import org.apache.commons.fileupload2.core.FileItemFactory;
import org.apache.commons.fileupload2.core.FileItemHeaders;
import org.apache.commons.fileupload2.core.FileItemHeadersProvider;
import org.apache.commons.io.FilenameUtils;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
Expand Down Expand Up @@ -146,10 +148,15 @@ public String getOriginalFileName() {
return originalFileName;
}

@WithBridgeMethods(value = org.apache.commons.fileupload.FileItem.class, adapterMethod = "fromFileUpload2FileItem")
public FileItem getFile() {
return file;
}

private Object fromFileUpload2FileItem(FileItem fileItem, Class<?> type) {
return org.apache.commons.fileupload.FileItem.fromFileUpload2FileItem(fileItem);
}

@Override
public BuildWrapper createBuildWrapper(AbstractBuild<?, ?> build) {
return new BuildWrapper() {
Expand Down Expand Up @@ -294,8 +301,12 @@ public byte[] get() {
}

@Override
public String getString(String encoding) throws UnsupportedEncodingException {
return new String(get(), encoding);
public String getString(Charset toCharset) throws IOException {
try {
return new String(get(), toCharset);
} catch (UncheckedIOException e) {
throw e.getCause();
}
}

@Override
Expand All @@ -304,17 +315,19 @@ public String getString() {
}

@Override
public void write(File to) throws Exception {
new FilePath(file).copyTo(new FilePath(to));
public FileItem write(Path to) throws IOException {
try {
new FilePath(file).copyTo(new FilePath(to.toFile()));
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
return this;
}

@Override
public void delete() {
try {
Files.deleteIfExists(file.toPath());
} catch (IOException e) {
throw new UncheckedIOException(e);
}
public FileItem delete() throws IOException {
Files.deleteIfExists(Util.fileToPath(file));
return this;
}

@Override
Expand All @@ -323,7 +336,8 @@ public String getFieldName() {
}

@Override
public void setFieldName(String name) {
public FileItem setFieldName(String name) {
return this;
}

@Override
Expand All @@ -332,7 +346,8 @@ public boolean isFormField() {
}

@Override
public void setFormField(boolean state) {
public FileItem setFormField(boolean state) {
return this;
}

@Override
Expand All @@ -343,11 +358,12 @@ public OutputStream getOutputStream() throws IOException {

@Override
public FileItemHeaders getHeaders() {
return new FileItemHeadersImpl();
return FileItemFactory.AbstractFileItemBuilder.newFileItemHeaders();
}

@Override
public void setHeaders(FileItemHeaders headers) {
public FileItemHeadersProvider setHeaders(FileItemHeaders headers) {
return this;
}
}
}
Loading

0 comments on commit 681658b

Please sign in to comment.