001/*
002 * Copyright 2002-2018 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.servlet.resource;
018
019import java.io.IOException;
020
021import org.springframework.core.io.ByteArrayResource;
022import org.springframework.core.io.Resource;
023import org.springframework.lang.Nullable;
024
025/**
026 * An extension of {@link ByteArrayResource} that a {@link ResourceTransformer}
027 * can use to represent an original resource preserving all other information
028 * except the content.
029 *
030 * @author Jeremy Grelle
031 * @author Rossen Stoyanchev
032 * @since 4.1
033 */
034public class TransformedResource extends ByteArrayResource {
035
036        @Nullable
037        private final String filename;
038
039        private final long lastModified;
040
041
042        public TransformedResource(Resource original, byte[] transformedContent) {
043                super(transformedContent);
044                this.filename = original.getFilename();
045                try {
046                        this.lastModified = original.lastModified();
047                }
048                catch (IOException ex) {
049                        // should never happen
050                        throw new IllegalArgumentException(ex);
051                }
052        }
053
054
055        @Override
056        @Nullable
057        public String getFilename() {
058                return this.filename;
059        }
060
061        @Override
062        public long lastModified() throws IOException {
063                return this.lastModified;
064        }
065
066}