/*
* The MIT License
*
* Copyright (c) 2004-2011, Sun Microsystems, Inc., Kohsuke Kawaguchi, Tom Huybrechts,
* Yahoo!, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package hudson.model;
import com.thoughtworks.xstream.converters.ConversionException;
import com.thoughtworks.xstream.io.StreamException;
import com.thoughtworks.xstream.io.xml.XppDriver;
import hudson.DescriptorExtensionList;
import hudson.Extension;
import hudson.ExtensionPoint;
import hudson.Indenter;
import hudson.Util;
import hudson.XmlFile;
import hudson.model.Descriptor.FormException;
import hudson.model.Node.Mode;
import hudson.model.labels.LabelAtom;
import hudson.model.labels.LabelAtomPropertyDescriptor;
import hudson.model.listeners.SaveableListener;
import hudson.scm.ChangeLogSet.Entry;
import hudson.search.CollectionSearchIndex;
import hudson.search.SearchIndexBuilder;
import hudson.security.ACL;
import hudson.security.AccessControlled;
import hudson.security.Permission;
import hudson.security.PermissionGroup;
import hudson.security.PermissionScope;
import hudson.util.AlternativeUiTextProvider;
import hudson.util.AlternativeUiTextProvider.Message;
import hudson.util.AtomicFileWriter;
import hudson.util.DescribableList;
import hudson.util.DescriptorList;
import hudson.util.IOException2;
import hudson.util.IOUtils;
import hudson.util.RunList;
import hudson.util.XStream2;
import hudson.views.ListViewColumn;
import hudson.widgets.Widget;
import jenkins.model.Jenkins;
import org.kohsuke.stapler.HttpResponse;
import org.kohsuke.stapler.HttpResponses;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
import org.kohsuke.stapler.WebMethod;
import org.kohsuke.stapler.export.Exported;
import org.kohsuke.stapler.export.ExportedBean;
import org.kohsuke.stapler.interceptor.RequirePOST;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import static javax.servlet.http.HttpServletResponse.SC_BAD_REQUEST;
import static jenkins.model.Jenkins.*;
/**
* Encapsulates the rendering of the list of {@link TopLevelItem}s
* that {@link Jenkins} owns.
*
*
* This is an extension point in Hudson, allowing different kind of
* rendering to be added as plugins.
*
*
Note for implementors
*
*
* {@link View} subtypes need the newViewDetail.jelly page,
* which is included in the "new view" page. This page should have some
* description of what the view is about.
*
*
* @author Kohsuke Kawaguchi
* @see ViewDescriptor
* @see ViewGroup
*/
@ExportedBean
public abstract class View extends AbstractModelObject implements AccessControlled, Describable, ExtensionPoint, Saveable {
/**
* Container of this view. Set right after the construction
* and never change thereafter.
*/
protected /*final*/ ViewGroup owner;
/**
* Name of this view.
*/
protected String name;
/**
* Message displayed in the view page.
*/
protected String description;
/**
* If true, only show relevant executors
*/
protected boolean filterExecutors;
/**
* If true, only show relevant queue items
*/
protected boolean filterQueue;
protected transient List transientActions;
/**
* List of {@link ViewProperty}s configured for this view.
* @since 1.406
*/
private volatile DescribableList properties = new PropertyList(this);
protected View(String name) {
this.name = name;
}
protected View(String name, ViewGroup owner) {
this.name = name;
this.owner = owner;
}
/**
* Gets all the items in this collection in a read-only view.
*/
@Exported(name="jobs")
public abstract Collection getItems();
/**
* Gets the {@link TopLevelItem} of the given name.
*/
public TopLevelItem getItem(String name) {
return getOwnerItemGroup().getItem(name);
}
/**
* Alias for {@link #getItem(String)}. This is the one used in the URL binding.
*/
public final TopLevelItem getJob(String name) {
return getItem(name);
}
/**
* Checks if the job is in this collection.
*/
public abstract boolean contains(TopLevelItem item);
/**
* Gets the name of all this collection.
*
* @see #rename(String)
*/
@Exported(visibility=2,name="name")
public String getViewName() {
return name;
}
/**
* Renames this view.
*/
public void rename(String newName) throws Failure, FormException {
if(name.equals(newName)) return; // noop
checkGoodName(newName);
if(owner.getView(newName)!=null)
throw new FormException(Messages.Hudson_ViewAlreadyExists(newName),"name");
String oldName = name;
name = newName;
owner.onViewRenamed(this,oldName,newName);
}
/**
* Gets the {@link ViewGroup} that this view belongs to.
*/
public ViewGroup getOwner() {
return owner;
}
/**
* Backward-compatible way of getting {@code getOwner().getItemGroup()}
*/
public ItemGroup extends TopLevelItem> getOwnerItemGroup() {
try {
return _getOwnerItemGroup();
} catch (AbstractMethodError e) {
return Hudson.getInstance();
}
}
/**
* A pointless function to work around what appears to be a HotSpot problem. See JENKINS-5756 and bug 6933067
* on BugParade for more details.
*/
private ItemGroup extends TopLevelItem> _getOwnerItemGroup() {
return owner.getItemGroup();
}
public View getOwnerPrimaryView() {
try {
return _getOwnerPrimaryView();
} catch (AbstractMethodError e) {
return null;
}
}
private View _getOwnerPrimaryView() {
return owner.getPrimaryView();
}
public List getOwnerViewActions() {
try {
return _getOwnerViewActions();
} catch (AbstractMethodError e) {
return Hudson.getInstance().getActions();
}
}
private List _getOwnerViewActions() {
return owner.getViewActions();
}
/**
* Message displayed in the top page. Can be null. Includes HTML.
*/
@Exported
public String getDescription() {
return description;
}
/**
* Gets the view properties configured for this view.
* @since 1.406
*/
public DescribableList getProperties() {
// readResolve was the best place to do this, but for compatibility reasons,
// this class can no longer have readResolve() (the mechanism itself isn't suitable for class hierarchy)
// see JENKINS-9431
//
// until we have that, putting this logic here.
synchronized (this) {
if (properties == null) {
properties = new PropertyList(this);
} else {
properties.setOwner(this);
}
}
return properties;
}
/**
* Returns all the {@link LabelAtomPropertyDescriptor}s that can be potentially configured
* on this label.
*/
public List getApplicablePropertyDescriptors() {
List r = new ArrayList();
for (ViewPropertyDescriptor pd : ViewProperty.all()) {
if (pd.isEnabledFor(this))
r.add(pd);
}
return r;
}
public void save() throws IOException {
// persistence is a part of the owner
// due to initialization timing issue, it can be null when this method is called
if (owner != null) {
owner.save();
}
}
/**
* List of all {@link ViewProperty}s exposed primarily for the remoting API.
* @since 1.406
*/
@Exported(name="property",inline=true)
public List getAllProperties() {
return getProperties().toList();
}
public ViewDescriptor getDescriptor() {
return (ViewDescriptor) Jenkins.getInstance().getDescriptorOrDie(getClass());
}
public String getDisplayName() {
return getViewName();
}
public String getNewPronoun() {
return AlternativeUiTextProvider.get(NEW_PRONOUN, this, Messages.AbstractItem_Pronoun());
}
/**
* By default, return true to render the "Edit view" link on the page.
* This method is really just for the default "All" view to hide the edit link
* so that the default Hudson top page remains the same as before 1.316.
*
* @since 1.316
*/
public boolean isEditable() {
return true;
}
/**
* If true, only show relevant executors
*/
public boolean isFilterExecutors() {
return filterExecutors;
}
/**
* If true, only show relevant queue items
*/
public boolean isFilterQueue() {
return filterQueue;
}
/**
* Gets the {@link Widget}s registered on this object.
*
*
* For now, this just returns the widgets registered to Hudson.
*/
public List getWidgets() {
return Collections.unmodifiableList(Jenkins.getInstance().getWidgets());
}
/**
* If this view uses <t:projectView> for rendering, this method returns columns to be displayed.
*/
public Iterable extends ListViewColumn> getColumns() {
return ListViewColumn.createDefaultInitialColumnList();
}
/**
* If this view uses <t:projectView> for rendering, this method returns the indenter used
* to indent each row.
*/
public Indenter getIndenter() {
return null;
}
/**
* If true, this is a view that renders the top page of Hudson.
*/
public boolean isDefault() {
return getOwnerPrimaryView()==this;
}
public List getComputers() {
Computer[] computers = Jenkins.getInstance().getComputers();
if (!isFilterExecutors()) {
return Arrays.asList(computers);
}
List result = new ArrayList();
HashSet