/** * Is Jenkins running in HTTPS? * * Note that we can't really trust {@link StaplerRequest#isSecure()} because HTTPS might be terminated * in the reverse proxy. */ public boolean isRootUrlSecure() { String url = getRootUrl(); return url!=null && url.startsWith("https"); }
@Override public String getUrlName() { String urlName = Jenkins.getInstance().getRootUrl() + URL_NAME; return urlName; }
/** * Computes the key that identifies this Hudson among other Hudsons that the user has a credential for. */ @VisibleForTesting String getPropertyKey() { Jenkins j = Jenkins.getActiveInstance(); String url = j.getRootUrl(); if (url!=null) return url; return j.getLegacyInstanceId(); }
/** * The URL of the user page. */ @Exported(visibility = 999) public @Nonnull String getAbsoluteUrl() { return Jenkins.get().getRootUrl() + getUrl(); }
/** * Infers the hudson installation URL from the given request. */ public static String inferHudsonURL(StaplerRequest req) { String rootUrl = Jenkins.getInstance().getRootUrl(); if(rootUrl !=null) // prefer the one explicitly configured, to work with load-balancer, frontend, etc. return rootUrl; StringBuilder buf = new StringBuilder(); buf.append(req.getScheme()).append("://"); buf.append(req.getServerName()); if(! (req.getScheme().equals("http") && req.getLocalPort()==80 || req.getScheme().equals("https") && req.getLocalPort()==443)) buf.append(':').append(req.getLocalPort()); buf.append(req.getContextPath()).append('/'); return buf.toString(); }
/** * Obtains the host name of the Hudson server that clients can use to talk back to. * <p> * This is primarily used in {@code slave-agent.jnlp.jelly} to specify the destination * that the agents talk to. */ public String getServerName() { // Try to infer this from the configured root URL. // This makes it work correctly when Hudson runs behind a reverse proxy. String url = Jenkins.getInstance().getRootUrl(); try { if(url!=null) { String host = new URL(url).getHost(); if(host!=null) return host; } } catch (MalformedURLException e) { // fall back to HTTP request } return Stapler.getCurrentRequest().getServerName(); }
/** * Returns the absolute URL of this item. This relies on the current * {@link StaplerRequest} to figure out what the host name is, * so can be used only during processing client requests. * * @return * absolute URL. * @throws IllegalStateException * if the method is invoked outside the HTTP request processing. * * @deprecated * This method shall <b>NEVER</b> be used during HTML page rendering, as it won't work with * network set up like Apache reverse proxy. * This method is only intended for the remote API clients who cannot resolve relative references * (even this won't work for the same reason, which should be fixed.) */ @Deprecated default String getAbsoluteUrl() { String r = Jenkins.getInstance().getRootUrl(); if(r==null) throw new IllegalStateException("Root URL isn't configured yet. Cannot compute absolute URL."); return Util.encode(r+getUrl()); }
/** * Resolve an avatar image URL string for the user. * Note that this method must be called from an HTTP request to be reliable; else use {@link #resolveOrNull}. * @param u user * @param avatarSize the preferred image size, "[width]x[height]" * @return a URL string for a user Avatar image. */ public static String resolve(User u, String avatarSize) { String avatar = resolveOrNull(u, avatarSize); return avatar != null ? avatar : Jenkins.getInstance().getRootUrl() + Functions.getResourcePath() + "/images/" + avatarSize + "/user.png"; }
/** * Gets the absolute URL of this view. */ @Exported(visibility=2,name="url") public String getAbsoluteUrl() { return Jenkins.getInstance().getRootUrl()+getUrl(); }
private StringBuilder retrieveProjectUrl(Job<?, ?> project) { return new StringBuilder() .append(Jenkins.getInstance().getRootUrl()) .append(GitLabWebHook.WEBHOOK_URL) .append(retrieveParentUrl(project)) .append('/').append(Util.rawEncode(project.getName())); }
@Override public ConsoleAnnotator annotate(Object context, MarkupText text, int charPos) { String url = this.url; if (url.startsWith("/")) { StaplerRequest req = Stapler.getCurrentRequest(); if (req!=null) { // if we are serving HTTP request, we want to use app relative URL url = req.getContextPath()+url; } else { // otherwise presumably this is rendered for e-mails and other non-HTTP stuff url = Jenkins.getInstance().getRootUrl()+url.substring(1); } } text.addMarkup(charPos, charPos + length, "<a href='" + url + "'"+extraAttributes()+">", "</a>"); return null; }
public HttpResponse doTest() { String referer = Stapler.getCurrentRequest().getReferer(); Jenkins j = Jenkins.getInstance(); // 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. String redirect = j.getRootUrl() + "administrativeMonitor/" + id + "/testForReverseProxySetup/" + (referer != null ? Util.rawEncode(referer) : "NO-REFERER") + "/"; LOGGER.log(Level.FINE, "coming from {0} and redirecting to {1}", new Object[] {referer, redirect}); return new HttpRedirect(redirect); }
String rootURL = jenkins.getRootUrl(); if (rootURL==null) return null;
tag(rsp,"url", jenkins.getRootUrl()); tag(rsp,"server-id", jenkins.getLegacyInstanceId()); tag(rsp,"slave-port",tal==null?null:tal.getPort());
/** * Sends the RSS feed to the client. * * @param title * Title of the feed. * @param url * URL of the model object that owns this feed. Relative to the context root. * @param entries * Entries to be listed in the RSS feed. * @param adapter * Controls how to render entries to RSS. */ public static <E> void forwardToRss(String title, String url, Collection<? extends E> entries, FeedAdapter<E> adapter, StaplerRequest req, HttpServletResponse rsp) throws IOException, ServletException { req.setAttribute("adapter",adapter); req.setAttribute("title",title); req.setAttribute("url",url); req.setAttribute("entries",entries); req.setAttribute("rootURL", Jenkins.getInstance().getRootUrl()); String flavor = req.getParameter("flavor"); if(flavor==null) flavor="atom"; flavor = flavor.replace('/', '_'); // Don't allow path to any jelly req.getView(Jenkins.getInstance(),"/hudson/"+flavor+".jelly").forward(req,rsp); } }
/** * Creates an environment variable override to be used for launching processes on this node. * * @see ProcStarter#envs(Map) * @since 1.489 */ public @Nonnull EnvVars buildEnvironment(@Nonnull TaskListener listener) throws IOException, InterruptedException { EnvVars env = new EnvVars(); Node node = getNode(); if (node==null) return env; // bail out for (NodeProperty nodeProperty: Jenkins.getInstance().getGlobalNodeProperties()) { nodeProperty.buildEnvVars(env,listener); } for (NodeProperty nodeProperty: node.getNodeProperties()) { nodeProperty.buildEnvVars(env,listener); } // TODO: hmm, they don't really belong String rootUrl = Jenkins.getInstance().getRootUrl(); if(rootUrl!=null) { env.put("HUDSON_URL", rootUrl); // Legacy. env.put("JENKINS_URL", rootUrl); } return env; }
@Override public void buildEnvironmentFor(Run r, EnvVars env, TaskListener listener) throws IOException, InterruptedException { Computer c = Computer.currentComputer(); if (c!=null){ EnvVars compEnv = c.getEnvironment().overrideAll(env); env.putAll(compEnv); } env.put("BUILD_DISPLAY_NAME",r.getDisplayName()); Jenkins j = Jenkins.getInstance(); String rootUrl = j.getRootUrl(); if(rootUrl!=null) { env.put("BUILD_URL", rootUrl+r.getUrl()); } }
@Override public void buildEnvironmentFor(Job j, EnvVars env, TaskListener listener) throws IOException, InterruptedException { Jenkins jenkins = Jenkins.getInstance(); String rootUrl = jenkins.getRootUrl(); if(rootUrl!=null) { env.put("JENKINS_URL", rootUrl); env.put("HUDSON_URL", rootUrl); // Legacy compatibility env.put("JOB_URL", rootUrl+j.getUrl()); } String root = jenkins.getRootDir().getPath(); env.put("JENKINS_HOME", root); env.put("HUDSON_HOME", root); // legacy compatibility Thread t = Thread.currentThread(); if (t instanceof Executor) { Executor e = (Executor) t; env.put("EXECUTOR_NUMBER", String.valueOf(e.getNumber())); if (e.getOwner() instanceof MasterComputer) { env.put("NODE_NAME", "master"); } else { env.put("NODE_NAME", e.getOwner().getName()); } Node n = e.getOwner().getNode(); if (n != null) env.put("NODE_LABELS", Util.join(n.getAssignedLabels(), " ")); } } }
j.getRootUrl(), Util.escape(l.getName()), l.getUrl(), l.getNodes().size(), l.getClouds().size()) );
/** * Exposes the name/value as an environment variable. */ @Override public void buildEnvironment(Run<?,?> build, EnvVars env) { Run run = getRun(); String value = (null == run) ? "UNKNOWN" : Jenkins.getInstance().getRootUrl() + run.getUrl(); env.put(name, value); env.put(name + ".jobName", getJobName()); // undocumented, left for backward compatibility env.put(name + "_JOBNAME", getJobName()); // prefer this version env.put(name + ".number" , getNumber ()); // same as above env.put(name + "_NUMBER" , getNumber ()); // if run is null, default to the standard '#1' display name format env.put(name + "_NAME", (null == run) ? "#" + getNumber() : run.getDisplayName()); // since 1.504 String buildResult = (null == run || null == run.getResult()) ? "UNKNOWN" : run.getResult().toString(); env.put(name + "_RESULT", buildResult); // since 1.517 env.put(name.toUpperCase(Locale.ENGLISH),value); // backward compatibility pre 1.345 }