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:03 UTC

[commons-fileupload] branch master updated (a655e14 -> 0b4516e)

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/commons-fileupload.git


    from a655e14  Refactor common code
     new 5342e2a  Refactor commons code
     new 0b4516e  Don't import String.format() statically

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../commons/fileupload2/AbstractFileUpload.java    |  3 +-
 .../fileupload2/AbstractRequestContext.java        | 72 ++++++++++++++++++++++
 .../commons/fileupload2/MultipartStream.java       |  4 +-
 .../commons/fileupload2/disk/DiskFileItem.java     |  8 +--
 .../fileupload2/impl/FileItemIteratorImpl.java     | 11 ++--
 .../fileupload2/impl/FileItemStreamImpl.java       |  9 ++-
 .../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 +---------
 12 files changed, 100 insertions(+), 120 deletions(-)
 create mode 100644 src/main/java/org/apache/commons/fileupload2/AbstractRequestContext.java


[commons-fileupload] 02/02: Don't import String.format() statically

Posted by gg...@apache.org.
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 0b4516e1fa7f52831a4cfd6aa62b3c583afe5609
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Fri Apr 7 19:17:02 2023 -0400

    Don't import String.format() statically
---
 .../java/org/apache/commons/fileupload2/AbstractFileUpload.java  | 3 +--
 .../java/org/apache/commons/fileupload2/MultipartStream.java     | 4 ++--
 .../java/org/apache/commons/fileupload2/disk/DiskFileItem.java   | 8 +++-----
 .../apache/commons/fileupload2/impl/FileItemIteratorImpl.java    | 9 ++++-----
 .../org/apache/commons/fileupload2/impl/FileItemStreamImpl.java  | 9 ++++-----
 5 files changed, 14 insertions(+), 19 deletions(-)

diff --git a/src/main/java/org/apache/commons/fileupload2/AbstractFileUpload.java b/src/main/java/org/apache/commons/fileupload2/AbstractFileUpload.java
index 46bc68e..4340bae 100644
--- a/src/main/java/org/apache/commons/fileupload2/AbstractFileUpload.java
+++ b/src/main/java/org/apache/commons/fileupload2/AbstractFileUpload.java
@@ -16,7 +16,6 @@
  */
 package org.apache.commons.fileupload2;
 
-import static java.lang.String.format;
 
 import java.io.IOException;
 import java.io.InputStream;
@@ -471,7 +470,7 @@ public abstract class AbstractFileUpload {
                 } catch (final FileUploadException e) {
                     throw e;
                 } catch (final IOException e) {
-                    throw new FileUploadException(format("Processing of %s request failed. %s", MULTIPART_FORM_DATA, e.getMessage()), e);
+                    throw new FileUploadException(String.format("Processing of %s request failed. %s", MULTIPART_FORM_DATA, e.getMessage()), e);
                 }
                 fileItem.setHeaders(item.getHeaders());
             }
diff --git a/src/main/java/org/apache/commons/fileupload2/MultipartStream.java b/src/main/java/org/apache/commons/fileupload2/MultipartStream.java
index a9b56af..9d9613a 100644
--- a/src/main/java/org/apache/commons/fileupload2/MultipartStream.java
+++ b/src/main/java/org/apache/commons/fileupload2/MultipartStream.java
@@ -16,7 +16,6 @@
  */
 package org.apache.commons.fileupload2;
 
-import static java.lang.String.format;
 
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
@@ -819,7 +818,8 @@ public class MultipartStream {
                 throw new MalformedStreamException("Stream ended unexpectedly", e);
             }
             if (++size > HEADER_PART_SIZE_MAX) {
-                throw new MalformedStreamException(format("Header section has more than %s bytes (maybe it is not properly terminated)", HEADER_PART_SIZE_MAX));
+                throw new MalformedStreamException(
+                        String.format("Header section has more than %s bytes (maybe it is not properly terminated)", HEADER_PART_SIZE_MAX));
             }
             if (b == HEADER_SEPARATOR[i]) {
                 i++;
diff --git a/src/main/java/org/apache/commons/fileupload2/disk/DiskFileItem.java b/src/main/java/org/apache/commons/fileupload2/disk/DiskFileItem.java
index ad35d39..9974e02 100644
--- a/src/main/java/org/apache/commons/fileupload2/disk/DiskFileItem.java
+++ b/src/main/java/org/apache/commons/fileupload2/disk/DiskFileItem.java
@@ -16,7 +16,6 @@
  */
 package org.apache.commons.fileupload2.disk;
 
-import static java.lang.String.format;
 
 import java.io.ByteArrayInputStream;
 import java.io.File;
@@ -454,7 +453,7 @@ public class DiskFileItem implements FileItem {
             if (tempDir == null) {
                 tempDir = FileUtils.getTempDirectory();
             }
-            final String tempFileName = format("upload_%s_%s.tmp", UID, getUniqueId());
+            final String tempFileName = String.format("upload_%s_%s.tmp", UID, getUniqueId());
             tempFile = new File(tempDir, tempFileName);
         }
         return tempFile;
@@ -539,9 +538,8 @@ public class DiskFileItem implements FileItem {
      */
     @Override
     public String toString() {
-        return format("name=%s, StoreLocation=%s, size=%s bytes, isFormField=%s, FieldName=%s",
-                      getName(), getStoreLocation(), getSize(),
-                isFormField(), getFieldName());
+        return String.format("name=%s, StoreLocation=%s, size=%s bytes, isFormField=%s, FieldName=%s", getName(), getStoreLocation(), getSize(), isFormField(),
+                getFieldName());
     }
 
     /**
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 c68300e..6aff516 100644
--- a/src/main/java/org/apache/commons/fileupload2/impl/FileItemIteratorImpl.java
+++ b/src/main/java/org/apache/commons/fileupload2/impl/FileItemIteratorImpl.java
@@ -16,7 +16,6 @@
  */
 package org.apache.commons.fileupload2.impl;
 
-import static java.lang.String.format;
 
 import java.io.IOException;
 import java.io.InputStream;
@@ -252,7 +251,7 @@ public class FileItemIteratorImpl implements FileItemIterator {
     protected void init(final AbstractFileUpload fileUploadBase, final RequestContext requestContext) throws FileUploadException, IOException {
         final String contentType = ctx.getContentType();
         if ((null == contentType) || (!contentType.toLowerCase(Locale.ENGLISH).startsWith(AbstractFileUpload.MULTIPART))) {
-            throw new FileUploadContentTypeException(format("the request doesn't contain a %s or %s stream, content type header is %s",
+            throw new FileUploadContentTypeException(String.format("the request doesn't contain a %s or %s stream, content type header is %s",
                     AbstractFileUpload.MULTIPART_FORM_DATA, AbstractFileUpload.MULTIPART_MIXED, contentType), contentType);
         }
         final long contentLengthInt = ctx.getContentLength();
@@ -267,7 +266,7 @@ public class FileItemIteratorImpl implements FileItemIterator {
         if (sizeMax >= 0) {
             if (requestSize != -1 && requestSize > sizeMax) {
                 throw new FileUploadSizeException(
-                        format("the request was rejected because its size (%s) exceeds the configured maximum (%s)", requestSize, sizeMax), sizeMax,
+                        String.format("the request was rejected because its size (%s) exceeds the configured maximum (%s)", requestSize, sizeMax), sizeMax,
                         requestSize);
             }
             // N.B. this is eventually closed in MultipartStream processing
@@ -275,7 +274,7 @@ public class FileItemIteratorImpl implements FileItemIterator {
                 @Override
                 protected void raiseError(final long maxLen, final long count) throws IOException {
                     throw new FileUploadSizeException(
-                            format("The request was rejected because its size (%s) exceeds the configured maximum (%s)", count, maxLen), maxLen, count);
+                            String.format("The request was rejected because its size (%s) exceeds the configured maximum (%s)", count, maxLen), maxLen, count);
                 }
             };
         } else {
@@ -298,7 +297,7 @@ public class FileItemIteratorImpl implements FileItemIterator {
             multiPartStream = new MultipartStream(input, multiPartBoundary, progressNotifier);
         } catch (final IllegalArgumentException e) {
             IOUtils.closeQuietly(input); // avoid possible resource leak
-            throw new FileUploadContentTypeException(format("The boundary specified in the %s header is too long", AbstractFileUpload.CONTENT_TYPE), e);
+            throw new FileUploadContentTypeException(String.format("The boundary specified in the %s header is too long", AbstractFileUpload.CONTENT_TYPE), e);
         }
         multiPartStream.setHeaderEncoding(charEncoding);
     }
diff --git a/src/main/java/org/apache/commons/fileupload2/impl/FileItemStreamImpl.java b/src/main/java/org/apache/commons/fileupload2/impl/FileItemStreamImpl.java
index c389960..e89bc01 100644
--- a/src/main/java/org/apache/commons/fileupload2/impl/FileItemStreamImpl.java
+++ b/src/main/java/org/apache/commons/fileupload2/impl/FileItemStreamImpl.java
@@ -16,7 +16,6 @@
  */
 package org.apache.commons.fileupload2.impl;
 
-import static java.lang.String.format;
 
 import java.io.IOException;
 import java.io.InputStream;
@@ -98,9 +97,8 @@ public class FileItemStreamImpl implements FileItemStream {
         this.formField = formField;
         final long fileSizeMax = fileItemIteratorImpl.getFileSizeMax();
         if (fileSizeMax != -1 && contentLength != -1 && contentLength > fileSizeMax) {
-            throw new FileUploadByteCountLimitException(
-                    format("The field %s exceeds its maximum permitted size of %s bytes.", fieldName, fileSizeMax), contentLength, fileSizeMax, fileName,
-                    fieldName);
+            throw new FileUploadByteCountLimitException(String.format("The field %s exceeds its maximum permitted size of %s bytes.", fieldName, fileSizeMax),
+                    contentLength, fileSizeMax, fileName, fieldName);
         }
         // OK to construct stream now
         final ItemInputStream itemInputStream = fileItemIteratorImpl.getMultiPartStream().newInputStream();
@@ -111,7 +109,8 @@ public class FileItemStreamImpl implements FileItemStream {
                 protected void raiseError(final long sizeMax, final long count) throws IOException {
                     itemInputStream.close(true);
                     throw new FileUploadByteCountLimitException(
-                            format("The field %s exceeds its maximum permitted size of %s bytes.", fieldName, sizeMax), count, sizeMax, fileName, fieldName);
+                            String.format("The field %s exceeds its maximum permitted size of %s bytes.", fieldName, sizeMax), count, sizeMax, fileName,
+                            fieldName);
                 }
             };
         }


[commons-fileupload] 01/02: Refactor commons code

Posted by gg...@apache.org.
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());
-    }
-
 }