Example usage for org.springframework.http MediaType parseMediaType

List of usage examples for org.springframework.http MediaType parseMediaType

Introduction

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

Prototype

public static MediaType parseMediaType(String mediaType) 

Source Link

Document

Parse the given String into a single MediaType .

Usage

From source file:com.epam.ta.reportportal.ws.resolver.MvcJacksonViewsTest.java

/**
 * Validate that view-specific fields are included into response marked with
 * specific view/*from w  w  w  . j  a va  2s.c  om*/
 * 
 * @throws Exception
 */
@Test
public void testSpecificView() throws Exception {
    this.mvcMock
            .perform(get("/user/" + AuthConstants.TEST_USER).principal(AuthConstants.ADMINISTRATOR).secure(true)
                    .accept(MediaType.parseMediaType("application/json;charset=UTF-8")))
            .andExpect(status().isOk()).andExpect(content().contentType("application/json;charset=UTF-8"))
            .andExpect(jsonPath("$.userId").exists());

}

From source file:org.dawnsci.marketplace.controllers.FileController.java

@RequestMapping(value = "/{solution}/**", method = { RequestMethod.GET, RequestMethod.HEAD })
@ResponseBody/*  w  ww  . j  a  va2s  .co m*/
public ResponseEntity<FileSystemResource> getFile(@PathVariable("solution") String solution,
        HttpServletRequest request) {
    String path = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
    String bestMatchPattern = (String) request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);
    AntPathMatcher apm = new AntPathMatcher();
    path = apm.extractPathWithinPattern(bestMatchPattern, path);
    File file = fileService.getFile(solution, path);
    if (file.exists() && file.isFile()) {
        try {
            String detect = tika.detect(file);
            MediaType mediaType = MediaType.parseMediaType(detect);
            return ResponseEntity.ok().contentLength(file.length()).contentType(mediaType)
                    .lastModified(file.lastModified()).body(new FileSystemResource(file));
        } catch (IOException e) {
            e.printStackTrace();
        }
    } else {
        throw new ResourceNotFoundException();
    }
    return null;
}

From source file:th.co.geniustree.intenship.advisor.controller.AdviseController.java

@RequestMapping(value = "/getfileadvise/{id}", method = RequestMethod.GET)
public ResponseEntity<InputStreamResource> getFile(@PathVariable("id") FileUpload fileUpload) {
    ResponseEntity<InputStreamResource> body = ResponseEntity.ok().contentLength(fileUpload.getContent().length)
            .contentType(MediaType.parseMediaType(fileUpload.getMimeType()))
            .header("Content-Disposition", "attachment; filename=\"" + fileUpload.getName() + "\"")
            .body(new InputStreamResource(new ByteArrayInputStream(fileUpload.getContent())));
    return body;/*from w  w w  . j  a  va2 s.  co  m*/
}

From source file:guru.nidi.ramltester.uc.spring.MockContextTest.java

@Test
public void testGreetingWithMatcher() throws Exception {
    mockMvc.perform(get("/greeting").accept(MediaType.parseMediaType("application/json")))
            .andExpect(api.matches().aggregating(aggregator)).andExpect(status().isOk())
            .andExpect(content().contentTypeCompatibleWith("application/json"))
            .andExpect(jsonPath("$.content").value("Hello, World!"));
}

From source file:newcontroller.handler.impl.DefaultRequest.java

@Override
public <T> T body(Class<T> clazz) {
    MediaType mediaType = MediaType.parseMediaType(this.request.getContentType());
    HttpMessageConverter converter = HttpMessageConvertersHelper.findConverter(this.converters, clazz,
            mediaType);//from   w w  w . jav  a2  s .com
    try {
        return clazz.cast(converter.read(clazz, new ServletServerHttpRequest(this.request)));
    } catch (IOException e) {
        throw new UncheckedIOException(e); // TODO
    }
}

From source file:th.co.geniustree.intenship.advisor.controller.BehaviorController.java

@RequestMapping(value = "/getfilebehavior/{id}", method = RequestMethod.GET)
public ResponseEntity<InputStreamResource> getFile(@PathVariable("id") FileUpload fileUpload) {
    ResponseEntity<InputStreamResource> body = ResponseEntity.ok().contentLength(fileUpload.getContent().length)
            .contentType(MediaType.parseMediaType(fileUpload.getMimeType()))
            .header("Content-Disposition", "attachment; filename=\"" + fileUpload.getName() + "\"")
            .body(new InputStreamResource(new ByteArrayInputStream(fileUpload.getContent())));
    return body;/*from  w w w  .  j a va 2 s  .  co m*/
}

From source file:com.phoenixnap.oss.ramlapisync.generation.rules.spring.SpringRequestMappingClassAnnotationRule.java

private String generateMediaType(ApiResourceMetadata controllerMetadata) {
    String ramlMediaType = controllerMetadata.getDocument().getMediaType();
    if (!StringUtils.hasText(ramlMediaType)) {
        return null;
    }//from  ww  w . ja v a 2 s .c om
    return MediaType.parseMediaType(ramlMediaType).toString();
}

From source file:org.ala.spatial.web.services.LayerDistancesWSController.java

@RequestMapping(value = "/layers/analysis/inter_layer_association.csv", method = RequestMethod.GET)
public ResponseEntity<String> CSV(HttpServletRequest req) {
    String csv = makeCSV("displayname");
    HttpHeaders responseHeaders = new HttpHeaders();
    responseHeaders.setContentType(MediaType.parseMediaType("text/csv"));
    return new ResponseEntity<String>(csv, responseHeaders, HttpStatus.CREATED);
}

From source file:com.create.controller.TicketControllerTest.java

@Test
public void testGetAllTickets() throws Exception {
    // given//from w ww.  j  a  v  a 2 s. com
    final Person person = new Person("0", "Eva", "Mendes");
    final Ticket ticket = new Ticket("0", person);
    final String json = gson.toJson(Stream.of(ticket).collect(Collectors.toList()));

    // when
    // then
    mockMvc.perform(get("/tickets/list").accept(MediaType.parseMediaType("application/json;charset=UTF-8")))
            .andExpect(status().isOk()).andExpect(content().contentType("application/json;charset=UTF-8"))
            .andExpect(content().string(json));
}

From source file:wad.controller.ReceiptController.java

@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public ResponseEntity<byte[]> showReceipt(@PathVariable Long expenseId, @PathVariable Long id) {
    Receipt receipt = receiptRepository.findOne(id);
    Expense expense = expenseService.getExpense(expenseId);

    if (expense == null || !expense.isViewableBy(userService.getCurrentUser())) {
        throw new ResourceNotFoundException();
    }//  w  w  w. j  av a 2  s. c om

    final HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.parseMediaType(receipt.getMediaType()));
    headers.setContentLength(receipt.getSize());
    headers.add("Content-Disposition", "attachment; filename=" + receipt.getName());

    return new ResponseEntity<>(receipt.getContent(), headers, HttpStatus.CREATED);
}