Java Code Examples for javax.ws.rs.ext.ContextResolver#getContext()

The following examples show how to use javax.ws.rs.ext.ContextResolver#getContext() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractJAXBProvider.java    From cxf with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public JAXBContext getJAXBContext(Class<?> type, Type genericType) throws JAXBException {
    if (mc != null) {
        ContextResolver<JAXBContext> resolver =
            mc.getResolver(ContextResolver.class, JAXBContext.class);
        if (resolver != null) {
            JAXBContext customContext = resolver.getContext(type);
            if (customContext != null) {
                return customContext;
            }
        }
    }

    JAXBContext context = classContexts.get(type);
    if (context != null) {
        return context;
    }

    context = getPackageContext(type, genericType);

    return context != null ? context : getClassContext(type, genericType);
}
 
Example 2
Source File: ConstraintViolationExceptionMapper.java    From microservices-springboot with MIT License 5 votes vote down vote up
/**
 * Handle an invalid property. Can be:
 * <p>
 * 1. Invalid request parameter (annotated bean param field or annotated resource class field)
 * 2. Invalid request entity property (annotated bean param field)
 *
 * @param constraintViolation
 */
private ValidationError handleInvalidProperty(ConstraintViolation constraintViolation) {

    Path.Node leafNode = getLeafNode(constraintViolation.getPropertyPath()).get();
    Class<?> beanClass = constraintViolation.getLeafBean().getClass();

    // Can be an invalid request parameter (annotated bean param field or annotated resource class field)
    Optional<Field> optionalField = getField(leafNode.getName(), beanClass);
    if (optionalField.isPresent()) {
        Optional<ParameterDetails> optionalParameterDetails = getParameterDetails(optionalField.get().getAnnotations());
        if (optionalParameterDetails.isPresent()) {
            return createErrorForParameter(optionalParameterDetails.get(), constraintViolation);
        }
    }

    // Get Jackson ObjectMapper
    ContextResolver<ObjectMapper> resolver = providers.getContextResolver(ObjectMapper.class, MediaType.WILDCARD_TYPE);
    ObjectMapper mapper = resolver.getContext(ObjectMapper.class);

    // Can be an invalid request entity property (annotated bean param field)
    Optional<String> optionalJsonProperty = getJsonPropertyName(mapper, beanClass, leafNode.getName());
    if (optionalJsonProperty.isPresent()) {
        ValidationError error = new ValidationError();
        error.setType(REQUEST_ENTITY_PROPERTY);
        error.setName(optionalJsonProperty.get());
        error.setMessage(constraintViolation.getMessage());
        return error;
    }

    return handleUnknownSource(constraintViolation);
}
 
Example 3
Source File: ProviderFactory.java    From cxf with Apache License 2.0 5 votes vote down vote up
public T getContext(Class<?> cls) {
    for (ContextResolver<T> resolver : candidates) {
        T context = resolver.getContext(cls);
        if (context != null) {
            return context;
        }
    }
    return null;
}
 
Example 4
Source File: MetadataResource.java    From mrgeo with Apache License 2.0 5 votes vote down vote up
private void getService()
{
  if (service == null)
  {
    ContextResolver<MrsPyramidService> resolver =
        providers.getContextResolver(MrsPyramidService.class, MediaType.WILDCARD_TYPE);
    if (resolver != null)
    {
      service = resolver.getContext(MrsPyramidService.class);
    }
  }
}
 
Example 5
Source File: AsynchronousJobService.java    From everrest with Eclipse Public License 2.0 5 votes vote down vote up
private AsynchronousJobPool getJobPool() {
    if (providers != null) {
        ContextResolver<AsynchronousJobPool> asyncJobsResolver = providers.getContextResolver(AsynchronousJobPool.class, null);
        if (asyncJobsResolver != null) {
            return asyncJobsResolver.getContext(null);
        }
    }
    throw new IllegalStateException("Asynchronous jobs feature is not configured properly. ");
}
 
Example 6
Source File: VersionResource.java    From mrgeo with Apache License 2.0 5 votes vote down vote up
private void getService()
{
  if (service == null)
  {
    ContextResolver<VersionService> resolver =
        providers.getContextResolver(VersionService.class, MediaType.WILDCARD_TYPE);
    service = resolver.getContext(VersionService.class);
  }
}
 
Example 7
Source File: XSLTTransformationTest.java    From everrest with Eclipse Public License 2.0 5 votes vote down vote up
@GET
public Response m0() {
    ContextResolver<XSLTTemplatesContextResolver> resolverWrapper = providers.getContextResolver(XSLTTemplatesContextResolver.class, APPLICATION_XML_TYPE);
    XSLTTemplatesContextResolver resolver = resolverWrapper.getContext(null);
    return Response.ok()
                   .type(TEXT_HTML_TYPE)
                   .entity(new XSLTStreamingOutput(loadSource("book-in.xml"), resolver.getTemplates("template1")))
                   .build();
}
 
Example 8
Source File: ApplicationContext.java    From everrest with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @param methodDescriptor
 *         method descriptor
 * @return invoker that must be used for processing methods
 */
public MethodInvoker getMethodInvoker(GenericResourceMethod methodDescriptor) {
    String method = request.getMethod();
    if ("OPTIONS".equals(method) && methodDescriptor.getMethod() == null) {
        // GenericMethodResource.getMethod() always return null if method for
        // "OPTIONS" request was not described in source code of service. In
        // this case we provide mechanism for "fake" method invoking.
        return new OptionsRequestMethodInvoker(new WadlProcessor());
    }
    MethodInvoker invoker = null;
    // Never use AsynchronousMethodInvoker for process SubResourceLocatorDescriptor.
    // Locators can't be processed in asynchronous mode since it is not end point of request.
    if (isAsynchronous() && methodDescriptor instanceof ResourceMethodDescriptor) {
        ContextResolver<AsynchronousJobPool> asyncJobsResolver = getProviders().getContextResolver(AsynchronousJobPool.class, null);
        if (asyncJobsResolver == null) {
            throw new IllegalStateException("Asynchronous jobs feature is not configured properly. ");
        }
        invoker = new AsynchronousMethodInvoker(asyncJobsResolver.getContext(null), new ParameterResolverFactory());
    }
    if (invoker == null) {
        invoker = new DefaultMethodInvoker(new ParameterResolverFactory());
    }
    if (methodInvokerDecoratorFactory != null) {
        return methodInvokerDecoratorFactory.makeDecorator(invoker);
    }
    return invoker;
}
 
Example 9
Source File: MattermostModelMapperProviderTest.java    From mattermost4j with Apache License 2.0 5 votes vote down vote up
@Test
public void ignoreUnknownPropertyCorrectly()
    throws JsonParseException, JsonMappingException, IOException {
  ContextResolver<ObjectMapper> provider = new MattermostModelMapperProvider(true);
  ObjectMapper objectMapper = provider.getContext(ObjectMapper.class);

  User user = objectMapper.readValue(JSON_INCLUDE_UNKNOWN_PROPERTY, User.class);
  assertNotNull(user);
}
 
Example 10
Source File: ColorScaleResource.java    From mrgeo with Apache License 2.0 5 votes vote down vote up
private void getService()
{
  if (service == null)
  {
    ContextResolver<MrsPyramidService> resolver =
        providers.getContextResolver(MrsPyramidService.class, MediaType.WILDCARD_TYPE);
    if (resolver != null)
    {
      service = resolver.getContext(MrsPyramidService.class);
    }
  }
}
 
Example 11
Source File: UIResource.java    From exhibitor with Apache License 2.0 4 votes vote down vote up
public UIResource(@Context ContextResolver< UIContext > resolver)
{
    context = resolver.getContext(UIContext.class);
    tabs = buildTabs();
}
 
Example 12
Source File: ExceptionMapperBase.java    From Processor with Apache License 2.0 4 votes vote down vote up
public MediaTypes getMediaTypes()
{
    ContextResolver<MediaTypes> cr = getProviders().getContextResolver(MediaTypes.class, null);
    return cr.getContext(MediaTypes.class);
}
 
Example 13
Source File: ExceptionMapperBase.java    From Processor with Apache License 2.0 4 votes vote down vote up
public TemplateCall getTemplateCall()
{
    ContextResolver<TemplateCall> cr = getProviders().getContextResolver(TemplateCall.class, null);
    return cr.getContext(TemplateCall.class);
}
 
Example 14
Source File: ExceptionMapperBase.java    From Processor with Apache License 2.0 4 votes vote down vote up
public Ontology getOntology()
{
    ContextResolver<Ontology> cr = getProviders().getContextResolver(Ontology.class, null);
    return cr.getContext(Ontology.class);
}
 
Example 15
Source File: ValidatingModelProvider.java    From Processor with Apache License 2.0 4 votes vote down vote up
public Ontology getOntology()
{
    ContextResolver<Ontology> cr = getProviders().getContextResolver(Ontology.class, null);
    return cr.getContext(Ontology.class);
}
 
Example 16
Source File: ValidatingDatasetProvider.java    From Processor with Apache License 2.0 4 votes vote down vote up
public Ontology getOntology()
{
    ContextResolver<Ontology> cr = getProviders().getContextResolver(Ontology.class, null);
    return cr.getContext(Ontology.class);
}
 
Example 17
Source File: IndexResource.java    From exhibitor with Apache License 2.0 4 votes vote down vote up
public IndexResource(@Context ContextResolver<UIContext> resolver)
{
    context = resolver.getContext(UIContext.class);
}
 
Example 18
Source File: ClusterResource.java    From exhibitor with Apache License 2.0 4 votes vote down vote up
public ClusterResource(@Context ContextResolver<UIContext> resolver)
{
    context = resolver.getContext(UIContext.class);
}
 
Example 19
Source File: JAXBObjectEntityProvider.java    From everrest with Eclipse Public License 2.0 3 votes vote down vote up
/**
 * @param type
 *         type
 * @param mediaType
 *         media type
 * @return JAXBContext JAXBContext
 * @throws JAXBException
 *         if JAXBContext creation failed
 */
protected JAXBContext getJAXBContext(Class<?> type, MediaType mediaType) throws JAXBException {
    ContextResolver<JAXBContextResolver> resolver = providers.getContextResolver(JAXBContextResolver.class, mediaType);
    if (resolver == null) {
        throw new RuntimeException(String.format("Not found any JAXBContextResolver for media type %s", mediaType));
    }
    JAXBContextResolver jaxbContextResolver = resolver.getContext(null);
    return jaxbContextResolver.getJAXBContext(type);
}
 
Example 20
Source File: JAXBElementEntityProvider.java    From everrest with Eclipse Public License 2.0 3 votes vote down vote up
/**
 * @param type
 *         type
 * @param mediaType
 *         media type
 * @return JAXBContext JAXBContext
 * @throws JAXBException
 *         if JAXBContext creation failed
 */
protected JAXBContext getJAXBContext(Class<?> type, MediaType mediaType) throws JAXBException {
    ContextResolver<JAXBContextResolver> resolver = providers.getContextResolver(JAXBContextResolver.class, mediaType);
    if (resolver == null) {
        throw new RuntimeException(String.format("Not found any JAXBContextResolver for media type %s", mediaType));
    }
    JAXBContextResolver jaxbContextResolver = resolver.getContext(null);
    return jaxbContextResolver.getJAXBContext(type);
}