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:cz.zcu.kiv.eeg.mobile.base.ws.asynctask.FetchElectrodeTypes.java

/**
 * Method, where all electrodeTypes are read from server.
 * All heavy lifting is made here./*from w  w  w. j av a2s .  c  o m*/
 *
 * @param params omitted here
 * @return list of fetched electrodeTypes
 */
@Override
protected List<ElectrodeType> doInBackground(Void... params) {
    SharedPreferences credentials = getCredentials();
    String username = credentials.getString("username", null);
    String password = credentials.getString("password", null);
    String url = credentials.getString("url", null) + Values.SERVICE_ELECTRODE_TYPES;

    setState(RUNNING, R.string.working_ws_electrode_type);
    HttpAuthentication authHeader = new HttpBasicAuthentication(username, password);
    HttpHeaders requestHeaders = new HttpHeaders();
    requestHeaders.setAuthorization(authHeader);
    requestHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_XML));
    HttpEntity<Object> entity = new HttpEntity<Object>(requestHeaders);

    SSLSimpleClientHttpRequestFactory factory = new SSLSimpleClientHttpRequestFactory();
    // Create a new RestTemplate instance
    RestTemplate restTemplate = new RestTemplate(factory);
    restTemplate.getMessageConverters().add(new SimpleXmlHttpMessageConverter());

    try {
        // Make the network request
        Log.d(TAG, url);
        ResponseEntity<ElectrodeTypeList> response = restTemplate.exchange(url, HttpMethod.GET, entity,
                ElectrodeTypeList.class);
        ElectrodeTypeList body = response.getBody();

        if (body != null) {
            return body.getElectrodeTypes();
        }

    } catch (Exception e) {
        Log.e(TAG, e.getLocalizedMessage(), e);
        setState(ERROR, e);
    } finally {
        setState(DONE);
    }
    return Collections.emptyList();
}

From source file:cz.zcu.kiv.eeg.mobile.base.ws.asynctask.FetchHardwareList.java

/**
 * Method, where all hardwareList are read from server.
 * All heavy lifting is made here./*from   w  w w . j a v  a 2  s.com*/
 *
 * @param params omitted here
 * @return list of fetched hardware
 */
@Override
protected List<Hardware> doInBackground(Void... params) {
    SharedPreferences credentials = getCredentials();
    String username = credentials.getString("username", null);
    String password = credentials.getString("password", null);
    String url = credentials.getString("url", null) + Values.SERVICE_HARDWARE;

    setState(RUNNING, R.string.working_ws_hardware);
    HttpAuthentication authHeader = new HttpBasicAuthentication(username, password);
    HttpHeaders requestHeaders = new HttpHeaders();
    requestHeaders.setAuthorization(authHeader);
    requestHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_XML));
    HttpEntity<Object> entity = new HttpEntity<Object>(requestHeaders);

    SSLSimpleClientHttpRequestFactory factory = new SSLSimpleClientHttpRequestFactory();
    // Create a new RestTemplate instance
    RestTemplate restTemplate = new RestTemplate(factory);
    restTemplate.getMessageConverters().add(new SimpleXmlHttpMessageConverter());

    try {
        // Make the network request
        Log.d(TAG, url);
        ResponseEntity<HardwareList> response = restTemplate.exchange(url, HttpMethod.GET, entity,
                HardwareList.class);
        HardwareList body = response.getBody();

        if (body != null) {
            return body.getHardwareList();
        }

    } catch (Exception e) {
        Log.e(TAG, e.getLocalizedMessage(), e);
        setState(ERROR, e);
    } finally {
        setState(DONE);
    }
    return Collections.emptyList();
}

From source file:cz.zcu.kiv.eeg.mobile.base.ws.asynctask.FetchPharmaceuticals.java

/**
 * Method, where all pharmaceuticals are read from server.
 * All heavy lifting is made here.//from   w  w w. j  a va2  s .  c  om
 *
 * @param params omitted here
 * @return list of fetched pharmaceuticals
 */
@Override
protected List<Pharmaceutical> doInBackground(Void... params) {
    SharedPreferences credentials = getCredentials();
    String username = credentials.getString("username", null);
    String password = credentials.getString("password", null);
    String url = credentials.getString("url", null) + Values.SERVICE_PHARMACEUTICAL;

    setState(RUNNING, R.string.working_ws_pharmaceutical);
    HttpAuthentication authHeader = new HttpBasicAuthentication(username, password);
    HttpHeaders requestHeaders = new HttpHeaders();
    requestHeaders.setAuthorization(authHeader);
    requestHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_XML));
    HttpEntity<Object> entity = new HttpEntity<Object>(requestHeaders);

    SSLSimpleClientHttpRequestFactory factory = new SSLSimpleClientHttpRequestFactory();
    // Create a new RestTemplate instance
    RestTemplate restTemplate = new RestTemplate(factory);
    restTemplate.getMessageConverters().add(new SimpleXmlHttpMessageConverter());

    try {
        // Make the network request
        Log.d(TAG, url);
        ResponseEntity<PharmaceuticalList> response = restTemplate.exchange(url, HttpMethod.GET, entity,
                PharmaceuticalList.class);
        PharmaceuticalList body = response.getBody();

        if (body != null) {
            return body.getPharmaceuticals();
        }

    } catch (Exception e) {
        Log.e(TAG, e.getLocalizedMessage(), e);
        setState(ERROR, e);
    } finally {
        setState(DONE);
    }
    return Collections.emptyList();
}

From source file:cz.zcu.kiv.eeg.mobile.base.ws.asynctask.FetchSoftwareList.java

/**
 * Method, where all softwareList are read from server.
 * All heavy lifting is made here.//from   w w  w.  j  ava2  s .  c  o  m
 *
 * @param params omitted here
 * @return list of fetched software
 */
@Override
protected List<Software> doInBackground(Void... params) {
    SharedPreferences credentials = getCredentials();
    String username = credentials.getString("username", null);
    String password = credentials.getString("password", null);
    String url = credentials.getString("url", null) + Values.SERVICE_SOFTWARE;

    setState(RUNNING, R.string.working_ws_software);
    HttpAuthentication authHeader = new HttpBasicAuthentication(username, password);
    HttpHeaders requestHeaders = new HttpHeaders();
    requestHeaders.setAuthorization(authHeader);
    requestHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_XML));
    HttpEntity<Object> entity = new HttpEntity<Object>(requestHeaders);

    SSLSimpleClientHttpRequestFactory factory = new SSLSimpleClientHttpRequestFactory();
    // Create a new RestTemplate instance
    RestTemplate restTemplate = new RestTemplate(factory);
    restTemplate.getMessageConverters().add(new SimpleXmlHttpMessageConverter());

    try {
        // Make the network request
        Log.d(TAG, url);
        ResponseEntity<SoftwareList> response = restTemplate.exchange(url, HttpMethod.GET, entity,
                SoftwareList.class);
        SoftwareList body = response.getBody();

        if (body != null) {
            return body.getSoftwareList();
        }

    } catch (Exception e) {
        Log.e(TAG, e.getLocalizedMessage(), e);
        setState(ERROR, e);
    } finally {
        setState(DONE);
    }
    return Collections.emptyList();
}

From source file:com.develcom.cliente.Cliente.java

public void buscarArchivo() throws FileNotFoundException, IOException {

    CodDecodArchivos cda = new CodDecodArchivos();
    RestTemplate restTemplate = new RestTemplate();
    HttpHeaders headers = new HttpHeaders();
    //        Bufer bufer = new Bufer();
    byte[] buffer = null;
    ResponseEntity<byte[]> response;

    restTemplate.getMessageConverters().add(new ByteArrayHttpMessageConverter());
    headers.setAccept(Arrays.asList(MediaType.ALL));
    HttpEntity<String> entity = new HttpEntity<>(headers);

    response = restTemplate.exchange(/*from   w w  w.ja v  a 2 s . com*/
            "http://localhost:8080/ServicioDW4J/expediente/buscarFisicoDocumento/21593", HttpMethod.GET, entity,
            byte[].class);

    if (response.getStatusCode().equals(HttpStatus.OK)) {
        buffer = response.getBody();
        FileOutputStream output = new FileOutputStream(new File("c:/documentos/archivo.cod"));
        IOUtils.write(response.getBody(), output);
        cda.decodificar("c:/documentos/archivo.cod", "c:/documentos/archivo.pdf");
    }

}

From source file:org.apigw.util.OAuthTestHelper.java

public ResponseEntity<Void> attemptToGetConfirmationPage(String clientId, String redirectUri, String scope) {

    String cookie = loginAndGrabCookie();

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

    return serverRunning.getForResponse(getAuthorizeUrl(clientId, redirectUri, scope), headers);

}

From source file:cz.zcu.kiv.eeg.mobile.base.ws.asynctask.FetchWeatherList.java

/**
 * Method, where all weatherList are read from server.
 * All heavy lifting is made here.//from  w w w .  j ava  2 s.  c  om
 *
 * @param params parameters - omitted here
 * @return list of fetched weather
 */
@Override
protected List<Weather> doInBackground(Void... params) {

    SharedPreferences credentials = getCredentials();
    String username = credentials.getString("username", null);
    String password = credentials.getString("password", null);
    String url = credentials.getString("url", null) + Values.SERVICE_WEATHER + "/" + researchGroupId;

    setState(RUNNING, R.string.working_ws_weather);
    HttpAuthentication authHeader = new HttpBasicAuthentication(username, password);
    HttpHeaders requestHeaders = new HttpHeaders();
    requestHeaders.setAuthorization(authHeader);
    requestHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_XML));
    HttpEntity<Object> entity = new HttpEntity<Object>(requestHeaders);

    SSLSimpleClientHttpRequestFactory factory = new SSLSimpleClientHttpRequestFactory();
    // Create a new RestTemplate instance
    RestTemplate restTemplate = new RestTemplate(factory);
    restTemplate.getMessageConverters().add(new SimpleXmlHttpMessageConverter());

    try {
        // Make the network request
        Log.d(TAG, url);
        ResponseEntity<WeatherList> response = restTemplate.exchange(url, HttpMethod.GET, entity,
                WeatherList.class);
        WeatherList body = response.getBody();

        if (body != null) {
            return body.getWeatherList();
        }

    } catch (Exception e) {
        Log.e(TAG, e.getLocalizedMessage(), e);
        setState(ERROR, e);
    } finally {
        setState(DONE);
    }
    return Collections.emptyList();
}

From source file:org.apigw.util.OAuthTestHelper.java

public String getAuthorizationCode(String clientId, String redirectUri, String scope) {
    String cookie = loginAndGetConfirmationPage(clientId, redirectUri, 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", "true");
    ResponseEntity<Void> result = serverRunning.postForStatus("/apigw-auth-server-web/oauth/authorize", headers,
            formData);/*www.  jav  a  2s. c om*/
    assertEquals(HttpStatus.FOUND, result.getStatusCode());
    // Get the authorization code using the same session
    return getAuthorizationCode(result);
}

From source file:uk.gov.nationalarchives.discovery.taxonomy.ws.controller.TaxonomyControllerTest.java

@Test
public final void testTestCategoriseSingleDocumentWithMissingDescriptionAndDocRef() {
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
    headers.setContentType((MediaType.APPLICATION_JSON));

    TestCategoriseSingleRequest request = new TestCategoriseSingleRequest();
    request.setTitle("TRINITY Church of England School.");
    HttpEntity<TestCategoriseSingleRequest> requestEntity = new HttpEntity<TestCategoriseSingleRequest>(request,
            headers);/*from w  ww.j av a 2s . c o  m*/

    ResponseEntity<TaxonomyErrorResponse> response = restTemplate
            .postForEntity(WS_URL + WS_PATH_TEST_CATEGORISE_SINGLE, requestEntity, TaxonomyErrorResponse.class);

    assertThat(response.getBody(), is(notNullValue()));
    assertThat(response.getBody().getError(), equalTo(TaxonomyErrorType.INVALID_PARAMETER));

}

From source file:org.cloudfoundry.identity.app.integration.AuthenticationIntegrationTests.java

@Test
public void formLoginSucceeds() throws Exception {

    ResponseEntity<Void> result;
    String location;/*from  w w  w.jav a2 s .co  m*/
    String cookie;

    HttpHeaders uaaHeaders = new HttpHeaders();
    HttpHeaders appHeaders = new HttpHeaders();
    uaaHeaders.setAccept(Arrays.asList(MediaType.TEXT_HTML));
    appHeaders.setAccept(Arrays.asList(MediaType.TEXT_HTML));

    // *** GET /app/id
    result = serverRunning.getForResponse("/id", appHeaders);
    assertEquals(HttpStatus.FOUND, result.getStatusCode());
    location = result.getHeaders().getLocation().toString();

    cookie = result.getHeaders().getFirst("Set-Cookie");
    assertNotNull("Expected cookie in " + result.getHeaders(), cookie);
    appHeaders.set("Cookie", cookie);

    assertTrue("Wrong location: " + location, location.contains("/oauth/authorize"));
    // *** GET /uaa/oauth/authorize
    result = serverRunning.getForResponse(location, uaaHeaders);
    assertEquals(HttpStatus.FOUND, result.getStatusCode());
    location = result.getHeaders().getLocation().toString();

    cookie = result.getHeaders().getFirst("Set-Cookie");
    assertNotNull("Expected cookie in " + result.getHeaders(), cookie);
    uaaHeaders.set("Cookie", cookie);

    assertTrue("Wrong location: " + location, location.contains("/login"));
    location = serverRunning.getAuthServerUrl("/login.do");

    MultiValueMap<String, String> formData;
    formData = new LinkedMultiValueMap<String, String>();
    formData.add("username", testAccounts.getUserName());
    formData.add("password", testAccounts.getPassword());

    // *** POST /uaa/login.do
    result = serverRunning.postForResponse(location, uaaHeaders, formData);

    cookie = result.getHeaders().getFirst("Set-Cookie");
    assertNotNull("Expected cookie in " + result.getHeaders(), cookie);
    uaaHeaders.set("Cookie", cookie);

    assertEquals(HttpStatus.FOUND, result.getStatusCode());
    location = result.getHeaders().getLocation().toString();

    assertTrue("Wrong location: " + location, location.contains("/oauth/authorize"));
    // *** GET /uaa/oauth/authorize
    result = serverRunning.getForResponse(location, uaaHeaders);

    // If there is no token in place already for this client we get the approval page.
    // TODO: revoke the token so we always get the approval page
    if (result.getStatusCode() == HttpStatus.OK) {
        location = serverRunning.getAuthServerUrl("/oauth/authorize");

        formData = new LinkedMultiValueMap<String, String>();
        formData.add("user_oauth_approval", "true");

        // *** POST /uaa/oauth/authorize
        result = serverRunning.postForResponse(location, uaaHeaders, formData);
    }

    location = result.getHeaders().getLocation().toString();

    // SUCCESS
    assertTrue("Wrong location: " + location, location.contains("/id"));

    // *** GET /app/id
    result = serverRunning.getForResponse(location, appHeaders);
    // System.err.println(result.getHeaders());
    assertEquals(HttpStatus.OK, result.getStatusCode());
}