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:org.messic.android.util.RestJSONClient.java

/**
 * Rest POST petition to the server at the url param, sending all the post parameters defiend at formData. This post
 * return an object (json marshalling) of class defined at clazz parameter. You should register a
 * {@link RestListener} in order to obtain the returned object, this is because the petition is done in an async
 * process.//from   w  ww .j  av a 2  s. co m
 * 
 * @param url {@link string} URL to attack
 * @param formData {@link MultiValueMap}<?,?/> map of parameters to send
 * @param clazz Class<T/> class that you will marshall to a json object
 * @param rl {@link RestListener} listener to push the object returned
 */
public static <T> void post(final String url, MultiValueMap<?, ?> formData, final Class<T> clazz,
        final RestListener<T> rl) {
    final RestTemplate restTemplate = new RestTemplate();
    restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());

    HttpHeaders requestHeaders = new HttpHeaders();
    requestHeaders.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
    // Sending multipart/form-data
    requestHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
    // Populate the MultiValueMap being serialized and headers in an HttpEntity object to use for the request
    final HttpEntity<MultiValueMap<?, ?>> requestEntity = new HttpEntity<MultiValueMap<?, ?>>(formData,
            requestHeaders);

    AsyncTask<Void, Void, Void> at = new AsyncTask<Void, Void, Void>() {
        @Override
        protected Void doInBackground(Void... params) {
            try {
                ResponseEntity<T> response = restTemplate.exchange(url, HttpMethod.POST, requestEntity, clazz);
                rl.response(response.getBody());
            } catch (Exception e) {
                rl.fail(e);
            }
            return null;
        }

    };

    at.execute();
}

From source file:cn.aozhi.songify.rest.RestExceptionHandler.java

/**
 * ?RestException.// w w  w .j  av a2 s  . c  o  m
 */
@ExceptionHandler(value = { RestException.class })
public final ResponseEntity<?> handleException(RestException ex, WebRequest request) {
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.parseMediaType(MediaTypes.TEXT_PLAIN_UTF_8));
    return handleExceptionInternal(ex, ex.getMessage(), headers, ex.status, request);
}

From source file:com.ge.predix.acs.commons.web.ResponseEntityBuilder.java

/**
 * Creates a typed ResponseEntity with HTTP status code 201/204 with a given location.
 *
 * @param location/*  w  w w . jav a  2 s . co m*/
 *            The location of the created resource
 * @param noContent
 *            false means updated resource which returns 204, true means created resource which returns 201
 * @return The corresponding ResponseEntity
 */
public static <T> ResponseEntity<T> created(final String location, final boolean noContent) {
    HttpStatus status = noContent ? HttpStatus.NO_CONTENT : HttpStatus.CREATED;

    if (location != null) {

        HttpHeaders headers = new HttpHeaders();
        headers.setLocation(URI.create(location));
        return new ResponseEntity<T>(headers, status);
    }
    return new ResponseEntity<T>(status);
}

From source file:org.zalando.github.spring.AbstractTemplateTest.java

@Before
public void setup() {
    restTemplate = new RestTemplate();
    // this.gitHub = new GitHubTemplate("ACCESS_TOKEN");
    this.mockServer = MockRestServiceServer.createServer(restTemplate);

    this.responseHeaders = new HttpHeaders();
    responseHeaders.setContentType(MediaType.APPLICATION_JSON);

    // this.unauthorizedGitHub = new GitHubTemplate();

    // Create a mock server just to avoid hitting real GitHub if something
    // gets past the authorization check.
    // MockRestServiceServer.createServer(unauthorizedGitHub.getRestTemplate());
}

From source file:org.bitcoinrt.spring.config.ClientConfig.java

@Bean
public WebSocketConnectionManager connectionManager() {

    HttpHeaders headers = new HttpHeaders();
    headers.setOrigin("http://websocket.mtgox.com");

    WebSocketConnectionManager cm = new WebSocketConnectionManager(wsClient(), wsHandler(), WS_URL);
    cm.setHeaders(headers);//  w ww  . ja v a 2s  .  com
    cm.setAutoStartup(true);
    return cm;
}

From source file:org.cloudfoundry.identity.varz.integration.VarzEndpointIntegrationTests.java

/**
 * tests a happy-day flow of the <code>/varz</code> endpoint
 */// w ww. j  ava  2 s  .co  m
@Test
public void testHappyDay() throws Exception {

    HttpHeaders headers = new HttpHeaders();
    headers.add("Authorization", testAccounts.getVarzAuthorizationHeader());
    ResponseEntity<String> response = serverRunning.getForString("/", headers);
    assertEquals(HttpStatus.OK, response.getStatusCode());

    String map = response.getBody();
    assertTrue(map.contains("thread_pool"));

}

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

/**
 * tests a happy-day flow of the <code>/varz</code> endpoint
 *///from   w  ww.  j  a  va2 s.  com
@Test
public void testHappyDay() throws Exception {

    HttpHeaders headers = new HttpHeaders();
    headers.add("Authorization", testAccounts.getVarzAuthorizationHeader());
    ResponseEntity<String> response = serverRunning.getForString("/varz", headers);
    assertEquals(HttpStatus.OK, response.getStatusCode());

    String map = response.getBody();
    assertTrue(map.contains("spring.application"));
    assertTrue(map.contains("Catalina"));

}

From source file:org.busko.routemanager.web.RoutePlotterController.java

@RequestMapping(method = RequestMethod.GET)
public ResponseEntity<byte[]> handheldApk(Model uiModel, HttpServletRequest httpServletRequest)
        throws Exception {
    InputStream input = getClass().getClassLoader().getResourceAsStream("BuskoPlotter.apk");
    int fileSize = input.available();
    byte[] bytes = new byte[fileSize];
    input.read(bytes);// w  w w.j a v  a 2s.c  om
    input.close();

    HttpHeaders responseHeaders = new HttpHeaders();
    responseHeaders.set("Content-Disposition", "attachment;filename=BuskoPlotter.apk");
    responseHeaders.set("Content-Length", Integer.toString(fileSize));
    responseHeaders.set("Content-Type", "application/vnd.android.package-archive");
    return new ResponseEntity<byte[]>(bytes, responseHeaders, HttpStatus.OK);
}

From source file:monkeys.web.MonkeysController.java

@RequestMapping(method = RequestMethod.GET, value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Monkey> getMonkey(@PathVariable Long id) {
    Monkey monkey = monkeyRepository.findOne(id);
    HttpHeaders headers = new HttpHeaders();
    return new ResponseEntity<>(monkey, headers, HttpStatus.OK);
}

From source file:com.expedia.seiso.web.controller.v1.ResponseHeadersV1.java

public HttpHeaders buildResponseHeaders(@NonNull PagedResources page) {
    val links = page.getLinks();
    val meta = page.getMetadata();

    val headers = new HttpHeaders();
    for (val link : links) {
        val rel = link.getRel();
        val href = link.getHref();
        if (Relations.FIRST.equals(rel)) {
            headers.add(X_PAGINATION_FIRST, href);
        } else if (Relations.PREVIOUS.equals(rel)) {
            headers.add(X_PAGINATION_PREV, href);
        } else if (Relations.NEXT.equals(rel)) {
            headers.add(X_PAGINATION_NEXT, href);
        } else if (Relations.LAST.equals(rel)) {
            headers.add(X_PAGINATION_LAST, href);
        }// w ww  . j  a  v  a 2s  .com
    }
    headers.add(X_PAGINATION_TOTAL_ELEMENTS, String.valueOf(meta.getTotalItems()));
    headers.add(X_PAGINATION_TOTAL_PAGES, String.valueOf(meta.getTotalPages()));
    return headers;
}