org.springframework.core.codec.CharSequenceEncoder Java Examples

The following examples show how to use org.springframework.core.codec.CharSequenceEncoder. 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: BaseDefaultCodecs.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Return writers that support specific types.
 * @param forMultipart whether to returns writers for general use ("false"),
 * or for multipart requests only ("true"). Generally the two sets are the
 * same except for the multipart writer itself.
 */
@SuppressWarnings("unchecked")
final List<HttpMessageWriter<?>> getTypedWriters(boolean forMultipart) {
	if (!this.registerDefaults) {
		return Collections.emptyList();
	}
	List<HttpMessageWriter<?>> writers = new ArrayList<>();
	writers.add(new EncoderHttpMessageWriter<>(new ByteArrayEncoder()));
	writers.add(new EncoderHttpMessageWriter<>(new ByteBufferEncoder()));
	writers.add(new EncoderHttpMessageWriter<>(new DataBufferEncoder()));
	writers.add(new ResourceHttpMessageWriter());
	writers.add(new EncoderHttpMessageWriter<>(CharSequenceEncoder.textPlainOnly()));
	// No client or server specific multipart writers currently..
	if (!forMultipart) {
		extendTypedWriters(writers);
	}
	if (protobufPresent) {
		Encoder<?> encoder = this.protobufEncoder != null ? this.protobufEncoder : new ProtobufEncoder();
		writers.add(new ProtobufHttpMessageWriter((Encoder) encoder));
	}
	return writers;
}
 
Example #2
Source File: DefaultClientRequestBuilderTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void bodyClass() {
	String body = "foo";
	Publisher<String> publisher = Mono.just(body);
	ClientRequest result = ClientRequest.create(POST, URI.create("http://example.com"))
			.body(publisher, String.class).build();

	List<HttpMessageWriter<?>> messageWriters = new ArrayList<>();
	messageWriters.add(new EncoderHttpMessageWriter<>(CharSequenceEncoder.allMimeTypes()));

	ExchangeStrategies strategies = mock(ExchangeStrategies.class);
	when(strategies.messageWriters()).thenReturn(messageWriters);

	MockClientHttpRequest request = new MockClientHttpRequest(GET, "/");
	result.writeTo(request, strategies).block();
	assertNotNull(request.getBody());

	StepVerifier.create(request.getBody())
			.expectNextCount(1)
			.verifyComplete();
}
 
Example #3
Source File: MessageWriterResultHandlerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
private AbstractMessageWriterResultHandler initResultHandler(HttpMessageWriter<?>... writers) {
	List<HttpMessageWriter<?>> writerList;
	if (ObjectUtils.isEmpty(writers)) {
		writerList = new ArrayList<>();
		writerList.add(new EncoderHttpMessageWriter<>(new ByteBufferEncoder()));
		writerList.add(new EncoderHttpMessageWriter<>(CharSequenceEncoder.allMimeTypes()));
		writerList.add(new ResourceHttpMessageWriter());
		writerList.add(new EncoderHttpMessageWriter<>(new Jaxb2XmlEncoder()));
		writerList.add(new EncoderHttpMessageWriter<>(new Jackson2JsonEncoder()));
	}
	else {
		writerList = Arrays.asList(writers);
	}
	RequestedContentTypeResolver resolver = new RequestedContentTypeResolverBuilder().build();
	return new AbstractMessageWriterResultHandler(writerList, resolver) {};
}
 
Example #4
Source File: MessageMappingMessageHandlerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
private MessageMappingMessageHandler initMesssageHandler() {

		List<Decoder<?>> decoders = Collections.singletonList(StringDecoder.allMimeTypes());
		List<Encoder<?>> encoders = Collections.singletonList(CharSequenceEncoder.allMimeTypes());

		ReactiveAdapterRegistry registry = ReactiveAdapterRegistry.getSharedInstance();
		this.returnValueHandler = new TestEncoderMethodReturnValueHandler(encoders, registry);

		PropertySource<?> source = new MapPropertySource("test", Collections.singletonMap("path", "path123"));

		StaticApplicationContext context = new StaticApplicationContext();
		context.getEnvironment().getPropertySources().addFirst(source);
		context.registerSingleton("testController", TestController.class);
		context.refresh();

		MessageMappingMessageHandler messageHandler = new MessageMappingMessageHandler();
		messageHandler.getReturnValueHandlerConfigurer().addCustomHandler(this.returnValueHandler);
		messageHandler.setApplicationContext(context);
		messageHandler.setEmbeddedValueResolver(new EmbeddedValueResolver(context.getBeanFactory()));
		messageHandler.setDecoders(decoders);
		messageHandler.afterPropertiesSet();

		return messageHandler;
	}
 
Example #5
Source File: DefaultClientRequestBuilderTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void bodyClass() {
	String body = "foo";
	Publisher<String> publisher = Mono.just(body);
	ClientRequest result = ClientRequest.create(POST, URI.create("https://example.com"))
			.body(publisher, String.class).build();

	List<HttpMessageWriter<?>> messageWriters = new ArrayList<>();
	messageWriters.add(new EncoderHttpMessageWriter<>(CharSequenceEncoder.allMimeTypes()));

	ExchangeStrategies strategies = mock(ExchangeStrategies.class);
	given(strategies.messageWriters()).willReturn(messageWriters);

	MockClientHttpRequest request = new MockClientHttpRequest(GET, "/");
	result.writeTo(request, strategies).block();
	assertNotNull(request.getBody());

	StepVerifier.create(request.getBody())
			.expectNextCount(1)
			.verifyComplete();
}
 
Example #6
Source File: DefaultClientRequestBuilderTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void bodyParameterizedTypeReference() {
	String body = "foo";
	Publisher<String> publisher = Mono.just(body);
	ParameterizedTypeReference<String> typeReference = new ParameterizedTypeReference<String>() {};
	ClientRequest result = ClientRequest.create(POST, URI.create("https://example.com"))
			.body(publisher, typeReference).build();

	List<HttpMessageWriter<?>> messageWriters = new ArrayList<>();
	messageWriters.add(new EncoderHttpMessageWriter<>(CharSequenceEncoder.allMimeTypes()));

	ExchangeStrategies strategies = mock(ExchangeStrategies.class);
	given(strategies.messageWriters()).willReturn(messageWriters);

	MockClientHttpRequest request = new MockClientHttpRequest(GET, "/");
	result.writeTo(request, strategies).block();
	assertNotNull(request.getBody());

	StepVerifier.create(request.getBody())
			.expectNextCount(1)
			.verifyComplete();
}
 
Example #7
Source File: DefaultClientRequestBuilderTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void bodyParameterizedTypeReference() {
	String body = "foo";
	Publisher<String> publisher = Mono.just(body);
	ParameterizedTypeReference<String> typeReference = new ParameterizedTypeReference<String>() {};
	ClientRequest result = ClientRequest.create(POST, URI.create("http://example.com"))
			.body(publisher, typeReference).build();

	List<HttpMessageWriter<?>> messageWriters = new ArrayList<>();
	messageWriters.add(new EncoderHttpMessageWriter<>(CharSequenceEncoder.allMimeTypes()));

	ExchangeStrategies strategies = mock(ExchangeStrategies.class);
	when(strategies.messageWriters()).thenReturn(messageWriters);

	MockClientHttpRequest request = new MockClientHttpRequest(GET, "/");
	result.writeTo(request, strategies).block();
	assertNotNull(request.getBody());

	StepVerifier.create(request.getBody())
			.expectNextCount(1)
			.verifyComplete();
}
 
Example #8
Source File: ResponseEntityResultHandlerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
private ResponseEntityResultHandler createHandler(HttpMessageWriter<?>... writers) {
	List<HttpMessageWriter<?>> writerList;
	if (ObjectUtils.isEmpty(writers)) {
		writerList = new ArrayList<>();
		writerList.add(new EncoderHttpMessageWriter<>(new ByteBufferEncoder()));
		writerList.add(new EncoderHttpMessageWriter<>(CharSequenceEncoder.textPlainOnly()));
		writerList.add(new ResourceHttpMessageWriter());
		writerList.add(new EncoderHttpMessageWriter<>(new Jaxb2XmlEncoder()));
		writerList.add(new EncoderHttpMessageWriter<>(new Jackson2JsonEncoder()));
		writerList.add(new EncoderHttpMessageWriter<>(CharSequenceEncoder.allMimeTypes()));
	}
	else {
		writerList = Arrays.asList(writers);
	}
	RequestedContentTypeResolver resolver = new RequestedContentTypeResolverBuilder().build();
	return new ResponseEntityResultHandler(writerList, resolver);
}
 
Example #9
Source File: HttpMessageWriterViewTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void multipleMatchesNotSupported() throws Exception {
	this.view = new HttpMessageWriterView(CharSequenceEncoder.allMimeTypes());
	this.view.setModelKeys(new HashSet<>(Arrays.asList("foo1", "foo2")));
	this.model.addAttribute("foo1", "bar1");
	this.model.addAttribute("foo2", "bar2");

	try {
		doRender();
		fail();
	}
	catch (IllegalStateException ex) {
		String message = ex.getMessage();
		assertTrue(message, message.contains("Map rendering is not supported"));
	}
}
 
Example #10
Source File: HttpMessageWriterViewTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void multipleMatchesNotSupported() throws Exception {
	this.view = new HttpMessageWriterView(CharSequenceEncoder.allMimeTypes());
	this.view.setModelKeys(new HashSet<>(Arrays.asList("foo1", "foo2")));
	this.model.addAttribute("foo1", "bar1");
	this.model.addAttribute("foo2", "bar2");

	try {
		doRender();
		fail();
	}
	catch (IllegalStateException ex) {
		String message = ex.getMessage();
		assertTrue(message, message.contains("Map rendering is not supported"));
	}
}
 
Example #11
Source File: ResponseEntityResultHandlerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
private ResponseEntityResultHandler createHandler(HttpMessageWriter<?>... writers) {
	List<HttpMessageWriter<?>> writerList;
	if (ObjectUtils.isEmpty(writers)) {
		writerList = new ArrayList<>();
		writerList.add(new EncoderHttpMessageWriter<>(new ByteBufferEncoder()));
		writerList.add(new EncoderHttpMessageWriter<>(CharSequenceEncoder.textPlainOnly()));
		writerList.add(new ResourceHttpMessageWriter());
		writerList.add(new EncoderHttpMessageWriter<>(new Jaxb2XmlEncoder()));
		writerList.add(new EncoderHttpMessageWriter<>(new Jackson2JsonEncoder()));
		writerList.add(new EncoderHttpMessageWriter<>(CharSequenceEncoder.allMimeTypes()));
	}
	else {
		writerList = Arrays.asList(writers);
	}
	RequestedContentTypeResolver resolver = new RequestedContentTypeResolverBuilder().build();
	return new ResponseEntityResultHandler(writerList, resolver);
}
 
Example #12
Source File: MessageWriterResultHandlerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
private AbstractMessageWriterResultHandler initResultHandler(HttpMessageWriter<?>... writers) {
	List<HttpMessageWriter<?>> writerList;
	if (ObjectUtils.isEmpty(writers)) {
		writerList = new ArrayList<>();
		writerList.add(new EncoderHttpMessageWriter<>(new ByteBufferEncoder()));
		writerList.add(new EncoderHttpMessageWriter<>(CharSequenceEncoder.allMimeTypes()));
		writerList.add(new ResourceHttpMessageWriter());
		writerList.add(new EncoderHttpMessageWriter<>(new Jaxb2XmlEncoder()));
		writerList.add(new EncoderHttpMessageWriter<>(new Jackson2JsonEncoder()));
	}
	else {
		writerList = Arrays.asList(writers);
	}
	RequestedContentTypeResolver resolver = new RequestedContentTypeResolverBuilder().build();
	return new AbstractMessageWriterResultHandler(writerList, resolver) {};
}
 
Example #13
Source File: BaseDefaultCodecs.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Return writers that support specific types.
 * @param forMultipart whether to returns writers for general use ("false"),
 * or for multipart requests only ("true"). Generally the two sets are the
 * same except for the multipart writer itself.
 */
@SuppressWarnings("unchecked")
final List<HttpMessageWriter<?>> getTypedWriters(boolean forMultipart) {
	if (!this.registerDefaults) {
		return Collections.emptyList();
	}
	List<HttpMessageWriter<?>> writers = new ArrayList<>();
	writers.add(new EncoderHttpMessageWriter<>(new ByteArrayEncoder()));
	writers.add(new EncoderHttpMessageWriter<>(new ByteBufferEncoder()));
	writers.add(new EncoderHttpMessageWriter<>(new DataBufferEncoder()));
	writers.add(new ResourceHttpMessageWriter());
	writers.add(new EncoderHttpMessageWriter<>(CharSequenceEncoder.textPlainOnly()));
	// No client or server specific multipart writers currently..
	if (!forMultipart) {
		extendTypedWriters(writers);
	}
	if (protobufPresent) {
		Encoder<?> encoder = this.protobufEncoder != null ? this.protobufEncoder : new ProtobufEncoder();
		writers.add(new ProtobufHttpMessageWriter((Encoder) encoder));
	}
	return writers;
}
 
Example #14
Source File: CodecConfigurerTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void defaultAndCustomWriters() {
	Encoder<?> customEncoder1 = mock(Encoder.class);
	Encoder<?> customEncoder2 = mock(Encoder.class);

	given(customEncoder1.canEncode(ResolvableType.forClass(Object.class), null)).willReturn(false);
	given(customEncoder2.canEncode(ResolvableType.forClass(Object.class), null)).willReturn(true);

	HttpMessageWriter<?> customWriter1 = mock(HttpMessageWriter.class);
	HttpMessageWriter<?> customWriter2 = mock(HttpMessageWriter.class);

	given(customWriter1.canWrite(ResolvableType.forClass(Object.class), null)).willReturn(false);
	given(customWriter2.canWrite(ResolvableType.forClass(Object.class), null)).willReturn(true);

	this.configurer.customCodecs().encoder(customEncoder1);
	this.configurer.customCodecs().encoder(customEncoder2);

	this.configurer.customCodecs().writer(customWriter1);
	this.configurer.customCodecs().writer(customWriter2);

	List<HttpMessageWriter<?>> writers = this.configurer.getWriters();

	assertEquals(14, writers.size());
	assertSame(customEncoder1, getNextEncoder(writers));
	assertSame(customWriter1, writers.get(this.index.getAndIncrement()));
	assertEquals(ByteArrayEncoder.class, getNextEncoder(writers).getClass());
	assertEquals(ByteBufferEncoder.class, getNextEncoder(writers).getClass());
	assertEquals(DataBufferEncoder.class, getNextEncoder(writers).getClass());
	assertEquals(ResourceHttpMessageWriter.class, writers.get(index.getAndIncrement()).getClass());
	assertEquals(CharSequenceEncoder.class, getNextEncoder(writers).getClass());
	assertEquals(ProtobufHttpMessageWriter.class, writers.get(index.getAndIncrement()).getClass());
	assertSame(customEncoder2, getNextEncoder(writers));
	assertSame(customWriter2, writers.get(this.index.getAndIncrement()));
	assertEquals(Jackson2JsonEncoder.class, getNextEncoder(writers).getClass());
	assertEquals(Jackson2SmileEncoder.class, getNextEncoder(writers).getClass());
	assertEquals(Jaxb2XmlEncoder.class, getNextEncoder(writers).getClass());
	assertEquals(CharSequenceEncoder.class, getNextEncoder(writers).getClass());
}
 
Example #15
Source File: CancelWithoutDemandCodecTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test // gh-22107
public void cancelWithEncoderHttpMessageWriterAndSingleValue() {
	CharSequenceEncoder encoder = CharSequenceEncoder.allMimeTypes();
	HttpMessageWriter<CharSequence> writer = new EncoderHttpMessageWriter<>(encoder);
	CancellingOutputMessage outputMessage = new CancellingOutputMessage(this.bufferFactory);

	writer.write(Mono.just("foo"), ResolvableType.forType(String.class), MediaType.TEXT_PLAIN,
			outputMessage, Collections.emptyMap()).block(Duration.ofSeconds(5));
}
 
Example #16
Source File: DefaultEntityResponseBuilderTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void bodyInserter() {
	String body = "foo";
	Publisher<String> publisher = Mono.just(body);

	Mono<EntityResponse<Publisher<String>>> result = EntityResponse.fromPublisher(publisher, String.class).build();

	MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("http://localhost"));

	ServerResponse.Context context = new ServerResponse.Context() {
		@Override
		public List<HttpMessageWriter<?>> messageWriters() {
			return Collections.singletonList(new EncoderHttpMessageWriter<>(CharSequenceEncoder.allMimeTypes()));
		}

		@Override
		public List<ViewResolver> viewResolvers() {
			return Collections.emptyList();
		}
	};
	StepVerifier.create(result)
			.consumeNextWith(response -> {
				StepVerifier.create(response.entity())
						.expectNext(body)
						.expectComplete()
						.verify();
				response.writeTo(exchange, context);
			})
			.expectComplete()
			.verify();

	assertNotNull(exchange.getResponse().getBody());
}
 
Example #17
Source File: BodyInsertersTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Before
public void createContext() {
	final List<HttpMessageWriter<?>> messageWriters = new ArrayList<>();
	messageWriters.add(new EncoderHttpMessageWriter<>(new ByteBufferEncoder()));
	messageWriters.add(new EncoderHttpMessageWriter<>(CharSequenceEncoder.textPlainOnly()));
	messageWriters.add(new ResourceHttpMessageWriter());
	messageWriters.add(new EncoderHttpMessageWriter<>(new Jaxb2XmlEncoder()));
	Jackson2JsonEncoder jsonEncoder = new Jackson2JsonEncoder();
	messageWriters.add(new EncoderHttpMessageWriter<>(jsonEncoder));
	messageWriters.add(new ServerSentEventHttpMessageWriter(jsonEncoder));
	messageWriters.add(new FormHttpMessageWriter());
	messageWriters.add(new EncoderHttpMessageWriter<>(CharSequenceEncoder.allMimeTypes()));
	messageWriters.add(new MultipartHttpMessageWriter(messageWriters));

	this.context = new BodyInserter.Context() {
		@Override
		public List<HttpMessageWriter<?>> messageWriters() {
			return messageWriters;
		}
		@Override
		public Optional<ServerHttpRequest> serverRequest() {
			return Optional.empty();
		}
		@Override
		public Map<String, Object> hints() {
			return hints;
		}
	};
	this.hints = new HashMap<>();
}
 
Example #18
Source File: WebFluxConfigurationSupportTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
protected void configureHttpMessageCodecs(ServerCodecConfigurer configurer) {
	configurer.registerDefaults(false);
	configurer.customCodecs().decoder(StringDecoder.textPlainOnly());
	configurer.customCodecs().decoder(new Jaxb2XmlDecoder());
	configurer.customCodecs().encoder(CharSequenceEncoder.textPlainOnly());
	configurer.customCodecs().encoder(new Jaxb2XmlEncoder());
}
 
Example #19
Source File: ResponseBodyResultHandlerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Before
public void setup() throws Exception {
	List<HttpMessageWriter<?>> writerList = new ArrayList<>(5);
	writerList.add(new EncoderHttpMessageWriter<>(new ByteBufferEncoder()));
	writerList.add(new EncoderHttpMessageWriter<>(CharSequenceEncoder.allMimeTypes()));
	writerList.add(new ResourceHttpMessageWriter());
	writerList.add(new EncoderHttpMessageWriter<>(new Jaxb2XmlEncoder()));
	writerList.add(new EncoderHttpMessageWriter<>(new Jackson2JsonEncoder()));
	RequestedContentTypeResolver resolver = new RequestedContentTypeResolverBuilder().build();
	this.resultHandler = new ResponseBodyResultHandler(writerList, resolver);
}
 
Example #20
Source File: MultipartHttpMessageWriter.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Constructor with a default list of part writers (String and Resource).
 */
public MultipartHttpMessageWriter() {
	this(Arrays.asList(
			new EncoderHttpMessageWriter<>(CharSequenceEncoder.textPlainOnly()),
			new ResourceHttpMessageWriter()
	));
}
 
Example #21
Source File: BaseDefaultCodecs.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Return writers that need to be at the end, after all others.
 */
List<HttpMessageWriter<?>> getCatchAllWriters() {
	if (!this.registerDefaults) {
		return Collections.emptyList();
	}
	List<HttpMessageWriter<?>> result = new ArrayList<>();
	result.add(new EncoderHttpMessageWriter<>(CharSequenceEncoder.allMimeTypes()));
	return result;
}
 
Example #22
Source File: CodecConfigurerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void defaultAndCustomWriters() {
	Encoder<?> customEncoder1 = mock(Encoder.class);
	Encoder<?> customEncoder2 = mock(Encoder.class);

	when(customEncoder1.canEncode(ResolvableType.forClass(Object.class), null)).thenReturn(false);
	when(customEncoder2.canEncode(ResolvableType.forClass(Object.class), null)).thenReturn(true);

	HttpMessageWriter<?> customWriter1 = mock(HttpMessageWriter.class);
	HttpMessageWriter<?> customWriter2 = mock(HttpMessageWriter.class);

	when(customWriter1.canWrite(ResolvableType.forClass(Object.class), null)).thenReturn(false);
	when(customWriter2.canWrite(ResolvableType.forClass(Object.class), null)).thenReturn(true);

	this.configurer.customCodecs().encoder(customEncoder1);
	this.configurer.customCodecs().encoder(customEncoder2);

	this.configurer.customCodecs().writer(customWriter1);
	this.configurer.customCodecs().writer(customWriter2);

	List<HttpMessageWriter<?>> writers = this.configurer.getWriters();

	assertEquals(14, writers.size());
	assertEquals(ByteArrayEncoder.class, getNextEncoder(writers).getClass());
	assertEquals(ByteBufferEncoder.class, getNextEncoder(writers).getClass());
	assertEquals(DataBufferEncoder.class, getNextEncoder(writers).getClass());
	assertEquals(ResourceHttpMessageWriter.class, writers.get(index.getAndIncrement()).getClass());
	assertEquals(CharSequenceEncoder.class, getNextEncoder(writers).getClass());
	assertEquals(ProtobufHttpMessageWriter.class, writers.get(index.getAndIncrement()).getClass());
	assertSame(customEncoder1, getNextEncoder(writers));
	assertSame(customWriter1, writers.get(this.index.getAndIncrement()));
	assertEquals(Jackson2JsonEncoder.class, getNextEncoder(writers).getClass());
	assertEquals(Jackson2SmileEncoder.class, getNextEncoder(writers).getClass());
	assertEquals(Jaxb2XmlEncoder.class, getNextEncoder(writers).getClass());
	assertSame(customEncoder2, getNextEncoder(writers));
	assertSame(customWriter2, writers.get(this.index.getAndIncrement()));
	assertEquals(CharSequenceEncoder.class, getNextEncoder(writers).getClass());
}
 
Example #23
Source File: BaseDefaultCodecs.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Return writers that need to be at the end, after all others.
 */
List<HttpMessageWriter<?>> getCatchAllWriters() {
	if (!this.registerDefaults) {
		return Collections.emptyList();
	}
	List<HttpMessageWriter<?>> result = new ArrayList<>();
	result.add(new EncoderHttpMessageWriter<>(CharSequenceEncoder.allMimeTypes()));
	return result;
}
 
Example #24
Source File: MultipartHttpMessageWriter.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Constructor with a default list of part writers (String and Resource).
 */
public MultipartHttpMessageWriter() {
	this(Arrays.asList(
			new EncoderHttpMessageWriter<>(CharSequenceEncoder.textPlainOnly()),
			new ResourceHttpMessageWriter()
	));
}
 
Example #25
Source File: ResponseBodyResultHandlerTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Before
public void setup() throws Exception {
	List<HttpMessageWriter<?>> writerList = new ArrayList<>(5);
	writerList.add(new EncoderHttpMessageWriter<>(new ByteBufferEncoder()));
	writerList.add(new EncoderHttpMessageWriter<>(CharSequenceEncoder.allMimeTypes()));
	writerList.add(new ResourceHttpMessageWriter());
	writerList.add(new EncoderHttpMessageWriter<>(new Jaxb2XmlEncoder()));
	writerList.add(new EncoderHttpMessageWriter<>(new Jackson2JsonEncoder()));
	RequestedContentTypeResolver resolver = new RequestedContentTypeResolverBuilder().build();
	this.resultHandler = new ResponseBodyResultHandler(writerList, resolver);
}
 
Example #26
Source File: WebFluxConfigurationSupportTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
protected void configureHttpMessageCodecs(ServerCodecConfigurer configurer) {
	configurer.registerDefaults(false);
	configurer.customCodecs().decoder(StringDecoder.textPlainOnly());
	configurer.customCodecs().decoder(new Jaxb2XmlDecoder());
	configurer.customCodecs().encoder(CharSequenceEncoder.textPlainOnly());
	configurer.customCodecs().encoder(new Jaxb2XmlEncoder());
}
 
Example #27
Source File: BodyInsertersTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Before
public void createContext() {
	final List<HttpMessageWriter<?>> messageWriters = new ArrayList<>();
	messageWriters.add(new EncoderHttpMessageWriter<>(new ByteBufferEncoder()));
	messageWriters.add(new EncoderHttpMessageWriter<>(CharSequenceEncoder.textPlainOnly()));
	messageWriters.add(new ResourceHttpMessageWriter());
	messageWriters.add(new EncoderHttpMessageWriter<>(new Jaxb2XmlEncoder()));
	Jackson2JsonEncoder jsonEncoder = new Jackson2JsonEncoder();
	messageWriters.add(new EncoderHttpMessageWriter<>(jsonEncoder));
	messageWriters.add(new ServerSentEventHttpMessageWriter(jsonEncoder));
	messageWriters.add(new FormHttpMessageWriter());
	messageWriters.add(new EncoderHttpMessageWriter<>(CharSequenceEncoder.allMimeTypes()));
	messageWriters.add(new MultipartHttpMessageWriter(messageWriters));

	this.context = new BodyInserter.Context() {
		@Override
		public List<HttpMessageWriter<?>> messageWriters() {
			return messageWriters;
		}
		@Override
		public Optional<ServerHttpRequest> serverRequest() {
			return Optional.empty();
		}
		@Override
		public Map<String, Object> hints() {
			return hints;
		}
	};
	this.hints = new HashMap<>();
}
 
Example #28
Source File: DefaultEntityResponseBuilderTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void bodyInserter() {
	String body = "foo";
	Publisher<String> publisher = Mono.just(body);

	Mono<EntityResponse<Publisher<String>>> result = EntityResponse.fromPublisher(publisher, String.class).build();

	MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("http://localhost"));

	ServerResponse.Context context = new ServerResponse.Context() {
		@Override
		public List<HttpMessageWriter<?>> messageWriters() {
			return Collections.singletonList(new EncoderHttpMessageWriter<>(CharSequenceEncoder.allMimeTypes()));
		}

		@Override
		public List<ViewResolver> viewResolvers() {
			return Collections.emptyList();
		}
	};
	StepVerifier.create(result)
			.consumeNextWith(response -> {
				StepVerifier.create(response.entity())
						.expectNext(body)
						.expectComplete()
						.verify();
				response.writeTo(exchange, context);
			})
			.expectComplete()
			.verify();

	assertNotNull(exchange.getResponse().getBody());
}
 
Example #29
Source File: RSocketClientToServerIntegrationTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Bean
public RSocketStrategies rsocketStrategies() {
	return RSocketStrategies.builder()
			.decoder(StringDecoder.allMimeTypes())
			.encoder(CharSequenceEncoder.allMimeTypes())
			.dataBufferFactory(new NettyDataBufferFactory(PooledByteBufAllocator.DEFAULT))
			.build();
}
 
Example #30
Source File: DefaultRSocketRequesterTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Before
public void setUp() {
	RSocketStrategies strategies = RSocketStrategies.builder()
			.decoder(StringDecoder.allMimeTypes())
			.encoder(CharSequenceEncoder.allMimeTypes())
			.build();
	this.rsocket = new TestRSocket();
	this.requester = RSocketRequester.wrap(this.rsocket, MimeTypeUtils.TEXT_PLAIN, strategies);
}