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.request.resource; 018 019import java.util.Locale; 020 021import javax.servlet.http.HttpServletResponse; 022 023import org.apache.wicket.Application; 024 025/** 026 * A {@link ResourceReference} which should be used to lookup a {@link IResource} from the globally 027 * registered ones (also known as application shared resources). If there is no shared resource with 028 * such {@link Key key} then it checks whether there is a {@link PackageResource} with this 029 * {@link Key key} and registers it automatically if it exists. 030 * <p> 031 * Note: Cannot be registered in {@link ResourceReferenceRegistry} because 032 * {@link SharedResourceReference} is just a shortcut to the {@link IResource resource} of another 033 * {@link ResourceReference} 034 */ 035public class SharedResourceReference extends ResourceReference 036{ 037 private static final long serialVersionUID = 1L; 038 039 /** 040 * Construct. 041 * 042 * @param scope 043 * Scope of resource 044 * @param name 045 * Logical name of resource 046 * @param locale 047 * The locale of the resource 048 * @param style 049 * The resource style (see {@link org.apache.wicket.Session}) 050 * @param variation 051 * The component specific variation of the style 052 */ 053 public SharedResourceReference(Class<?> scope, String name, Locale locale, String style, 054 String variation) 055 { 056 super(scope, name, locale, style, variation); 057 } 058 059 /** 060 * Construct. 061 * 062 * @param scope 063 * Scope of resource 064 * @param name 065 * Logical name of resource 066 */ 067 public SharedResourceReference(Class<?> scope, String name) 068 { 069 super(scope, name); 070 } 071 072 /** 073 * Construct. 074 * 075 * @param name 076 * resource name 077 */ 078 public SharedResourceReference(String name) 079 { 080 super(name); 081 } 082 083 @Override 084 public IResource getResource() 085 { 086 ResourceReference ref = Application.get() 087 .getResourceReferenceRegistry() 088 .getResourceReference(getScope(), getName(), getLocale(), getStyle(), getVariation(), 089 false, true); 090 091 if (ref == null) 092 { 093 return new AbstractResource() 094 { 095 private static final long serialVersionUID = 1L; 096 097 @Override 098 protected ResourceResponse newResourceResponse(Attributes attributes) 099 { 100 ResourceResponse res = new ResourceResponse(); 101 res.setError(HttpServletResponse.SC_NOT_FOUND); 102 return res; 103 } 104 }; 105 } 106 else if (ref != this) 107 { 108 return ref.getResource(); 109 } 110 else 111 { 112 throw new IllegalStateException( 113 "SharedResourceReference can not be registered globally. See the documentation of this class."); 114 } 115 } 116 117 @Override 118 public boolean canBeRegistered() 119 { 120 return false; 121 } 122}