package stroom.util.rest;

import stroom.util.logging.LogUtil;
import stroom.util.shared.HasIntegerId;
import stroom.util.shared.HasUuid;

import javax.ws.rs.BadRequestException;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import java.util.function.Supplier;

public class RestUtil {

    private RestUtil() {
    }

    public static void requireNonNull(final Object object) throws BadRequestException {
        if (object == null) {
            throw new BadRequestException();
        }
    }

    public static void requireNonNull(final Object object, String message) throws BadRequestException {
        if (object == null) {
            throw new BadRequestException(message);
        }
    }

    public static void requireNonNull(final Object object, Supplier<String> messageSupplier) throws BadRequestException {
        if (object == null) {
            throw new BadRequestException(messageSupplier != null
                    ? messageSupplier.get()
                    : null);
        }
    }

    public static void requireMatchingIds(final int id, final HasIntegerId object) {
        if (object == null) {
            throw new BadRequestException("Object is null");
        }
        if (object.getId() != id) {
            throw new BadRequestException("Id " + id + " doesn't match id in object " + object.getId());
        }
    }

    public static void requireMatchingUuids(final String uuid, final HasUuid object) {
        if (object == null) {
            throw new BadRequestException("Object is null");
        }
        if (uuid == null) {
            throw new BadRequestException("uuid is null");
        }
        if (object.getUuid().equals(uuid)) {
            throw new BadRequestException("UUID " + uuid + " doesn't match UUID in object " + object.getUuid());
        }
    }

    public static Response badRequest(final String msg, final Object... args) {
        return Response
                .status(Response.Status.BAD_REQUEST)
                .entity(LogUtil.message(msg, args))
                .build();
    }

    public static Response notFound(final String msg, final Object... args) {
        return Response
                .status(Status.NOT_FOUND)
                .entity(LogUtil.message(msg, args))
                .build();
    }

    public static Response ok(final String msg, final Object... args) {
        return Response
                .ok(LogUtil.message(msg, args))
                .build();
    }
}

@@ -172,16 +172,54 @@
 eption;%0A
+import javax.ws.rs.NotFoundException;%0A
 import j
@@ -244,16 +244,16 @@
 sponse;%0A
-
 import j
@@ -285,16 +285,43 @@
 Status;%0A
+import java.util.Optional;%0A
 import j
@@ -398,32 +398,145 @@
 Util() %7B%0A    %7D%0A%0A
+    /**%0A     * Used to validate a request argument and throw a %7B@link BadRequestException%7D if it is null%0A     */%0A
     public stati
@@ -691,32 +691,145 @@
        %7D%0A    %7D%0A%0A
+    /**%0A     * Used to validate a request argument and throw a %7B@link BadRequestException%7D if it is null%0A     */%0A
     public stati
@@ -1007,32 +1007,145 @@
        %7D%0A    %7D%0A%0A
+    /**%0A     * Used to validate a request argument and throw a %7B@link BadRequestException%7D if it is null%0A     */%0A
     public stati
@@ -2847,32 +2847,32 @@
 age(msg, args))%0A
-
                 
@@ -2886,11 +2886,966 @@
 );%0A    %7D
+%0A%0A    /**%0A     * Ensure value is non null or throw a %7B@link NotFoundException%7D. For use in validating%0A     * responses.%0A     */%0A    public static %3CT%3E T ensureNonNullResult(final T value,%0A                                            final String msg,%0A                                            final Object... args) %7B%0A        if (value == null) %7B%0A            throw new NotFoundException(LogUtil.message(msg, args));%0A        %7D%0A        return value;%0A    %7D%0A%0A    /**%0A     * Ensure value is not empty or throw a %7B@link NotFoundException%7D. For use in validating%0A     * responses.%0A     */%0A    public static %3CT%3E T ensureNotEmptyResult(final Optional%3CT%3E optValue,%0A                                             final String msg,%0A                                             final Object... args) %7B%0A        if (optValue == null %7C%7C optValue.isEmpty()) %7B%0A            throw new NotFoundException(LogUtil.message(msg, args));%0A        %7D%0A        return optValue.get();%0A    %7D
 %0A%7D%0A

Fix bugs in TestRestResources