use of org.glassfish.jersey.server.model.ResourceMethod in project jersey by jersey.
the class ResourceMxBeanImpl method updateResourceStatistics.
/**
* Update the statistics of this MXBean and of nested MXBeans.
* @param resourceStatistics New resource statistics.
*/
public void updateResourceStatistics(ResourceStatistics resourceStatistics) {
this.methodsExecutionStatisticsBean.updateExecutionStatistics(resourceStatistics.getResourceMethodExecutionStatistics());
this.requestExecutionStatisticsBean.updateExecutionStatistics(resourceStatistics.getRequestExecutionStatistics());
for (Map.Entry<ResourceMethod, ResourceMethodStatistics> entry : resourceStatistics.getResourceMethodStatistics().entrySet()) {
final ResourceMethodStatistics methodStats = entry.getValue();
final ResourceMethod method = entry.getKey();
final String methodId = MonitoringUtils.getMethodUniqueId(method);
ResourceMethodMXBeanImpl methodMXBean = this.resourceMethods.get(methodId);
if (methodMXBean == null) {
methodMXBean = new ResourceMethodMXBeanImpl(methodStats, uriResource, mBeanExposer, resourcePropertyName, methodId);
resourceMethods.put(methodId, methodMXBean);
}
methodMXBean.updateResourceMethodStatistics(methodStats);
}
}
use of org.glassfish.jersey.server.model.ResourceMethod in project jersey by jersey.
the class MethodSelectingRouter method getMethodRouter.
private List<Router> getMethodRouter(final RequestProcessingContext context) {
final ContainerRequest request = context.request();
final List<ConsumesProducesAcceptor> acceptors = consumesProducesAcceptors.get(request.getMethod());
if (acceptors == null) {
throw new NotAllowedException(Response.status(Status.METHOD_NOT_ALLOWED).allow(consumesProducesAcceptors.keySet()).build());
}
final List<ConsumesProducesAcceptor> satisfyingAcceptors = new LinkedList<>();
final Set<ResourceMethod> differentInvokableMethods = Collections.newSetFromMap(new IdentityHashMap<>());
for (ConsumesProducesAcceptor cpi : acceptors) {
if (cpi.isConsumable(request)) {
satisfyingAcceptors.add(cpi);
differentInvokableMethods.add(cpi.methodRouting.method);
}
}
if (satisfyingAcceptors.isEmpty()) {
throw new NotSupportedException();
}
final List<AcceptableMediaType> acceptableMediaTypes = request.getQualifiedAcceptableMediaTypes();
final MediaType requestContentType = request.getMediaType();
final MediaType effectiveContentType = requestContentType == null ? MediaType.WILDCARD_TYPE : requestContentType;
final MethodSelector methodSelector = selectMethod(acceptableMediaTypes, satisfyingAcceptors, effectiveContentType, differentInvokableMethods.size() == 1);
if (methodSelector.selected != null) {
final RequestSpecificConsumesProducesAcceptor selected = methodSelector.selected;
if (methodSelector.sameFitnessAcceptors != null) {
reportMethodSelectionAmbiguity(acceptableMediaTypes, methodSelector.selected, methodSelector.sameFitnessAcceptors);
}
context.push(new Function<ContainerResponse, ContainerResponse>() {
@Override
public ContainerResponse apply(final ContainerResponse responseContext) {
// - either there is an entity, or we are responding to a HEAD request
if (responseContext.getMediaType() == null && ((responseContext.hasEntity() || HttpMethod.HEAD.equals(request.getMethod())))) {
MediaType effectiveResponseType = determineResponseMediaType(responseContext.getEntityClass(), responseContext.getEntityType(), methodSelector.selected, acceptableMediaTypes);
if (MediaTypes.isWildcard(effectiveResponseType)) {
if (effectiveResponseType.isWildcardType() || "application".equalsIgnoreCase(effectiveResponseType.getType())) {
effectiveResponseType = MediaType.APPLICATION_OCTET_STREAM_TYPE;
} else {
throw new NotAcceptableException();
}
}
responseContext.setMediaType(effectiveResponseType);
}
return responseContext;
}
});
return selected.methodRouting.routers;
}
throw new NotAcceptableException();
}
use of org.glassfish.jersey.server.model.ResourceMethod in project jersey by jersey.
the class ModelProcessorUtil method enhanceResource.
/**
* Enhance the runtime resource referenced by {@code resource} parameter with a list of additional methods.
*
* The new {@code methods} are added to the runtime resource. In case of method conflicts, the existing resource methods
* will be preserved and will not be 'overridden' by any new method from the {@code methods} list.
* Overriding check takes into account media types of methods so that new resource methods with same HTTP method
* can be defined only for a more more specific media type.
*
* @param resource Runtime resource to be enhanced.
* @param enhancedModelBuilder Builder for the enhanced resource model to be used.
* @param methods List of enhancing methods.
* @param extended This flag will initialize the property
* {@link org.glassfish.jersey.server.model.ResourceMethod#isExtended()}.
*/
public static void enhanceResource(RuntimeResource resource, ResourceModel.Builder enhancedModelBuilder, List<Method> methods, boolean extended) {
final Resource firstResource = resource.getResources().get(0);
if (methodsSuitableForResource(firstResource, methods)) {
for (Method method : methods) {
final Set<MediaType> produces = new HashSet<>(method.produces);
for (ResourceMethod resourceMethod : resource.getResourceMethods()) {
for (final MediaType produce : method.produces) {
if (ModelProcessorUtil.isMethodOverridden(resourceMethod, method.httpMethod, method.consumes.get(0), produce)) {
produces.remove(produce);
}
}
}
if (!produces.isEmpty()) {
final Resource parentResource = resource.getParentResources().get(0);
if (parentResource != null && method.path != null) {
continue;
}
final Resource.Builder resourceBuilder = Resource.builder(firstResource.getPath());
final Resource.Builder builder = method.path != null ? resourceBuilder.addChildResource(method.path) : resourceBuilder;
final ResourceMethod.Builder methodBuilder = builder.addMethod(method.httpMethod).consumes(method.consumes).produces(produces);
if (method.inflector != null) {
methodBuilder.handledBy(method.inflector);
} else {
methodBuilder.handledBy(method.inflectorClass);
}
methodBuilder.extended(extended);
final Resource newResource = resourceBuilder.build();
if (parentResource != null) {
final Resource.Builder parentBuilder = Resource.builder(parentResource.getPath());
parentBuilder.addChildResource(newResource);
enhancedModelBuilder.addResource(parentBuilder.build());
} else {
enhancedModelBuilder.addResource(newResource);
}
}
}
}
for (RuntimeResource child : resource.getChildRuntimeResources()) {
enhanceResource(child, enhancedModelBuilder, methods, extended);
}
}
use of org.glassfish.jersey.server.model.ResourceMethod in project jersey by jersey.
the class ModelProcessorUtil method getAllowedMethods.
/**
* Return allowed methods for the given {@code resource}. OPTIONS and HEAD are always returned in the result.
*
* @param resource Resource for which resource methods should be found.
* @return Set of resource methods that can be invoked on the given resource.
*/
public static Set<String> getAllowedMethods(RuntimeResource resource) {
boolean getFound = false;
Set<String> allowedMethods = new HashSet<>();
for (ResourceMethod resourceMethod : resource.getResourceMethods()) {
final String httpMethod = resourceMethod.getHttpMethod();
allowedMethods.add(httpMethod);
if (!getFound && httpMethod.equals(HttpMethod.GET)) {
getFound = true;
}
}
allowedMethods.add(HttpMethod.OPTIONS);
if (getFound) {
allowedMethods.add(HttpMethod.HEAD);
}
return allowedMethods;
}
use of org.glassfish.jersey.server.model.ResourceMethod in project jersey by jersey.
the class WadlBuilder method generateResource.
private Resource generateResource(final org.glassfish.jersey.server.model.Resource resource, String path, Set<org.glassfish.jersey.server.model.Resource> visitedResources) {
try {
if (!detailedWadl && resource.isExtended()) {
return null;
}
Resource wadlResource = _wadlGenerator.createResource(resource, path);
// prevent infinite recursion
if (visitedResources.contains(resource)) {
return wadlResource;
} else {
visitedResources = new HashSet<>(visitedResources);
visitedResources.add(resource);
}
// if the resource contains subresource locator create new resource for this locator and return it instead
// of this resource
final ResourceMethod locator = resource.getResourceLocator();
if (locator != null) {
try {
org.glassfish.jersey.server.model.Resource.Builder builder = org.glassfish.jersey.server.model.Resource.builder(locator.getInvocable().getRawResponseType());
if (builder == null) {
// for example in the case the return type of the sub resource locator is Object
builder = org.glassfish.jersey.server.model.Resource.builder().path(resource.getPath());
}
org.glassfish.jersey.server.model.Resource subResource = builder.build();
Resource wadlSubResource = generateResource(subResource, resource.getPath(), visitedResources);
if (wadlSubResource == null) {
return null;
}
if (locator.isExtended()) {
wadlSubResource.getAny().add(WadlApplicationContextImpl.EXTENDED_ELEMENT);
}
for (Parameter param : locator.getInvocable().getParameters()) {
Param wadlParam = generateParam(resource, locator, param);
if (wadlParam != null && wadlParam.getStyle() == ParamStyle.TEMPLATE) {
wadlSubResource.getParam().add(wadlParam);
}
}
return wadlSubResource;
} catch (RuntimeException e) {
throw new ProcessingException(LocalizationMessages.ERROR_WADL_BUILDER_GENERATION_RESOURCE_LOCATOR(locator, resource), e);
}
}
Map<String, Param> wadlResourceParams = new HashMap<>();
// for each resource method
for (org.glassfish.jersey.server.model.ResourceMethod method : resource.getResourceMethods()) {
if (!detailedWadl && method.isExtended()) {
continue;
}
com.sun.research.ws.wadl.Method wadlMethod = generateMethod(resource, wadlResourceParams, method);
wadlResource.getMethodOrResource().add(wadlMethod);
}
// add method parameters that are associated with the resource PATH template
for (Param wadlParam : wadlResourceParams.values()) {
wadlResource.getParam().add(wadlParam);
}
// for each sub-resource method
Map<String, Resource> wadlSubResources = new HashMap<>();
Map<String, Map<String, Param>> wadlSubResourcesParams = new HashMap<>();
for (org.glassfish.jersey.server.model.Resource childResource : resource.getChildResources()) {
Resource childWadlResource = generateResource(childResource, childResource.getPath(), visitedResources);
if (childWadlResource == null) {
continue;
}
wadlResource.getMethodOrResource().add(childWadlResource);
}
return wadlResource;
} catch (Exception e) {
throw new ProcessingException(LocalizationMessages.ERROR_WADL_BUILDER_GENERATION_RESOURCE_PATH(resource, path), e);
}
}
Aggregations