Logging in Java 8 Optional - java

I have some code where I'm using Java 8 Optional in which I want to log an error when I don't get the required result.
As shown in following Example I have commented on a line where I get the error when I'm trying to log Error Message:
#PutMapping("/organs/{id}")
public Organ updateorgan(#PathVariable(value = "id") Long organId,
#Valid #RequestBody Organ organDetails) {
Organ organ = organRepository.findById(organId)
.orElseThrow(() ->
// LOG.log(Level.SEVERE,"Organ with id "+organId + "not found");
new ResourceNotFoundException("organ", "id", organId)
);
organ.setName(organDetails.getName());
Organ updatedOrgan = organRepository.save(organ);
LOG.info("Updated organ details. Response :"+updatedOrgan);
return updatedOrgan;
}
P.S - I only want to use the Java 8 method and not conventional approach.
Thanks in advance!

Make it a lambda with a body enclosed by curly braces and a return statement instead of an expression lambda:
Organ organ = organRepository.findById(organId)
.orElseThrow(() -> {
LOG.log(Level.SEVERE,"Organ with id "+organId + "not found");
return new ResourceNotFoundException("organ", "id", organId);
});

You have to use return
Organ organ = organRepository.findById(organId)
.orElseThrow(() -> {
LOG.log(Level.SEVERE,"Organ with id "+organId + "not found");
return new ResourceNotFoundException("organ", "id", organId);
});

Related

convert for loop in java reactive programming - helidon

I am new to reactive programming and using helidon reactive java libraries in our code.
I am unable to achieve the below use case.
I have a scenario as below.
First I invoke a REST API and get response.From the response that contains list of countries I have to invoke another
REST api that retrieves the response for a country id and update the country object.
By the time I invoke second API and set value to country object as below the response is already returned.
I get use .get() and wait() on Single as it blocks the thread.
Please suggest how to overcome the below for loop and update the list of objects reactive way.
Single<WebClientResponse> singleWebClientResp = webClient.get("REST_URL");
Single<String> apiResponse = singleWebClientResponse.flatMapSingle(webClientResponse -> {
return webClientResponse.content().as(String.class);
});
apiResponse.flatMapSingle(fusionAPIResponseString -> {
List<Country> countries =
objectMapper.readValue(fusionAPIResponseString,new TypeReference<List<Country>>() {});
for (Country country : countries) {
getCountryByRegion(country.getRegion()).forSingle(newCountry -> {
LOGGER.log(Level.FINE, "newCountry ---> " + newCountry);
country.setRegion(country.getRegion() + "modified" + newCountry);
});
}
});
private Single<String> getCountryByRegion(String regionName) {
LOGGER.log(Level.FINE, "Entering getCountryByRegion");
Single<WebClientResponse> singleWebClientResponse2 = webClient.get().path("v3.1/region/" + regionName)
.contentType(MediaType.APPLICATION_JSON).request();
Single<String> retVal = singleWebClientResponse2.flatMapSingle(webClientResponse -> {
return webClientResponse.content().as(String.class);
});
LOGGER.log(Level.FINE, "Exiting getCountryByRegion");
return retVal;
}
Regards
// NOTE: this should be a static constant
GenericType<List<Country>> countriesType = new GenericType<>() {};
// NOTE: create the webClient only once, not for every request
WebClient webClient = WebClient.builder()
.addMediaSupport(JacksonSupport.create())
.baseUri("service-url")
.build();
// the pipeline starts with the initial countries (i.e. Single<List<Country>>)
webClient.get()
.path("/countries")
// get the countries as List<Country>
.request(countriesType)
// add each country to the reactive pipeline (i.e. Multi<Country>)
// to allow individual reactive mapping
.flatMap(Multi::just)
// map each country by creating a new country with new region
// use flatMap to inline the webClient result in the reactive pipeline
.flatMap(country ->
webClient.get()
.path("/region/" + country.getRegion())
.request(String.class)
.map(newCountry -> new Country(newCountry, country.getRegion())))
// aggregate all items (i.e. Single<List<Country>>)
.collectList()
.onError(res::send)
.forSingle(res::send))

How to use a variable from Uni, Mutiny

I created a reactive resteasy service with Quarkus and Mutiny. In POST method I insert object in my PostgreSQL table and get an Uni< id > in return. I need to set this id as part of my Response class which contains f.ex. "id", "errorCode", "errorString" and other variables, but I struggle as id comes as Uni object and I don't know how to extract id from it.
Create method:
public static Uni<Long> create(PgPool client, RetailPlace retailPlace) {
return client.withTransaction(conn -> conn
.preparedQuery("INSERT INTO retail_place (title) VALUES ($1) RETURNING id")
.execute(Tuple.of(retailPlace.getRetailPlaceTitle()))
.onItem().transformToUni(id -> conn.
preparedQuery("INSERT INTO retail_place_address (retail_place_id,region_code,city,locality_id,apartment,house,region,street) " +
"VALUES ($1,$2,$3,$4,$5,$6,$7,$8) returning retail_place_id")
.execute(Tuple.tuple(Arrays.asList(id.iterator().next().getLong("id"), retailPlace.getRetailPlaceAddress().getRegionCode(),
retailPlace.getRetailPlaceAddress().getCity(), retailPlace.getRetailPlaceAddress().getLocalityId(),
retailPlace.getRetailPlaceAddress().getApartment(), retailPlace.getRetailPlaceAddress().getHouse(),
retailPlace.getRetailPlaceAddress().getRegion(), retailPlace.getRetailPlaceAddress().getStreet())))))
.onItem().transform(pgRowSet -> pgRowSet.iterator().next().getLong("retail_place_id"));
}
I get long value of id in return. Now I need to return a RetailPlaceResponse with id value in it:
public RetailPlaceResponse(String errorCode, long id, boolean isSuccessful) {
this.errorCode = errorCode;
this.id = id;
this.isSuccessful = isSuccessful;
}
If you need both successful and non-successful responses to map to the same class, you could write separate transforms. One for success and one for failure.
public static Uni<RetailPlaceResponse> create(PgPool client, RetailPlace retailPlace) {
return client.withTransaction(conn -> conn
.preparedQuery("INSERT INTO retail_place (title) VALUES ($1) RETURNING id")
.execute(Tuple.of(retailPlace.getRetailPlaceTitle()))
.onItem().transformToUni(id -> conn.
preparedQuery("INSERT INTO retail_place_address (retail_place_id,region_code,city,locality_id,apartment,house,region,street) " +
"VALUES ($1,$2,$3,$4,$5,$6,$7,$8) returning retail_place_id")
.execute(Tuple.tuple(Arrays.asList(id.iterator().next().getLong("id"), retailPlace.getRetailPlaceAddress().getRegionCode(),
retailPlace.getRetailPlaceAddress().getCity(), retailPlace.getRetailPlaceAddress().getLocalityId(),
retailPlace.getRetailPlaceAddress().getApartment(), retailPlace.getRetailPlaceAddress().getHouse(),
retailPlace.getRetailPlaceAddress().getRegion(), retailPlace.getRetailPlaceAddress().getStreet())))))
.onItem().transform(pgRowSet -> new RetailPlaceResponse(null, pgRowSet.iterator().next().getLong("retail_place_id"), true))
.onFailure().transform(error -> new RetailPlaceResponse(error.toString(), 0, false));
}

How to reduce Optionals to throw an error once

I have the following response coming from a rest call and performing some logic based on what is returned.
This is what I want.
If the overall status code is NOT 200 OR
If within the list of SimpleResponse, none of the SimpleResponse objects has a 200 httpCode, throw an error.
The example below is incomplete.
Too much going on there, having an optional within another optional.
And then throwing the same error at 2 different places.
Also if the optional result is null say at
responseEntity.getBody(), would want to throw same error too.
Is there a cleaner way to write this?
These are the 2 related objects
#Getter
#Setter
public class SimpleResponses {
private List<SimpleResponse> simpleResponsesList;
}
#Getter
#Setter
public class SimpleResponse {
private String httpCode;
// ... other fields
}
Method calling rest call and throwing error if needed.
public ResponseEntity<SimpleResponses> get() {
HttpEntity<Object> httpEntity = this.getEntity();
// restTemplate is from Spring
ResponseEntity<SimpleResponses> responseEntity = restTemplate.exchange(url, HttpMethod.GET, httpEntity, SimpleResponses.class);
// START
// This is the logic to throw error depending on output as mentioned above.
// looking for a better way to write this.
// if none of the object inside the list has 200 code, throw error
Optional.ofNullable(responseEntity.getBody())
.map(SimpleResponses::getSimpleResponses)
.ifPresent(response -> {
Optional<SimpleResponse> simpleResponse = response.stream()
.filter(responseStream -> responseStream.getHttpCode().equals("200"))
.findAny();
if (!simpleResponse.isPresent()) {
throw new CustomRuntimeException("Failed ..... "); // repetitive same error being thrown again below.
}
});
// if overall code is not 200, throw error too
if (!responseEntity.getStatusCode().is2xxSuccessful()) {
throw new CustomRuntimeException("Failed ..... ");
}
// END
return responseEntity;
}
Using Optional::flatMap and Optional::ifPresentOrElse the following solution may be offered:
Optional.ofNullable(responseEntity.getBody())
.flatMap(body -> SimpleResponses.getSimpleResponses(body) // SimpleResponses
.getSimpleResponsesList()
.stream() // Stream<SimpleResponse>
.filter(sr -> "200".equals(sr.getHtppCode()))
.findAny() // Optional<SimpleResponse>
)
.ifPresentOrElse( // Java 9
System.out::println,
() -> { throw new CustomRuntimeException("Bad response"); }
);
For Java 8, Optional::orElseThrow may be used:
SimpleResponse good = Optional.ofNullable(responseEntity.getBody())
.flatMap(body -> SimpleResponses.getSimpleResponses(body) // SimpleResponses
.getSimpleResponsesList()
.stream() // Stream<SimpleResponse>
.filter(sr -> "200".equals(sr.getHtppCode()))
.findAny() // Optional<SimpleResponse>
)
.orElseThrow(() -> new CustomRuntimeException("Bad response"));
System.out.println(good);

Data validation and account creation with Either - how to write it better?

I have an AccountCreator class with a create method that takes a DTO with the data needed to create an account. At the beginning there is an attempt to create 2 value objects (UserName and Password), then validate the uniqueness of the user name, create the Account entity which takes these 2 value objects in the constructor and save it in the repo. Of course, errors such as incorrect password length, etc. may be returned. I used Eithers for this and now the question is whether this code is ok or maybe it can be written somehow better?
public Either<Error, AccountDto> create(AccountCreateDto accountCreateDto) {
var errorType = ErrorType.ACCOUNT_PERSISTENCE_ERROR;
var errorMessage = "Not unique user name: " + accountCreateDto.userName;
var error = new Error(errorType, errorMessage);
return UserName
.create(accountCreateDto.userName)
.flatMap(userName ->
userNameUniquenessChecker.isUnique(userName.text) ?
Password
.create(accountCreateDto.password)
.flatMap(password -> {
var createdAccount = new Account(
userName,
password,
AccountStatus.OPEN,
LocalDateTime.now(),
new ArrayList<>()
);
var addedAccount = accountRepository.add(createdAccount);
var accountDto = new AccountDto(
addedAccount.userName.text,
addedAccount.password.text,
addedAccount.status,
addedAccount.creationDate,
(long) addedAccount.tasks.size()
);
return Either.right(accountDto);
}) : Either.left(error));
}
The customary FP approach would be to use a Validation style applicative functor - there's one in Vavr called Validation. You can use Validation.combine to combine multiple Validation values into one, e.g.:
public Validation<Seq<<Error>, AccountDto> create(AccountCreateDto accountCreateDto) {
Validation<Seq<<Error>, Account> validAcc =
Validation.combine(
UserName.create(accountCreateDto.userName),
Password.create(accountCreateDto.password)
).ap((un, pw) -> new Account(
un,
pw,
AccountStatus.OPEN,
LocalDateTime.now()
);
Validation<Seq<<Error>, Account> validAcc2 =
validAcc.flatMap(acc -> validateUserIdIsUnique(acc));
Validation<<Seq<Error>, AccountDto> validAccDto =
validAcc2.map(accountRepository::add)
.map(addedAccount ->
new AccountDto(
addedAccount.userName.text,
addedAccount.password.text,
addedAccount.status,
addedAccount.creationDate,
(long) addedAccount.tasks.size()
)
);
}
return validAccDto;
}
private static final var errorType = ErrorType.ACCOUNT_PERSISTENCE_ERROR;
private static final var errorMessage = "Not unique user name: " + accountCreateDto.userName;
private static final var error = new Error(errorType, errorMessage);
Validation<Seq<Error>, Account> validateUserIdIsUnique(Account acc) {
return userNameUniquenessChecker.isUnique(acc.userName.text) ?
Validation.valid(userName) :
Validation.invalid(error);
}
You can elide the temporaries - validAcc, validAcc2 & validAccDto, however I left them in for clarity.
(caveat emptor - haven't tested that this code works, or even compiles)

Understanding Mono.when

I'm new to Spring WebFlux and do not fully understand the Mono.when(). The following code does not work as expected:
List<Mono<Void>> results= ....;
String textVar = "my text";
processors.forEach(p -> {
Mono<Object> restResponseMono = client.getSomething();
results.add(restResponseMono.doOnNext(resp -> {
textVar = textVar + resp.getText();
}).then());
});
Mono.when(results).then(
//here it would expect modification of 'textVar'
Mono.just(textVar);
)
After calling the Mono.when(results).then(...) I would expect all my changes to be applied to the textVar because in the docu it is written:
[...Aggregate given publishers into a new Mono that will befulfilled when all of the given Publishers have completed....]
And the restResponseMono.then() should also wait until everything is completed. So I do not know exactly where is my lack of understanding.
Publishers in the when parameter,When subscribe automatically subscribes.
The way it works is simply this.
Flux<Integer> m1 = Flux.just(1,2).doOnNext(e -> System.out.println("M1 doOnNext: " + e));
Mono<Integer> m2 = Mono.just(12).doOnSuccess(e -> System.out.println("M2 doOnSuccess: " + e));
Mono<String> mono = Mono.when(m1,m2).then(Mono.just("STR")).doOnSuccess(e -> System.out.println("_________when doOnSuccess: " + e));
mono.log().subscribe(System.out::println, Exception::new, () -> System.out.println("Completed2."));

Categories

Resources