View Javadoc
1   /*
2    * Copyright 2002-2014 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.web.servlet.resource;
18  
19  import java.io.IOException;
20  
21  import org.springframework.core.io.ByteArrayResource;
22  import org.springframework.core.io.Resource;
23  
24  /**
25   * An extension of {@link org.springframework.core.io.ByteArrayResource} that a
26   * {@link ResourceTransformer} can use to represent an original resource
27   * preserving all other information except the content.
28   *
29   * @author Jeremy Grelle
30   * @author Rossen Stoyanchev
31   * @since 4.1
32   */
33  public class TransformedResource extends ByteArrayResource {
34  
35  	private final String filename;
36  
37  	private final long lastModified;
38  
39  
40  	public TransformedResource(Resource original, byte[] transformedContent) {
41  		super(transformedContent);
42  		this.filename = original.getFilename();
43  		try {
44  			this.lastModified = original.lastModified();
45  		}
46  		catch (IOException e) {
47  			// should never happen
48  			throw new IllegalArgumentException(e);
49  		}
50  	}
51  
52  
53  	@Override
54  	public String getFilename() {
55  		return this.filename;
56  	}
57  
58  	@Override
59  	public long lastModified() throws IOException {
60  		return this.lastModified;
61  	}
62  
63  }