001/*
002 * Copyright 2002-2019 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.http.codec;
018
019import java.util.Map;
020
021import org.springframework.core.ResolvableType;
022import org.springframework.core.codec.Hints;
023import org.springframework.core.codec.ResourceDecoder;
024import org.springframework.core.io.Resource;
025import org.springframework.http.server.reactive.ServerHttpRequest;
026import org.springframework.http.server.reactive.ServerHttpResponse;
027import org.springframework.util.StringUtils;
028
029/**
030 * {@code HttpMessageReader} that wraps and delegates to a {@link ResourceDecoder}
031 * that extracts the filename from the {@code "Content-Disposition"} header, if
032 * available, and passes it as the {@link ResourceDecoder#FILENAME_HINT}.
033 *
034 * @author Rossen Stoyanchev
035 * @since 5.2
036 */
037public class ResourceHttpMessageReader extends DecoderHttpMessageReader<Resource> {
038
039        public ResourceHttpMessageReader() {
040                super(new ResourceDecoder());
041        }
042
043        public ResourceHttpMessageReader(ResourceDecoder resourceDecoder) {
044                super(resourceDecoder);
045        }
046
047
048        @Override
049        protected Map<String, Object> getReadHints(ResolvableType actualType, ResolvableType elementType,
050                        ServerHttpRequest request, ServerHttpResponse response) {
051
052                String name = request.getHeaders().getContentDisposition().getFilename();
053                return StringUtils.hasText(name) ? Hints.from(ResourceDecoder.FILENAME_HINT, name) : Hints.none();
054        }
055
056}