Skip to content

Commit

Permalink
GH-4374: Eliminate commons-fileupload dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
erikgb committed Feb 25, 2023
1 parent 6c4ba11 commit 19c7f3c
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 35 deletions.
5 changes: 0 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -391,11 +391,6 @@
<artifactId>commons-cli</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
Expand Down
5 changes: 0 additions & 5 deletions tools/workbench/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,6 @@
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<!-- Required for CommonsMultipartResolver from the Spring Framework -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileUploadException;
import org.eclipse.rdf4j.model.Resource;
import org.eclipse.rdf4j.query.QueryResultHandlerException;
import org.eclipse.rdf4j.repository.RepositoryConnection;
Expand All @@ -41,7 +40,7 @@ public class AddServlet extends TransformationServlet {

@Override
protected void doPost(WorkbenchRequest req, HttpServletResponse resp, String xslPath)
throws IOException, RepositoryException, FileUploadException, QueryResultHandlerException {
throws IOException, RepositoryException, QueryResultHandlerException {
try {
String baseURI = req.getParameter("baseURI");
String contentType = req.getParameter("Content-Type");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

Expand All @@ -31,6 +32,7 @@
/**
* All requests are serviced by this Servlet, though it usually delegates to other Servlets.
*/
@MultipartConfig
public class WorkbenchGateway extends AbstractServlet {

private static final String DEFAULT_SERVER = "default-server";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,15 @@
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.Part;

import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.eclipse.rdf4j.model.IRI;
import org.eclipse.rdf4j.model.Resource;
import org.eclipse.rdf4j.model.Value;
Expand All @@ -40,7 +39,7 @@
import org.eclipse.rdf4j.workbench.exceptions.BadRequestException;

/**
* Request wrapper used by {@link org.eclipse.rdf4j.workbench.base TransformationServlet}.
* Request wrapper used by {@link org.eclipse.rdf4j.workbench.base.TransformationServlet}.
*/
public class WorkbenchRequest extends HttpServletRequestWrapper {

Expand All @@ -62,22 +61,36 @@ public class WorkbenchRequest extends HttpServletRequestWrapper {
* @param defaults application default parameter values
* @throws RepositoryException if there is an issue retrieving the parameter map
* @throws IOException if there is an issue retrieving the parameter map
* @throws FileUploadException if there is an issue retrieving the parameter map
* @throws ServletException if there is an issue retrieving the parameter map
*/
public WorkbenchRequest(Repository repository, HttpServletRequest request, Map<String, String> defaults)
throws RepositoryException, IOException, FileUploadException {
throws RepositoryException, IOException, ServletException {
super(request);
this.defaults = defaults;
this.decoder = new ValueDecoder(repository,
(repository == null) ? SimpleValueFactory.getInstance() : repository.getValueFactory());
String url = request.getRequestURL().toString();
if (ServletFileUpload.isMultipartContent(this)) {
if (isMultipartContent(this)) {
parameters = getMultipartParameterMap();
} else if (request.getQueryString() == null && url.contains(";")) {
parameters = getUrlParameterMap(url);
}
}

private static boolean isMultipartContent(HttpServletRequest request) {
if (!"POST".equalsIgnoreCase(request.getMethod())) {
return false;
}
String contentType = request.getContentType();
if (contentType == null) {
return false;
}
if (contentType.toLowerCase(Locale.ENGLISH).startsWith("multipart/")) {
return true;
}
return false;
}

/**
* Get the content of any uploaded file that is part of this request.
*
Expand Down Expand Up @@ -265,26 +278,21 @@ public Value getValue(String name) throws BadRequestException, RepositoryExcepti
return decoder.decodeValue(getParameter(name));
}

private String firstLine(FileItemStream item) throws IOException {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(item.openStream()))) {
return reader.readLine();
}
}

private Map<String, String> getMultipartParameterMap()
throws RepositoryException, IOException, FileUploadException {
throws RepositoryException, IOException, ServletException {
Map<String, String> parameters = new HashMap<>();
ServletFileUpload upload = new ServletFileUpload();
FileItemIterator iter = upload.getItemIterator(this);
while (iter.hasNext()) {
FileItemStream item = iter.next();
String name = item.getFieldName();
for (Part part : getParts()) {
String name = part.getName();
if ("content".equals(name)) {
content = item.openStream();
contentFileName = item.getName();
content = part.getInputStream();
contentFileName = part.getSubmittedFileName();
break;
} else {
parameters.put(name, firstLine(item));
String firstLine;
try (BufferedReader reader = new BufferedReader(new InputStreamReader(part.getInputStream()))) {
firstLine = reader.readLine();
}
parameters.put(name, firstLine);
}
}
return parameters;
Expand Down

0 comments on commit 19c7f3c

Please sign in to comment.