Exemplo n.º 1
0
  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 (query != null) {
          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);
  }
Exemplo n.º 2
0
 /** Finds the inner-most {@link SearchableModelObject} in scope. */
 public static String getSearchURL() {
   List list = Stapler.getCurrentRequest().getAncestors();
   for (int i = list.size() - 1; i >= 0; i--) {
     Ancestor anc = (Ancestor) list.get(i);
     if (anc.getObject() instanceof SearchableModelObject) return anc.getUrl() + "/search/";
   }
   return null;
 }
Exemplo n.º 3
0
 /**
  * Finds the given object in the ancestor list and returns its URL. This is used to determine the
  * "current" URL assigned to the given object, so that one can compute relative URLs from it.
  */
 public static String getNearestAncestorUrl(StaplerRequest req, Object it) {
   List list = req.getAncestors();
   for (int i = list.size() - 1; i >= 0; i--) {
     Ancestor anc = (Ancestor) list.get(i);
     if (anc.getObject() == it) return anc.getUrl();
   }
   return null;
 }
Exemplo n.º 4
0
 /** Creates merged search index for suggestion. */
 private SearchIndex makeSuggestIndex(StaplerRequest req) {
   SearchIndexBuilder builder = new SearchIndexBuilder();
   for (Ancestor a : req.getAncestors()) {
     if (a.getObject() instanceof SearchableModelObject) {
       SearchableModelObject smo = (SearchableModelObject) a.getObject();
       builder.add(smo.getSearchIndex());
     }
   }
   return builder.make();
 }
Exemplo n.º 5
0
 private SearchableModelObject findClosestSearchableModelObject(StaplerRequest req) {
   List<Ancestor> l = req.getAncestors();
   for (int i = l.size() - 1; i >= 0; i--) {
     Ancestor a = l.get(i);
     if (a.getObject() instanceof SearchableModelObject) {
       return (SearchableModelObject) a.getObject();
     }
   }
   return null;
 }
Exemplo n.º 6
0
  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;
      }
    }
    if (l == null) return null; // there was no Run object

    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);
  }
 /*
  * Used to store a reference to the Jenkins project related to this parameter.
  * A bit hacky, probably using another extension point would be a good idea.
  */
 @Override
 public ParameterDefinition newInstance(StaplerRequest req, JSONObject formData)
     throws hudson.model.Descriptor.FormException {
   List<Ancestor> ancestors = req.getAncestors();
   AbstractProject<?, ?> project = null;
   for (Ancestor ancestor : ancestors) {
     Object object = ancestor.getObject();
     if (object instanceof AbstractProject<?, ?>) {
       project = (AbstractProject<?, ?>) object;
       break;
     }
   }
   this.project = project;
   return super.newInstance(req, formData);
 }
Exemplo n.º 8
0
 /**
  * This version is so that the 'hasPermission' can degrade gracefully if "it" is not an {@link
  * AccessControlled} object.
  */
 public static boolean hasPermission(Object object, Permission permission)
     throws IOException, ServletException {
   if (permission == null) return true;
   if (object instanceof AccessControlled)
     return ((AccessControlled) object).hasPermission(permission);
   else {
     List<Ancestor> ancs = Stapler.getCurrentRequest().getAncestors();
     for (Ancestor anc : Iterators.reverse(ancs)) {
       Object o = anc.getObject();
       if (o instanceof AccessControlled) {
         return ((AccessControlled) o).hasPermission(permission);
       }
     }
     return Hudson.getInstance().hasPermission(permission);
   }
 }
Exemplo n.º 9
0
  /**
   * 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);
    }
  }
Exemplo n.º 10
0
  /** 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;

      assert ig instanceof Item; // if not, ig must have been the Hudson instance
      i = (Item) ig;
    }
  }
Exemplo n.º 11
0
 private String getCurrentDescriptorByNameUrl() {
   StaplerRequest req = Stapler.getCurrentRequest();
   Ancestor a = req.findAncestor(DescriptorByNameOwner.class);
   return a.getUrl();
 }