All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.springframework.web.accept.MappingMediaTypeFileExtensionResolver Maven / Gradle / Ivy

/*
 * Copyright 2002-2019 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.web.accept;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.CopyOnWriteArrayList;

import org.springframework.http.MediaType;
import org.springframework.lang.Nullable;

/**
 * An implementation of {@code MediaTypeFileExtensionResolver} that maintains
 * lookups between file extensions and MediaTypes in both directions.
 *
 * 

Initially created with a map of file extensions and media types. * Subsequently subclasses can use {@link #addMapping} to add more mappings. * * @author Rossen Stoyanchev * @author Juergen Hoeller * @since 3.2 */ public class MappingMediaTypeFileExtensionResolver implements MediaTypeFileExtensionResolver { private final ConcurrentMap mediaTypes = new ConcurrentHashMap<>(64); private final ConcurrentMap> fileExtensions = new ConcurrentHashMap<>(64); private final List allFileExtensions = new CopyOnWriteArrayList<>(); /** * Create an instance with the given map of file extensions and media types. */ public MappingMediaTypeFileExtensionResolver(@Nullable Map mediaTypes) { if (mediaTypes != null) { List allFileExtensions = new ArrayList<>(); mediaTypes.forEach((extension, mediaType) -> { String lowerCaseExtension = extension.toLowerCase(Locale.ENGLISH); this.mediaTypes.put(lowerCaseExtension, mediaType); addFileExtension(mediaType, lowerCaseExtension); allFileExtensions.add(lowerCaseExtension); }); this.allFileExtensions.addAll(allFileExtensions); } } public Map getMediaTypes() { return this.mediaTypes; } protected List getAllMediaTypes() { return new ArrayList<>(this.mediaTypes.values()); } /** * Map an extension to a MediaType. Ignore if extension already mapped. */ protected void addMapping(String extension, MediaType mediaType) { MediaType previous = this.mediaTypes.putIfAbsent(extension, mediaType); if (previous == null) { addFileExtension(mediaType, extension); this.allFileExtensions.add(extension); } } private void addFileExtension(MediaType mediaType, String extension) { this.fileExtensions.computeIfAbsent(mediaType, key -> new CopyOnWriteArrayList<>()) .add(extension); } @Override public List resolveFileExtensions(MediaType mediaType) { List fileExtensions = this.fileExtensions.get(mediaType); return (fileExtensions != null ? fileExtensions : Collections.emptyList()); } @Override public List getAllFileExtensions() { return Collections.unmodifiableList(this.allFileExtensions); } /** * Use this method for a reverse lookup from extension to MediaType. * @return a MediaType for the extension, or {@code null} if none found */ @Nullable protected MediaType lookupMediaType(String extension) { return this.mediaTypes.get(extension.toLowerCase(Locale.ENGLISH)); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy