Search in sources :

Example 1 with Ancestor

use of org.kohsuke.stapler.Ancestor in project hudson-2.x by hudson.

the class Search method doIndex.

public void doIndex(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException {
    List<Ancestor> l = req.getAncestors();
    for (int i = l.size() - 1; i >= 0; i--) {
        Ancestor a = l.get(i);
        if (a.getObject() instanceof SearchableModelObject) {
            SearchableModelObject smo = (SearchableModelObject) a.getObject();
            SearchIndex index = smo.getSearchIndex();
            String query = req.getParameter("q");
            if (!StringUtils.isEmpty(query)) {
                SuggestedItem target = find(index, query);
                if (target != null) {
                    // found
                    rsp.sendRedirect2(a.getUrl() + target.getUrl());
                    return;
                }
            }
        }
    }
    // no exact match. show the suggestions
    rsp.setStatus(SC_NOT_FOUND);
    req.getView(this, "search-failed.jelly").forward(req, rsp);
}
Also used : Ancestor(org.kohsuke.stapler.Ancestor)

Example 2 with Ancestor

use of org.kohsuke.stapler.Ancestor in project blueocean-plugin by jenkinsci.

the class BlueOceanWebURLBuilder method getTryBlueOceanURLs.

/**
     * Get the {@link TryBlueOceanURLs} instance for the {@link ModelObject}
     * associated with the current Stapler request.
     *
     * @return The {@link TryBlueOceanURLs} instance for the current classic
     * Jenkins page. The URL to the Blue Ocean homepage is returned if a more
     * appropriate URL is not found.
     */
@Nonnull
public static TryBlueOceanURLs getTryBlueOceanURLs() {
    StaplerRequest staplerRequest = Stapler.getCurrentRequest();
    List<Ancestor> list = staplerRequest.getAncestors();
    // Blue Ocean page we can link onto.
    for (int i = list.size() - 1; i >= 0; i--) {
        Ancestor ancestor = list.get(i);
        Object object = ancestor.getObject();
        if (object instanceof ModelObject) {
            String blueUrl = toBlueOceanURL((ModelObject) object);
            if (blueUrl != null) {
                if (object instanceof Item) {
                    return new TryBlueOceanURLs(blueUrl, ((Item) object).getUrl());
                } else if (object instanceof Run) {
                    return new TryBlueOceanURLs(blueUrl, ((Run) object).getUrl());
                } else {
                    return new TryBlueOceanURLs(blueUrl);
                }
            } else if (object instanceof Item) {
                return new TryBlueOceanURLs(getBlueHome(), ((Item) object).getUrl());
            }
        }
    }
    // Otherwise just return Blue Ocean home.
    return new TryBlueOceanURLs(getBlueHome());
}
Also used : Item(hudson.model.Item) ModelObject(hudson.model.ModelObject) StaplerRequest(org.kohsuke.stapler.StaplerRequest) ModelObject(hudson.model.ModelObject) Run(hudson.model.Run) Ancestor(org.kohsuke.stapler.Ancestor) Nonnull(javax.annotation.Nonnull)

Example 3 with Ancestor

use of org.kohsuke.stapler.Ancestor in project hudson-2.x by hudson.

the class Functions method getRelativeLinkTo.

/**
     * Computes the relative path from the current page to the given item.
     */
public static String getRelativeLinkTo(Item p) {
    Map<Object, String> ancestors = new HashMap<Object, String>();
    View view = null;
    StaplerRequest request = Stapler.getCurrentRequest();
    for (Ancestor a : request.getAncestors()) {
        ancestors.put(a.getObject(), a.getRelativePath());
        if (a.getObject() instanceof View)
            view = (View) a.getObject();
    }
    String path = ancestors.get(p);
    if (path != null)
        return path;
    Item i = p;
    String url = "";
    while (true) {
        ItemGroup ig = i.getParent();
        url = i.getShortUrl() + url;
        if (ig == Hudson.getInstance()) {
            assert i instanceof TopLevelItem;
            if (view != null && view.contains((TopLevelItem) i)) {
                // if p and the current page belongs to the same view, then return a relative path
                return ancestors.get(view) + '/' + url;
            } else {
                // otherwise return a path from the root Hudson
                return request.getContextPath() + '/' + p.getUrl();
            }
        }
        path = ancestors.get(ig);
        if (path != null)
            return path + '/' + url;
        // if not, ig must have been the Hudson instance
        assert ig instanceof Item;
        i = (Item) ig;
    }
}
Also used : Item(hudson.model.Item) TopLevelItem(hudson.model.TopLevelItem) ItemGroup(hudson.model.ItemGroup) HashMap(java.util.HashMap) StaplerRequest(org.kohsuke.stapler.StaplerRequest) TopLevelItem(hudson.model.TopLevelItem) SearchableModelObject(hudson.search.SearchableModelObject) ModelObject(hudson.model.ModelObject) View(hudson.model.View) Ancestor(org.kohsuke.stapler.Ancestor)

Example 4 with Ancestor

use of org.kohsuke.stapler.Ancestor in project hudson-2.x by hudson.

the class Functions method checkPermission.

/**
     * This version is so that the 'checkPermission' on <tt>layout.jelly</tt>
     * degrades gracefully if "it" is not an {@link AccessControlled} object.
     * Otherwise it will perform no check and that problem is hard to notice.
     */
public static void checkPermission(Object object, Permission permission) throws IOException, ServletException {
    if (permission == null)
        return;
    if (object instanceof AccessControlled)
        checkPermission((AccessControlled) object, permission);
    else {
        List<Ancestor> ancs = Stapler.getCurrentRequest().getAncestors();
        for (Ancestor anc : Iterators.reverse(ancs)) {
            Object o = anc.getObject();
            if (o instanceof AccessControlled) {
                checkPermission((AccessControlled) o, permission);
                return;
            }
        }
        checkPermission(Hudson.getInstance(), permission);
    }
}
Also used : AccessControlled(hudson.security.AccessControlled) SearchableModelObject(hudson.search.SearchableModelObject) ModelObject(hudson.model.ModelObject) Ancestor(org.kohsuke.stapler.Ancestor)

Example 5 with Ancestor

use of org.kohsuke.stapler.Ancestor in project hudson-2.x by hudson.

the class Functions method decompose.

public static RunUrl decompose(StaplerRequest req) {
    List<Ancestor> ancestors = req.getAncestors();
    // find the first and last Run instances
    Ancestor f = null, l = null;
    for (Ancestor anc : ancestors) {
        if (anc.getObject() instanceof Run) {
            if (f == null)
                f = anc;
            l = anc;
        }
    }
    // there was no Run object
    if (l == null)
        return null;
    String head = f.getPrev().getUrl() + '/';
    String base = l.getUrl();
    String reqUri = req.getOriginalRequestURI();
    // Find "rest" or URI by removing N path components.
    // Not using reqUri.substring(f.getUrl().length()) to avoid mismatches due to
    // url-encoding or extra slashes.  Former may occur in Tomcat (despite the spec saying
    // this string is not decoded, Tomcat apparently decodes this string. You see ' '
    // instead of '%20', which is what the browser has sent), latter may occur in some
    // proxy or URL-rewriting setups where extra slashes are inadvertently added.
    String furl = f.getUrl();
    int slashCount = 0;
    // Count components in ancestor URL
    for (int i = furl.indexOf('/'); i >= 0; i = furl.indexOf('/', i + 1)) slashCount++;
    // Remove that many from request URL, ignoring extra slashes
    String rest = reqUri.replaceFirst("(?:/+[^/]*){" + slashCount + "}", "");
    return new RunUrl((Run) f.getObject(), head, base, rest);
}
Also used : Run(hudson.model.Run) Ancestor(org.kohsuke.stapler.Ancestor)

Aggregations

Ancestor (org.kohsuke.stapler.Ancestor)8 ModelObject (hudson.model.ModelObject)3 Item (hudson.model.Item)2 Run (hudson.model.Run)2 SearchableModelObject (hudson.search.SearchableModelObject)2 StaplerRequest (org.kohsuke.stapler.StaplerRequest)2 ItemGroup (hudson.model.ItemGroup)1 TopLevelItem (hudson.model.TopLevelItem)1 View (hudson.model.View)1 AccessControlled (hudson.security.AccessControlled)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Nonnull (javax.annotation.Nonnull)1 Cookie (javax.servlet.http.Cookie)1