use of org.kohsuke.stapler.StaplerRequest in project hudson-2.x by hudson.
the class Hudson method getRootUrlFromRequest.
/**
* Gets the absolute URL of Hudson top page, such as "http://localhost/hudson/".
*
* <p>
* Unlike {@link #getRootUrl()}, which uses the manually configured value,
* this one uses the current request to reconstruct the URL. The benefit is
* that this is immune to the configuration mistake (users often fail to set the root URL
* correctly, especially when a migration is involved), but the downside
* is that unless you are processing a request, this method doesn't work.
*
* @since 1.263
*/
public String getRootUrlFromRequest() {
StaplerRequest req = Stapler.getCurrentRequest();
StringBuilder buf = new StringBuilder();
buf.append(req.getScheme() + "://");
buf.append(req.getServerName());
if (req.getServerPort() != 80) {
buf.append(':').append(req.getServerPort());
}
buf.append(req.getContextPath()).append('/');
return buf.toString();
}
use of org.kohsuke.stapler.StaplerRequest in project hudson-2.x by hudson.
the class Hudson method getRootUrl.
/**
* Gets the absolute URL of Hudson,
* such as "http://localhost/hudson/".
*
* <p>
* This method first tries to use the manually configured value, then
* fall back to {@link StaplerRequest#getRootPath()}.
* It is done in this order so that it can work correctly even in the face
* of a reverse proxy.
*
* @return
* This method returns null if this parameter is not configured by the user.
* The caller must gracefully deal with this situation.
* The returned URL will always have the trailing '/'.
* @since 1.66
* @see Descriptor#getCheckUrl(String)
* @see #getRootUrlFromRequest()
*/
public String getRootUrl() {
// for compatibility. the actual data is stored in Mailer
String url = Mailer.descriptor().getUrl();
if (url != null) {
return url;
}
StaplerRequest req = Stapler.getCurrentRequest();
if (req != null) {
return getRootUrlFromRequest();
}
return null;
}
use of org.kohsuke.stapler.StaplerRequest in project blueocean-plugin by jenkinsci.
the class GitScm method getStaplerRequest.
protected StaplerRequest getStaplerRequest() {
StaplerRequest request = Stapler.getCurrentRequest();
Objects.requireNonNull(request, "Must be called in HTTP request context");
return request;
}
use of org.kohsuke.stapler.StaplerRequest in project blueocean-plugin by jenkinsci.
the class GithubScm method getCredential.
private StandardUsernamePasswordCredentials getCredential() {
StaplerRequest request = Stapler.getCurrentRequest();
String credentialId = GithubCredentialUtils.computeCredentialId(getCredentialIdFromRequest(request), getId(), getUri());
User authenticatedUser = getAuthenticatedUser();
final StandardUsernamePasswordCredentials credential = CredentialsUtils.findCredential(credentialId, StandardUsernamePasswordCredentials.class, new BlueOceanDomainRequirement());
if (credential == null) {
throw new ServiceException.BadRequestException(String.format("Credential id: %s not found for user %s", credentialId, authenticatedUser.getId()));
}
return credential;
}
use of org.kohsuke.stapler.StaplerRequest in project blueocean-plugin by jenkinsci.
the class BitbucketServerScmContentProviderTest method unauthorizedSaveContentShouldFail.
@Test
public void unauthorizedSaveContentShouldFail() throws UnirestException, IOException {
User alice = User.get("alice");
alice.setFullName("Alice Cooper");
alice.addProperty(new Mailer.UserProperty("alice@jenkins-ci.org"));
String aliceCredentialId = createCredential(BitbucketServerScm.ID, alice);
StaplerRequest staplerRequest = mockStapler();
MultiBranchProject mbp = mockMbp(aliceCredentialId, alice);
GitContent content = new GitContent.Builder().autoCreateBranch(true).base64Data("bm9kZXsKICBlY2hvICdoZWxsbyB3b3JsZCEnCn0K").branch("master").message("new commit").owner("TESTP").path("README.md").repo("pipeline-demo-test").build();
when(staplerRequest.bindJSON(Mockito.eq(BitbucketScmSaveFileRequest.class), Mockito.any(JSONObject.class))).thenReturn(new BitbucketScmSaveFileRequest(content));
String request = "{\n" + " \"content\" : {\n" + " \"message\" : \"new commit\",\n" + " \"path\" : \"README.md\",\n" + " \"branch\" : \"master\",\n" + " \"repo\" : \"pipeline-demo-test\",\n" + " \"base64Data\" : " + "\"bm9kZXsKICBlY2hvICdoZWxsbyB3b3JsZCEnCn0K\"" + " }\n" + "}";
when(staplerRequest.getReader()).thenReturn(new BufferedReader(new StringReader(request), request.length()));
try {
new BitbucketServerScmContentProvider().saveContent(staplerRequest, mbp);
} catch (ServiceException.PreconditionRequired e) {
assertEquals("Can't access content from Bitbucket: no credential found", e.getMessage());
return;
}
fail("Should have failed with PreConditionException");
}
Aggregations