All Downloads are FREE. Search and download functionalities are using the official Maven repository.

java-msf4j-server.LocalDateProvider.mustache Maven / Gradle / Ivy

There is a newer version: 7.9.0
Show newest version
package {{apiPackage}};

import com.sun.jersey.core.spi.component.ComponentContext;
import com.sun.jersey.spi.inject.Injectable;
import com.sun.jersey.spi.inject.PerRequestTypeInjectableProvider;

import {{javaxPackage}}.ws.rs.QueryParam;
import {{javaxPackage}}.ws.rs.WebApplicationException;
import {{javaxPackage}}.ws.rs.core.Context;
import {{javaxPackage}}.ws.rs.core.Response;
import {{javaxPackage}}.ws.rs.core.Response.Status;
import {{javaxPackage}}.ws.rs.core.UriInfo;
import {{javaxPackage}}.ws.rs.ext.Provider;
import java.time.LocalDate;
import java.util.List;

@Provider
public class LocalDateProvider extends PerRequestTypeInjectableProvider {
    private final UriInfo uriInfo;

    public LocalDateProvider(@Context UriInfo uriInfo) {
        super(LocalDate.class);
        this.uriInfo = uriInfo;
    }

    @Override
    public Injectable getInjectable(final ComponentContext cc, final QueryParam a) {
        return new Injectable() {
            @Override
            public LocalDate getValue() {
                final List values = uriInfo.getQueryParameters().get(a.value());

                if (values == null || values.isEmpty())
                    return null;
                if (values.size() > 1) {
                    throw new WebApplicationException(Response.status(Status.BAD_REQUEST).
                            entity(a.value() + " cannot contain multiple values").build());
                }

                return LocalDate.parse(values.get(0));
            }
        };
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy