jenkins.model.Jenkins.getRootUrl()方法的使用及代码示例

x33g5p2x  于2022-01-21 转载在 其他  
字(10.1k)|赞(0)|评价(0)|浏览(352)

本文整理了Java中jenkins.model.Jenkins.getRootUrl()方法的一些代码示例,展示了Jenkins.getRootUrl()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Jenkins.getRootUrl()方法的具体详情如下:
包路径:jenkins.model.Jenkins
类名称:Jenkins
方法名:getRootUrl

Jenkins.getRootUrl介绍

[英]Gets the absolute URL of Jenkins, such as http://localhost/jenkins/.

This method first tries to use the manually configured value, then fall back to #getRootUrlFromRequest. It is done in this order so that it can work correctly even in the face of a reverse proxy.
[中]获取Jenkins的绝对URL,例如http://localhost/jenkins/.
此方法首先尝试使用手动配置的值,然后返回到#getRootUrlFromRequest。它是按此顺序进行的,以便即使在面对反向代理时也能正常工作。

代码示例

代码示例来源:origin: jenkinsci/jenkins

  1. /**
  2. * Is Jenkins running in HTTPS?
  3. *
  4. * Note that we can't really trust {@link StaplerRequest#isSecure()} because HTTPS might be terminated
  5. * in the reverse proxy.
  6. */
  7. public boolean isRootUrlSecure() {
  8. String url = getRootUrl();
  9. return url!=null && url.startsWith("https");
  10. }

代码示例来源:origin: jenkinsci/jenkins

  1. @Override
  2. public String getUrlName() {
  3. String urlName = Jenkins.getInstance().getRootUrl() + URL_NAME;
  4. return urlName;
  5. }

代码示例来源:origin: jenkinsci/jenkins

  1. /**
  2. * Computes the key that identifies this Hudson among other Hudsons that the user has a credential for.
  3. */
  4. @VisibleForTesting
  5. String getPropertyKey() {
  6. Jenkins j = Jenkins.getActiveInstance();
  7. String url = j.getRootUrl();
  8. if (url!=null) return url;
  9. return j.getLegacyInstanceId();
  10. }

代码示例来源:origin: jenkinsci/jenkins

  1. /**
  2. * The URL of the user page.
  3. */
  4. @Exported(visibility = 999)
  5. public @Nonnull
  6. String getAbsoluteUrl() {
  7. return Jenkins.get().getRootUrl() + getUrl();
  8. }

代码示例来源:origin: jenkinsci/jenkins

  1. /**
  2. * Infers the hudson installation URL from the given request.
  3. */
  4. public static String inferHudsonURL(StaplerRequest req) {
  5. String rootUrl = Jenkins.getInstance().getRootUrl();
  6. if(rootUrl !=null)
  7. // prefer the one explicitly configured, to work with load-balancer, frontend, etc.
  8. return rootUrl;
  9. StringBuilder buf = new StringBuilder();
  10. buf.append(req.getScheme()).append("://");
  11. buf.append(req.getServerName());
  12. if(! (req.getScheme().equals("http") && req.getLocalPort()==80 || req.getScheme().equals("https") && req.getLocalPort()==443))
  13. buf.append(':').append(req.getLocalPort());
  14. buf.append(req.getContextPath()).append('/');
  15. return buf.toString();
  16. }

代码示例来源:origin: jenkinsci/jenkins

  1. /**
  2. * Obtains the host name of the Hudson server that clients can use to talk back to.
  3. * <p>
  4. * This is primarily used in {@code slave-agent.jnlp.jelly} to specify the destination
  5. * that the agents talk to.
  6. */
  7. public String getServerName() {
  8. // Try to infer this from the configured root URL.
  9. // This makes it work correctly when Hudson runs behind a reverse proxy.
  10. String url = Jenkins.getInstance().getRootUrl();
  11. try {
  12. if(url!=null) {
  13. String host = new URL(url).getHost();
  14. if(host!=null)
  15. return host;
  16. }
  17. } catch (MalformedURLException e) {
  18. // fall back to HTTP request
  19. }
  20. return Stapler.getCurrentRequest().getServerName();
  21. }

代码示例来源:origin: jenkinsci/jenkins

  1. /**
  2. * Returns the absolute URL of this item. This relies on the current
  3. * {@link StaplerRequest} to figure out what the host name is,
  4. * so can be used only during processing client requests.
  5. *
  6. * @return
  7. * absolute URL.
  8. * @throws IllegalStateException
  9. * if the method is invoked outside the HTTP request processing.
  10. *
  11. * @deprecated
  12. * This method shall <b>NEVER</b> be used during HTML page rendering, as it won't work with
  13. * network set up like Apache reverse proxy.
  14. * This method is only intended for the remote API clients who cannot resolve relative references
  15. * (even this won't work for the same reason, which should be fixed.)
  16. */
  17. @Deprecated
  18. default String getAbsoluteUrl() {
  19. String r = Jenkins.getInstance().getRootUrl();
  20. if(r==null)
  21. throw new IllegalStateException("Root URL isn't configured yet. Cannot compute absolute URL.");
  22. return Util.encode(r+getUrl());
  23. }

代码示例来源:origin: jenkinsci/jenkins

  1. /**
  2. * Resolve an avatar image URL string for the user.
  3. * Note that this method must be called from an HTTP request to be reliable; else use {@link #resolveOrNull}.
  4. * @param u user
  5. * @param avatarSize the preferred image size, "[width]x[height]"
  6. * @return a URL string for a user Avatar image.
  7. */
  8. public static String resolve(User u, String avatarSize) {
  9. String avatar = resolveOrNull(u, avatarSize);
  10. return avatar != null ? avatar : Jenkins.getInstance().getRootUrl() + Functions.getResourcePath() + "/images/" + avatarSize + "/user.png";
  11. }

代码示例来源:origin: jenkinsci/jenkins

  1. /**
  2. * Gets the absolute URL of this view.
  3. */
  4. @Exported(visibility=2,name="url")
  5. public String getAbsoluteUrl() {
  6. return Jenkins.getInstance().getRootUrl()+getUrl();
  7. }

代码示例来源:origin: jenkinsci/gitlab-plugin

  1. private StringBuilder retrieveProjectUrl(Job<?, ?> project) {
  2. return new StringBuilder()
  3. .append(Jenkins.getInstance().getRootUrl())
  4. .append(GitLabWebHook.WEBHOOK_URL)
  5. .append(retrieveParentUrl(project))
  6. .append('/').append(Util.rawEncode(project.getName()));
  7. }

代码示例来源:origin: jenkinsci/jenkins

  1. @Override
  2. public ConsoleAnnotator annotate(Object context, MarkupText text, int charPos) {
  3. String url = this.url;
  4. if (url.startsWith("/")) {
  5. StaplerRequest req = Stapler.getCurrentRequest();
  6. if (req!=null) {
  7. // if we are serving HTTP request, we want to use app relative URL
  8. url = req.getContextPath()+url;
  9. } else {
  10. // otherwise presumably this is rendered for e-mails and other non-HTTP stuff
  11. url = Jenkins.getInstance().getRootUrl()+url.substring(1);
  12. }
  13. }
  14. text.addMarkup(charPos, charPos + length, "<a href='" + url + "'"+extraAttributes()+">", "</a>");
  15. return null;
  16. }

代码示例来源:origin: jenkinsci/jenkins

  1. public HttpResponse doTest() {
  2. String referer = Stapler.getCurrentRequest().getReferer();
  3. Jenkins j = Jenkins.getInstance();
  4. // May need to send an absolute URL, since handling of HttpRedirect with a relative URL does not currently honor X-Forwarded-Proto/Port at all.
  5. String redirect = j.getRootUrl() + "administrativeMonitor/" + id + "/testForReverseProxySetup/" + (referer != null ? Util.rawEncode(referer) : "NO-REFERER") + "/";
  6. LOGGER.log(Level.FINE, "coming from {0} and redirecting to {1}", new Object[] {referer, redirect});
  7. return new HttpRedirect(redirect);
  8. }

代码示例来源:origin: jenkinsci/jenkins

  1. String rootURL = jenkins.getRootUrl();
  2. if (rootURL==null) return null;

代码示例来源:origin: jenkinsci/jenkins

  1. tag(rsp,"url", jenkins.getRootUrl());
  2. tag(rsp,"server-id", jenkins.getLegacyInstanceId());
  3. tag(rsp,"slave-port",tal==null?null:tal.getPort());

代码示例来源:origin: jenkinsci/jenkins

  1. /**
  2. * Sends the RSS feed to the client.
  3. *
  4. * @param title
  5. * Title of the feed.
  6. * @param url
  7. * URL of the model object that owns this feed. Relative to the context root.
  8. * @param entries
  9. * Entries to be listed in the RSS feed.
  10. * @param adapter
  11. * Controls how to render entries to RSS.
  12. */
  13. public static <E> void forwardToRss(String title, String url, Collection<? extends E> entries, FeedAdapter<E> adapter, StaplerRequest req, HttpServletResponse rsp) throws IOException, ServletException {
  14. req.setAttribute("adapter",adapter);
  15. req.setAttribute("title",title);
  16. req.setAttribute("url",url);
  17. req.setAttribute("entries",entries);
  18. req.setAttribute("rootURL", Jenkins.getInstance().getRootUrl());
  19. String flavor = req.getParameter("flavor");
  20. if(flavor==null) flavor="atom";
  21. flavor = flavor.replace('/', '_'); // Don't allow path to any jelly
  22. req.getView(Jenkins.getInstance(),"/hudson/"+flavor+".jelly").forward(req,rsp);
  23. }
  24. }

代码示例来源:origin: jenkinsci/jenkins

  1. /**
  2. * Creates an environment variable override to be used for launching processes on this node.
  3. *
  4. * @see ProcStarter#envs(Map)
  5. * @since 1.489
  6. */
  7. public @Nonnull EnvVars buildEnvironment(@Nonnull TaskListener listener) throws IOException, InterruptedException {
  8. EnvVars env = new EnvVars();
  9. Node node = getNode();
  10. if (node==null) return env; // bail out
  11. for (NodeProperty nodeProperty: Jenkins.getInstance().getGlobalNodeProperties()) {
  12. nodeProperty.buildEnvVars(env,listener);
  13. }
  14. for (NodeProperty nodeProperty: node.getNodeProperties()) {
  15. nodeProperty.buildEnvVars(env,listener);
  16. }
  17. // TODO: hmm, they don't really belong
  18. String rootUrl = Jenkins.getInstance().getRootUrl();
  19. if(rootUrl!=null) {
  20. env.put("HUDSON_URL", rootUrl); // Legacy.
  21. env.put("JENKINS_URL", rootUrl);
  22. }
  23. return env;
  24. }

代码示例来源:origin: jenkinsci/jenkins

  1. @Override
  2. public void buildEnvironmentFor(Run r, EnvVars env, TaskListener listener) throws IOException, InterruptedException {
  3. Computer c = Computer.currentComputer();
  4. if (c!=null){
  5. EnvVars compEnv = c.getEnvironment().overrideAll(env);
  6. env.putAll(compEnv);
  7. }
  8. env.put("BUILD_DISPLAY_NAME",r.getDisplayName());
  9. Jenkins j = Jenkins.getInstance();
  10. String rootUrl = j.getRootUrl();
  11. if(rootUrl!=null) {
  12. env.put("BUILD_URL", rootUrl+r.getUrl());
  13. }
  14. }

代码示例来源:origin: jenkinsci/jenkins

  1. @Override
  2. public void buildEnvironmentFor(Job j, EnvVars env, TaskListener listener) throws IOException, InterruptedException {
  3. Jenkins jenkins = Jenkins.getInstance();
  4. String rootUrl = jenkins.getRootUrl();
  5. if(rootUrl!=null) {
  6. env.put("JENKINS_URL", rootUrl);
  7. env.put("HUDSON_URL", rootUrl); // Legacy compatibility
  8. env.put("JOB_URL", rootUrl+j.getUrl());
  9. }
  10. String root = jenkins.getRootDir().getPath();
  11. env.put("JENKINS_HOME", root);
  12. env.put("HUDSON_HOME", root); // legacy compatibility
  13. Thread t = Thread.currentThread();
  14. if (t instanceof Executor) {
  15. Executor e = (Executor) t;
  16. env.put("EXECUTOR_NUMBER", String.valueOf(e.getNumber()));
  17. if (e.getOwner() instanceof MasterComputer) {
  18. env.put("NODE_NAME", "master");
  19. } else {
  20. env.put("NODE_NAME", e.getOwner().getName());
  21. }
  22. Node n = e.getOwner().getNode();
  23. if (n != null)
  24. env.put("NODE_LABELS", Util.join(n.getAssignedLabels(), " "));
  25. }
  26. }
  27. }

代码示例来源:origin: jenkinsci/jenkins

  1. j.getRootUrl(), Util.escape(l.getName()), l.getUrl(), l.getNodes().size(), l.getClouds().size())
  2. );

代码示例来源:origin: jenkinsci/jenkins

  1. /**
  2. * Exposes the name/value as an environment variable.
  3. */
  4. @Override
  5. public void buildEnvironment(Run<?,?> build, EnvVars env) {
  6. Run run = getRun();
  7. String value = (null == run) ? "UNKNOWN" : Jenkins.getInstance().getRootUrl() + run.getUrl();
  8. env.put(name, value);
  9. env.put(name + ".jobName", getJobName()); // undocumented, left for backward compatibility
  10. env.put(name + "_JOBNAME", getJobName()); // prefer this version
  11. env.put(name + ".number" , getNumber ()); // same as above
  12. env.put(name + "_NUMBER" , getNumber ());
  13. // if run is null, default to the standard '#1' display name format
  14. env.put(name + "_NAME", (null == run) ? "#" + getNumber() : run.getDisplayName()); // since 1.504
  15. String buildResult = (null == run || null == run.getResult()) ? "UNKNOWN" : run.getResult().toString();
  16. env.put(name + "_RESULT", buildResult); // since 1.517
  17. env.put(name.toUpperCase(Locale.ENGLISH),value); // backward compatibility pre 1.345
  18. }

相关文章

Jenkins类方法