Java Code Examples for org.springframework.http.MediaType#isCompatibleWith()
The following examples show how to use
org.springframework.http.MediaType#isCompatibleWith() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EncoderHttpMessageWriter.java From spring-analysis-note with MIT License | 5 votes |
private boolean isStreamingMediaType(@Nullable MediaType mediaType) { if (mediaType == null || !(this.encoder instanceof HttpMessageEncoder)) { return false; } for (MediaType streamingMediaType : ((HttpMessageEncoder<?>) this.encoder).getStreamingMediaTypes()) { if (mediaType.isCompatibleWith(streamingMediaType) && matchParameters(mediaType, streamingMediaType)) { return true; } } return false; }
Example 2
Source File: FormHttpMessageConverter.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Override public boolean canWrite(Class<?> clazz, MediaType mediaType) { if (!MultiValueMap.class.isAssignableFrom(clazz)) { return false; } if (mediaType == null || MediaType.ALL.equals(mediaType)) { return true; } for (MediaType supportedMediaType : getSupportedMediaTypes()) { if (supportedMediaType.isCompatibleWith(mediaType)) { return true; } } return false; }
Example 3
Source File: JSONLDW3CAnnotationPageMessageConverter.java From elucidate-server with MIT License | 5 votes |
@Override protected boolean canWrite(MediaType mediaType) { if (mediaType == null || MediaType.ALL.equals(mediaType)) { return true; } for (MediaType supportedMediaType : getSupportedMediaTypes()) { if (supportedMediaType.isCompatibleWith(mediaType)) { return true; } } return false; }
Example 4
Source File: FakeApiController.java From openapi-generator with Apache License 2.0 | 5 votes |
/** * POST /fake/{petId}/uploadImageWithRequiredFile : uploads an image (required) * * @param petId ID of pet to update (required) * @param requiredFile file to upload (required) * @param additionalMetadata Additional data to pass to server (optional) * @return successful operation (status code 200) * @see FakeApi#uploadFileWithRequiredFile */ public ResponseEntity<ModelApiResponse> uploadFileWithRequiredFile(@ApiParam(value = "ID of pet to update",required=true) @PathVariable("petId") Long petId,@ApiParam(value = "file to upload") @Valid @RequestPart(value = "requiredFile", required = true) MultipartFile requiredFile,@ApiParam(value = "Additional data to pass to server") @Valid @RequestPart(value = "additionalMetadata", required = false) String additionalMetadata) { for (MediaType mediaType: MediaType.parseMediaTypes(request.getHeader("Accept"))) { if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) { String exampleString = "{ \"code\" : 0, \"type\" : \"type\", \"message\" : \"message\" }"; ApiUtil.setExampleResponse(request, "application/json", exampleString); break; } } return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED); }
Example 5
Source File: JSONLDW3CAnnotationPageMessageConverter.java From elucidate-server with MIT License | 5 votes |
@Override protected boolean canRead(MediaType mediaType) { if (mediaType == null) { return true; } for (MediaType supportedMediaType : getSupportedMediaTypes()) { if (supportedMediaType.isCompatibleWith(mediaType)) { return true; } } return false; }
Example 6
Source File: PetApiController.java From openapi-generator with Apache License 2.0 | 5 votes |
/** * POST /pet/{petId}/uploadImage : uploads an image * * @param petId ID of pet to update (required) * @param additionalMetadata Additional data to pass to server (optional) * @param file file to upload (optional) * @return successful operation (status code 200) * @see PetApi#uploadFile */ public ResponseEntity<ModelApiResponse> uploadFile(@ApiParam(value = "ID of pet to update",required=true) @PathVariable("petId") Long petId,@ApiParam(value = "Additional data to pass to server") @Valid @RequestPart(value = "additionalMetadata", required = false) String additionalMetadata,@ApiParam(value = "file to upload") @Valid @RequestPart(value = "file", required = false) MultipartFile file) { for (MediaType mediaType: MediaType.parseMediaTypes(request.getHeader("Accept"))) { if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) { String exampleString = "{ \"code\" : 0, \"type\" : \"type\", \"message\" : \"message\" }"; ApiUtil.setExampleResponse(request, "application/json", exampleString); break; } } return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED); }
Example 7
Source File: AbstractHttpMessageConverter.java From lams with GNU General Public License v2.0 | 5 votes |
/** * Returns {@code true} if the given media type includes any of the * {@linkplain #setSupportedMediaTypes(List) supported media types}. * @param mediaType the media type to write, can be {@code null} if not specified. * Typically the value of an {@code Accept} header. * @return {@code true} if the supported media types are compatible with the media type, * or if the media type is {@code null} */ protected boolean canWrite(MediaType mediaType) { if (mediaType == null || MediaType.ALL.equals(mediaType)) { return true; } for (MediaType supportedMediaType : getSupportedMediaTypes()) { if (supportedMediaType.isCompatibleWith(mediaType)) { return true; } } return false; }
Example 8
Source File: AnotherFakeApiController.java From openapi-generator with Apache License 2.0 | 5 votes |
/** * PATCH /another-fake/dummy : To test special tags * To test special tags and operation ID starting with number * * @param body client model (required) * @return successful operation (status code 200) * @see AnotherFakeApi#call123testSpecialTags */ public ResponseEntity<Client> call123testSpecialTags(@ApiParam(value = "client model" ,required=true ) @Valid @RequestBody Client body) { for (MediaType mediaType: MediaType.parseMediaTypes(request.getHeader("Accept"))) { if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) { String exampleString = "{ \"client\" : \"client\" }"; ApiUtil.setExampleResponse(request, "application/json", exampleString); break; } } return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED); }
Example 9
Source File: AbstractHttpMessageConverter.java From spring4-understanding with Apache License 2.0 | 5 votes |
/** * Returns {@code true} if the given media type includes any of the * {@linkplain #setSupportedMediaTypes(List) supported media types}. * @param mediaType the media type to write, can be {@code null} if not specified. * Typically the value of an {@code Accept} header. * @return {@code true} if the supported media types are compatible with the media type, * or if the media type is {@code null} */ protected boolean canWrite(MediaType mediaType) { if (mediaType == null || MediaType.ALL.equals(mediaType)) { return true; } for (MediaType supportedMediaType : getSupportedMediaTypes()) { if (supportedMediaType.isCompatibleWith(mediaType)) { return true; } } return false; }
Example 10
Source File: JSONLDOAAnnotationHistoryMessageConverter.java From elucidate-server with MIT License | 5 votes |
@Override protected boolean canRead(MediaType mediaType) { if (mediaType == null) { return true; } for (MediaType supportedMediaType : getSupportedMediaTypes()) { if (supportedMediaType.isCompatibleWith(mediaType)) { return true; } } return false; }
Example 11
Source File: StringHttpMessageConverter.java From spring-analysis-note with MIT License | 5 votes |
private Charset getContentTypeCharset(@Nullable MediaType contentType) { if (contentType != null && contentType.getCharset() != null) { return contentType.getCharset(); } else if (contentType != null && contentType.isCompatibleWith(MediaType.APPLICATION_JSON)) { // Matching to AbstractJackson2HttpMessageConverter#DEFAULT_CHARSET return StandardCharsets.UTF_8; } else { Charset charset = getDefaultCharset(); Assert.state(charset != null, "No default charset"); return charset; } }
Example 12
Source File: JSONLDOAStatisticsPageMessageConverter.java From elucidate-server with MIT License | 5 votes |
@Override protected boolean canWrite(MediaType mediaType) { if (mediaType == null || MediaType.ALL.equals(mediaType)) { return true; } for (MediaType supportedMediaType : getSupportedMediaTypes()) { if (supportedMediaType.isCompatibleWith(mediaType)) { return true; } } return false; }
Example 13
Source File: ProtobufHttpMessageConverter.java From spring-analysis-note with MIT License | 5 votes |
@Override public void print(Message message, OutputStream output, MediaType contentType, Charset charset) throws IOException, HttpMessageConversionException { if (contentType.isCompatibleWith(APPLICATION_JSON)) { OutputStreamWriter writer = new OutputStreamWriter(output, charset); this.printer.appendTo(message, writer); writer.flush(); } else { throw new HttpMessageConversionException( "protobuf-java-util does not support printing " + contentType); } }
Example 14
Source File: FakeApiController.java From openapi-generator with Apache License 2.0 | 5 votes |
/** * POST /fake/{petId}/uploadImageWithRequiredFile : uploads an image (required) * * @param petId ID of pet to update (required) * @param requiredFile file to upload (required) * @param additionalMetadata Additional data to pass to server (optional) * @return successful operation (status code 200) * @see FakeApi#uploadFileWithRequiredFile */ public ResponseEntity<ModelApiResponse> uploadFileWithRequiredFile(@ApiParam(value = "ID of pet to update",required=true) @PathVariable("petId") Long petId,@ApiParam(value = "file to upload") @Valid @RequestPart(value = "requiredFile", required = true) MultipartFile requiredFile,@ApiParam(value = "Additional data to pass to server") @Valid @RequestPart(value = "additionalMetadata", required = false) String additionalMetadata) { for (MediaType mediaType: MediaType.parseMediaTypes(request.getHeader("Accept"))) { if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) { String exampleString = "{ \"code\" : 0, \"type\" : \"type\", \"message\" : \"message\" }"; ApiUtil.setExampleResponse(request, "application/json", exampleString); break; } } return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED); }
Example 15
Source File: JSONLDOAAnnotationMessageConverter.java From elucidate-server with MIT License | 5 votes |
@Override protected boolean canRead(MediaType mediaType) { if (mediaType == null) { return true; } for (MediaType supportedMediaType : getSupportedMediaTypes()) { if (supportedMediaType.isCompatibleWith(mediaType)) { return true; } } return false; }
Example 16
Source File: JSONLDOAStatisticsPageMessageConverter.java From elucidate-server with MIT License | 5 votes |
@Override protected boolean canRead(MediaType mediaType) { if (mediaType == null) { return true; } for (MediaType supportedMediaType : getSupportedMediaTypes()) { if (supportedMediaType.isCompatibleWith(mediaType)) { return true; } } return false; }
Example 17
Source File: JSONLDW3CStatisticsPageMessageConverter.java From elucidate-server with MIT License | 5 votes |
@Override protected boolean canRead(MediaType mediaType) { if (mediaType == null) { return true; } for (MediaType supportedMediaType : getSupportedMediaTypes()) { if (supportedMediaType.isCompatibleWith(mediaType)) { return true; } } return false; }
Example 18
Source File: ContentNegotiatingViewResolver.java From spring-analysis-note with MIT License | 5 votes |
/** * Determines the list of {@link MediaType} for the given {@link HttpServletRequest}. * @param request the current servlet request * @return the list of media types requested, if any */ @Nullable protected List<MediaType> getMediaTypes(HttpServletRequest request) { Assert.state(this.contentNegotiationManager != null, "No ContentNegotiationManager set"); try { ServletWebRequest webRequest = new ServletWebRequest(request); List<MediaType> acceptableMediaTypes = this.contentNegotiationManager.resolveMediaTypes(webRequest); List<MediaType> producibleMediaTypes = getProducibleMediaTypes(request); Set<MediaType> compatibleMediaTypes = new LinkedHashSet<>(); for (MediaType acceptable : acceptableMediaTypes) { for (MediaType producible : producibleMediaTypes) { if (acceptable.isCompatibleWith(producible)) { compatibleMediaTypes.add(getMostSpecificMediaType(acceptable, producible)); } } } List<MediaType> selectedMediaTypes = new ArrayList<>(compatibleMediaTypes); MediaType.sortBySpecificityAndQuality(selectedMediaTypes); return selectedMediaTypes; } catch (HttpMediaTypeNotAcceptableException ex) { if (logger.isDebugEnabled()) { logger.debug(ex.getMessage()); } return null; } }
Example 19
Source File: JSONLDOAAnnotationCollectionMessageConverter.java From elucidate-server with MIT License | 5 votes |
@Override protected boolean canRead(MediaType mediaType) { if (mediaType == null) { return true; } for (MediaType supportedMediaType : getSupportedMediaTypes()) { if (supportedMediaType.isCompatibleWith(mediaType)) { return true; } } return false; }
Example 20
Source File: HandlerResultHandlerSupport.java From spring-analysis-note with MIT License | 4 votes |
/** * Select the best media type for the current request through a content negotiation algorithm. * @param exchange the current request * @param producibleTypesSupplier the media types that can be produced for the current request * @return the selected media type, or {@code null} if none */ @Nullable protected MediaType selectMediaType( ServerWebExchange exchange, Supplier<List<MediaType>> producibleTypesSupplier) { MediaType contentType = exchange.getResponse().getHeaders().getContentType(); if (contentType != null && contentType.isConcrete()) { if (logger.isDebugEnabled()) { logger.debug(exchange.getLogPrefix() + "Found 'Content-Type:" + contentType + "' in response"); } return contentType; } List<MediaType> acceptableTypes = getAcceptableTypes(exchange); List<MediaType> producibleTypes = getProducibleTypes(exchange, producibleTypesSupplier); Set<MediaType> compatibleMediaTypes = new LinkedHashSet<>(); for (MediaType acceptable : acceptableTypes) { for (MediaType producible : producibleTypes) { if (acceptable.isCompatibleWith(producible)) { compatibleMediaTypes.add(selectMoreSpecificMediaType(acceptable, producible)); } } } List<MediaType> result = new ArrayList<>(compatibleMediaTypes); MediaType.sortBySpecificityAndQuality(result); MediaType selected = null; for (MediaType mediaType : result) { if (mediaType.isConcrete()) { selected = mediaType; break; } else if (mediaType.isPresentIn(ALL_APPLICATION_MEDIA_TYPES)) { selected = MediaType.APPLICATION_OCTET_STREAM; break; } } if (selected != null) { if (logger.isDebugEnabled()) { logger.debug("Using '" + selected + "' given " + acceptableTypes + " and supported " + producibleTypes); } } else if (logger.isDebugEnabled()) { logger.debug(exchange.getLogPrefix() + "No match for " + acceptableTypes + ", supported: " + producibleTypes); } return selected; }