Example usage for org.springframework.http HttpHeaders set

List of usage examples for org.springframework.http HttpHeaders set

Introduction

In this page you can find the example usage for org.springframework.http HttpHeaders set.

Prototype

@Override
public void set(String headerName, @Nullable String headerValue) 

Source Link

Document

Set the given, single header value under the given name.

Usage

From source file:com.interop.webapp.WebAppTests.java

private HttpHeaders getHeaders() {
    HttpHeaders headers = new HttpHeaders();
    ResponseEntity<String> page = new TestRestTemplate()
            .getForEntity("http://localhost:" + this.port + "/login", String.class);
    assertEquals(HttpStatus.OK, page.getStatusCode());
    String cookie = page.getHeaders().getFirst("Set-Cookie");
    headers.set("Cookie", cookie);
    Matcher matcher = Pattern.compile("(?s).*name=\"_csrf\".*?value=\"([^\"]+).*").matcher(page.getBody());
    assertTrue("No csrf token: " + page.getBody(), matcher.matches());
    headers.set("X-CSRF-TOKEN", matcher.group(1));
    return headers;
}

From source file:net.solarnetwork.node.setup.web.NodeCertificatesController.java

/**
 * Return a node's current certificate./*from   w w w . j a  v  a2s .c  o  m*/
 * 
 * @return a map with the PEM encoded certificate on key {@code cert} if
 *         {@code download} is not <em>true</em>, otherwise the content is
 *         returned as a file attachment
 */
@RequestMapping(value = "/nodeCert", method = RequestMethod.GET)
@ResponseBody
public Object viewNodeCert(@RequestParam(value = "download", required = false) final Boolean download,
        @RequestParam(value = "chain", required = false) final Boolean asChain) {
    final String cert = (Boolean.TRUE.equals(asChain) ? pkiService.generateNodePKCS7CertificateChainString()
            : pkiService.generateNodePKCS7CertificateString());

    if (!Boolean.TRUE.equals(download)) {
        Map<String, Object> result = new HashMap<String, Object>(1);
        result.put("cert", cert);
        return result;
    }

    HttpHeaders headers = new HttpHeaders();
    headers.setContentLength(cert.length());
    headers.setContentType(MediaType.parseMediaType("application/x-pem-file"));
    headers.setLastModified(System.currentTimeMillis());
    headers.setCacheControl("no-cache");

    headers.set("Content-Disposition",
            "attachment; filename=solarnode-" + getIdentityService().getNodeId() + ".pem");

    return new ResponseEntity<String>(cert, headers, HttpStatus.OK);
}

From source file:org.cloudfoundry.identity.uaa.integration.ClientAdminEndpointsIntegrationTests.java

public HttpHeaders getAuthenticatedHeaders(OAuth2AccessToken token) {
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
    headers.setContentType(MediaType.APPLICATION_JSON);
    headers.set("Authorization", "Bearer " + token.getValue());
    return headers;
}

From source file:com.appglu.impl.BasicAuthHttpRequestInterceptor.java

public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)
        throws IOException {
    HttpRequestWrapper wrapper = new HttpRequestWrapper(request) {

        @Override//from   w w w . j  av  a 2s.c o m
        public HttpHeaders getHeaders() {
            HttpHeaders headers = super.getHeaders();
            this.addBasicAuthHeader(headers);
            return headers;
        }

        private void addBasicAuthHeader(HttpHeaders headers) {
            String credentials = applicationKey + ":" + applicationSecret;
            String basicHeaderValue = "Basic " + Base64Utils.encodeBytes(credentials.getBytes());
            headers.set("Authorization", basicHeaderValue);
        }

    };

    return execution.execute(wrapper, body);
}

From source file:sparklr.common.AbstractAuthorizationCodeProviderTests.java

@Test
public void testInvalidAccessToken() throws Exception {

    // now make sure an unauthorized request fails the right way.
    HttpHeaders headers = new HttpHeaders();
    headers.set("Authorization", String.format("%s %s", OAuth2AccessToken.BEARER_TYPE, "FOO"));
    ResponseEntity<String> response = http.getForString("/admin/beans", headers);
    assertEquals(HttpStatus.UNAUTHORIZED, response.getStatusCode());

    String authenticate = response.getHeaders().getFirst("WWW-Authenticate");
    assertNotNull(authenticate);// w  w  w  .ja v  a  2s. co  m
    assertTrue(authenticate.startsWith("Bearer"));
    // Resource Server doesn't know what scopes are required until the token can be validated
    assertFalse(authenticate.contains("scope=\""));

}

From source file:com.ge.predix.acceptance.test.zone.admin.ZoneEnforcementStepsDefinitions.java

@When("^client_two does a PUT on (.*?) with (.*?) in zone (.*?)$")
public void client_two_does_a_PUT_on_subject_with_subject_id__in_zone(final String api, final String identifier,
        final String subdomainSuffix) throws Throwable {
    HttpHeaders zoneHeaders = new HttpHeaders();
    OAuth2RestTemplate acsTemplate = this.acsZone2Template;
    zoneHeaders.set(PolicyHelper.PREDIX_ZONE_ID, getZoneName(subdomainSuffix));

    try {/*from w ww.j a v a2  s  .c om*/
        switch (api) {
        case "subject":
            this.privilegeHelper.putSubject(acsTemplate, this.subject, this.acsUrl, zoneHeaders,
                    this.privilegeHelper.getDefaultAttribute());
            break;
        case "resource":
            this.privilegeHelper.putResource(acsTemplate, this.resource, this.acsUrl, zoneHeaders,
                    this.privilegeHelper.getDefaultAttribute());
            break;
        case "policy-set":
            this.testPolicyName = "single-action-defined-policy-set";
            CreatePolicyStatus s = this.policyHelper.createPolicySet(
                    "src/test/resources/single-action-defined-policy-set.json", acsTemplate, zoneHeaders);
            Assert.assertEquals(s, CreatePolicyStatus.SUCCESS);
            break;
        default:
            Assert.fail("Api " + api + " does not match/is not yet implemented for this test code.");
        }

    } catch (HttpClientErrorException e) {
        Assert.fail("Unable to PUT identifier: " + identifier + " for api: " + api, e);
    }
}

From source file:org.centralperf.controller.ScriptController.java

/**
 * Download the content of a script (content is only stored on the version level)
 * @param projectId   ID of the project/* w  w w .java 2  s .co  m*/
 * @param scriptId   ID of the script
 * @param versionId   ID of the version
 * @return the script content as application/octet-stream content (to download it instead of displaying it in the browser)
 */
@RequestMapping(value = "/project/{projectId}/script/{scriptId}/version/{versionId}/download", method = RequestMethod.GET, produces = "application/octet-stream")
public ResponseEntity<String> downloadScriptVersionContent(@PathVariable("projectId") Long projectId,
        @PathVariable("scriptId") Long scriptId, @PathVariable("versionId") Long versionId) {
    ScriptVersion scriptVersion = scriptVersionRepository.findOne(versionId);
    Sampler sampler = samplerService.getSamplerByUID(scriptVersion.getScript().getSamplerUID());
    HttpHeaders responseHeaders = new HttpHeaders();
    // Build the file name based on script label and script version number
    responseHeaders.set("Content-Disposition",
            "attachment; filename=" + scriptVersion.getScript().getLabel().replaceAll("\\s+", "_") + "_"
                    + scriptVersion.getNumber() + sampler.getScriptFileExtension());
    return new ResponseEntity<String>(scriptVersion.getContent(), responseHeaders, HttpStatus.CREATED);
}

From source file:com.zhm.config.MyAuthorizationCodeAccessTokenProvider.java

private HttpHeaders getHeadersForAuthorizationRequest(AccessTokenRequest request) {
    HttpHeaders headers = new HttpHeaders();
    headers.putAll(request.getHeaders());
    if (request.getCookie() != null) {
        headers.set("Cookie", request.getCookie());
    }/*from  w ww . j  ava2s. c o  m*/
    return headers;
}