handlebars.JavaJaxRS.libraries.jersey1.OffsetDateTimeProvider.mustache Maven / Gradle / Ivy
package {{apiPackage}};
import com.sun.jersey.core.spi.component.ComponentContext;
import com.sun.jersey.spi.inject.Injectable;
import com.sun.jersey.spi.inject.PerRequestTypeInjectableProvider;
{{#jakarta}}
import jakarta.ws.rs.QueryParam;
import jakarta.ws.rs.WebApplicationException;
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.Response.Status;
import jakarta.ws.rs.core.UriInfo;
import jakarta.ws.rs.ext.Provider;
{{/jakarta}}
{{^jakarta}}
import javax.ws.rs.QueryParam;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.ext.Provider;
{{/jakarta}}
import java.time.OffsetDateTime;
import java.util.List;
@Provider
public class OffsetDateTimeProvider extends PerRequestTypeInjectableProvider {
private final UriInfo uriInfo;
public OffsetDateTimeProvider(@Context UriInfo uriInfo) {
super(OffsetDateTime.class);
this.uriInfo = uriInfo;
}
@Override
public Injectable getInjectable(final ComponentContext cc, final QueryParam a) {
return new Injectable() {
@Override
public OffsetDateTime 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 OffsetDateTime.parse(values.get(0));
}
};
}
}