001/*
002 * Copyright 2002-2020 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.web.accept;
018
019import java.util.ArrayList;
020import java.util.Collections;
021import java.util.HashSet;
022import java.util.List;
023import java.util.Locale;
024import java.util.Map;
025import java.util.Set;
026import java.util.concurrent.ConcurrentHashMap;
027import java.util.concurrent.ConcurrentMap;
028import java.util.concurrent.CopyOnWriteArrayList;
029
030import org.springframework.http.MediaType;
031import org.springframework.lang.Nullable;
032
033/**
034 * An implementation of {@code MediaTypeFileExtensionResolver} that maintains
035 * lookups between file extensions and MediaTypes in both directions.
036 *
037 * <p>Initially created with a map of file extensions and media types.
038 * Subsequently subclasses can use {@link #addMapping} to add more mappings.
039 *
040 * @author Rossen Stoyanchev
041 * @author Juergen Hoeller
042 * @since 3.2
043 */
044public class MappingMediaTypeFileExtensionResolver implements MediaTypeFileExtensionResolver {
045
046        private final ConcurrentMap<String, MediaType> mediaTypes = new ConcurrentHashMap<>(64);
047
048        private final ConcurrentMap<MediaType, List<String>> fileExtensions = new ConcurrentHashMap<>(64);
049
050        private final List<String> allFileExtensions = new CopyOnWriteArrayList<>();
051
052
053        /**
054         * Create an instance with the given map of file extensions and media types.
055         */
056        public MappingMediaTypeFileExtensionResolver(@Nullable Map<String, MediaType> mediaTypes) {
057                if (mediaTypes != null) {
058                        Set<String> allFileExtensions = new HashSet<>(mediaTypes.size());
059                        mediaTypes.forEach((extension, mediaType) -> {
060                                String lowerCaseExtension = extension.toLowerCase(Locale.ENGLISH);
061                                this.mediaTypes.put(lowerCaseExtension, mediaType);
062                                addFileExtension(mediaType, lowerCaseExtension);
063                                allFileExtensions.add(lowerCaseExtension);
064                        });
065                        this.allFileExtensions.addAll(allFileExtensions);
066                }
067        }
068
069
070        public Map<String, MediaType> getMediaTypes() {
071                return this.mediaTypes;
072        }
073
074        protected List<MediaType> getAllMediaTypes() {
075                return new ArrayList<>(this.mediaTypes.values());
076        }
077
078        /**
079         * Map an extension to a MediaType. Ignore if extension already mapped.
080         */
081        protected void addMapping(String extension, MediaType mediaType) {
082                MediaType previous = this.mediaTypes.putIfAbsent(extension, mediaType);
083                if (previous == null) {
084                        addFileExtension(mediaType, extension);
085                        this.allFileExtensions.add(extension);
086                }
087        }
088
089        private void addFileExtension(MediaType mediaType, String extension) {
090                this.fileExtensions.computeIfAbsent(mediaType, key -> new CopyOnWriteArrayList<>())
091                                .add(extension);
092        }
093
094
095        @Override
096        public List<String> resolveFileExtensions(MediaType mediaType) {
097                List<String> fileExtensions = this.fileExtensions.get(mediaType);
098                return (fileExtensions != null ? fileExtensions : Collections.emptyList());
099        }
100
101        @Override
102        public List<String> getAllFileExtensions() {
103                return Collections.unmodifiableList(this.allFileExtensions);
104        }
105
106        /**
107         * Use this method for a reverse lookup from extension to MediaType.
108         * @return a MediaType for the extension, or {@code null} if none found
109         */
110        @Nullable
111        protected MediaType lookupMediaType(String extension) {
112                return this.mediaTypes.get(extension.toLowerCase(Locale.ENGLISH));
113        }
114
115}