private boolean canReadResponse(Type responseType, HttpMessageConverter<?> converter) { Class<?> responseClass = (responseType instanceof Class ? (Class<?>) responseType : null); if (responseClass != null) { return converter.canRead(responseClass, null); } else if (converter instanceof GenericHttpMessageConverter) { GenericHttpMessageConverter<?> genericConverter = (GenericHttpMessageConverter<?>) converter; return genericConverter.canRead(responseType, null, null); } return false; }
/** * Indicates whether the {@code Accept-Charset} should be written to any outgoing request. * <p>Default is {@code true}. */ public void setWriteAcceptCharset(boolean writeAcceptCharset) { this.stringHttpMessageConverter.setWriteAcceptCharset(writeAcceptCharset); }
/** * The default character set to use for reading form data. * This is a shortcut for:<br> * {@code getFormConverter.setCharset(charset)}. */ public void setCharset(Charset charset) { this.formConverter.setCharset(charset); }
public FormHttpMessageConverter() { this.supportedMediaTypes.add(MediaType.APPLICATION_FORM_URLENCODED); this.supportedMediaTypes.add(MediaType.MULTIPART_FORM_DATA); StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter(); stringHttpMessageConverter.setWriteAcceptCharset(false); // see SPR-7316 this.partConverters.add(new ByteArrayHttpMessageConverter()); this.partConverters.add(stringHttpMessageConverter); this.partConverters.add(new ResourceHttpMessageConverter()); applyDefaultCharset(); }
@Override protected Object readInternal(Class<?> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { String value = this.stringHttpMessageConverter.readInternal(String.class, inputMessage); Object result = this.conversionService.convert(value, clazz); if (result == null) { throw new HttpMessageNotReadableException( "Unexpected null conversion result for '" + value + "' to " + clazz, inputMessage); } return result; }
@Override public Object read(Class<?> clazz, HttpInputMessage inputMessage) { throw new HttpMessageNotReadableException("Could not read", inputMessage); }
@Override public void handleReturnValue(Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception { throw new HttpMessageNotWritableException("oops, can't write"); } }
/** * This implementation checks if the given class is {@linkplain #supports(Class) supported}, * and if the {@linkplain #getSupportedMediaTypes() supported media types} * {@linkplain MediaType#includes(MediaType) include} the given media type. */ @Override public boolean canRead(Class<?> clazz, @Nullable MediaType mediaType) { return supports(clazz) && canRead(mediaType); }
@Override @SuppressWarnings("unchecked") public void write(MultiValueMap<String, ?> map, @Nullable MediaType contentType, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException { if (!isMultipart(map, contentType)) { writeForm((MultiValueMap<String, Object>) map, contentType, outputMessage); } else { writeMultipart((MultiValueMap<String, Object>) map, outputMessage); } }
/** * Apply the configured charset as a default to registered part converters. */ private void applyDefaultCharset() { for (HttpMessageConverter<?> candidate : this.partConverters) { if (candidate instanceof AbstractHttpMessageConverter) { AbstractHttpMessageConverter<?> converter = (AbstractHttpMessageConverter<?>) candidate; // Only override default charset if the converter operates with a charset to begin with... if (converter.getDefaultCharset() != null) { converter.setDefaultCharset(this.charset); } } } }
/** * Construct an {@code AbstractHttpMessageConverter} with one supported media type. * @param supportedMediaType the supported media type */ protected AbstractHttpMessageConverter(MediaType supportedMediaType) { setSupportedMediaTypes(Collections.singletonList(supportedMediaType)); }
@Test public void canWriteResourceCollection() { Type resourceRegionList = new ParameterizedTypeReference<List<ResourceRegion>>() {}.getType(); assertTrue(converter.canWrite(resourceRegionList, null, MediaType.APPLICATION_OCTET_STREAM)); assertTrue(converter.canWrite(resourceRegionList, null, MediaType.ALL)); assertFalse(converter.canWrite(List.class, MediaType.APPLICATION_OCTET_STREAM)); assertFalse(converter.canWrite(List.class, MediaType.ALL)); Type resourceObjectList = new ParameterizedTypeReference<List<Object>>() {}.getType(); assertFalse(converter.canWrite(resourceObjectList, null, MediaType.ALL)); }
private void writeBoundary(OutputStream os, byte[] boundary) throws IOException { os.write('-'); os.write('-'); os.write(boundary); writeNewLine(os); }
/** * This implementation simple delegates to {@link #readInternal(Class, HttpInputMessage)}. * Future implementations might add some default behavior, however. */ @Override public final T read(Class<? extends T> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { return readInternal(clazz, inputMessage); }
@Override public OutputStream getBody() throws IOException { writeHeaders(); return this.outputStream; }
@Override protected void writeInternal(Resource resource, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException { writeContent(resource, outputMessage); }
@Override protected void writeInternal(T t, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException { writeInternal(t, null, outputMessage); }
/** * The default character set to use for reading form data. * This is a shortcut for:<br> * {@code getFormConverter.setCharset(charset)}. */ public void setCharset(Charset charset) { this.formConverter.setCharset(charset); }
/** * Construct an {@code AbstractHttpMessageConverter} with multiple supported media types. * @param supportedMediaTypes the supported media types */ protected AbstractHttpMessageConverter(MediaType... supportedMediaTypes) { setSupportedMediaTypes(Arrays.asList(supportedMediaTypes)); }
private static void writeEnd(OutputStream os, byte[] boundary) throws IOException { os.write('-'); os.write('-'); os.write(boundary); os.write('-'); os.write('-'); writeNewLine(os); }