com.holonplatform.jaxrs.server.ResourceUtils Maven / Gradle / Ivy
/*
* Copyright 2016-2017 Axioma srl.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.holonplatform.jaxrs.server;
import java.io.Serializable;
import java.util.Optional;
import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Providers;
import com.holonplatform.core.Context;
import com.holonplatform.core.internal.utils.ClassUtils;
import com.holonplatform.core.internal.utils.ObjectUtils;
/**
* Utility class to handle generic resources within a JAX-RS server context.
*
* @since 5.0.0
*/
public final class ResourceUtils implements Serializable {
private static final long serialVersionUID = -6271475972085523510L;
private ResourceUtils() {
}
/**
* Lookup a resource of given resourceType
type, either using a suitable {@link ContextResolver} if
* providers
is not null
and a {@link ContextResolver} for given resourceType
* is available, or trying to obtain the resource from {@link Context} using given resourceType
as
* context resource key.
* @param Resource type
* @param caller Caller class
* @param resourceType Resource type to lookup (not null)
* @param providers JAX-RS {@link Providers}, if available
* @return Resource instance, or an empty optional if not available
*/
public static Optional lookupResource(Class> caller, Class resourceType, Providers providers) {
ObjectUtils.argumentNotNull(resourceType, "Resource type must be not null");
R resource = null;
// try to use a ContextResolver, if available
ContextResolver resolver = providers.getContextResolver(resourceType, null);
if (resolver != null) {
resource = resolver.getContext(caller);
}
if (resource == null) {
// lookup in context
resource = Context.get()
.resource(resourceType,
(caller != null) ? caller.getClassLoader() : ClassUtils.getDefaultClassLoader())
.orElse(null);
}
return Optional.ofNullable(resource);
}
}