AbstractRequestContext.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.apache.commons.fileupload2.core;

  18. import java.util.Objects;
  19. import java.util.function.Function;
  20. import java.util.function.LongSupplier;
  21. import java.util.regex.Pattern;

  22. /**
  23.  * Abstracts a RequestContext for implementations.
  24.  *
  25.  * @param <T> The request type.
  26.  */
  27. public abstract class AbstractRequestContext<T> implements RequestContext {
  28.     /**
  29.      * The Content-Type Pattern for multipart/related Requests.
  30.      */
  31.     private static final Pattern MULTIPART_RELATED =
  32.             Pattern.compile("^\\s*multipart/related.*", Pattern.CASE_INSENSITIVE);

  33.     /**
  34.      * Supplies the content length default.
  35.      */
  36.     private final LongSupplier contentLengthDefault;

  37.     /**
  38.      * Supplies the content length string.
  39.      */
  40.     private final Function<String, String> contentLengthString;

  41.     /**
  42.      * The request.
  43.      */
  44.     private final T request;

  45.     /**
  46.      * Constructs a new instance.
  47.      *
  48.      * @param contentLengthString  How to get the content length string.
  49.      * @param contentLengthDefault How to get the content length default.
  50.      * @param request              The request.
  51.      */
  52.     protected AbstractRequestContext(final Function<String, String> contentLengthString, final LongSupplier contentLengthDefault, final T request) {
  53.         this.contentLengthString = Objects.requireNonNull(contentLengthString, "contentLengthString");
  54.         this.contentLengthDefault = Objects.requireNonNull(contentLengthDefault, "contentLengthDefault");
  55.         this.request = Objects.requireNonNull(request, "request");
  56.     }

  57.     /**
  58.      * Gets the content length of the request.
  59.      *
  60.      * @return The content length of the request.
  61.      */
  62.     @Override
  63.     public long getContentLength() {
  64.         try {
  65.             return Long.parseLong(contentLengthString.apply(AbstractFileUpload.CONTENT_LENGTH));
  66.         } catch (final NumberFormatException e) {
  67.             return contentLengthDefault.getAsLong();
  68.         }
  69.     }

  70.     /**
  71.      * Gets the request.
  72.      *
  73.      * @return the request.
  74.      */
  75.     public T getRequest() {
  76.         return request;
  77.     }

  78.     /**
  79.      * Tests whether the Request of type {@code multipart/related}?
  80.      *
  81.      * @return whether the Request is of type {@code multipart/related}
  82.      * @since 2.0.0
  83.      */
  84.     @Override
  85.     public boolean isMultipartRelated() {
  86.         return MULTIPART_RELATED.matcher(getContentType()).matches();
  87.     }

  88.     /**
  89.      * Returns a string representation of this object.
  90.      *
  91.      * @return a string representation of this object.
  92.      */
  93.     @Override
  94.     public String toString() {
  95.         return String.format("%s [ContentLength=%s, ContentType=%s]", getClass().getSimpleName(), getContentLength(), getContentType());
  96.     }
  97. }