|
| 1 | +/* |
| 2 | + * Copyright 2002-2016 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.http.codec; |
| 18 | + |
| 19 | +import java.io.UnsupportedEncodingException; |
| 20 | +import java.net.URLEncoder; |
| 21 | +import java.nio.ByteBuffer; |
| 22 | +import java.nio.charset.Charset; |
| 23 | +import java.nio.charset.StandardCharsets; |
| 24 | +import java.util.Collections; |
| 25 | +import java.util.Iterator; |
| 26 | +import java.util.List; |
| 27 | +import java.util.Map; |
| 28 | + |
| 29 | +import org.reactivestreams.Publisher; |
| 30 | +import reactor.core.publisher.Flux; |
| 31 | +import reactor.core.publisher.Mono; |
| 32 | + |
| 33 | +import org.springframework.core.ResolvableType; |
| 34 | +import org.springframework.core.io.buffer.DataBuffer; |
| 35 | +import org.springframework.http.MediaType; |
| 36 | +import org.springframework.http.ReactiveHttpOutputMessage; |
| 37 | +import org.springframework.util.Assert; |
| 38 | +import org.springframework.util.MultiValueMap; |
| 39 | + |
| 40 | +/** |
| 41 | + * Implementation of {@link HttpMessageWriter} to write 'normal' HTML |
| 42 | + * forms with {@code "application/x-www-form-urlencoded"} media type. |
| 43 | + * |
| 44 | + * @author Sebastien Deleuze |
| 45 | + * @since 5.0 |
| 46 | + * @see MultiValueMap |
| 47 | + */ |
| 48 | +public class FormHttpMessageWriter implements HttpMessageWriter<MultiValueMap<String, String>> { |
| 49 | + |
| 50 | + public static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8; |
| 51 | + |
| 52 | + private static final ResolvableType formType = ResolvableType.forClassWithGenerics(MultiValueMap.class, String.class, String.class); |
| 53 | + |
| 54 | + private Charset charset = DEFAULT_CHARSET; |
| 55 | + |
| 56 | + |
| 57 | + @Override |
| 58 | + public boolean canWrite(ResolvableType elementType, MediaType mediaType) { |
| 59 | + return (mediaType == null || MediaType.APPLICATION_FORM_URLENCODED.isCompatibleWith(mediaType)) && |
| 60 | + formType.isAssignableFrom(elementType); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public Mono<Void> write(Publisher<? extends MultiValueMap<String, String>> inputStream, |
| 65 | + ResolvableType elementType, MediaType mediaType, ReactiveHttpOutputMessage outputMessage, |
| 66 | + Map<String, Object> hints) { |
| 67 | + |
| 68 | + MediaType contentType = outputMessage.getHeaders().getContentType(); |
| 69 | + Charset charset; |
| 70 | + if (contentType != null) { |
| 71 | + outputMessage.getHeaders().setContentType(contentType); |
| 72 | + charset = (contentType != null && contentType.getCharset() != null ? contentType.getCharset() : this.charset); |
| 73 | + } |
| 74 | + else { |
| 75 | + outputMessage.getHeaders().setContentType(MediaType.APPLICATION_FORM_URLENCODED); |
| 76 | + charset = this.charset; |
| 77 | + } |
| 78 | + return Flux |
| 79 | + .from(inputStream) |
| 80 | + .single() |
| 81 | + .map(form -> generateForm(form)) |
| 82 | + .then(value -> { |
| 83 | + ByteBuffer byteBuffer = charset.encode(value); |
| 84 | + DataBuffer buffer = outputMessage.bufferFactory().wrap(byteBuffer); |
| 85 | + outputMessage.getHeaders().setContentLength(byteBuffer.remaining()); |
| 86 | + return outputMessage.writeWith(Mono.just(buffer)); |
| 87 | + }); |
| 88 | + |
| 89 | + } |
| 90 | + |
| 91 | + private String generateForm(MultiValueMap<String, String> form) { |
| 92 | + StringBuilder builder = new StringBuilder(); |
| 93 | + try { |
| 94 | + for (Iterator<String> nameIterator = form.keySet().iterator(); nameIterator.hasNext();) { |
| 95 | + String name = nameIterator.next(); |
| 96 | + for (Iterator<String> valueIterator = form.get(name).iterator(); valueIterator.hasNext();) { |
| 97 | + String value = valueIterator.next(); |
| 98 | + builder.append(URLEncoder.encode(name, charset.name())); |
| 99 | + if (value != null) { |
| 100 | + builder.append('='); |
| 101 | + builder.append(URLEncoder.encode(value, charset.name())); |
| 102 | + if (valueIterator.hasNext()) { |
| 103 | + builder.append('&'); |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + if (nameIterator.hasNext()) { |
| 108 | + builder.append('&'); |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + catch (UnsupportedEncodingException ex) { |
| 113 | + throw new IllegalStateException(ex); |
| 114 | + } |
| 115 | + return builder.toString(); |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public List<MediaType> getWritableMediaTypes() { |
| 120 | + return Collections.singletonList(MediaType.APPLICATION_FORM_URLENCODED); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Set the default character set to use for writing form data when the response |
| 125 | + * Content-Type header does not explicitly specify it. |
| 126 | + * <p>By default this is set to "UTF-8". |
| 127 | + */ |
| 128 | + public void setCharset(Charset charset) { |
| 129 | + Assert.notNull(charset, "'charset' must not be null"); |
| 130 | + this.charset = charset; |
| 131 | + } |
| 132 | + |
| 133 | +} |
0 commit comments