| Class | Line # | Actions | |||||
|---|---|---|---|---|---|---|---|
| AbstractResourceSkinExtensionPlugin | 33 | 9 | 0% | 6 | 15 |
| 1 | /* | |
| 2 | * See the NOTICE file distributed with this work for additional | |
| 3 | * information regarding copyright ownership. | |
| 4 | * | |
| 5 | * This is free software; you can redistribute it and/or modify it | |
| 6 | * under the terms of the GNU Lesser General Public License as | |
| 7 | * published by the Free Software Foundation; either version 2.1 of | |
| 8 | * the License, or (at your option) any later version. | |
| 9 | * | |
| 10 | * This software is distributed in the hope that it will be useful, | |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
| 13 | * Lesser General Public License for more details. | |
| 14 | * | |
| 15 | * You should have received a copy of the GNU Lesser General Public | |
| 16 | * License along with this software; if not, write to the Free | |
| 17 | * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | |
| 18 | * 02110-1301 USA, or see the FSF site: http://www.fsf.org. | |
| 19 | */ | |
| 20 | package com.xpn.xwiki.plugin.skinx; | |
| 21 | ||
| 22 | import java.util.Collections; | |
| 23 | import java.util.Set; | |
| 24 | ||
| 25 | import com.xpn.xwiki.XWikiContext; | |
| 26 | import com.xpn.xwiki.XWikiException; | |
| 27 | ||
| 28 | /** | |
| 29 | * Skin Extension plugin to use extension files from JAR resources. | |
| 30 | * | |
| 31 | * @version $Id: 0319ff88218ce304648adcb6324d71547c6d5c2b $ | |
| 32 | */ | |
| 33 | public abstract class AbstractResourceSkinExtensionPlugin extends AbstractSkinExtensionPlugin | |
| 34 | { | |
| 35 | /** | |
| 36 | * XWiki plugin constructor. | |
| 37 | * | |
| 38 | * @param name The name of the plugin, which can be used for retrieving the plugin API from velocity. Unused. | |
| 39 | * @param className The canonical classname of the plugin. Unused. | |
| 40 | * @param context The current request context. | |
| 41 | * @see com.xpn.xwiki.plugin.XWikiDefaultPlugin#XWikiDefaultPlugin(String,String,com.xpn.xwiki.XWikiContext) | |
| 42 | */ | |
| 43 | 0 | public AbstractResourceSkinExtensionPlugin(String name, String className, XWikiContext context) |
| 44 | { | |
| 45 | 0 | super(name, className, context); |
| 46 | } | |
| 47 | ||
| 48 | /** | |
| 49 | * Get the action which the url should specify for calling this resource. | |
| 50 | * | |
| 51 | * @return String Action name. | |
| 52 | */ | |
| 53 | protected abstract String getAction(); | |
| 54 | ||
| 55 | /** | |
| 56 | * Takes a URL string and outputs a link which will cause the browser to load the url. | |
| 57 | * | |
| 58 | * @param url String representation of the url to load (eg: {@code /res/url.js}) | |
| 59 | * @param resourceName name of the pulled resource | |
| 60 | * @param context the current request context | |
| 61 | * @return HTML code linking to the pulled resource (eg: {@code <script type="text/javascript" src="/res/url.js"/>}) | |
| 62 | */ | |
| 63 | protected abstract String generateLink(String url, String resourceName, XWikiContext context); | |
| 64 | ||
| 65 | 0 | @Override |
| 66 | public String getLink(String resourceName, XWikiContext context) | |
| 67 | { | |
| 68 | // If the current user has access to Main.WebHome, we will use this document in the URL | |
| 69 | // to serve the resource. This way, the resource can be efficiently cached, since it has a | |
| 70 | // common URL for any page. | |
| 71 | // Otherwise, we link to the current document, which means that the extension will be accessible if the current | |
| 72 | // document is accessible as well. | |
| 73 | 0 | try { |
| 74 | 0 | String page = context.getWiki().getDefaultSpace(context) + "." + context.getWiki().getDefaultPage(context); |
| 75 | 0 | if (!context.getWiki().getRightService().hasAccessLevel("view", context.getUser(), page, context)) { |
| 76 | 0 | page = context.getDoc().getFullName(); |
| 77 | } | |
| 78 | 0 | return generateLink(context.getWiki().getURL(page, getAction(), |
| 79 | "resource=" + sanitize(resourceName) + parametersAsQueryString(resourceName, context), context), | |
| 80 | resourceName, context); | |
| 81 | } catch (XWikiException e) { | |
| 82 | // Do nothing here; we can't access the wiki, so don't link to this resource at all. | |
| 83 | 0 | return ""; |
| 84 | } | |
| 85 | } | |
| 86 | ||
| 87 | /** | |
| 88 | * {@inheritDoc} | |
| 89 | * <p> | |
| 90 | * There is no support for always used resource-based extensions yet. | |
| 91 | * </p> | |
| 92 | * | |
| 93 | * @see AbstractSkinExtensionPlugin#getAlwaysUsedExtensions(XWikiContext) | |
| 94 | */ | |
| 95 | 0 | @Override |
| 96 | public Set<String> getAlwaysUsedExtensions(XWikiContext context) | |
| 97 | { | |
| 98 | // There is no mean to define an always used extension for something else than a document extension now, | |
| 99 | // so for resources-based extensions, we return an emtpy set. | |
| 100 | // An idea for the future could be to have an API for plugins and components to register always used resources | |
| 101 | // extensions. | |
| 102 | 0 | return Collections.emptySet(); |
| 103 | } | |
| 104 | ||
| 105 | /** | |
| 106 | * {@inheritDoc} | |
| 107 | * <p> | |
| 108 | * Not supported for resource extensions. | |
| 109 | * </p> | |
| 110 | * | |
| 111 | * @see com.xpn.xwiki.plugin.skinx.AbstractSkinExtensionPlugin#hasPageExtensions(com.xpn.xwiki.XWikiContext) | |
| 112 | */ | |
| 113 | 0 | @Override |
| 114 | public boolean hasPageExtensions(XWikiContext context) | |
| 115 | { | |
| 116 | 0 | return false; |
| 117 | } | |
| 118 | } |