use of org.springframework.core.io.buffer.DataBuffer in project spring-security by spring-projects.
the class HttpStatusServerAccessDeniedHandler method handle.
@Override
public Mono<Void> handle(ServerWebExchange exchange, AccessDeniedException ex) {
return Mono.defer(() -> Mono.just(exchange.getResponse())).flatMap((response) -> {
response.setStatusCode(this.httpStatus);
response.getHeaders().setContentType(MediaType.TEXT_PLAIN);
DataBufferFactory dataBufferFactory = response.bufferFactory();
DataBuffer buffer = dataBufferFactory.wrap(ex.getMessage().getBytes(Charset.defaultCharset()));
return response.writeWith(Mono.just(buffer)).doOnError((error) -> DataBufferUtils.release(buffer));
});
}
use of org.springframework.core.io.buffer.DataBuffer in project spring-security by spring-projects.
the class AuthenticationPayloadInterceptorTests method createRequestPayload.
private Payload createRequestPayload() {
UsernamePasswordMetadata credentials = new UsernamePasswordMetadata("user", "password");
BasicAuthenticationEncoder encoder = new BasicAuthenticationEncoder();
DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
ResolvableType elementType = ResolvableType.forClass(UsernamePasswordMetadata.class);
MimeType mimeType = UsernamePasswordMetadata.BASIC_AUTHENTICATION_MIME_TYPE;
Map<String, Object> hints = null;
DataBuffer dataBuffer = encoder.encodeValue(credentials, factory, elementType, mimeType, hints);
ByteBufAllocator allocator = ByteBufAllocator.DEFAULT;
CompositeByteBuf metadata = allocator.compositeBuffer();
CompositeMetadataCodec.encodeAndAddMetadata(metadata, allocator, mimeType.toString(), NettyDataBufferFactory.toByteBuf(dataBuffer));
return DefaultPayload.create(allocator.buffer(), metadata);
}
use of org.springframework.core.io.buffer.DataBuffer in project spring-security by spring-projects.
the class BasicAuthenticationEncoder method encodeValue.
@Override
public DataBuffer encodeValue(UsernamePasswordMetadata credentials, DataBufferFactory bufferFactory, ResolvableType valueType, MimeType mimeType, Map<String, Object> hints) {
String username = credentials.getUsername();
String password = credentials.getPassword();
byte[] usernameBytes = username.getBytes(StandardCharsets.UTF_8);
byte[] usernameBytesLengthBytes = ByteBuffer.allocate(4).putInt(usernameBytes.length).array();
DataBuffer metadata = bufferFactory.allocateBuffer();
boolean release = true;
try {
metadata.write(usernameBytesLengthBytes);
metadata.write(usernameBytes);
metadata.write(password.getBytes(StandardCharsets.UTF_8));
release = false;
return metadata;
} finally {
if (release) {
DataBufferUtils.release(metadata);
}
}
}
use of org.springframework.core.io.buffer.DataBuffer in project spring-framework by spring-projects.
the class ChannelSendOperatorTests method errorFromWriteFunctionWhileItemCached.
// gh-22720
@Test
public void errorFromWriteFunctionWhileItemCached() {
// 1. First item received
// 2. writeFunction applied and writeCompletionBarrier subscribed to it
// 3. writeFunction fails, e.g. to flush status and headers, before request(n) from server
LeakAwareDataBufferFactory bufferFactory = new LeakAwareDataBufferFactory();
ChannelSendOperator<DataBuffer> operator = new ChannelSendOperator<>(Flux.create(sink -> {
DataBuffer dataBuffer = bufferFactory.allocateBuffer();
dataBuffer.write("foo", StandardCharsets.UTF_8);
sink.next(dataBuffer);
}), publisher -> {
publisher.subscribe(new ZeroDemandSubscriber());
return Mono.error(new IllegalStateException("err"));
});
StepVerifier.create(operator).expectErrorMessage("err").verify(Duration.ofSeconds(5));
bufferFactory.checkForLeaks();
}
use of org.springframework.core.io.buffer.DataBuffer in project spring-framework by spring-projects.
the class ChannelSendOperatorTests method cancelWhileItemCached.
// gh-22720
@Test
public void cancelWhileItemCached() {
LeakAwareDataBufferFactory bufferFactory = new LeakAwareDataBufferFactory();
ChannelSendOperator<DataBuffer> operator = new ChannelSendOperator<>(Mono.fromCallable(() -> {
DataBuffer dataBuffer = bufferFactory.allocateBuffer();
dataBuffer.write("foo", StandardCharsets.UTF_8);
return dataBuffer;
}), publisher -> {
ZeroDemandSubscriber subscriber = new ZeroDemandSubscriber();
publisher.subscribe(subscriber);
return Mono.never();
});
BaseSubscriber<Void> subscriber = new BaseSubscriber<>() {
};
operator.subscribe(subscriber);
subscriber.cancel();
bufferFactory.checkForLeaks();
}
Aggregations