/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. package org.hudsonci.utils.plugin.ui;
  25. import org.hudsonci.inject.internal.plugin.PluginClassLoader;
  26. import hudson.PluginWrapper;
  27. import hudson.model.Action;
  28. import hudson.model.Hudson;
  29. import hudson.security.Permission;
  30. import org.kohsuke.stapler.Ancestor;
  31. import org.kohsuke.stapler.Stapler;
  32. import org.kohsuke.stapler.StaplerRequest;
  33. import org.kohsuke.stapler.StaplerResponse;
  34. import org.slf4j.Logger;
  35. import org.slf4j.LoggerFactory;
  36. import java.io.IOException;
  37. import static com.google.common.base.Preconditions.checkState;
  38. /**
  39. * Support for UI components.
  40. *
  41. * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
  42. * @since 2.1.0
  43. */
  44. public abstract class UIComponentSupport<P extends Action>
  45. implements Action
  46. {
  47. protected final Logger log = LoggerFactory.getLogger(getClass());
  48. protected final P parent;
  49. protected UIComponentSupport(final P parent) {
  50. this.parent = parent;
  51. }
  52. protected UIComponentSupport() {
  53. this(null);
  54. }
  55. public P getParent() {
  56. return parent;
  57. }
  58. /**
  59. * Returns our parent's icon.
  60. */
  61. public String getIconFileName() {
  62. if (parent != null) {
  63. return getParent().getIconFileName();
  64. }
  65. return null;
  66. }
  67. // @Override
  68. // public String getDisplayName() {
  69. // if (parent != null) {
  70. // return getParent().getDisplayName();
  71. // }
  72. // return null;
  73. // }
  74. protected String getIconFileName(final String name) {
  75. assert name != null;
  76. // This path is relative to the context path, not root
  77. return String.format("/plugin/%s/images/%s", getPluginName(), name);
  78. }
  79. /**
  80. * No URL.
  81. */
  82. public String getUrlName() {
  83. return null;
  84. }
  85. // FIXME: Sync up with PluginUtil helpers
  86. /**
  87. * Returns the {@link PluginWrapper} for the current class context.
  88. *
  89. * @throws IllegalStateException Unable to determine plugin wrapper source.
  90. */
  91. protected PluginWrapper getPluginWrapper() {
  92. ClassLoader cl = getClass().getClassLoader();
  93. if (cl instanceof PluginClassLoader) {
  94. return ((PluginClassLoader)cl).getPlugin();
  95. }
  96. throw new IllegalStateException();
  97. }
  98. @JellyAccessible
  99. public String getRootPath() {
  100. StaplerRequest req = Stapler.getCurrentRequest();
  101. checkState(req != null, "StaplerRequest not bound");
  102. return req.getContextPath();
  103. }
  104. @JellyAccessible
  105. public String getPluginName() {
  106. return getPluginWrapper().getShortName();
  107. }
  108. @JellyAccessible
  109. public String getPluginPath() {
  110. return String.format("%s/plugin/%s", getRootPath(), getPluginName());
  111. }
  112. @JellyAccessible
  113. public String getImagesPath() {
  114. return String.format("%s/images", getPluginPath());
  115. }
  116. @JellyAccessible
  117. public String getHelpPath() {
  118. // Help path is relative to the context path, not root
  119. return String.format("/plugin/%s/help", getPluginName());
  120. }
  121. @JellyAccessible
  122. public String getIconPath() {
  123. String iconPath = getIconFileName();
  124. if (iconPath.startsWith("/")) {
  125. iconPath = iconPath.substring(1, iconPath.length());
  126. }
  127. return String.format("%s/%s", getRootPath(), iconPath);
  128. }
  129. /**
  130. * The object which owns the <tt>sidepanel.jelly</tt> view.
  131. */
  132. @JellyAccessible
  133. public Object getSidePanelOwner() {
  134. if (parent instanceof UIComponentSupport) {
  135. return ((UIComponentSupport)parent).getSidePanelOwner();
  136. }
  137. return parent;
  138. }
  139. //
  140. // TODO: Add getBaseUrl() which should end up similar to getSidePanelOwner().getUrlName()
  141. //
  142. /**
  143. * The permission needed to render the components view.
  144. */
  145. @JellyAccessible
  146. public Permission getViewPermission() {
  147. return Hudson.READ;
  148. }
  149. @JellyAccessible
  150. public String getPageTitle() {
  151. return getDisplayName();
  152. }
  153. protected void checkPermission(final Permission perm) {
  154. // TODO: inject
  155. Hudson.getInstance().checkPermission(perm);
  156. }
  157. protected void redirect(final StaplerRequest req, final StaplerResponse resp, final String location) throws IOException {
  158. log.trace("Redirecting to: {}", location);
  159. resp.sendRedirect(location);
  160. }
  161. protected void redirectAncestor(final StaplerRequest req, final StaplerResponse resp, final Class type) throws IOException {
  162. log.trace("Redirect ancestor: {}", type);
  163. redirect(req, resp, req.findAncestor(type).getUrl());
  164. }
  165. protected void redirectAncestor(final StaplerRequest req, final StaplerResponse resp, final Object obj) throws IOException {
  166. log.trace("Redirect ancestor: {}", obj);
  167. Ancestor ancestor = req.findAncestor(obj);
  168. redirect(req, resp, ancestor.getUrl());
  169. }
  170. protected void redirectParent(final StaplerRequest req, final StaplerResponse resp) throws IOException {
  171. redirectAncestor(req, resp, getParent());
  172. }
  173. protected void redirectSelf(final StaplerRequest req, final StaplerResponse resp) throws IOException {
  174. redirectAncestor(req, resp, this);
  175. }
  176. @JellyAccessible
  177. public String getBaseRestURI() {
  178. return String.format("%s/rest/plugin/%s", getRootPath(), getPluginName());
  179. }
  180. }