001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.wicket.core.util.resource; 018 019import java.io.IOException; 020import java.io.InputStream; 021import java.time.Instant; 022import java.util.Locale; 023import org.apache.wicket.Application; 024import org.apache.wicket.WicketRuntimeException; 025import org.apache.wicket.core.util.resource.locator.IResourceStreamLocator; 026import org.apache.wicket.util.lang.Bytes; 027import org.apache.wicket.util.lang.Packages; 028import org.apache.wicket.util.resource.AbstractResourceStream; 029import org.apache.wicket.util.resource.IResourceStream; 030import org.apache.wicket.util.resource.ResourceStreamNotFoundException; 031 032 033/** 034 * An {@link IResourceStream} that reads data from a resource in the classpath. It simply delegates 035 * all operations to the {@link IResourceStream} returned by the application's 036 * {@link IResourceStreamLocator}. 037 * 038 * @author <a href="mailto:jbq@apache.org">Jean-Baptiste Quenot</a> 039 * @author Tobias Soloschenko 040 */ 041public class PackageResourceStream extends AbstractResourceStream 042{ 043 private static final long serialVersionUID = 1L; 044 045 private final IResourceStream resourceStream; 046 047 /** 048 * Obtains an {@link IResourceStream} from the application's 049 * {@link IResourceStreamLocator#locate(Class, String)} 050 * 051 * @param scope 052 * This argument will be used to get the class loader for loading the package 053 * resource, and to determine what package it is in. 054 * @param path 055 * The path to the resource 056 */ 057 public PackageResourceStream(Class<?> scope, String path) 058 { 059 this(scope, path, null, null, null); 060 } 061 062 /** 063 * Obtains an {@link IResourceStream} from the application's 064 * {@link IResourceStreamLocator#locate(Class, String)} 065 * 066 * @param scope 067 * This argument will be used to get the class loader for loading the package 068 * resource, and to determine what package it is in. 069 * @param path 070 * The path to the resource 071 * @param locale 072 * the locale of the resource to get 073 * @param style 074 * the style of the resource to get 075 * @param variation 076 * the variation of the resource to get 077 */ 078 public PackageResourceStream(Class<?> scope, String path, Locale locale, String style, 079 String variation) 080 { 081 String absolutePath = Packages.absolutePath(scope, path); 082 resourceStream = Application.get() 083 .getResourceSettings() 084 .getResourceStreamLocator() 085 .locate(scope, absolutePath, style, variation, locale, null, false); 086 087 if (resourceStream == null) 088 { 089 throw new WicketRuntimeException("Cannot find resource with " + scope.getName() + 090 " and path " + path); 091 } 092 } 093 094 @Override 095 public void close() throws IOException 096 { 097 resourceStream.close(); 098 } 099 100 @Override 101 public String getContentType() 102 { 103 return resourceStream.getContentType(); 104 } 105 106 @Override 107 public InputStream getInputStream() throws ResourceStreamNotFoundException 108 { 109 return resourceStream.getInputStream(); 110 } 111 112 @Override 113 public Bytes length() 114 { 115 return resourceStream.length(); 116 } 117 118 @Override 119 public Instant lastModifiedTime() 120 { 121 return resourceStream.lastModifiedTime(); 122 } 123}