use of com.liferay.apio.architect.functional.Try in project com-liferay-apio-architect by liferay.
the class FormUtil method _getDateList.
private static void _getDateList(Body body, String key, boolean required, Consumer<List<Date>> consumer) {
String message = _getWrongDateMessage(key);
_getListField(body, key, required, consumer, (Stream<String> stream) -> stream.map(Try::success).map((Try<String> stringTry) -> stringTry.flatMap(DateTransformer::asDate).orElseThrow(() -> new BadRequestException(message))));
}
use of com.liferay.apio.architect.functional.Try in project com-liferay-apio-architect by liferay.
the class PageEndpointImpl method deleteCollectionItem.
@Override
public Response deleteCollectionItem(String id) throws Exception {
Try<ItemRoutes<T, S>> itemRoutesTry = Try.fromOptional(_itemRoutesSupplier::get, notFound(_name));
ThrowableConsumer<S> throwableConsumer = itemRoutesTry.mapOptional(ItemRoutes::getDeleteConsumerOptional, notAllowed(DELETE, _name, id)).map(function -> function.apply(_httpServletRequest)).getUnchecked();
Path path = new Path(_name, id);
throwableConsumer.accept(_identifierFunction.apply(path));
return noContent().build();
}
use of com.liferay.apio.architect.functional.Try in project com-liferay-apio-architect by liferay.
the class FailureFilter method filter.
@Override
public void filter(ContainerRequestContext containerRequestContext, ContainerResponseContext containerResponseContext) throws IOException {
Try<Object> objectTry = Try.fromFallible(containerResponseContext::getEntity);
objectTry.map(Failure.class::cast).map(Failure::getException).map(exception -> _errorUtil.getErrorResponse(exception, _request, _httpHeaders)).ifSuccess(response -> {
containerResponseContext.setStatus(response.getStatus());
MultivaluedMap<String, Object> headers = containerResponseContext.getHeaders();
headers.remove(CONTENT_TYPE);
MediaType mediaType = response.getMediaType();
if (mediaType != null) {
headers.add(CONTENT_TYPE, mediaType.toString());
}
Object entity = response.getEntity();
if (entity != null) {
containerResponseContext.setEntity(entity);
}
});
}
use of com.liferay.apio.architect.functional.Try in project com-liferay-apio-architect by liferay.
the class GenericUtil method getGenericTypeArgumentTry.
/**
* Returns the class of the parameterized class's n-th type argument.
*
* @param clazz the parameterized class
* @param interfaceClass the interface class
* @param position the n-th type argument's position in the parameterized
* class
* @return the class of the parameterized class's n-th type argument
*/
public static <S> Try<Class<S>> getGenericTypeArgumentTry(Class<?> clazz, Class<?> interfaceClass, int position) {
Type[] genericInterfaces = clazz.getGenericInterfaces();
Try<Class<S>> classTry = Try.fail(new IllegalArgumentException("Class " + clazz + " does not implement any interfaces"));
for (Type genericInterface : genericInterfaces) {
classTry = classTry.recoverWith(throwable -> getGenericTypeArgumentFromTypeTry(genericInterface, interfaceClass, position));
}
return classTry.recoverWith(throwable -> getGenericTypeArgumentTry(clazz.getSuperclass(), interfaceClass, position));
}
Aggregations