You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2023/04/07 23:17:04 UTC
[commons-fileupload] 01/02: Refactor commons code
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-fileupload.git
commit 5342e2acd3f4cfe4013f10dddf1bcbf220293b46
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Fri Apr 7 19:09:08 2023 -0400
Refactor commons code
---
.../fileupload2/AbstractRequestContext.java | 72 ++++++++++++++++++++++
.../fileupload2/impl/FileItemIteratorImpl.java | 2 +-
.../fileupload2/jaksrvlt/JakSrvltFileUpload.java | 2 +-
.../jaksrvlt/JakSrvltRequestContext.java | 33 +---------
.../fileupload2/portlet/PortletFileUpload.java | 2 +-
.../fileupload2/portlet/PortletRequestContext.java | 41 ++----------
.../fileupload2/servlet/ServletFileUpload.java | 2 +-
.../fileupload2/servlet/ServletRequestContext.java | 33 +---------
8 files changed, 86 insertions(+), 101 deletions(-)
diff --git a/src/main/java/org/apache/commons/fileupload2/AbstractRequestContext.java b/src/main/java/org/apache/commons/fileupload2/AbstractRequestContext.java
new file mode 100644
index 0000000..bb1422e
--- /dev/null
+++ b/src/main/java/org/apache/commons/fileupload2/AbstractRequestContext.java
@@ -0,0 +1,72 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.fileupload2;
+
+import java.util.function.Function;
+import java.util.function.LongSupplier;
+
+public abstract class AbstractRequestContext implements RequestContext {
+
+ /**
+ * Supplies the content length default.
+ */
+ private final LongSupplier contentLengthDefault;
+
+ /**
+ * Supplies the content length string.
+ */
+ private final Function<String, String> contentLengthString;
+
+ /**
+ * Constructs a new instance.
+ *
+ * @param contentLengthString How to get the content length string.
+ * @param contentLengthDefault How to get the content length default.
+ */
+ protected AbstractRequestContext(final Function<String, String> contentLengthString, final LongSupplier contentLengthDefault) {
+ super();
+ this.contentLengthString = contentLengthString;
+ this.contentLengthDefault = contentLengthDefault;
+ }
+
+ /**
+ * Gets the content length of the request.
+ *
+ * @return The content length of the request.
+ * @since 1.3
+ */
+ @Override
+ public long getContentLength() {
+ try {
+ return Long.parseLong(contentLengthString.apply(AbstractFileUpload.CONTENT_LENGTH));
+ } catch (final NumberFormatException e) {
+ return contentLengthDefault.getAsLong();
+ }
+ }
+
+ /**
+ * Returns a string representation of this object.
+ *
+ * @return a string representation of this object.
+ */
+ @Override
+ public String toString() {
+ return String.format("%s [ContentLength=%s, ContentType=%s]", getClass().getSimpleName(), getContentLength(), getContentType());
+ }
+
+}
diff --git a/src/main/java/org/apache/commons/fileupload2/impl/FileItemIteratorImpl.java b/src/main/java/org/apache/commons/fileupload2/impl/FileItemIteratorImpl.java
index 524c301..c68300e 100644
--- a/src/main/java/org/apache/commons/fileupload2/impl/FileItemIteratorImpl.java
+++ b/src/main/java/org/apache/commons/fileupload2/impl/FileItemIteratorImpl.java
@@ -26,11 +26,11 @@ import java.util.Locale;
import java.util.NoSuchElementException;
import java.util.Objects;
+import org.apache.commons.fileupload2.AbstractFileUpload;
import org.apache.commons.fileupload2.FileItem;
import org.apache.commons.fileupload2.FileItemHeaders;
import org.apache.commons.fileupload2.FileItemIterator;
import org.apache.commons.fileupload2.FileItemStream;
-import org.apache.commons.fileupload2.AbstractFileUpload;
import org.apache.commons.fileupload2.FileUploadException;
import org.apache.commons.fileupload2.MultipartStream;
import org.apache.commons.fileupload2.ProgressListener;
diff --git a/src/main/java/org/apache/commons/fileupload2/jaksrvlt/JakSrvltFileUpload.java b/src/main/java/org/apache/commons/fileupload2/jaksrvlt/JakSrvltFileUpload.java
index 47f922c..d012da1 100644
--- a/src/main/java/org/apache/commons/fileupload2/jaksrvlt/JakSrvltFileUpload.java
+++ b/src/main/java/org/apache/commons/fileupload2/jaksrvlt/JakSrvltFileUpload.java
@@ -20,11 +20,11 @@ import java.io.IOException;
import java.util.List;
import java.util.Map;
+import org.apache.commons.fileupload2.AbstractFileUpload;
import org.apache.commons.fileupload2.FileItem;
import org.apache.commons.fileupload2.FileItemFactory;
import org.apache.commons.fileupload2.FileItemIterator;
import org.apache.commons.fileupload2.FileUpload;
-import org.apache.commons.fileupload2.AbstractFileUpload;
import org.apache.commons.fileupload2.FileUploadException;
import jakarta.servlet.http.HttpServletRequest;
diff --git a/src/main/java/org/apache/commons/fileupload2/jaksrvlt/JakSrvltRequestContext.java b/src/main/java/org/apache/commons/fileupload2/jaksrvlt/JakSrvltRequestContext.java
index 202def7..e182807 100644
--- a/src/main/java/org/apache/commons/fileupload2/jaksrvlt/JakSrvltRequestContext.java
+++ b/src/main/java/org/apache/commons/fileupload2/jaksrvlt/JakSrvltRequestContext.java
@@ -19,8 +19,7 @@ package org.apache.commons.fileupload2.jaksrvlt;
import java.io.IOException;
import java.io.InputStream;
-import org.apache.commons.fileupload2.AbstractFileUpload;
-import org.apache.commons.fileupload2.RequestContext;
+import org.apache.commons.fileupload2.AbstractRequestContext;
import jakarta.servlet.http.HttpServletRequest;
@@ -30,7 +29,7 @@ import jakarta.servlet.http.HttpServletRequest;
*
* @since 1.1
*/
-public class JakSrvltRequestContext implements RequestContext {
+public class JakSrvltRequestContext extends AbstractRequestContext {
/**
* The request for which the context is being provided.
@@ -43,26 +42,10 @@ public class JakSrvltRequestContext implements RequestContext {
* @param request The request to which this context applies.
*/
public JakSrvltRequestContext(final HttpServletRequest request) {
+ super(request::getHeader, request::getContentLength);
this.request = request;
}
- /**
- * Gets the content length of the request.
- *
- * @return The content length of the request.
- * @since 1.3
- */
- @Override
- public long getContentLength() {
- long size;
- try {
- size = Long.parseLong(request.getHeader(AbstractFileUpload.CONTENT_LENGTH));
- } catch (final NumberFormatException e) {
- size = request.getContentLength();
- }
- return size;
- }
-
/**
* Gets the character encoding for the request.
*
@@ -95,14 +78,4 @@ public class JakSrvltRequestContext implements RequestContext {
return request.getInputStream();
}
- /**
- * Gets a string representation of this object.
- *
- * @return a string representation of this object.
- */
- @Override
- public String toString() {
- return String.format("ContentLength=%s, ContentType=%s", this.getContentLength(), this.getContentType());
- }
-
}
diff --git a/src/main/java/org/apache/commons/fileupload2/portlet/PortletFileUpload.java b/src/main/java/org/apache/commons/fileupload2/portlet/PortletFileUpload.java
index 813b70d..2224018 100644
--- a/src/main/java/org/apache/commons/fileupload2/portlet/PortletFileUpload.java
+++ b/src/main/java/org/apache/commons/fileupload2/portlet/PortletFileUpload.java
@@ -22,11 +22,11 @@ import java.util.Map;
import javax.portlet.ActionRequest;
+import org.apache.commons.fileupload2.AbstractFileUpload;
import org.apache.commons.fileupload2.FileItem;
import org.apache.commons.fileupload2.FileItemFactory;
import org.apache.commons.fileupload2.FileItemIterator;
import org.apache.commons.fileupload2.FileUpload;
-import org.apache.commons.fileupload2.AbstractFileUpload;
import org.apache.commons.fileupload2.FileUploadException;
/**
diff --git a/src/main/java/org/apache/commons/fileupload2/portlet/PortletRequestContext.java b/src/main/java/org/apache/commons/fileupload2/portlet/PortletRequestContext.java
index 235ff52..c1b4077 100644
--- a/src/main/java/org/apache/commons/fileupload2/portlet/PortletRequestContext.java
+++ b/src/main/java/org/apache/commons/fileupload2/portlet/PortletRequestContext.java
@@ -16,23 +16,19 @@
*/
package org.apache.commons.fileupload2.portlet;
-import static java.lang.String.format;
-
import java.io.IOException;
import java.io.InputStream;
import javax.portlet.ActionRequest;
-import org.apache.commons.fileupload2.AbstractFileUpload;
-import org.apache.commons.fileupload2.RequestContext;
+import org.apache.commons.fileupload2.AbstractRequestContext;
/**
- * Provides access to the request information needed for a request made to
- * a portlet.
+ * Provides access to the request information needed for a request made to a portlet.
*
* @since 1.1
*/
-public class PortletRequestContext implements RequestContext {
+public class PortletRequestContext extends AbstractRequestContext {
/**
* The request for which the context is being provided.
@@ -45,26 +41,10 @@ public class PortletRequestContext implements RequestContext {
* @param request The request to which this context applies.
*/
public PortletRequestContext(final ActionRequest request) {
+ super(request::getProperty, request::getContentLength);
this.request = request;
}
- /**
- * Gets the content length of the request.
- *
- * @return The content length of the request.
- * @since 1.3
- */
- @Override
- public long getContentLength() {
- long size;
- try {
- size = Long.parseLong(request.getProperty(AbstractFileUpload.CONTENT_LENGTH));
- } catch (final NumberFormatException e) {
- size = request.getContentLength();
- }
- return size;
- }
-
/**
* Gets the character encoding for the request.
*
@@ -89,7 +69,6 @@ public class PortletRequestContext implements RequestContext {
* Gets the input stream for the request.
*
* @return The input stream for the request.
- *
* @throws IOException if a problem occurs.
*/
@Override
@@ -97,16 +76,4 @@ public class PortletRequestContext implements RequestContext {
return request.getPortletInputStream();
}
- /**
- * Returns a string representation of this object.
- *
- * @return a string representation of this object.
- */
- @Override
- public String toString() {
- return format("ContentLength=%s, ContentType=%s",
- this.getContentLength(),
- this.getContentType());
- }
-
}
diff --git a/src/main/java/org/apache/commons/fileupload2/servlet/ServletFileUpload.java b/src/main/java/org/apache/commons/fileupload2/servlet/ServletFileUpload.java
index 4c9d394..d514fae 100644
--- a/src/main/java/org/apache/commons/fileupload2/servlet/ServletFileUpload.java
+++ b/src/main/java/org/apache/commons/fileupload2/servlet/ServletFileUpload.java
@@ -22,11 +22,11 @@ import java.util.Map;
import javax.servlet.http.HttpServletRequest;
+import org.apache.commons.fileupload2.AbstractFileUpload;
import org.apache.commons.fileupload2.FileItem;
import org.apache.commons.fileupload2.FileItemFactory;
import org.apache.commons.fileupload2.FileItemIterator;
import org.apache.commons.fileupload2.FileUpload;
-import org.apache.commons.fileupload2.AbstractFileUpload;
import org.apache.commons.fileupload2.FileUploadException;
/**
diff --git a/src/main/java/org/apache/commons/fileupload2/servlet/ServletRequestContext.java b/src/main/java/org/apache/commons/fileupload2/servlet/ServletRequestContext.java
index 5f22d75..73a0a15 100644
--- a/src/main/java/org/apache/commons/fileupload2/servlet/ServletRequestContext.java
+++ b/src/main/java/org/apache/commons/fileupload2/servlet/ServletRequestContext.java
@@ -21,8 +21,7 @@ import java.io.InputStream;
import javax.servlet.http.HttpServletRequest;
-import org.apache.commons.fileupload2.AbstractFileUpload;
-import org.apache.commons.fileupload2.RequestContext;
+import org.apache.commons.fileupload2.AbstractRequestContext;
/**
* Provides access to the request information needed for a request made to
@@ -30,7 +29,7 @@ import org.apache.commons.fileupload2.RequestContext;
*
* @since 1.1
*/
-public class ServletRequestContext implements RequestContext {
+public class ServletRequestContext extends AbstractRequestContext {
/**
* The request for which the context is being provided.
@@ -43,26 +42,10 @@ public class ServletRequestContext implements RequestContext {
* @param request The request to which this context applies.
*/
public ServletRequestContext(final HttpServletRequest request) {
+ super(request::getHeader, request::getContentLength);
this.request = request;
}
- /**
- * Gets the content length of the request.
- *
- * @return The content length of the request.
- * @since 1.3
- */
- @Override
- public long getContentLength() {
- long size;
- try {
- size = Long.parseLong(request.getHeader(AbstractFileUpload.CONTENT_LENGTH));
- } catch (final NumberFormatException e) {
- size = request.getContentLength();
- }
- return size;
- }
-
/**
* Gets the character encoding for the request.
*
@@ -95,14 +78,4 @@ public class ServletRequestContext implements RequestContext {
return request.getInputStream();
}
- /**
- * Returns a string representation of this object.
- *
- * @return a string representation of this object.
- */
- @Override
- public String toString() {
- return String.format("ContentLength=%s, ContentType=%s", this.getContentLength(), this.getContentType());
- }
-
}