Example usage for org.springframework.http HttpHeaders setAccept

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

Introduction

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

Prototype

public void setAccept(List<MediaType> acceptableMediaTypes) 

Source Link

Document

Set the list of acceptable MediaType media types , as specified by the Accept header.

Usage

From source file:fi.helsinki.opintoni.service.usefullink.UsefulLinkService.java

public SearchPageTitleDto searchPageTitle(SearchPageTitleDto searchPageTitleDto) throws NotFoundException {
    try {/*ww w.  ja  v a  2 s .com*/
        HttpHeaders headers = new HttpHeaders();
        headers.setAccept(Lists.newArrayList(MediaType.TEXT_HTML));
        headers.add("User-Agent", "Mozilla");
        HttpEntity<String> entity = new HttpEntity<>("parameters", headers);

        ResponseEntity<String> responseEntity = linkUrlLoaderRestTemplate.exchange(searchPageTitleDto.searchUrl,
                HttpMethod.GET, entity, String.class);
        if (responseEntity.getStatusCode().equals(HttpStatus.OK)) {
            Document document = Jsoup.parse(responseEntity.getBody());
            searchPageTitleDto.searchResult = document.title();
        }
    } catch (Exception e) {
    }
    return searchPageTitleDto;
}

From source file:sample.RestTests.java

private HttpHeaders getHttpHeaders() {
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
    return headers;
}

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

@Test
public void testLoginPage() throws Exception {
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
    ResponseEntity<String> entity = new TestRestTemplate().exchange("http://localhost:" + this.port + "/login",
            HttpMethod.GET, new HttpEntity<Void>(headers), String.class);
    assertEquals(HttpStatus.OK, entity.getStatusCode());
    assertTrue("Wrong content:\n" + entity.getBody(), entity.getBody().contains("_csrf"));
}

From source file:sample.RestTests.java

@Test(expected = HttpClientErrorException.class)
public void unauthenticatedUserSentToLogInPage() {
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
    ResponseEntity<String> entity = getForUser(this.baseUrl + "/", headers, String.class);
    assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED);
}

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

@Test
public void testHome() throws Exception {
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
    ResponseEntity<String> entity = new TestRestTemplate().exchange("http://localhost:" + this.port,
            HttpMethod.GET, new HttpEntity<Void>(headers), String.class);
    assertEquals(HttpStatus.FOUND, entity.getStatusCode());
    assertTrue("Wrong location:\n" + entity.getHeaders(),
            entity.getHeaders().getLocation().toString().endsWith(port + "/login"));
}

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

@Test
public void testLogin() throws Exception {
    HttpHeaders headers = getHeaders();
    headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
    headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
    MultiValueMap<String, String> form = new LinkedMultiValueMap<String, String>();
    form.set("username", "user");
    form.set("password", "user");
    ResponseEntity<String> entity = new TestRestTemplate().exchange("http://localhost:" + this.port + "/login",
            HttpMethod.POST, new HttpEntity<MultiValueMap<String, String>>(form, headers), String.class);
    assertEquals(HttpStatus.FOUND, entity.getStatusCode());
    assertTrue("Wrong location:\n" + entity.getHeaders(),
            entity.getHeaders().getLocation().toString().endsWith(port + "/"));
    assertNotNull("Missing cookie:\n" + entity.getHeaders(), entity.getHeaders().get("Set-Cookie"));
}

From source file:com.recursivechaos.clearent.service.ClearentService.java

public Transaction postTransaction(Transaction transaction) {
    RestTemplate restTemplate = new RestTemplate();
    ResponseEntity<Response> responseEntity;
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
    headers.set("api-key", gatewayProperties.getKey());
    HttpEntity<String> request = new HttpEntity<>(transaction.toString(), headers);
    try {//from   w w w .  ja  v a 2 s  . co  m
        responseEntity = restTemplate.postForEntity(gatewayProperties.getUrl() + "/transactions", request,
                Response.class);
    } catch (HttpClientErrorException he) {
        logger.error("Gateway Request Failed: {}", he.getLocalizedMessage());
        logger.error(he.getResponseBodyAsString());
        throw new GatewayException(he.getLocalizedMessage());
    }
    assert responseEntity != null;
    return responseEntity.getBody().getPayload().getTransaction();
}

From source file:sample.RestTests.java

@Test
public void authenticateWithXAuthTokenWorks() {
    String auth = getAuth("user", "password");
    HttpHeaders headers = getHttpHeaders();
    headers.set(AUTHORIZATION, BASIC + auth);
    ResponseEntity<User> entity = getForUser(this.baseUrl + "/", headers, User.class);

    String token = entity.getHeaders().getFirst(X_AUTH_TOKEN);

    HttpHeaders authTokenHeader = new HttpHeaders();
    authTokenHeader.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
    authTokenHeader.set(X_AUTH_TOKEN, token);
    ResponseEntity<User> authTokenResponse = getForUser(this.baseUrl + "/", authTokenHeader, User.class);
    assertThat(authTokenResponse.getStatusCode()).isEqualTo(HttpStatus.OK);
    assertThat(authTokenResponse.getBody().getUsername()).isEqualTo("user");
}

From source file:comsat.sample.ui.method.SampleMethodSecurityApplicationTests.java

@Test
public void testLogin() throws Exception {
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
    MultiValueMap<String, String> form = new LinkedMultiValueMap<String, String>();
    form.set("username", "admin");
    form.set("password", "admin");
    getCsrf(form, headers);//from ww  w .  j a  v a 2  s  .  c  om
    ResponseEntity<String> entity = new TestRestTemplate().exchange("http://localhost:" + this.port + "/login",
            HttpMethod.POST, new HttpEntity<MultiValueMap<String, String>>(form, headers), String.class);
    assertEquals(HttpStatus.FOUND, entity.getStatusCode());
    assertEquals("http://localhost:" + this.port + "/", entity.getHeaders().getLocation().toString());
}

From source file:com.expedia.client.WunderGroundClient.java

@Override
public ResponseEntity<Response> getXMLResponse(Object request) {
    ResponseEntity<Response> responseEntity = null;
    Weather weather = null;/* www.  j a va  2  s. com*/

    if (request instanceof Weather) {
        weather = (Weather) request;
        List<MediaType> mediaTypes = new ArrayList<MediaType>();
        mediaTypes.add(MediaType.APPLICATION_XML);
        HttpHeaders headers = new HttpHeaders();
        headers.setAccept(mediaTypes);
        HttpEntity<Weather> httpEntity = new HttpEntity<Weather>(null, headers);
        try {
            System.out.println("Hitting weather service!");
            responseEntity = restTemplate.exchange(weatherServiceXmlUrl, HttpMethod.GET, httpEntity,
                    Response.class, weatherApiKey, weather.getZipCode());
        } catch (RuntimeException e) {
            e.printStackTrace();
            weather.setErrorDesc("Get failed" + e.getMessage());
        }
    }
    return responseEntity;
}