Example usage for org.springframework.http HttpHeaders setETag

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

Introduction

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

Prototype

public void setETag(@Nullable String etag) 

Source Link

Document

Set the (new) entity tag of the body, as specified by the ETag header.

Usage

From source file:net.eusashead.hateoas.response.argumentresolver.AsyncEntityControllerITCase.java

@Test
public void testPut() throws Exception {

    // Expected result
    HttpHeaders headers = new HttpHeaders();
    headers.setETag("W/\"b0c689597eb9dd62c0a1fb90a7c5c5a5\"");
    ResponseEntity<Entity> expectedResult = new ResponseEntity<Entity>(headers, HttpStatus.NO_CONTENT);

    // Execute asynchronously
    MvcResult mvcResult = this.mockMvc
            .perform(put("http://localhost/async/foo").content(mapper.writeValueAsBytes(new Entity("foo")))
                    .contentType(MediaType.APPLICATION_JSON)
                    .characterEncoding(Charset.forName("UTF-8").toString()).accept(MediaType.APPLICATION_JSON))
            .andExpect(request().asyncStarted()).andExpect(request().asyncResult(expectedResult)).andReturn();

    // Perform asynchronous dispatch
    this.mockMvc.perform(asyncDispatch(mvcResult)).andDo(print()).andExpect(status().isNoContent());

}

From source file:net.eusashead.hateoas.response.argumentresolver.AsyncEntityControllerITCase.java

@Test
public void testGet() throws Exception {

    // Expected result
    HttpHeaders headers = new HttpHeaders();
    headers.setETag("W/\"b0c689597eb9dd62c0a1fb90a7c5c5a5\"");
    ResponseEntity<Entity> expectedResult = new ResponseEntity<Entity>(new Entity("foo"), headers,
            HttpStatus.OK);//from w  w  w . java 2  s.  c o m

    // Execute asynchronously
    MvcResult mvcResult = this.mockMvc
            .perform(get("http://localhost/async/foo").contentType(MediaType.APPLICATION_JSON)
                    .accept(MediaType.APPLICATION_JSON))
            .andExpect(request().asyncStarted()).andExpect(request().asyncResult(expectedResult)).andReturn();

    // Perform asynchronous dispatch
    this.mockMvc.perform(asyncDispatch(mvcResult)).andDo(print()).andExpect(status().isOk())
            .andExpect(header().string("ETag", "W/\"b0c689597eb9dd62c0a1fb90a7c5c5a5\""))
            .andExpect(content().contentType(new MediaType("application", "json", Charset.forName("UTF-8"))));

}

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

private void answerWithAccount() {
    final HttpHeaders headers = new HttpHeaders();
    headers.setETag(REVISION);

    server.expect(requestTo(url)).andRespond(withSuccess().body(new ClassPathResource("account.json"))
            .contentType(APPLICATION_JSON).headers(headers));
}

From source file:com.appglu.impl.StorageTemplateTest.java

@Test(expected = AppGluRestClientException.class)
public void streamStorageFile_invalidETag() {
    String invalidETag = "\"1234567890\"";

    HttpHeaders responseHeaders = new HttpHeaders();
    responseHeaders.setETag(invalidETag);

    downloadMockServer.expect(requestTo(URL)).andExpect(method(HttpMethod.GET))
            .andRespond(withStatus(HttpStatus.OK).body(CONTENT).headers(responseHeaders));

    this.storageOperations.streamStorageFile(new StorageFile(URL), new InputStreamCallback() {
        public void doWithInputStream(InputStream inputStream) throws IOException {
            IOUtils.copy(inputStream, new ByteArrayOutputStream());
        }/*from  w  w  w .  j  a  va 2  s  .  co  m*/
    });
}

From source file:net.eusashead.hateoas.conditional.interceptor.AsyncTestControllerITCase.java

@Test
public void testPutPreconditionOK() throws Exception {

    // Expected result
    HttpHeaders headers = new HttpHeaders();
    headers.setETag("\"123456\"");
    ResponseEntity<String> expectedResult = new ResponseEntity<String>(headers, HttpStatus.NO_CONTENT);

    // 204 should be returned
    MvcResult mvcResult = this.mockMvc
            .perform(put("http://localhost/async/123").contentType(MediaType.APPLICATION_JSON)
                    .accept(MediaType.APPLICATION_JSON).header("If-Match", "\"123456\""))
            .andExpect(request().asyncStarted()).andExpect(request().asyncResult(expectedResult)).andReturn();

    // Perform asynchronous dispatch
    this.mockMvc.perform(asyncDispatch(mvcResult)).andExpect(status().isNoContent())
            .andExpect(header().string("ETag", "\"123456\"")).andExpect(content().string(""));

}

From source file:net.eusashead.hateoas.conditional.interceptor.AsyncTestControllerITCase.java

@Test
public void testPostFlushesETag() throws Exception {

    // Post asynchronously
    MvcResult mvcResult = this.mockMvc.perform(post("http://localhost/async/123")
            .contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON)).andReturn();

    // Perform asynchronous dispatch
    this.mockMvc.perform(asyncDispatch(mvcResult)).andExpect(status().isCreated())
            .andExpect(content().string(""));

    // Expected result
    HttpHeaders headers = new HttpHeaders();
    headers.setETag("\"123456\"");
    ResponseEntity<String> expectedResult = new ResponseEntity<String>("hello", headers, HttpStatus.OK);

    // Get the resource      
    this.mockMvc//from w w  w. ja va 2  s  .com
            .perform(get("http://localhost/async/123").contentType(MediaType.APPLICATION_JSON)
                    .accept(MediaType.APPLICATION_JSON))
            .andExpect(request().asyncStarted()).andExpect(request().asyncResult(expectedResult)).andReturn();

}

From source file:net.eusashead.hateoas.conditional.interceptor.AsyncTestControllerITCase.java

@Test
public void testConditionalHead() throws Exception {

    // Expected result
    HttpHeaders headers = new HttpHeaders();
    headers.setETag("\"123456\"");
    ResponseEntity<String> expectedResult = new ResponseEntity<String>(headers, HttpStatus.OK);

    // Execute asynchronously
    MvcResult mvcResult = this.mockMvc
            .perform(request(HttpMethod.HEAD, "http://localhost/async/123")
                    .contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON))
            .andExpect(request().asyncStarted()).andExpect(request().asyncResult(expectedResult)).andReturn();

    // Perform asynchronous dispatch
    this.mockMvc.perform(asyncDispatch(mvcResult)).andExpect(status().isOk())
            .andExpect(header().string("ETag", "\"123456\"")).andExpect(content().string(""));

    // The 304 should be returned synchronously (it bypasses the controller)
    this.mockMvc.perform(
            request(HttpMethod.HEAD, "http://localhost/async/123").contentType(MediaType.APPLICATION_JSON)
                    .accept(MediaType.APPLICATION_JSON).header("If-None-Match", "\"123456\""))
            .andExpect(status().isNotModified()).andReturn();

}

From source file:net.eusashead.hateoas.conditional.interceptor.AsyncTestControllerITCase.java

@Test
public void testConditionalGet() throws Exception {

    // Expected result
    HttpHeaders headers = new HttpHeaders();
    headers.setETag("\"123456\"");
    ResponseEntity<String> expectedResult = new ResponseEntity<String>("hello", headers, HttpStatus.OK);

    // Execute asynchronously
    MvcResult mvcResult = this.mockMvc
            .perform(get("http://localhost/async/123").contentType(MediaType.APPLICATION_JSON)
                    .accept(MediaType.APPLICATION_JSON))
            .andExpect(request().asyncStarted()).andExpect(request().asyncResult(expectedResult)).andReturn();

    // Perform asynchronous dispatch
    this.mockMvc.perform(asyncDispatch(mvcResult)).andExpect(status().isOk())
            .andExpect(header().string("ETag", "\"123456\""))
            .andExpect(content().contentType(new MediaType("application", "json", Charset.forName("UTF-8"))));

    // The 304 should be returned synchronously (it bypasses the controller)
    this.mockMvc/*from w w w.j a  v  a 2  s  .  co  m*/
            .perform(get("http://localhost/async/123").contentType(MediaType.APPLICATION_JSON)
                    .accept(MediaType.APPLICATION_JSON).header("If-None-Match", "\"123456\""))
            .andExpect(status().isNotModified()).andReturn();

}

From source file:com.alehuo.wepas2016projekti.controller.ImageController.java

/**
 * Hakee tietokannasta kuvan. Kuvan hakemisessa hydynnetn ETag
 * -otsaketta.//w w w. j  a  v a  2  s.  co  m
 *
 * @param a Autentikointi
 * @param imageUuid Kuvan UUID
 * @param ifNoneMatch If-None-Match -headeri vlimuistia varten
 * @return Kuva
 */
@RequestMapping(value = "/{imageUuid}", method = RequestMethod.GET)
@ResponseBody
public ResponseEntity<byte[]> getImage(Authentication a, @PathVariable String imageUuid,
        @RequestHeader(required = false, value = "If-None-Match") String ifNoneMatch) {
    if (ifNoneMatch != null) {
        //            LOG.log(Level.INFO, "Kuva ''{0}'' loytyy kayttajan selaimen valimuistista eika sita tarvitse ladata. Kuvaa pyysi kayttaja ''{1}''", new Object[]{imageUuid, a.getName()});
        //Jos If-None-Match -headeri lytyy, niin lhet NOT MODIFIED vastaus
        return new ResponseEntity<>(HttpStatus.NOT_MODIFIED);
    }
    Image i = imageService.findOneImageByUuid(imageUuid);
    if (i != null && i.isVisible()) {
        //Luodaan ETag kuvalle
        final HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.parseMediaType(i.getContentType()));
        headers.setContentLength(i.getImageData().length);
        headers.setCacheControl("public");
        headers.setExpires(Long.MAX_VALUE);
        headers.setETag("\"" + imageUuid + "\"");
        //            LOG.log(Level.INFO, "Kuva ''{0}'' loytyi tietokannasta, ja sita pyysi kayttaja ''{1}''", new Object[]{imageUuid, a.getName()});
        //Palautetaan kuva uutena resurssina
        return new ResponseEntity<>(i.getImageData(), headers, HttpStatus.CREATED);
    } else {
        //Jos kuvaa ei lydy tietokannasta
        LOG.log(Level.WARNING, "Kuvaa ''{0}'' ei loytynyt tietokannasta, ja sita pyysi kayttaja ''{1}''",
                new Object[] { imageUuid, a.getName() });
        return new ResponseEntity<>(HttpStatus.NOT_FOUND);
    }
}

From source file:net.paslavsky.springrest.HttpHeadersHelper.java

public HttpHeaders getHttpHeaders(Map<String, Integer> headerParameters, Object[] arguments) {
    HttpHeaders headers = new HttpHeaders();
    for (String headerName : headerParameters.keySet()) {
        Object headerValue = arguments[headerParameters.get(headerName)];
        if (headerValue != null) {
            if (ACCEPT.equalsIgnoreCase(headerName)) {
                headers.setAccept(toList(headerValue, MediaType.class));
            } else if (ACCEPT_CHARSET.equalsIgnoreCase(headerName)) {
                headers.setAcceptCharset(toList(headerValue, Charset.class));
            } else if (ALLOW.equalsIgnoreCase(headerName)) {
                headers.setAllow(toSet(headerValue, HttpMethod.class));
            } else if (CONNECTION.equalsIgnoreCase(headerName)) {
                headers.setConnection(toList(headerValue, String.class));
            } else if (CONTENT_DISPOSITION.equalsIgnoreCase(headerName)) {
                setContentDisposition(headers, headerName, headerValue);
            } else if (CONTENT_LENGTH.equalsIgnoreCase(headerName)) {
                headers.setContentLength(toLong(headerValue));
            } else if (CONTENT_TYPE.equalsIgnoreCase(headerName)) {
                headers.setContentType(toMediaType(headerValue));
            } else if (DATE.equalsIgnoreCase(headerName)) {
                headers.setDate(toLong(headerValue));
            } else if (ETAG.equalsIgnoreCase(headerName)) {
                headers.setETag(toString(headerValue));
            } else if (EXPIRES.equalsIgnoreCase(headerName)) {
                headers.setExpires(toLong(headerValue));
            } else if (IF_MODIFIED_SINCE.equalsIgnoreCase(headerName)) {
                headers.setIfModifiedSince(toLong(headerValue));
            } else if (IF_NONE_MATCH.equalsIgnoreCase(headerName)) {
                headers.setIfNoneMatch(toList(headerValue, String.class));
            } else if (LAST_MODIFIED.equalsIgnoreCase(headerName)) {
                headers.setLastModified(toLong(headerValue));
            } else if (LOCATION.equalsIgnoreCase(headerName)) {
                headers.setLocation(toURI(headerValue));
            } else if (ORIGIN.equalsIgnoreCase(headerName)) {
                headers.setOrigin(toString(headerValue));
            } else if (PRAGMA.equalsIgnoreCase(headerName)) {
                headers.setPragma(toString(headerValue));
            } else if (UPGRADE.equalsIgnoreCase(headerName)) {
                headers.setUpgrade(toString(headerValue));
            } else if (headerValue instanceof String) {
                headers.set(headerName, (String) headerValue);
            } else if (headerValue instanceof String[]) {
                headers.put(headerName, Arrays.asList((String[]) headerValue));
            } else if (instanceOf(headerValue, String.class)) {
                headers.put(headerName, toList(headerValue, String.class));
            } else {
                headers.set(headerName, conversionService.convert(headerValue, String.class));
            }//from w  ww.  j a  v  a  2s  .co  m
        }
    }
    return headers;
}