Example usage for org.springframework.http MediaType ALL

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

Introduction

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

Prototype

MediaType ALL

To view the source code for org.springframework.http MediaType ALL.

Click Source Link

Document

Public constant media type that includes all media ranges (i.e.

Usage

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

public <T> void writeWithMessageConverters(T returnValue, HttpServletRequest request,
        HttpServletResponse response) throws IOException, HttpMediaTypeNotAcceptableException {
    Class<?> returnValueClass = returnValue.getClass();
    List<MediaType> requestedMediaTypes = getAcceptableMediaTypes(request);
    List<MediaType> producibleMediaTypes = getProducibleMediaTypes(returnValueClass);

    Set<MediaType> compatibleMediaTypes = new LinkedHashSet<MediaType>();
    for (MediaType r : requestedMediaTypes) {
        for (MediaType p : producibleMediaTypes) {
            if (r.isCompatibleWith(p)) {
                compatibleMediaTypes.add(getMostSpecificMediaType(r, p));
            }/* www.  j a v a 2s  . co m*/
        }
    }
    if (compatibleMediaTypes.isEmpty()) {
        throw new HttpMediaTypeNotAcceptableException(producibleMediaTypes);
    }

    List<MediaType> mediaTypes = new ArrayList<MediaType>(compatibleMediaTypes);
    MediaType.sortBySpecificityAndQuality(mediaTypes);

    MediaType selectedMediaType = null;
    for (MediaType mediaType : mediaTypes) {
        if (mediaType.isConcrete()) {
            selectedMediaType = mediaType;
            break;
        } else if (mediaType.equals(MediaType.ALL) || mediaType.equals(MEDIA_TYPE_APPLICATION)) {
            selectedMediaType = MediaType.APPLICATION_OCTET_STREAM;
            break;
        }
    }

    if (selectedMediaType != null) {
        selectedMediaType = selectedMediaType.removeQualityValue();
        for (HttpMessageConverter<?> messageConverter : messageConverters) {
            if (messageConverter.canWrite(returnValueClass, selectedMediaType)) {
                ((HttpMessageConverter<T>) messageConverter).write(returnValue, selectedMediaType,
                        new ServletServerHttpResponse(response));

                logger.debug(LOG_KEY_WRITTEN_WITH_MESSAGE_CONVERTER, returnValue, selectedMediaType,
                        messageConverter);

                return;
            }
        }
    }

    throw new HttpMediaTypeNotAcceptableException(allSupportedMediaTypes);
}

From source file:at.ac.tuwien.dsg.cloud.utilities.gateway.registry.KongService.java

public KongUserKeyObject createKeyForUser(String userName) {
    RequestEntity<Void> requestEntity = RequestEntity
            .post(URI.create(this.kongUris.getKongKeyForConsumerUri(userName)))
            //.contentType(MediaType.APPLICATION_JSON)
            .accept(MediaType.ALL).build();

    return this.restUtilities.simpleRestExchange(requestEntity, KongUserKeyObject.class).getBody();
}

From source file:at.ac.tuwien.dsg.cloud.utilities.gateway.registry.UserController.java

@RequestMapping(method = RequestMethod.DELETE, value = REST_USER_PATH_VARIABLE)
public ResponseEntity<String> remove(@PathVariable String user) {
    boolean res = this.kongUsers.getUsers().removeIf((KongUser u) -> {
        if (u.getUserName().equals(user)) {
            RequestEntity<Void> request = RequestEntity
                    .delete(URI.create(this.kongUris.getKongConsumerIdUri(u.getId()))).accept(MediaType.ALL)
                    .build();/* w  ww. j av a  2  s.  c o  m*/

            ResponseEntity<String> resp = restUtilities.simpleRestExchange(request, String.class);

            if (resp == null || resp.getStatusCode() != HttpStatus.NO_CONTENT) {
                return false;
            }

            return true;
        }
        return false;
    });

    if (!res) {
        return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
    }

    return new ResponseEntity<>(HttpStatus.OK);
}

From source file:com.onedrive.api.internal.MultipartRelatedHttpMessageConverter.java

public boolean canWrite(Class<?> clazz, MediaType mediaType) {
    if (!MultiValueMap.class.isAssignableFrom(clazz)) {
        return false;
    }/*from w w w  . j  a  v a 2 s . com*/
    if (mediaType == null || MediaType.ALL.equals(mediaType)) {
        return true;
    }
    for (MediaType supportedMediaType : getSupportedMediaTypes()) {
        if (supportedMediaType.isCompatibleWith(mediaType)) {
            return true;
        }
    }
    return false;
}

From source file:sample.HandsOnSecurityApplicationTests.java

@Test
public void authenticateBrowser() throws Exception {
    MockHttpServletRequestBuilder authenticate = get("/users/search/self").accept(MediaType.TEXT_HTML,
            MediaType.ALL);
    mockMvc.perform(authenticate).andExpect(status().isOk())
            .andExpect(jsonPath("$.email", is("rob@example.com")))
            .andExpect(header().doesNotExist("x-auth-token")).andExpect(cookie().exists("SESSION"));
}

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);
    }//from   w w  w  .  j ava 2 s .co  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:com.epam.ta.reportportal.commons.exception.rest.RestExceptionHandler.java

@SuppressWarnings({ "rawtypes", "unchecked", "resource" })
private ModelAndView handleResponseBody(Object body, ServletWebRequest webRequest)
        throws HttpMessageNotWritableException, IOException {

    HttpInputMessage inputMessage = new ServletServerHttpRequest(webRequest.getRequest());

    List<MediaType> acceptedMediaTypes = inputMessage.getHeaders().getAccept();
    if (acceptedMediaTypes.isEmpty()) {
        acceptedMediaTypes = Collections.singletonList(MediaType.ALL);
    }/*from   w w  w.  jav a2s  .c om*/

    MediaType.sortByQualityValue(acceptedMediaTypes);

    HttpOutputMessage outputMessage = new ServletServerHttpResponse(webRequest.getResponse());

    Class<?> bodyType = body.getClass();

    List<HttpMessageConverter<?>> converters = this.messageConverters;

    if (converters != null) {
        for (MediaType acceptedMediaType : acceptedMediaTypes) {
            for (HttpMessageConverter messageConverter : converters) {
                if (messageConverter.canWrite(bodyType, acceptedMediaType)) {
                    messageConverter.write(body, acceptedMediaType, outputMessage);
                    // return empty model and view to short circuit the
                    // iteration and to let
                    // Spring know that we've rendered the view ourselves:
                    return new ModelAndView();
                }
            }
        }
    }

    if (logger.isWarnEnabled()) {
        logger.warn("Could not find HttpMessageConverter that supports return type [" + bodyType + "] and "
                + acceptedMediaTypes);
    }
    return null;
}

From source file:de.qucosa.webapi.v1.RelationResourceTest.java

@Test
public void returns404WithNoContent() throws Exception {
    when(fedoraRepository.getPIDByIdentifier(anyString()))
            .thenThrow(new FedoraClientException(404, "NOT FOUND"));

    mockMvc.perform(get("/relation/urn/urn:not-there").accept(MediaType.ALL)).andExpect(status().isNotFound());
}

From source file:sample.HandsOnSecurityApplicationTests.java

@Test
public void authenticateCurl() throws Exception {
    MockHttpServletRequestBuilder authenticate = get("/users/search/self").accept(MediaType.ALL);
    mockMvc.perform(authenticate).andExpect(status().isOk())
            .andExpect(jsonPath("$.email", is("rob@example.com")))
            .andExpect(header().string("x-auth-token", notNullValue()))
            .andExpect(cookie().doesNotExist("SESSION"));
}