/** * Create a resource model builder initialized by introspecting an annotated * JAX-RS resource class. * * @param resourceClass resource class to be modelled. * @return resource model builder initialized by the class or {@code null} if the * class does not represent a resource. */ public static Builder builder(Class<?> resourceClass) { return builder(resourceClass, false); }
/** * Get a new resource model builder initialized from a given resource model. * * @param resource resource model initializing the resource builder. * @return new resource model builder. */ public static Builder builder(Resource resource) { return builder(resource.data); }
/** * Create a resource model builder initialized by introspecting an annotated * JAX-RS resource class. * * @param resourceClass resource class to be modelled. * @return resource model builder initialized by the class or {@code null} if the * class does not represent a resource. */ public static Builder builder(Class<?> resourceClass) { return builder(resourceClass, false); }
/** * Get a new resource model builder initialized from a given resource model. * * @param resource resource model initializing the resource builder. * @return new resource model builder. */ public static Builder builder(Resource resource) { return builder(resource.data); }
private LocatorRouting createRouting(final Class<?> locatorClass) { Resource.Builder builder = Resource.builder(locatorClass, disableValidation); if (builder == null) { // resource is empty - do not throw 404, wait if ModelProcessors add any method builder = Resource.builder().name(locatorClass.getName()); } return buildRouting(builder.build()); }
private <T> Set<Resource> prefixResources(String prefix, Set<Class<? extends T>> resources) { final String pathPrefix = prefix.endsWith("/") ? prefix.substring(0, prefix.length() - 1) : prefix; return resources .stream() .map(resource -> { final javax.ws.rs.Path pathAnnotation = Resource.getPath(resource); final String resourcePathSuffix = Strings.nullToEmpty(pathAnnotation.value()); final String resourcePath = resourcePathSuffix.startsWith("/") ? pathPrefix + resourcePathSuffix : pathPrefix + "/" + resourcePathSuffix; return Resource .builder(resource) .path(resourcePath) .build(); }) .collect(Collectors.toSet()); }
private LocatorRouting createRouting(final Class<?> locatorClass) { Resource.Builder builder = Resource.builder(locatorClass, disableValidation); if (builder == null) { // resource is empty - do not throw 404, wait if ModelProcessors add any method builder = Resource.builder().name(locatorClass.getName()); } return buildRouting(builder.build()); }
/** * Creates a {@link Builder resource builder} instance from the list of {@code resource} which can be merged * into a single resource. It must be possible to merge the {@code resources} into a single valid resource. * For example all resources must have the same {@link Resource#getPath() path}, they cannot have ambiguous methods * on the same path, etc. * * @param resources Resources with the same path. * @return Resource builder initialized from merged resources. */ public static Builder builder(List<Resource> resources) { if (resources == null || resources.isEmpty()) { return builder(); } final Iterator<Resource> it = resources.iterator(); Resource.Data resourceData = it.next().data; Builder builder = Resource.builder(resourceData); String path = resourceData.path; while (it.hasNext()) { resourceData = it.next().data; if ((resourceData.path == null && path == null) || (path != null && path.equals(resourceData.path))) { builder.mergeWith(resourceData); } else { throw new IllegalArgumentException(LocalizationMessages.ERROR_RESOURCES_CANNOT_MERGE()); } } return builder; }
private void registerModel(Resource resourceModel) { final String path = resourceModel.getPath(); if (path != null) { Resource existing = rootResourceMap.get(path); if (existing != null) { // merge resources existing = Resource.builder(existing).mergeWith(resourceModel).build(); rootResourceMap.put(path, existing); } else { rootResourceMap.put(path, resourceModel); } } else { models.add(resourceModel); } }
private void registerModel(Resource resourceModel) { final String path = resourceModel.getPath(); if (path != null) { Resource existing = rootResourceMap.get(path); if (existing != null) { // merge resources existing = Resource.builder(existing).mergeWith(resourceModel).build(); rootResourceMap.put(path, existing); } else { rootResourceMap.put(path, resourceModel); } } else { models.add(resourceModel); } }
@Override public ResourceModel processResourceModel(ResourceModel model, Configuration config) { // Create new resource model. final ResourceModel.Builder resourceModelBuilder = new ResourceModel.Builder(false); for (final Resource resource : model.getResources()) { for (Class handlerClass : resource.getHandlerClasses()) { final String packageName = handlerClass.getPackage().getName(); final Optional<String> packagePrefix = packagePrefixes.entrySet().stream() .filter(entry -> packageName.startsWith(entry.getKey())) .sorted((o1, o2) -> -o1.getKey().compareTo(o2.getKey())) .map(Map.Entry::getValue) .findFirst(); if (packagePrefix.isPresent()) { final String prefixedPath = prefixPath(packagePrefix.get(), resource.getPath()); final Resource newResource = Resource.builder(resource) .path(prefixedPath) .build(); resourceModelBuilder.addResource(newResource); } else { resourceModelBuilder.addResource(resource); } } } return resourceModelBuilder.build(); }
/** * Create example application resource configuration. * * @return initialized resource configuration of the example application. */ public static ResourceConfig create() { final ResourceConfig resourceConfig = new ResourceConfig(JaxrsInjectionReportingResource.class); final Resource.Builder resourceBuilder = Resource.builder(ROOT_PATH_PROGRAMMATIC); resourceBuilder.addMethod("GET").handledBy(JaxrsInjectionReportingInflector.class); return resourceConfig.registerResources(resourceBuilder.build()); } }
@Override public ResourceModel processResourceModel(final ResourceModel resourceModel, final Configuration configuration) { final boolean disabled = PropertiesHelper.isProperty(configuration.getProperty(ServerProperties.WADL_FEATURE_DISABLE)); if (disabled) { return resourceModel; } final ResourceModel.Builder builder = ModelProcessorUtil.enhanceResourceModel(resourceModel, false, methodList, true); // Do not add WadlResource if already present in the classes (i.e. added during scanning). if (!configuration.getClasses().contains(WadlResource.class)) { final Resource wadlResource = Resource.builder(WadlResource.class).build(); builder.addResource(wadlResource); } return builder.build(); }
@Override public ResourceModel processResourceModel(final ResourceModel resourceModel, final Configuration configuration) { final boolean disabled = PropertiesHelper.isProperty(configuration.getProperty(ServerProperties.WADL_FEATURE_DISABLE)); if (disabled) { return resourceModel; } final ResourceModel.Builder builder = ModelProcessorUtil.enhanceResourceModel(resourceModel, false, methodList, true); // Do not add WadlResource if already present in the classes (i.e. added during scanning). if (!configuration.getClasses().contains(WadlResource.class)) { final Resource wadlResource = Resource.builder(WadlResource.class).build(); builder.addResource(wadlResource); } return builder.build(); }
/** * Create example application resource configuration. * * @return initialized resource configuration of the example application. */ public static ResourceConfig create() { final ResourceConfig resourceConfig = new ResourceConfig(TracingResource.class); final Resource.Builder resourceBuilder = Resource.builder(ROOT_PATH_PROGRAMMATIC); resourceBuilder.addMethod(TRACE.NAME).handledBy(new Inflector<ContainerRequestContext, Response>() { @Override public Response apply(ContainerRequestContext request) { if (request == null) { return Response.noContent().build(); } else { return Response.ok(Stringifier.stringify((ContainerRequest) request), MediaType.TEXT_PLAIN).build(); } } }); return resourceConfig.registerResources(resourceBuilder.build()); } }
@Override public ResourceModel processSubResource(ResourceModel subResource, Configuration configuration) { final Resource resource = Resource.builder() .mergeWith(Resource.from(EnhancedSubResourceSingleton.class)) .mergeWith(Resource.from(EnhancedSubResource.class)) .mergeWith(subResource.getResources().get(0)).build(); return new ResourceModel.Builder(true).addResource(resource).build(); } }
@Override public void visitResourceMethod(final ResourceMethod resourceMethod) { if (resourceMethod.isExtended()) { return; } if (ResourceMethod.JaxrsType.SUB_RESOURCE_LOCATOR.equals(resourceMethod.getType())) { if (resourceMethod.getInvocable() != null) { final Invocable i = resourceMethod.getInvocable(); final Type type = i.getResponseType(); final StringBuilder template = getTemplate(); mappings.put((Class) type, getMapping(template)); // Process sub resources ? Resource.Builder builder = Resource .builder(i.getRawResponseType()); if (builder == null) { // for example in the case the return type of the sub resource locator is Object builder = Resource.builder().path(resourceMethod.getParent().getPath()); } final Resource subResource = builder.build(); visitChildResource(subResource); } } processComponents(resourceMethod); }
ClipboardDataProvider.TextPlain.class); final Resource.Builder resourceBuilder = Resource.builder("echo"); ResourceMethod.Builder rmBuilder = resourceBuilder.addMethod("POST"); rmBuilder.consumes(jsonAndTextTypes).produces(jsonAndTextTypes)
private Resource processResource(Resource resource) { Resource.Builder resourceBuilder = Resource.builder(resource.getPath()); for (ResourceMethod resourceMethod : resource.getResourceMethods()) { ResourceMethod.Builder builder = resourceBuilder.addMethod(resourceMethod); if (resourceMethod.getInvocable().getHandlingMethod().isAnnotationPresent(Template.class)) { builder.routingResponseType(Viewable.class); } } if (resource.getResourceLocator() != null) { resourceBuilder.addMethod(resource.getResourceLocator()); } for (Resource child : resource.getChildResources()) { resourceBuilder.addChildResource(processResource(child)); } return resourceBuilder.build(); }
@Override public ResourceModel processResourceModel(ResourceModel resourceModel, Configuration configuration) { ResourceModel.Builder builder = new ResourceModel.Builder(resourceModel.getRootResources(), false); final Resource singletonResource = Resource.from(SingletonResource.class); builder.addResource(singletonResource); final Resource requestScopeResource = Resource.from(RequestScopeResource.class); builder.addResource(requestScopeResource); final Resource.Builder resourceBuilder = Resource.builder("instance"); resourceBuilder.addMethod("GET").handledBy(new Inflector<ContainerRequestContext, String>() { private int counter = 0; @Override public String apply(ContainerRequestContext containerRequestContext) { return String.valueOf("Inflector:" + counter++); } }); final Resource instanceResource = resourceBuilder.build(); builder.addResource(instanceResource); return builder.build(); }