use of org.springframework.http.HttpStatus in project spring-integration by spring-projects.
the class WebFluxRequestExecutingMessageHandler method exchange.
@Override
protected Object exchange(Supplier<URI> uriSupplier, HttpMethod httpMethod, HttpEntity<?> httpRequest, Object expectedResponseType, Message<?> requestMessage) {
WebClient.RequestBodySpec requestSpec = this.webClient.method(httpMethod).uri(b -> uriSupplier.get()).headers(headers -> headers.putAll(httpRequest.getHeaders()));
if (httpRequest.hasBody()) {
requestSpec.body(BodyInserters.fromObject(httpRequest.getBody()));
}
Mono<ClientResponse> responseMono = requestSpec.exchange().flatMap(response -> {
HttpStatus httpStatus = response.statusCode();
if (httpStatus.isError()) {
return response.body(BodyExtractors.toDataBuffers()).reduce(DataBuffer::write).map(dataBuffer -> {
byte[] bytes = new byte[dataBuffer.readableByteCount()];
dataBuffer.read(bytes);
DataBufferUtils.release(dataBuffer);
return bytes;
}).defaultIfEmpty(new byte[0]).map(bodyBytes -> {
throw new WebClientResponseException("ClientResponse has erroneous status code: " + httpStatus.value() + " " + httpStatus.getReasonPhrase(), httpStatus.value(), httpStatus.getReasonPhrase(), response.headers().asHttpHeaders(), bodyBytes, response.headers().contentType().map(MimeType::getCharset).orElse(StandardCharsets.ISO_8859_1));
});
} else {
return Mono.just(response);
}
});
if (isExpectReply()) {
return responseMono.flatMap(response -> {
ResponseEntity.BodyBuilder httpEntityBuilder = ResponseEntity.status(response.statusCode()).headers(response.headers().asHttpHeaders());
Mono<?> bodyMono;
if (expectedResponseType != null) {
if (this.replyPayloadToFlux) {
BodyExtractor<? extends Flux<?>, ReactiveHttpInputMessage> extractor;
if (expectedResponseType instanceof ParameterizedTypeReference<?>) {
extractor = BodyExtractors.toFlux((ParameterizedTypeReference<?>) expectedResponseType);
} else {
extractor = BodyExtractors.toFlux((Class<?>) expectedResponseType);
}
Flux<?> flux = response.body(extractor);
bodyMono = Mono.just(flux);
} else {
BodyExtractor<? extends Mono<?>, ReactiveHttpInputMessage> extractor;
if (expectedResponseType instanceof ParameterizedTypeReference<?>) {
extractor = BodyExtractors.toMono((ParameterizedTypeReference<?>) expectedResponseType);
} else {
extractor = BodyExtractors.toMono((Class<?>) expectedResponseType);
}
bodyMono = response.body(extractor);
}
} else if (this.bodyExtractor != null) {
Object body = response.body(this.bodyExtractor);
if (body instanceof Mono) {
bodyMono = (Mono<?>) body;
} else {
bodyMono = Mono.just(body);
}
} else {
bodyMono = Mono.empty();
}
return bodyMono.map(httpEntityBuilder::body).defaultIfEmpty(httpEntityBuilder.build());
}).map(this::getReply);
} else {
responseMono.subscribe(v -> {
}, ex -> sendErrorMessage(requestMessage, ex));
return null;
}
}
use of org.springframework.http.HttpStatus in project spring-cloud-function by spring-cloud.
the class MessageController method function.
@PostMapping("/**")
public ResponseEntity<Object> function(@RequestAttribute("org.springframework.web.servlet.HandlerMapping.pathWithinHandlerMapping") String path, @RequestBody Object body, @RequestHeader HttpHeaders headers) {
Route route = input(path);
String channel = route.getChannel();
if (!inputs.containsKey(channel)) {
return ResponseEntity.notFound().build();
}
Collection<Object> collection;
boolean single = false;
if (body instanceof String) {
body = extract((String) body);
}
if (body instanceof Collection) {
@SuppressWarnings("unchecked") Collection<Object> list = (Collection<Object>) body;
collection = list;
} else {
if (ObjectUtils.isArray(body)) {
collection = Arrays.asList(ObjectUtils.toObjectArray(body));
} else {
single = true;
collection = Arrays.asList(body);
}
}
Map<String, Object> messageHeaders = new HashMap<>(HeaderUtils.fromHttp(headers));
if (route.getKey() != null) {
messageHeaders.put(ROUTE_KEY, route.getKey());
}
MessageChannel input = inputs.get(channel);
Map<String, Object> outputHeaders = null;
List<Object> results = new ArrayList<>();
HttpStatus status = HttpStatus.ACCEPTED;
// one of them.
if (this.outputs.containsKey(channel)) {
for (Object payload : collection) {
Message<?> result = template.sendAndReceive(input, MessageBuilder.withPayload(payload).copyHeadersIfAbsent(messageHeaders).setHeader(MessageHeaders.REPLY_CHANNEL, outputs.get(channel)).build());
if (result != null) {
if (outputHeaders == null) {
outputHeaders = new LinkedHashMap<>(result.getHeaders());
}
results.add(result.getPayload());
}
}
status = HttpStatus.OK;
if (results.isEmpty()) {
// If nothing came back, just assume it was intentional, and say that
// we accepted the inputs.
status = HttpStatus.ACCEPTED;
results.addAll(collection);
}
} else {
for (Object payload : collection) {
template.send(input, MessageBuilder.withPayload(payload).copyHeadersIfAbsent(messageHeaders).build());
}
outputHeaders = messageHeaders;
results.addAll(collection);
}
if (outputHeaders == null) {
outputHeaders = new LinkedHashMap<>();
}
outputHeaders.put(ROUTE_KEY, route.getKey());
if (single && results.size() == 1) {
body = results.get(0);
} else {
body = results;
}
if (headers.getContentType() != null && headers.getContentType().includes(MediaType.APPLICATION_JSON) && body.toString().contains("\"")) {
body = body.toString();
}
return convert(status, MessageBuilder.withPayload(body).copyHeadersIfAbsent(outputHeaders).build(), headers);
}
use of org.springframework.http.HttpStatus in project spring-cloud-gateway by spring-cloud.
the class ServerWebExchangeUtils method parse.
public static HttpStatus parse(String statusString) {
HttpStatus httpStatus;
try {
int status = Integer.parseInt(statusString);
httpStatus = HttpStatus.valueOf(status);
} catch (NumberFormatException e) {
// try the enum string
httpStatus = HttpStatus.valueOf(statusString.toUpperCase());
}
return httpStatus;
}
use of org.springframework.http.HttpStatus in project thingsboard by thingsboard.
the class HttpSessionCtx method reply.
private void reply(RuleEngineErrorMsg msg) {
HttpStatus status = HttpStatus.INTERNAL_SERVER_ERROR;
switch(msg.getError()) {
case PLUGIN_TIMEOUT:
status = HttpStatus.REQUEST_TIMEOUT;
break;
default:
if (msg.getInMsgType() == MsgType.TO_SERVER_RPC_REQUEST) {
status = HttpStatus.BAD_REQUEST;
}
break;
}
responseWriter.setResult(new ResponseEntity<>(JsonConverter.toErrorJson(msg.getErrorMsg()).toString(), status));
}
use of org.springframework.http.HttpStatus in project thingsboard by thingsboard.
the class AuthController method checkResetToken.
@RequestMapping(value = "/noauth/resetPassword", params = { "resetToken" }, method = RequestMethod.GET)
public ResponseEntity<String> checkResetToken(@RequestParam(value = "resetToken") String resetToken) {
HttpHeaders headers = new HttpHeaders();
HttpStatus responseStatus;
String resetURI = "/login/resetPassword";
UserCredentials userCredentials = userService.findUserCredentialsByResetToken(resetToken);
if (userCredentials != null) {
try {
URI location = new URI(resetURI + "?resetToken=" + resetToken);
headers.setLocation(location);
responseStatus = HttpStatus.SEE_OTHER;
} catch (URISyntaxException e) {
log.error("Unable to create URI with address [{}]", resetURI);
responseStatus = HttpStatus.BAD_REQUEST;
}
} else {
responseStatus = HttpStatus.CONFLICT;
}
return new ResponseEntity<>(headers, responseStatus);
}
Aggregations