001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * https://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018package org.apache.commons.fileupload2.core; 019 020import java.util.Objects; 021import java.util.function.Function; 022import java.util.function.LongSupplier; 023import java.util.regex.Pattern; 024 025/** 026 * Abstracts a RequestContext for implementations. 027 * 028 * @param <T> The request type. 029 */ 030public abstract class AbstractRequestContext<T> implements RequestContext { 031 032 /** 033 * The Content-Type Pattern for multipart/related Requests. 034 */ 035 private static final Pattern MULTIPART_RELATED = 036 Pattern.compile("^\\s*multipart/related.*", Pattern.CASE_INSENSITIVE); 037 038 /** 039 * Supplies the content length default. 040 */ 041 private final LongSupplier contentLengthDefault; 042 043 /** 044 * Supplies the content length string. 045 */ 046 private final Function<String, String> contentLengthString; 047 048 /** 049 * The request. 050 */ 051 private final T request; 052 053 /** 054 * Constructs a new instance. 055 * 056 * @param contentLengthString How to get the content length string. 057 * @param contentLengthDefault How to get the content length default. 058 * @param request The request. 059 */ 060 protected AbstractRequestContext(final Function<String, String> contentLengthString, final LongSupplier contentLengthDefault, final T request) { 061 this.contentLengthString = Objects.requireNonNull(contentLengthString, "contentLengthString"); 062 this.contentLengthDefault = Objects.requireNonNull(contentLengthDefault, "contentLengthDefault"); 063 this.request = Objects.requireNonNull(request, "request"); 064 } 065 066 /** 067 * Gets the content length of the request. 068 * 069 * @return The content length of the request. 070 */ 071 @Override 072 public long getContentLength() { 073 try { 074 return Long.parseLong(contentLengthString.apply(AbstractFileUpload.CONTENT_LENGTH)); 075 } catch (final NumberFormatException e) { 076 return contentLengthDefault.getAsLong(); 077 } 078 } 079 080 /** 081 * Gets the request. 082 * 083 * @return the request. 084 */ 085 public T getRequest() { 086 return request; 087 } 088 089 /** 090 * Tests whether the Request of type {@code multipart/related}? 091 * 092 * @return whether the Request is of type {@code multipart/related} 093 * @since 2.0.0 094 */ 095 @Override 096 public boolean isMultipartRelated() { 097 return MULTIPART_RELATED.matcher(getContentType()).matches(); 098 } 099 100 /** 101 * Returns a string representation of this object. 102 * 103 * @return a string representation of this object. 104 */ 105 @Override 106 public String toString() { 107 return String.format("%s [ContentLength=%s, ContentType=%s]", getClass().getSimpleName(), getContentLength(), getContentType()); 108 } 109}