Search in sources :

Example 51 with StaplerRequest

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();
}
Also used : StaplerRequest(org.kohsuke.stapler.StaplerRequest)

Example 52 with StaplerRequest

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;
}
Also used : StaplerRequest(org.kohsuke.stapler.StaplerRequest)

Example 53 with StaplerRequest

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;
}
Also used : StaplerRequest(org.kohsuke.stapler.StaplerRequest)

Example 54 with StaplerRequest

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;
}
Also used : StandardUsernamePasswordCredentials(com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials) GHUser(org.kohsuke.github.GHUser) User(hudson.model.User) BlueOceanDomainRequirement(io.jenkins.blueocean.rest.impl.pipeline.credential.BlueOceanDomainRequirement) StaplerRequest(org.kohsuke.stapler.StaplerRequest)

Example 55 with StaplerRequest

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");
}
Also used : User(hudson.model.User) StaplerRequest(org.kohsuke.stapler.StaplerRequest) Mailer(hudson.tasks.Mailer) GitContent(io.jenkins.blueocean.rest.impl.pipeline.scm.GitContent) JSONObject(net.sf.json.JSONObject) ServiceException(io.jenkins.blueocean.commons.ServiceException) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) BitbucketScmSaveFileRequest(io.jenkins.blueocean.blueocean_bitbucket_pipeline.BitbucketScmSaveFileRequest) MultiBranchProject(jenkins.branch.MultiBranchProject) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

StaplerRequest (org.kohsuke.stapler.StaplerRequest)79 Test (org.junit.Test)45 MultiBranchProject (jenkins.branch.MultiBranchProject)28 JSONObject (net.sf.json.JSONObject)20 GitContent (io.jenkins.blueocean.rest.impl.pipeline.scm.GitContent)18 ServiceException (io.jenkins.blueocean.commons.ServiceException)17 BufferedReader (java.io.BufferedReader)16 StringReader (java.io.StringReader)16 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)15 User (hudson.model.User)14 Mailer (hudson.tasks.Mailer)13 StaplerResponse (org.kohsuke.stapler.StaplerResponse)8 File (java.io.File)7 BitbucketScmSaveFileRequest (io.jenkins.blueocean.blueocean_bitbucket_pipeline.BitbucketScmSaveFileRequest)6 ScmFile (io.jenkins.blueocean.rest.impl.pipeline.scm.ScmFile)5 HashMap (java.util.HashMap)5 List (java.util.List)5 Nonnull (javax.annotation.Nonnull)5 Item (hudson.model.Item)4 ArrayList (java.util.ArrayList)4