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 * http://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 * The Content-Type Pattern for multipart/related Requests. 033 */ 034 private static final Pattern MULTIPART_RELATED = 035 Pattern.compile("^\\s*multipart/related.*", Pattern.CASE_INSENSITIVE); 036 037 /** 038 * Supplies the content length default. 039 */ 040 private final LongSupplier contentLengthDefault; 041 042 /** 043 * Supplies the content length string. 044 */ 045 private final Function<String, String> contentLengthString; 046 047 /** 048 * The request. 049 */ 050 private final T request; 051 052 /** 053 * Constructs a new instance. 054 * 055 * @param contentLengthString How to get the content length string. 056 * @param contentLengthDefault How to get the content length default. 057 * @param request The request. 058 */ 059 protected AbstractRequestContext(final Function<String, String> contentLengthString, final LongSupplier contentLengthDefault, final T request) { 060 this.contentLengthString = Objects.requireNonNull(contentLengthString, "contentLengthString"); 061 this.contentLengthDefault = Objects.requireNonNull(contentLengthDefault, "contentLengthDefault"); 062 this.request = Objects.requireNonNull(request, "request"); 063 } 064 065 /** 066 * Gets the content length of the request. 067 * 068 * @return The content length of the request. 069 */ 070 @Override 071 public long getContentLength() { 072 try { 073 return Long.parseLong(contentLengthString.apply(AbstractFileUpload.CONTENT_LENGTH)); 074 } catch (final NumberFormatException e) { 075 return contentLengthDefault.getAsLong(); 076 } 077 } 078 079 /** 080 * Gets the request. 081 * 082 * @return the request. 083 */ 084 public T getRequest() { 085 return request; 086 } 087 088 /** 089 * Tests whether the Request of type {@code multipart/related}? 090 * 091 * @return whether the Request is of type {@code multipart/related} 092 * @since 2.0.0 093 */ 094 @Override 095 public boolean isMultipartRelated() { 096 return MULTIPART_RELATED.matcher(getContentType()).matches(); 097 } 098 099 /** 100 * Returns a string representation of this object. 101 * 102 * @return a string representation of this object. 103 */ 104 @Override 105 public String toString() { 106 return String.format("%s [ContentLength=%s, ContentType=%s]", getClass().getSimpleName(), getContentLength(), getContentType()); 107 } 108}