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.List; 020 021import org.apache.wicket.protocol.http.WebApplication; 022import org.apache.wicket.request.IRequestHandler; 023import org.apache.wicket.request.Url; 024import org.apache.wicket.util.lang.Packages; 025import org.apache.wicket.util.string.Strings; 026 027/** 028 * Resource reference for static files. The resource must reside under the "/META-INF/resources/" 029 * directory. So if you have a foo.bar.Component and want to have a static icon.gif belonging to it 030 * the image must be available on the "META-INF/resources/foo/bar/icon.gif" classpath inside a jar 031 * file. 032 * 033 * If run under a Servlet 3.0 environment "foo/bar/icon.gif" like resource urls will be made and 034 * served by the servlet container instead of wicket (which is faster). 035 * 036 * If run under a non Servlet 3.0 environment (like 2.5) resources will be served by wicket (urls 037 * will look like "wicket/resource/foo/bar/icon.gif"). 038 * 039 * @author akiraly 040 */ 041public class MetaInfStaticResourceReference extends PackageResourceReference 042{ 043 private static final long serialVersionUID = -1858339228780709471L; 044 045 private static Boolean META_INF_RESOURCES_SUPPORTED = null; 046 047 /** 048 * Construct. 049 * 050 * @param scope 051 * mandatory parameter 052 * @param name 053 * mandatory parameter 054 */ 055 public MetaInfStaticResourceReference(Class<?> scope, String name) 056 { 057 super(scope, name); 058 } 059 060 /** 061 * Returns the {@link Url} for given {@link IRequestHandler} if "/META-INF/resources" Servlet 062 * 3.0 feature is supported or <code>null</code> if not (so standard url mapping can take 063 * place). 064 * 065 * @param requestHandler 066 * mandatory parameter 067 * @return Url instance or <code>null</code>. 068 */ 069 public Url mapHandler(IRequestHandler requestHandler) 070 { 071 if (!isMetaInfResourcesSupported()) 072 { 073 return null; 074 } 075 076 Url url = new Url(); 077 078 List<String> segments = url.getSegments(); 079 080 String packageName = Packages.extractPackageName(getScope()); 081 String[] parts = Strings.split(packageName, '.'); 082 for (String p : parts) 083 { 084 segments.add(p); 085 } 086 087 parts = Strings.split(getName(), '/'); 088 for (String p : parts) 089 { 090 segments.add(p); 091 } 092 093 return url; 094 } 095 096 protected boolean isMetaInfResourcesSupported() 097 { 098 if (META_INF_RESOURCES_SUPPORTED == null) 099 { 100 int majorVersion = WebApplication.get().getServletContext().getMajorVersion(); 101 META_INF_RESOURCES_SUPPORTED = majorVersion >= 3; 102 } 103 104 return META_INF_RESOURCES_SUPPORTED; 105 } 106 107}