/hudson-core/src/main/java/hudson/model/ProxyView.java

http://github.com/hudson/hudson · Java · 146 lines · 86 code · 22 blank · 38 comment · 10 complexity · 59163838f4d28d2062c7d5d5f2cb30f4 MD5 · raw file

  1. /*
  2. * The MIT License
  3. *
  4. * Copyright (c) 2004-2009, Sun Microsystems, Inc., Tom Huybrechts
  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 hudson.model;
  25. import hudson.Extension;
  26. import hudson.Util;
  27. import hudson.model.Descriptor.FormException;
  28. import hudson.util.FormValidation;
  29. import java.io.IOException;
  30. import java.util.Collection;
  31. import javax.servlet.ServletException;
  32. import org.kohsuke.stapler.DataBoundConstructor;
  33. import org.kohsuke.stapler.QueryParameter;
  34. import org.kohsuke.stapler.Stapler;
  35. import org.kohsuke.stapler.StaplerFallback;
  36. import org.kohsuke.stapler.StaplerRequest;
  37. import org.kohsuke.stapler.StaplerResponse;
  38. /**
  39. * A view that delegates to another.
  40. *
  41. * TODO: this does not respond to renaming or deleting the proxied view.
  42. *
  43. * @author Tom Huybrechts
  44. *
  45. */
  46. public class ProxyView extends View implements StaplerFallback {
  47. private String proxiedViewName;
  48. @DataBoundConstructor
  49. public ProxyView(String name) {
  50. super(name);
  51. if (Hudson.getInstance().getView(name) != null) {
  52. // if this is a valid global view name, let's assume the
  53. // user wants to show it
  54. proxiedViewName = name;
  55. }
  56. }
  57. public View getProxiedView() {
  58. if (proxiedViewName == null) {
  59. // just so we avoid errors just after creation
  60. return Hudson.getInstance().getPrimaryView();
  61. } else {
  62. return Hudson.getInstance().getView(proxiedViewName);
  63. }
  64. }
  65. public String getProxiedViewName() {
  66. return proxiedViewName;
  67. }
  68. public void setProxiedViewName(String proxiedViewName) {
  69. this.proxiedViewName = proxiedViewName;
  70. }
  71. @Override
  72. public Collection<TopLevelItem> getItems() {
  73. return getProxiedView().getItems();
  74. }
  75. @Override
  76. public boolean contains(TopLevelItem item) {
  77. return getProxiedView().contains(item);
  78. }
  79. @Override
  80. public void onJobRenamed(Item item, String oldName, String newName) {
  81. if (oldName.equals(proxiedViewName)) {
  82. proxiedViewName = newName;
  83. }
  84. }
  85. @Override
  86. protected void submit(StaplerRequest req) throws IOException, ServletException, FormException {
  87. String proxiedViewName = req.getSubmittedForm().getString("proxiedViewName");
  88. if (Hudson.getInstance().getView(proxiedViewName) == null) {
  89. throw new FormException("Not an existing global view", "proxiedViewName");
  90. }
  91. this.proxiedViewName = proxiedViewName;
  92. }
  93. @Override
  94. public Item doCreateItem(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException {
  95. return getProxiedView().doCreateItem(req, rsp);
  96. }
  97. /**
  98. * Fails if a global view with the given name does not exist.
  99. */
  100. public FormValidation doViewExistsCheck(@QueryParameter String value) {
  101. checkPermission(View.CREATE);
  102. String view = Util.fixEmpty(value);
  103. if(view==null) return FormValidation.ok();
  104. if(Hudson.getInstance().getView(view)!=null)
  105. return FormValidation.ok();
  106. else
  107. return FormValidation.error(Messages.ProxyView_NoSuchViewExists(value));
  108. }
  109. @Extension
  110. public static class DescriptorImpl extends ViewDescriptor {
  111. @Override
  112. public String getDisplayName() {
  113. return Messages.ProxyView_DisplayName();
  114. }
  115. @Override
  116. public boolean isInstantiable() {
  117. // doesn't make sense to add a ProxyView to the global views
  118. return !(Stapler.getCurrentRequest().findAncestorObject(ViewGroup.class) instanceof Hudson);
  119. }
  120. }
  121. public Object getStaplerFallback() {
  122. return getProxiedView();
  123. }
  124. }