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.MvcBinaryDataControllerTest.java

@Test
public void testPageableZero() throws Exception {
    this.mvcMock/*from  w w w.j  a va 2  s.  com*/
            .perform(get(PROJECT_BASE_URL + "/data/510e1f381812722383339f36").secure(true)
                    .accept(MediaType.parseMediaType("application/json;charset=UTF-8")))
            .andExpect(status().is(204));

}

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

/**
 * ?RestException.// w  w w.j a v  a  2s. c om
 */
@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:be.solidx.hot.utils.AbstractHttpDataDeserializer.java

@Override
public Object processRequestData(byte[] data, String contentType) {
    MediaType ct = MediaType.parseMediaType(contentType);

    if (LOGGER.isDebugEnabled())
        LOGGER.debug("Content-type: " + contentType);

    Charset charset = ct.getCharSet() == null ? Charset.forName("UTF-8") : ct.getCharSet();

    // Subtypes arre used because of possible encoding definitions
    if (ct.getSubtype().equals(MediaType.APPLICATION_OCTET_STREAM.getSubtype())) {
        return data;
    } else if (ct.getSubtype().equals(MediaType.APPLICATION_JSON.getSubtype())) {
        try {//from www  . jav  a 2 s.  co  m
            return fromJSON(data);
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage(), e.getCause());
        }
    } else if (ct.getSubtype().equals(MediaType.APPLICATION_XML.getSubtype())) {
        try {
            return toXML(data, ct.getCharSet() == null ? Charset.forName("UTF-8") : ct.getCharSet());
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage(), e.getCause());
        }
    } else if (ct.getSubtype().equals(MediaType.APPLICATION_FORM_URLENCODED.getSubtype())) {
        String decoded;
        try {
            decoded = URLDecoder.decode(new String(data, charset), charset.toString());
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage(), e.getCause());
        }
        return fromFormUrlEncoded(decoded);
    } else {
        return new String(data, ct.getCharSet() == null ? Charset.forName("UTF-8") : ct.getCharSet());
    }
}

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

@RequestMapping(value = "/student/{id}", method = RequestMethod.GET)
public ResponseEntity<InputStreamResource> getFileStudent(@PathVariable("id") FileUpload uploadFile) {
    ResponseEntity<InputStreamResource> body = ResponseEntity.ok().contentLength(uploadFile.getContent().length)
            .contentType(MediaType.parseMediaType(uploadFile.getMimeType()))
            .header("Content-Disposition", "attachment; filename=\"" + uploadFile.getName() + "\"")
            .body(new InputStreamResource(new ByteArrayInputStream(uploadFile.getContent())));
    return body;// w  ww .  jav a 2 s .  c  o  m
}

From source file:$.RestExceptionHandler.java

@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  www .  j  av  a 2  s  .  co m*/

From source file:com.yoncabt.ebr.ReportOutputFormat.java

public MediaType getMediaType() {
    return MediaType.parseMediaType(mimeType);
}

From source file:controllers.frontend.PostController.java

@GetMapping("/image/{postId}")
@ResponseBody/*from w w w . j av a  2s .  com*/
public ResponseEntity<byte[]> downloadPostImage(@PathVariable Long postId) throws SQLException {
    FileImage postImage = postService.getImageByPostId(postId);
    logger.info("Post Image Information: " + postImage.toString());
    return ResponseEntity.ok().contentLength(postImage.getSize())
            .contentType(MediaType.parseMediaType(postImage.getContentType())).body(postImage.getContent());
}

From source file:com.epam.ta.reportportal.ws.MvcPageableTest.java

@Test
public void testPageableZero() throws Exception {
    this.mvcMock/*from w ww . j av a  2 s .  com*/
            .perform(get(PROJECT_BASE_URL + getPageUrl("1")).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("$.page.number").value(1));

}

From source file:com.trenako.web.images.MultipartFileValidator.java

@Override
public void validate(Object target, Errors errors) {
    // skip validation for empty files.
    if (target == null)
        return;//from   w  w  w  . j av  a2s. c om

    MultipartFile file = (MultipartFile) target;
    if (file.isEmpty())
        return;

    // validate file size
    if (file.getSize() > AppGlobals.MAX_UPLOAD_SIZE) {
        errors.reject("uploadFile.size.range.notmet", "Invalid media size. Max size is 512 Kb");
    }

    // validate content type
    MediaType contentType = MediaType.parseMediaType(file.getContentType());
    if (!AppGlobals.ALLOWED_MEDIA_TYPES.contains(contentType.toString())) {
        errors.reject("uploadFile.contentType.notvalid", "Invalid media type");
    }
}

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

/**
 * Validate that there is no view-related fields included in response marked
 * with default view/*from   w  w w.  j av a 2 s .c o m*/
 * 
 * @throws Exception
 */
@Test
public void testDefaultView() throws Exception {
    this.mvcMock
            .perform(get("/project/" + PROJECT_BASE_URL + "/users").secure(true)
                    .accept(MediaType.parseMediaType("application/json;charset=UTF-8")))
            .andExpect(status().isOk()).andExpect(content().contentType("application/json;charset=UTF-8"));

}