Example usage for org.springframework.http.converter HttpMessageConverter getSupportedMediaTypes

List of usage examples for org.springframework.http.converter HttpMessageConverter getSupportedMediaTypes

Introduction

In this page you can find the example usage for org.springframework.http.converter HttpMessageConverter getSupportedMediaTypes.

Prototype

List<MediaType> getSupportedMediaTypes();

Source Link

Document

Return the list of MediaType objects supported by this converter.

Usage

From source file:org.cloudfoundry.identity.uaa.error.ExceptionReportHttpMessageConverter.java

@Override
public List<MediaType> getSupportedMediaTypes() {
    Set<MediaType> list = new LinkedHashSet<MediaType>();
    for (HttpMessageConverter<?> converter : messageConverters) {
        list.addAll(converter.getSupportedMediaTypes());
    }//  w  ww  .ja va 2s  . co  m
    return new ArrayList<MediaType>(list);
}

From source file:org.cloudfoundry.identity.uaa.error.ExceptionReportHttpMessageConverter.java

@Override
protected ExceptionReport readInternal(Class<? extends ExceptionReport> clazz, HttpInputMessage inputMessage)
        throws IOException, HttpMessageNotReadableException {
    for (HttpMessageConverter<?> converter : messageConverters) {
        for (MediaType mediaType : converter.getSupportedMediaTypes()) {
            if (converter.canRead(Map.class, mediaType)) {
                @SuppressWarnings({ "rawtypes", "unchecked" })
                HttpMessageConverter<Map> messageConverter = (HttpMessageConverter<Map>) converter;
                @SuppressWarnings("unchecked")
                Map<String, String> map = messageConverter.read(Map.class, inputMessage);
                return new ExceptionReport(getException(map));
            }/* www  .  java  2  s.  c  om*/
        }
    }
    return null;
}

From source file:org.cloudfoundry.identity.uaa.error.ExceptionReportHttpMessageConverter.java

@Override
protected void writeInternal(ExceptionReport report, HttpOutputMessage outputMessage)
        throws IOException, HttpMessageNotWritableException {
    Exception e = report.getException();
    Map<String, String> map = new HashMap<String, String>();
    map.put("error", UaaStringUtils.getErrorName(e));
    map.put("message", e.getMessage());
    if (report.isTrace()) {
        StringWriter trace = new StringWriter();
        e.printStackTrace(new PrintWriter(trace));
        map.put("trace", trace.toString());
    }// w  ww  .ja v  a2s.  c  o  m
    for (HttpMessageConverter<?> converter : messageConverters) {
        for (MediaType mediaType : converter.getSupportedMediaTypes()) {
            if (converter.canWrite(Map.class, mediaType)) {
                @SuppressWarnings({ "rawtypes", "unchecked" })
                HttpMessageConverter<Map> messageConverter = (HttpMessageConverter<Map>) converter;
                messageConverter.write(map, mediaType, outputMessage);
                return;
            }
        }
    }
}

From source file:org.craftercms.commons.rest.HttpMessageConvertingResponseWriter.java

/**
 * Return the media types supported by all provided message converters sorted by specificity via
 * {@link MediaType#sortBySpecificity(List)}.
 *//*w  w  w . j  av a2  s  .  co  m*/
protected List<MediaType> getAllSupportedMediaTypes(List<HttpMessageConverter<?>> messageConverters) {
    Set<MediaType> allSupportedMediaTypes = new LinkedHashSet<MediaType>();
    for (HttpMessageConverter<?> messageConverter : messageConverters) {
        allSupportedMediaTypes.addAll(messageConverter.getSupportedMediaTypes());
    }

    List<MediaType> result = new ArrayList<MediaType>(allSupportedMediaTypes);

    MediaType.sortBySpecificity(result);

    return Collections.unmodifiableList(result);
}

From source file:net.solarnetwork.web.test.SimpleCsvHttpMessageCoverterTest.java

@Test
public void testAPIContract() {
    HttpMessageConverter<?> hmc = new SimpleCsvHttpMessageConverter();
    List<MediaType> supportedTypes = hmc.getSupportedMediaTypes();
    Assert.assertEquals("Supports text/csv", Arrays.asList(CSV_MEDIA_TYPE), supportedTypes);
    Assert.assertTrue("Can write CSV", hmc.canWrite(Object.class, CSV_MEDIA_TYPE));
}

From source file:org.craftercms.commons.rest.HttpMessageConvertingResponseWriter.java

/**
 * Returns the media types that can be produced:
 * <ul>//from w  ww. j  av  a 2  s .  c o m
 *    <li>Media types of configured converters that can write the specific return value, or</li>
 *    <li>{@link org.springframework.http.MediaType#ALL}</li>
 * </ul>
 */
protected List<MediaType> getProducibleMediaTypes(Class<?> returnValueClass) {
    if (!allSupportedMediaTypes.isEmpty()) {
        List<MediaType> result = new ArrayList<>();
        for (HttpMessageConverter<?> converter : messageConverters) {
            if (converter.canWrite(returnValueClass, null)) {
                result.addAll(converter.getSupportedMediaTypes());
            }
        }

        return result;
    } else {
        return Collections.singletonList(MediaType.ALL);
    }
}

From source file:edu.colorado.orcid.impl.OrcidServicePublicImpl.java

public String createOrcid(String email, String givenNames, String familyName)
        throws OrcidException, OrcidEmailExistsException, OrcidHttpException {

    String newOrcid = null;/*from   w ww .j  av a  2 s  .com*/

    log.debug("Creating ORCID...");
    log.debug("email: " + email);
    log.debug("givenNames: " + givenNames);
    log.debug("familyName: " + familyName);

    HttpHeaders headers = new HttpHeaders();
    headers.set("Accept", "application/xml");
    headers.set("Content-Type", "application/vdn.orcid+xml");
    headers.set("Authorization", "Bearer " + orcidCreateToken);

    OrcidMessage message = new OrcidMessage();
    message.setEmail(email);
    message.setGivenNames(givenNames);
    message.setFamilyName(familyName);
    message.setMessageVersion(orcidMessageVersion);
    //TODO Affiliation should be set based on organization once faculty from more than one organization are processed
    message.setAffiliationType(OrcidMessage.AFFILIATION_TYPE_EMPLOYMENT);
    message.setAffiliationOrganizationName(OrcidMessage.CU_BOULDER);
    message.setAffiliationOrganizationAddressCity(OrcidMessage.BOULDER);
    message.setAffiliationOrganizationAddressRegion(OrcidMessage.CO);
    message.setAffiliationOrganizationAddressCountry(OrcidMessage.US);
    message.setAffiliationOrganizationDisambiguatedId(OrcidMessage.DISAMBIGUATED_ID_CU_BOULDER);
    message.setAffiliationOrganizationDisambiguationSource(OrcidMessage.DISAMBIGUATION_SOURCE_RINGOLD);

    HttpEntity entity = new HttpEntity(message, headers);

    log.debug("Configured RestTemplate Message Converters...");
    List<HttpMessageConverter<?>> converters = orcidRestTemplate.getMessageConverters();
    for (HttpMessageConverter<?> converter : converters) {
        log.debug("converter: " + converter);
        log.debug("supported media types: " + converter.getSupportedMediaTypes());
        log.debug("converter.canWrite(String.class, MediaType.APPLICATION_XML): "
                + converter.canWrite(String.class, MediaType.APPLICATION_XML));
        log.debug("converter.canWrite(Message.class, MediaType.APPLICATION_XML): "
                + converter.canWrite(OrcidMessage.class, MediaType.APPLICATION_XML));
    }

    log.debug("Request headers: " + headers);

    HttpStatus responseStatusCode = null;
    String responseBody = null;

    try {
        if (useTestHttpProxy.equalsIgnoreCase("TRUE")) {
            log.info("Using HTTP ***TEST*** proxy...");
            System.setProperty("http.proxyHost", testHttpProxyHost);
            System.setProperty("http.proxyPort", testHttpProxyPort);
            log.info("http.proxyHost: " + System.getProperty("http.proxyHost"));
            log.info("http.proxyPort: " + System.getProperty("http.proxyPort"));
        }
        ResponseEntity<String> responseEntity = orcidRestTemplate.postForEntity(orcidCreateURL, entity,
                String.class);
        responseStatusCode = responseEntity.getStatusCode();
        responseBody = responseEntity.getBody();
        HttpHeaders responseHeaders = responseEntity.getHeaders();
        URI locationURI = responseHeaders.getLocation();
        String uriString = locationURI.toString();
        newOrcid = extractOrcid(uriString);
        log.debug("HTTP response status code: " + responseStatusCode);
        log.debug("HTTP response headers:     " + responseHeaders);
        log.debug("HTTP response body:        " + responseBody);
        log.debug("HTTP response location:    " + locationURI);
        log.debug("New ORCID:                 " + newOrcid);
    } catch (HttpClientErrorException e) {
        if (e.getStatusCode().equals(HttpStatus.BAD_REQUEST)) {
            log.debug(e.getStatusCode());
            log.debug(e.getResponseBodyAsString());
            log.debug(e.getMessage());
            throw new OrcidEmailExistsException(e);
        }
        OrcidHttpException ohe = new OrcidHttpException(e);
        ohe.setStatusCode(e.getStatusCode());
        throw ohe;
    }

    return newOrcid;
}

From source file:com.revolsys.ui.web.rest.interceptor.WebAnnotationMethodHandlerAdapter.java

private View handleResponseBody(final Object returnValue, final ServletWebRequest webRequest)
        throws ServletException, IOException {

    final HttpServletRequest request = webRequest.getRequest();
    String jsonp = request.getParameter("jsonp");
    if (jsonp == null) {
        jsonp = request.getParameter("callback");
    }//  w w w .  j a va2s.com
    request.setAttribute(IoConstants.JSONP_PROPERTY, jsonp);
    List<MediaType> acceptedMediaTypes = MediaTypeUtil.getAcceptedMediaTypes(request,
            WebAnnotationMethodHandlerAdapter.this.mediaTypes,
            WebAnnotationMethodHandlerAdapter.this.mediaTypeOrder,
            WebAnnotationMethodHandlerAdapter.this.urlPathHelper,
            WebAnnotationMethodHandlerAdapter.this.parameterName,
            WebAnnotationMethodHandlerAdapter.this.defaultMediaType);
    if (acceptedMediaTypes.isEmpty()) {
        acceptedMediaTypes = Collections.singletonList(MediaType.ALL);
    }
    final Class<?> returnValueType = returnValue.getClass();
    final Set<MediaType> allSupportedMediaTypes = new LinkedHashSet<>();
    if (WebAnnotationMethodHandlerAdapter.this.messageConverters != null) {
        for (final MediaType acceptedMediaType : acceptedMediaTypes) {
            for (final HttpMessageConverter<?> messageConverter : WebAnnotationMethodHandlerAdapter.this.messageConverters) {
                allSupportedMediaTypes.addAll(messageConverter.getSupportedMediaTypes());
                if (messageConverter.canWrite(returnValueType, acceptedMediaType)) {
                    final MediaType mediaType = getMediaType(messageConverter.getSupportedMediaTypes(),
                            acceptedMediaType);
                    return new HttpMessageConverterView(messageConverter, mediaType, returnValue);
                }
            }
        }
    }
    throw new HttpMediaTypeNotAcceptableException(new ArrayList<>(allSupportedMediaTypes));
}

From source file:org.cloudfoundry.identity.uaa.error.ConvertingExceptionView.java

@SuppressWarnings("unchecked")
private void writeWithMessageConverters(Object returnValue, HttpInputMessage inputMessage,
        HttpOutputMessage outputMessage) throws IOException, HttpMediaTypeNotAcceptableException {
    List<MediaType> acceptedMediaTypes = inputMessage.getHeaders().getAccept();
    if (acceptedMediaTypes.isEmpty()) {
        acceptedMediaTypes = Collections.singletonList(MediaType.ALL);
    }//w w  w . j  a va  2  s  .  c o  m
    MediaType.sortByQualityValue(acceptedMediaTypes);
    Class<?> returnValueType = returnValue.getClass();
    List<MediaType> allSupportedMediaTypes = new ArrayList<MediaType>();
    if (messageConverters != null) {
        for (MediaType acceptedMediaType : acceptedMediaTypes) {
            for (@SuppressWarnings("rawtypes")
            HttpMessageConverter messageConverter : messageConverters) {
                if (messageConverter.canWrite(returnValueType, acceptedMediaType)) {
                    messageConverter.write(returnValue, acceptedMediaType, outputMessage);
                    if (logger.isDebugEnabled()) {
                        MediaType contentType = outputMessage.getHeaders().getContentType();
                        if (contentType == null) {
                            contentType = acceptedMediaType;
                        }
                        logger.debug("Written [" + returnValue + "] as \"" + contentType + "\" using ["
                                + messageConverter + "]");
                    }
                    // this.responseArgumentUsed = true;
                    return;
                }
            }
        }
        for (@SuppressWarnings("rawtypes")
        HttpMessageConverter messageConverter : messageConverters) {
            allSupportedMediaTypes.addAll(messageConverter.getSupportedMediaTypes());
        }
    }
    throw new HttpMediaTypeNotAcceptableException(allSupportedMediaTypes);
}

From source file:org.encuestame.oauth.AbstractOAuthSupport.java

/**
 * Define customizable list of {@link HttpMessageConverter}.
 * @param converters/*from w ww .  j ava 2s .  c o  m*/
 */
public AbstractOAuthSupport(final List<HttpMessageConverter<?>> converters) {
    this.restTemplate = new RestTemplate();
    this.restTemplate.setMessageConverters(converters);
    log.debug("OAuth Converters Size " + this.restTemplate.getMessageConverters().size());
    if (log.isDebugEnabled()) {
        for (HttpMessageConverter<?> httpMessageConverter : this.restTemplate.getMessageConverters()) {
            log.debug("--- OAuth Converters " + httpMessageConverter);
            for (MediaType medidaType : httpMessageConverter.getSupportedMediaTypes()) {
                log.debug("------ Converter Media Type " + medidaType.toString());
            }
        }
    }
}