Example usage for org.springframework.http HttpHeaders HttpHeaders

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

Introduction

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

Prototype

public HttpHeaders() 

Source Link

Document

Construct a new, empty instance of the HttpHeaders object.

Usage

From source file:com.tce.oauth2.spring.client.controller.UserInfoController.java

@RequestMapping("/userinfo")
public String index(HttpServletRequest request) {
    if (request.getSession().getAttribute("access_token") == null) {
        return "redirect:/";
    }//from  ww  w .  j  a  v  a 2  s . c o  m

    String accessToken = (String) request.getSession().getAttribute("access_token");
    HttpHeaders headers = new HttpHeaders();
    headers.set("Authorization", "Bearer " + accessToken);
    HttpEntity<?> entity = new HttpEntity<>(headers);

    ResponseEntity<User> response = restTemplate.exchange(OAUTH_URL + "/userinfo", HttpMethod.GET, entity,
            User.class);

    if (response.getStatusCode().is4xxClientError()) {
        return "redirect:/login";
    }

    User user = response.getBody();
    request.getSession(false).setAttribute("username", user.getUsername());
    return "redirect:/";
}

From source file:eu.impress.rest.api.BedAvailabilityController.java

@RequestMapping(value = "/bedavailability/hospital/{hospitalName}", method = RequestMethod.GET, produces = {
        MediaType.APPLICATION_JSON_VALUE })
public ResponseEntity<String> sendAvailableBeds(@PathVariable("hospitalName") String hospitalName)
        throws DatatypeConfigurationException {

    String bedavailability;//  www  . j  av  a2s  .  co  m
    String bedavailabilityDEEnvelope;
    String bedavailabilityDE;
    String bedavailabilityJSON;

    //get HAVE String
    bedavailability = b.getBedAvailablityHAVE(hospitalName);

    //get DE String
    bedavailabilityDEEnvelope = b.createBedAvailabilityDE();

    //encapsulate in DE
    bedavailabilityDE = b.getBedAvailabilityEDXLDE(bedavailabilityDEEnvelope, bedavailability);

    //produce json message
    bedavailabilityJSON = b.forwardBedAvailability(bedavailabilityDE);
    log.info("Json message: " + bedavailabilityJSON);

    //push message to EMCR
    RestTemplate restTemplate = new RestTemplate();
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);

    HttpEntity<String> entity = new HttpEntity<String>(bedavailabilityJSON, headers);
    String answer = restTemplate.postForObject(EMCRUrl, entity, String.class);

    log.info("EMCR response: " + answer);

    return new ResponseEntity<String>(answer, HttpStatus.OK);

}

From source file:org.openlmis.fulfillment.service.AuthService.java

/**
 * Retrieves access token from the auth service.
 *
 * @return token./*from w w  w  . j  a  va  2  s .c  o m*/
 */
@Cacheable("token")
public String obtainAccessToken() {
    String plainCreds = clientId + ":" + clientSecret;
    byte[] plainCredsBytes = plainCreds.getBytes();
    byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes);
    String base64Creds = new String(base64CredsBytes);

    HttpHeaders headers = new HttpHeaders();
    headers.add("Authorization", "Basic " + base64Creds);

    HttpEntity<String> request = new HttpEntity<>(headers);

    RequestParameters params = RequestParameters.init().set("grant_type", "client_credentials");

    ResponseEntity<?> response = restTemplate.exchange(createUri(authorizationUrl, params), HttpMethod.POST,
            request, Object.class);

    return ((Map<String, String>) response.getBody()).get(ACCESS_TOKEN);
}

From source file:com.bradley.musicapp.test.restapi.PersonRestControllerTest.java

private HttpEntity<?> getHttpEntity() {
    HttpHeaders requestHeaders = new HttpHeaders();
    requestHeaders.setAccept(Collections.singletonList(new MediaType("application", "json")));
    HttpEntity<?> requestEntity = new HttpEntity<>(requestHeaders);
    restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
    return requestEntity;
}

From source file:org.zalando.riptide.BufferingClientHttpResponseTest.java

@Test
public void redirectsStatusFields() throws IOException {
    when(response.getStatusCode()).thenReturn(HttpStatus.ACCEPTED);
    when(response.getStatusText()).thenReturn("status-text");
    when(response.getRawStatusCode()).thenReturn(42);
    when(response.getHeaders()).thenReturn(new HttpHeaders());

    unit = buffer(response);/*w ww . ja  v a  2s  .  c  o  m*/

    assertThat(unit.getStatusCode(), is(response.getStatusCode()));
    assertThat(unit.getStatusText(), is(response.getStatusText()));
    assertThat(unit.getRawStatusCode(), is(response.getRawStatusCode()));
    assertThat(unit.getHeaders(), is(response.getHeaders()));
}

From source file:com.dbi.jmmerge.MapController.java

@ExceptionHandler
public ResponseEntity<Map> handleException(Exception ex, HttpServletRequest request) {
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    Map msgs = new HashMap();
    ResponseEntity<Map> ret = new ResponseEntity<Map>(msgs, headers, HttpStatus.INTERNAL_SERVER_ERROR);
    msgs.put("message", "An error occurred . . . contact your administrator for details.");
    msgs.put("error", ex.getMessage());
    LOG.error("An error occurred handling a " + request.getMethod() + " to URL " + request.getRequestURL(), ex);
    return ret;//from  w  w  w  . j  a v a2s  .c o m
}

From source file:com.gopivotal.cla.github.GitHubConditionalTest.java

@Test
public void noNextLink() {
    HttpHeaders headers = new HttpHeaders();
    headers.add("Link", "<" + URL + ">; rel=\"first\"");
    ResponseEntity<Set> response = new ResponseEntity<Set>(Sets.asSet(), headers, HttpStatus.OK);
    when(this.restOperations.exchange(URL, HttpMethod.GET, REQUEST_ENTITY, Set.class)).thenReturn(response);

    this.gitHubType.getTrigger();

    assertTrue(this.gitHubType.initializedCalled);
}

From source file:edu.wisc.cypress.dao.taxstmt.RestTaxStatementDao.java

@Cacheable(cacheName = "taxStatement", exceptionCacheName = "cypressUnknownExceptionCache")
@Override/* w  w w  .ja  va  2s  .  co  m*/
public TaxStatements getTaxStatements(String emplid) {
    final HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.set("HRID", emplid);
    final XmlTaxStatements xmlTaxStatements = this.restOperations.getForObject(this.statementsUrl,
            XmlTaxStatements.class, httpHeaders, emplid);
    return mapTaxStatements(xmlTaxStatements);
}

From source file:me.j360.boot.standard.test.SessionRedisApplicationTests.java

@Test
public void sessionExpiry() throws Exception {

    String port = null;//w ww . java 2 s .  c  o  m

    try {
        ConfigurableApplicationContext context = new SpringApplicationBuilder().sources(J360Configuration.class)
                .properties("server.port:0").initializers(new ServerPortInfoApplicationContextInitializer())
                .run();
        port = context.getEnvironment().getProperty("local.server.port");
    } catch (RuntimeException ex) {
        if (!redisServerRunning(ex)) {
            return;
        }
    }

    URI uri = URI.create("http://localhost:" + port + "/");
    RestTemplate restTemplate = new RestTemplate();

    ResponseEntity<String> response = restTemplate.getForEntity(uri, String.class);
    String uuid1 = response.getBody();
    HttpHeaders requestHeaders = new HttpHeaders();
    requestHeaders.set("Cookie", response.getHeaders().getFirst("Set-Cookie"));

    RequestEntity<Void> request = new RequestEntity<Void>(requestHeaders, HttpMethod.GET, uri);

    String uuid2 = restTemplate.exchange(request, String.class).getBody();
    assertThat(uuid1, is(equalTo(uuid2)));

    Thread.sleep(5000);

    String uuid3 = restTemplate.exchange(request, String.class).getBody();
    assertThat(uuid2, is(not(equalTo(uuid3))));
}

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

@Test
public void testImplicitClientInfo() throws Exception {

    HttpHeaders headers = new HttpHeaders();
    ImplicitResourceDetails app = testAccounts.getDefaultImplicitResource();
    headers.set("Authorization", testAccounts.getAuthorizationHeader(app.getClientId(), ""));
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));

    @SuppressWarnings("rawtypes")
    ResponseEntity<Map> response = serverRunning.getForObject("/clientinfo", Map.class, headers);
    assertEquals(HttpStatus.OK, response.getStatusCode());
    assertEquals(app.getClientId(), response.getBody().get("client_id"));

}