PageRenderTime 14ms CodeModel.GetById 1ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/hudson-plugin-utils/src/main/java/org/hudsonci/utils/plugin/ui/UIComponentSupport.java

http://github.com/hudson/hudson
Java | 213 lines | 120 code | 33 blank | 60 comment | 7 complexity | 1220df1297f6961b50e60406108fece8 MD5 | raw file
  1/**
  2 * The MIT License
  3 *
  4 * Copyright (c) 2010-2011 Sonatype, Inc. All rights reserved.
  5 *
  6 * Permission is hereby granted, free of charge, to any person obtaining a copy
  7 * of this software and associated documentation files (the "Software"), to deal
  8 * in the Software without restriction, including without limitation the rights
  9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 10 * copies of the Software, and to permit persons to whom the Software is
 11 * furnished to do so, subject to the following conditions:
 12 *
 13 * The above copyright notice and this permission notice shall be included in
 14 * all copies or substantial portions of the Software.
 15 *
 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 22 * THE SOFTWARE.
 23 */
 24
 25package org.hudsonci.utils.plugin.ui;
 26
 27import org.hudsonci.inject.internal.plugin.PluginClassLoader;
 28
 29import hudson.PluginWrapper;
 30import hudson.model.Action;
 31import hudson.model.Hudson;
 32import hudson.security.Permission;
 33import org.kohsuke.stapler.Ancestor;
 34import org.kohsuke.stapler.Stapler;
 35import org.kohsuke.stapler.StaplerRequest;
 36import org.kohsuke.stapler.StaplerResponse;
 37import org.slf4j.Logger;
 38import org.slf4j.LoggerFactory;
 39
 40import java.io.IOException;
 41
 42import static com.google.common.base.Preconditions.checkState;
 43
 44/**
 45 * Support for UI components.
 46 *
 47 * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
 48 * @since 2.1.0
 49 */
 50public abstract class UIComponentSupport<P extends Action>
 51    implements Action
 52{
 53    protected final Logger log = LoggerFactory.getLogger(getClass());
 54
 55    protected final P parent;
 56
 57    protected UIComponentSupport(final P parent) {
 58        this.parent = parent;
 59    }
 60
 61    protected UIComponentSupport() {
 62        this(null);
 63    }
 64
 65    public P getParent() {
 66        return parent;
 67    }
 68
 69    /**
 70     * Returns our parent's icon.
 71     */
 72    public String getIconFileName() {
 73        if (parent != null) {
 74            return getParent().getIconFileName();
 75        }
 76        return null;
 77    }
 78
 79//    @Override
 80//    public String getDisplayName() {
 81//        if (parent != null) {
 82//            return getParent().getDisplayName();
 83//        }
 84//        return null;
 85//    }
 86
 87    protected String getIconFileName(final String name) {
 88        assert name != null;
 89        // This path is relative to the context path, not root
 90        return String.format("/plugin/%s/images/%s", getPluginName(), name);
 91    }
 92
 93    /**
 94     * No URL.
 95     */
 96    public String getUrlName() {
 97        return null;
 98    }
 99
100    // FIXME: Sync up with PluginUtil helpers
101
102    /**
103     * Returns the {@link PluginWrapper} for the current class context.
104     *
105     * @throws IllegalStateException Unable to determine plugin wrapper source.
106     */
107    protected PluginWrapper getPluginWrapper() {
108        ClassLoader cl = getClass().getClassLoader();
109        if (cl instanceof PluginClassLoader) {
110            return ((PluginClassLoader)cl).getPlugin();
111        }
112        throw new IllegalStateException();
113    }
114
115    @JellyAccessible
116    public String getRootPath() {
117        StaplerRequest req = Stapler.getCurrentRequest();
118        checkState(req != null, "StaplerRequest not bound");
119        return req.getContextPath();
120    }
121
122    @JellyAccessible
123    public String getPluginName() {
124        return getPluginWrapper().getShortName();
125    }
126
127    @JellyAccessible
128    public String getPluginPath() {
129        return String.format("%s/plugin/%s", getRootPath(), getPluginName());
130    }
131
132    @JellyAccessible
133    public String getImagesPath() {
134        return String.format("%s/images", getPluginPath());
135    }
136
137    @JellyAccessible
138    public String getHelpPath() {
139        // Help path is relative to the context path, not root
140        return String.format("/plugin/%s/help", getPluginName());
141    }
142
143    @JellyAccessible
144    public String getIconPath() {
145        String iconPath = getIconFileName();
146        if (iconPath.startsWith("/")) {
147            iconPath = iconPath.substring(1, iconPath.length());
148        }
149        return String.format("%s/%s", getRootPath(), iconPath);
150    }
151
152    /**
153     * The object which owns the <tt>sidepanel.jelly</tt> view.
154     */
155    @JellyAccessible
156    public Object getSidePanelOwner() {
157        if (parent instanceof UIComponentSupport) {
158            return ((UIComponentSupport)parent).getSidePanelOwner();
159        }
160        return parent;
161    }
162
163    //
164    // TODO: Add getBaseUrl() which should end up similar to getSidePanelOwner().getUrlName()
165    //
166    
167    /**
168     * The permission needed to render the components view.
169     */
170    @JellyAccessible
171    public Permission getViewPermission() {
172        return Hudson.READ;
173    }
174    
175    @JellyAccessible
176    public String getPageTitle() {
177        return getDisplayName();
178    }
179
180    protected void checkPermission(final Permission perm) {
181        // TODO: inject
182        Hudson.getInstance().checkPermission(perm);
183    }
184
185    protected void redirect(final StaplerRequest req, final StaplerResponse resp, final String location) throws IOException {
186        log.trace("Redirecting to: {}", location);
187        resp.sendRedirect(location);
188    }
189
190    protected void redirectAncestor(final StaplerRequest req, final StaplerResponse resp, final Class type) throws IOException {
191        log.trace("Redirect ancestor: {}", type);
192        redirect(req, resp, req.findAncestor(type).getUrl());
193    }
194
195    protected void redirectAncestor(final StaplerRequest req, final StaplerResponse resp, final Object obj) throws IOException {
196        log.trace("Redirect ancestor: {}", obj);
197        Ancestor ancestor = req.findAncestor(obj);
198        redirect(req, resp, ancestor.getUrl());
199    }
200
201    protected void redirectParent(final StaplerRequest req, final StaplerResponse resp) throws IOException {
202        redirectAncestor(req, resp, getParent());
203    }
204
205    protected void redirectSelf(final StaplerRequest req, final StaplerResponse resp) throws IOException {
206        redirectAncestor(req, resp, this);
207    }
208
209    @JellyAccessible
210    public String getBaseRestURI() {
211        return String.format("%s/rest/plugin/%s", getRootPath(), getPluginName());
212    }
213}