Search in sources :

Example 1 with Try

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))));
}
Also used : BadRequestException(javax.ws.rs.BadRequestException) Stream(java.util.stream.Stream) Try(com.liferay.apio.architect.functional.Try)

Example 2 with Try

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();
}
Also used : Body(com.liferay.apio.architect.form.Body) ItemRoutes(com.liferay.apio.architect.routes.ItemRoutes) ExceptionSupplierUtil.notFound(com.liferay.apio.architect.endpoint.ExceptionSupplierUtil.notFound) NestedCollectionRoutes(com.liferay.apio.architect.routes.NestedCollectionRoutes) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ExceptionSupplierUtil.notAllowed(com.liferay.apio.architect.endpoint.ExceptionSupplierUtil.notAllowed) PageEndpoint(com.liferay.apio.architect.endpoint.PageEndpoint) Representor(com.liferay.apio.architect.representor.Representor) Identifier(com.liferay.apio.architect.identifier.Identifier) ThrowableFunction(com.liferay.apio.architect.function.throwable.ThrowableFunction) HttpServletRequest(javax.servlet.http.HttpServletRequest) Response.noContent(javax.ws.rs.core.Response.noContent) Page(com.liferay.apio.architect.pagination.Page) CollectionRoutes(com.liferay.apio.architect.routes.CollectionRoutes) POST(com.liferay.apio.architect.operation.Method.POST) DELETE(com.liferay.apio.architect.operation.Method.DELETE) RelatedCollection(com.liferay.apio.architect.related.RelatedCollection) SingleModel(com.liferay.apio.architect.single.model.SingleModel) NoSuchElementException(java.util.NoSuchElementException) PUT(com.liferay.apio.architect.operation.Method.PUT) Try(com.liferay.apio.architect.functional.Try) Predicate(java.util.function.Predicate) Path(com.liferay.apio.architect.uri.Path) Response(javax.ws.rs.core.Response) ThrowableConsumer(com.liferay.apio.architect.consumer.throwable.ThrowableConsumer) Optional(java.util.Optional) Path(com.liferay.apio.architect.uri.Path) ItemRoutes(com.liferay.apio.architect.routes.ItemRoutes)

Example 3 with Try

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);
        }
    });
}
Also used : CONTENT_TYPE(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE) Try(com.liferay.apio.architect.functional.Try) Context(javax.ws.rs.core.Context) IOException(java.io.IOException) Failure(com.liferay.apio.architect.functional.Try.Failure) ContainerResponseFilter(javax.ws.rs.container.ContainerResponseFilter) ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) ErrorUtil(com.liferay.apio.architect.jaxrs.json.internal.util.ErrorUtil) MediaType(javax.ws.rs.core.MediaType) Component(org.osgi.service.component.annotations.Component) HttpHeaders(javax.ws.rs.core.HttpHeaders) Request(javax.ws.rs.core.Request) Reference(org.osgi.service.component.annotations.Reference) ContainerResponseContext(javax.ws.rs.container.ContainerResponseContext) MediaType(javax.ws.rs.core.MediaType) Failure(com.liferay.apio.architect.functional.Try.Failure)

Example 4 with Try

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));
}
Also used : Try(com.liferay.apio.architect.functional.Try) Unsafe(com.liferay.apio.architect.unsafe.Unsafe) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type)

Aggregations

Try (com.liferay.apio.architect.functional.Try)4 ThrowableConsumer (com.liferay.apio.architect.consumer.throwable.ThrowableConsumer)1 ExceptionSupplierUtil.notAllowed (com.liferay.apio.architect.endpoint.ExceptionSupplierUtil.notAllowed)1 ExceptionSupplierUtil.notFound (com.liferay.apio.architect.endpoint.ExceptionSupplierUtil.notFound)1 PageEndpoint (com.liferay.apio.architect.endpoint.PageEndpoint)1 Body (com.liferay.apio.architect.form.Body)1 ThrowableFunction (com.liferay.apio.architect.function.throwable.ThrowableFunction)1 Failure (com.liferay.apio.architect.functional.Try.Failure)1 Identifier (com.liferay.apio.architect.identifier.Identifier)1 ErrorUtil (com.liferay.apio.architect.jaxrs.json.internal.util.ErrorUtil)1 DELETE (com.liferay.apio.architect.operation.Method.DELETE)1 POST (com.liferay.apio.architect.operation.Method.POST)1 PUT (com.liferay.apio.architect.operation.Method.PUT)1 Page (com.liferay.apio.architect.pagination.Page)1 RelatedCollection (com.liferay.apio.architect.related.RelatedCollection)1 Representor (com.liferay.apio.architect.representor.Representor)1 CollectionRoutes (com.liferay.apio.architect.routes.CollectionRoutes)1 ItemRoutes (com.liferay.apio.architect.routes.ItemRoutes)1 NestedCollectionRoutes (com.liferay.apio.architect.routes.NestedCollectionRoutes)1 SingleModel (com.liferay.apio.architect.single.model.SingleModel)1