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:org.apigw.authserver.AuthorizationCodeProviderIntegrationtest.java

@Test
public void testUserDeniesConfirmation() throws Exception {
    log.debug("testUserDeniesConfirmation");
    String cookie = helper.loginAndGetConfirmationPage(CLIENT_ID, REDIRECT_URL, SCOPE);

    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
    headers.set("Cookie", cookie);

    MultiValueMap<String, String> formData = new LinkedMultiValueMap<String, String>();
    formData.add("user_oauth_approval", "false");
    ResponseEntity<Void> result = serverRunning.postForStatus("/apigw-auth-server-web/oauth/authorize", headers,
            formData);/*  w  w w .  j  av a2 s. com*/
    assertEquals(HttpStatus.FOUND, result.getStatusCode());

    String location = result.getHeaders().getFirst("Location");
    assertTrue(location.startsWith(REDIRECT_URL));
    assertTrue(location.substring(location.indexOf('?')).contains("error=access_denied"));
    assertTrue(location.contains("state=gzzFqB!!!"));
}

From source file:org.ngrinder.common.controller.BaseController.java

/**
 * Convert the given object into a {@link HttpEntity} containing the converted json message.
 *
 * @return {@link HttpEntity} class containing the converted json message
 *///www . jav  a  2 s.  com
public HttpEntity<String> successJsonHttpEntity() {
    HttpHeaders responseHeaders = new HttpHeaders();
    responseHeaders.set("content-type", "application/json; charset=UTF-8");
    responseHeaders.setPragma("no-cache");
    return toHttpEntity(successJson, responseHeaders);
}

From source file:org.ngrinder.common.controller.BaseController.java

/**
 * Convert the given object into a {@link HttpEntity} containing the converted json message.
 *
 * @return {@link HttpEntity} class containing the converted json message
 *///  w  w  w.  j a  va2  s  . c om
public HttpEntity<String> errorJsonHttpEntity() {
    HttpHeaders responseHeaders = new HttpHeaders();
    responseHeaders.set("content-type", "application/json; charset=UTF-8");
    responseHeaders.setPragma("no-cache");
    return toHttpEntity(errorJson, responseHeaders);
}

From source file:org.ngrinder.common.controller.BaseController.java

/**
 * Convert the object with the given serializer into {@link HttpEntity}.
 *
 * @param content    content//from w  w w .java  2s.com
 * @param serializer custom JSON serializer
 * @return json message
 */
public HttpEntity<String> toJsonHttpEntity(Object content, Gson serializer) {
    HttpHeaders responseHeaders = new HttpHeaders();
    responseHeaders.set("content-type", "application/json; charset=UTF-8");
    responseHeaders.setPragma("no-cache");
    return toHttpEntity(serializer.toJson(content), responseHeaders);
}

From source file:it.reply.orchestrator.service.OneDataServiceTest.java

private <T> HttpEntity<T> getEntity(String token) {
    HttpHeaders headers = new HttpHeaders();
    headers.set("macaroon", token);
    return new HttpEntity<>(headers);
}

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

private OAuth2AccessToken getClientCredentialsAccessToken(String scope) throws Exception {

    String clientId = testAccounts.getAdminClientId();
    String clientSecret = testAccounts.getAdminClientSecret();

    MultiValueMap<String, String> formData = new LinkedMultiValueMap<String, String>();
    formData.add("grant_type", "client_credentials");
    formData.add("client_id", clientId);
    formData.add("scope", scope);
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
    headers.set("Authorization",
            "Basic " + new String(Base64.encode(String.format("%s:%s", clientId, clientSecret).getBytes())));

    @SuppressWarnings("rawtypes")
    ResponseEntity<Map> response = serverRunning.postForMap("/oauth/token", formData, headers);
    assertEquals(HttpStatus.OK, response.getStatusCode());

    @SuppressWarnings("unchecked")
    OAuth2AccessToken accessToken = DefaultOAuth2AccessToken.valueOf(response.getBody());
    return accessToken;

}

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

@When("^client_two does a DELETE on (.*?) with (.*?) in zone (.*?)$")
public void client_two_does_a_DELETE_on_api_with_identifier_in_test_zone_dev(final String api,
        final String identifier, final String zone) throws Throwable {

    OAuth2RestTemplate acsTemplate = this.acsZone2Template;
    String zoneName = getZoneName(zone);

    HttpHeaders zoneHeaders = new HttpHeaders();
    zoneHeaders.set(PolicyHelper.PREDIX_ZONE_ID, zoneName);

    String encodedIdentifier = URLEncoder.encode(identifier, "UTF-8");
    URI uri = URI.create(this.acsUrl + ACS_VERSION + "/" + api + "/" + encodedIdentifier);
    try {/* ww  w  .  j ava2 s. c om*/
        this.status = acsTemplate
                .exchange(uri, HttpMethod.DELETE, new HttpEntity<>(zoneHeaders), ResponseEntity.class)
                .getStatusCode().value();
    } catch (HttpClientErrorException e) {
        Assert.fail("Unable to DELETE identifier: " + identifier + " for api: " + api);
    }
}

From source file:org.jresponder.util.WebApiUtil.java

/**
 * Helper that converts an object to a JSON-RPC 2.0 response with a result
 * and returns a corresponding/*w  w  w  .  j a v a 2s .  c om*/
 * ResponseEntity which can be returned directly back to Spring MVC and go
 * to the browser.
 * @param aId the JSON-RPC 2.0 request id
 * @param aCallback if non-null means JSONP is requested with the specified
 *                  callback function name 
 * @param aObject the response object - usually a Map
 * @return
 */
public ResponseEntity<String> jsonRpcResult(String aId, String aCallback, Object aObject) {

    Map<String, Object> ret = new HashMap<String, Object>();
    ret.put("id", aId);
    ret.put("result", aObject);
    ret.put("jsonrpc", "2.0");

    String myResultString = JSONValue.toJSONString(ret);

    HttpHeaders responseHeaders = new HttpHeaders();

    // normal JSON response
    if (aCallback == null || aCallback.trim().length() < 1) {
        responseHeaders.set("Content-type", "application/json-rpc");
    }
    // JSONP handled differently
    else {
        responseHeaders.set("Content-type", "application/javascript");
        // wrap result in JSONP callback
        myResultString = aCallback + "&&" + aCallback + "(" + myResultString + ")";
    }

    return new ResponseEntity<String>(myResultString, responseHeaders, HttpStatus.CREATED);
}

From source file:org.jresponder.util.WebApiUtil.java

/**
 * Make a JSON-RPC 2.0 error from a {@link ServiceException}.
 * @return// ww  w  .  j ava 2  s. c  o  m
 */
public ResponseEntity<String> jsonRpcError(String aId, String aCallback, ServiceException e) {

    Map<String, Object> err = new HashMap<String, Object>();
    err.put("code", e.getServiceExceptionType().getCode());
    err.put("message", e.toString());
    err.put("data", e.getDataMap());

    Map<String, Object> ret = new HashMap<String, Object>();
    ret.put("id", aId);
    ret.put("error", err);
    ret.put("jsonrpc", "2.0");

    String myResultString = JSONValue.toJSONString(ret);

    HttpHeaders responseHeaders = new HttpHeaders();

    // normal JSON response
    if (aCallback == null || aCallback.trim().length() < 1) {
        responseHeaders.set("Content-type", "application/json-rpc");
    }
    // JSONP handled differently
    else {
        responseHeaders.set("Content-type", "application/javascript");
        // wrap result in JSONP callback
        myResultString = aCallback + "&&" + aCallback + "(" + myResultString + ")";
    }

    return new ResponseEntity<String>(myResultString, responseHeaders, HttpStatus.CREATED);
}