Example usage for org.springframework.http MediaType getType

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

Introduction

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

Prototype

public String getType() 

Source Link

Document

Return the primary type.

Usage

From source file:org.unitedinternet.cosmo.dav.property.ContentType.java

private static String mt(String type, String encoding) {
    final MediaType mediaType = MediaType.parseMediaType(type);
    if (StringUtils.hasText(encoding)) {
        return new MediaType(mediaType.getType(), mediaType.getSubtype(), Charset.forName(encoding)).toString();
    }/* w  w w. j av a2  s .co m*/
    return mediaType.toString();
}

From source file:net.acesinc.convergentui.content.TextHttpMessageConverter.java

@Override
public boolean canRead(Class<?> type, MediaType mt) {
    return mt != null && ("text".equalsIgnoreCase(mt.getType())
            || (mt.getSubtype() != null && mt.getSubtype().contains("javascript")));
}

From source file:org.terasoluna.gfw.functionaltest.app.download.DownloadTest.java

@Test
public void test01_01_fileDownload() throws IOException {
    ResponseEntity<byte[]> response = restTemplate.getForEntity(applicationContextUrl + "/download/1_1",
            byte[].class);
    ClassPathResource images = new ClassPathResource("/image/Duke.png");

    byte[] expected = StreamUtils.copyToByteArray(images.getInputStream());

    HttpHeaders headers = response.getHeaders();
    System.out.println("test01_01_fileDownload: X-Track=" + headers.getFirst("X-Track"));
    assertThat(headers.getFirst("Content-Disposition"), is("attachment; filename=Duke.png"));

    MediaType contentType = headers.getContentType();
    assertThat(contentType.getType(), is("image"));
    assertThat(contentType.getSubtype(), is("png"));

    assertThat(response.getBody(), is(expected));
}

From source file:org.terasoluna.gfw.functionaltest.app.download.DownloadTest.java

@Test
public void test01_02_fileDownload() {
    ResponseEntity<String> response = restTemplate.getForEntity(applicationContextUrl + "/download/1_2",
            String.class);

    HttpHeaders headers = response.getHeaders();
    System.out.println("test01_02_fileDownload: X-Track=" + headers.getFirst("X-Track"));

    assertThat(headers.getFirst("Content-Disposition"), is("attachment; filename=framework.txt"));

    MediaType contentType = headers.getContentType();
    assertThat(contentType.getType(), is("text"));
    assertThat(contentType.getSubtype(), is("plain"));
    assertThat(contentType.getParameter("charset"), equalToIgnoringCase("UTF-8"));

    assertThat(response.getBody(), is("Spring Framework"));

}

From source file:ar.com.zauber.commons.web.filter.webkit.WebKitContentTypeFilter.java

/**
 * @param request/*  w ww  . j a  v  a  2  s .  com*/
 *            con user Agent de webkit
 * @return si existe un text/html con q < 1.
 */
private boolean wrongMediaType(final HttpServletRequest request) {
    for (MediaType mediaType : getMediaTypes(request)) {
        if (mediaType.getType().contains(TEXT) && mediaType.getSubtype().contains(HTML)
                && mediaType.getQualityValue() < 1.0) {
            // si tiene prioridad menor a 1 y es de tipo text/html
            return true;
        }
    }
    return false;
}

From source file:net.acesinc.convergentui.content.BufferedImageHttpMessageConverter.java

@Override
public boolean canRead(Class<?> type, MediaType mt) {
    log.debug("Can we read: Class[ " + type + "] & ContentType[ " + mt + " ]");
    if (BufferedImage.class.isAssignableFrom(type) || (mt != null && "image".equalsIgnoreCase(mt.getType()))) {
        return true;
    } else {// w w w  . java 2  s  .c  o  m
        return false;
    }
}

From source file:net.eusashead.hateoas.hal.http.converter.HalHttpMessageConverter.java

@Override
protected void writeInternal(Object target, HttpOutputMessage outputMessage)
        throws IOException, HttpMessageNotWritableException {
    ReadableRepresentation rep = getRepresentation(target);
    MediaType contentType = outputMessage.getHeaders().getContentType();
    String mediaType = contentType.getType() + "/" + contentType.getSubtype();
    Writer writer = new OutputStreamWriter(outputMessage.getBody());
    rep.toString(mediaType, writer);// ww  w . j a  va 2  s.com
}

From source file:edu.mayo.trilliumbridge.webapp.TransformerController.java

protected void doTransform(HttpServletRequest request, HttpServletResponse response, String acceptHeader,
        String formatOverride, Transformer transformer) throws IOException {

    // default to XML if no Accept Header (it should at least be */*, but just in case).
    if (StringUtils.isBlank(acceptHeader)) {
        acceptHeader = MediaType.APPLICATION_ATOM_XML_VALUE;
    }/*from   w w  w  .j a v a 2 s .co m*/

    TrilliumBridgeTransformer.Format responseFormat = null;

    if (StringUtils.isNotBlank(formatOverride)) {
        responseFormat = TrilliumBridgeTransformer.Format.valueOf(formatOverride);
    } else {
        String[] accepts = StringUtils.split(acceptHeader, ',');

        for (String accept : accepts) {
            MediaType askedForType = MediaType.parseMediaType(accept);
            if (askedForType.isCompatibleWith(MediaType.TEXT_XML)
                    || askedForType.isCompatibleWith(MediaType.APPLICATION_XML)) {
                responseFormat = TrilliumBridgeTransformer.Format.XML;
            } else if (askedForType.isCompatibleWith(MediaType.TEXT_HTML)
                    || askedForType.isCompatibleWith(MediaType.APPLICATION_XHTML_XML)) {
                responseFormat = TrilliumBridgeTransformer.Format.HTML;
            } else if (askedForType.getType().equals("application")
                    && askedForType.getSubtype().equals("pdf")) {
                responseFormat = TrilliumBridgeTransformer.Format.PDF;
            }

            if (responseFormat != null) {
                break;
            }
        }
    }

    if (responseFormat == null) {
        throw new UserInputException("Cannot return type: " + acceptHeader, HttpStatus.NOT_ACCEPTABLE);
    }

    String contentType;
    switch (responseFormat) {
    case XML:
        contentType = MediaType.APPLICATION_XML_VALUE;
        break;
    case HTML:
        contentType = MediaType.TEXT_HTML_VALUE.toString();
        break;
    case PDF:
        contentType = "application/pdf";
        break;
    default:
        throw new IllegalStateException("Illegal Response Format");
    }

    InputStream inputStream;
    if (request instanceof MultipartHttpServletRequest) {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        MultipartFile multipartFile = multipartRequest.getFile(INPUT_FILE_NAME);
        inputStream = multipartFile.getInputStream();
    } else {
        inputStream = request.getInputStream();
    }

    inputStream = this.checkForUtf8BOMAndDiscardIfAny(this.checkStreamIsNotEmpty(inputStream));

    // create a buffer so we don't use the servlet's output stream unless
    // we get a successful transform, because if we do use it,
    // we can't use the error view anymore.
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    transformer.transform(inputStream, baos, responseFormat);

    try {
        response.setContentType(contentType);
        response.getOutputStream().write(baos.toByteArray());
    } finally {
        IOUtils.closeQuietly(baos);
    }

}

From source file:com.thoughtworks.go.server.security.BasicAuthenticationFilter.java

private boolean hasAccept(ServletRequest request, String expectedContentType) {
    if (request instanceof HttpServletRequest) {

        String accept = ((HttpServletRequest) request).getHeader("Accept");
        if (accept != null) {
            List<MediaType> mediaTypes = MediaType.parseMediaTypes(accept);
            for (MediaType mediaType : mediaTypes) {
                String type = mediaType.getType() + "/" + mediaType.getSubtype();
                if (type.equals(expectedContentType)) {
                    return true;
                }//from www . ja  va 2  s.co  m
            }
        }
    }
    return false;
}

From source file:org.alfresco.rest.framework.webscripts.ResourceWebScriptPut.java

/**
  * Returns the basic content info from the request.
  * @param req WebScriptRequest/*  w w  w . j a va2  s .  c om*/
  * @return BasicContentInfo
  */
private BasicContentInfo getContentInfo(WebScriptRequest req) {

    String encoding = "UTF-8";
    String contentType = MimetypeMap.MIMETYPE_BINARY;

    if (StringUtils.isNotEmpty(req.getContentType())) {
        MediaType media = MediaType.parseMediaType(req.getContentType());
        contentType = media.getType() + '/' + media.getSubtype();
        if (media.getCharSet() != null) {
            encoding = media.getCharSet().toString();
        }
    }

    return new ContentInfoImpl(contentType, encoding, -1, Locale.getDefault());
}