/**
   * Evaluate the initial keys fromMap the configuration and applyChanges it to the field.
   *
   * @param target the target instance.
   * @throws ConfigException if evaluation or conversion failed.
   */
  public void applyValue(Object target, boolean resolve) throws ConfigException {
    String configValue = InjectionUtils.getConfigValue(this.setterMethod);
    Objects.requireNonNull(target);
    try {
      String evaluatedString =
          resolve && configValue != null ? InjectionUtils.evaluateValue(configValue) : configValue;

      // Check for adapter/filter
      Object value =
          InjectionUtils.adaptValue(
              this.setterMethod,
              TypeLiteral.of(this.setterMethod.getParameterTypes()[0]),
              evaluatedString);

      AccessController.doPrivileged(
          new PrivilegedExceptionAction<Object>() {
            @Override
            public Object run() throws Exception {
              setterMethod.setAccessible(true);
              return setterMethod;
            }
          });

      setterMethod.invoke(target, value);
    } catch (Exception e) {
      throw new ConfigException(
          "Failed to annotation configured method: "
              + this.setterMethod.getDeclaringClass().getName()
              + '.'
              + setterMethod.getName(),
          e);
    }
  }
Example #2
0
 public static Object[] createConstructorArguments(
     Constructor<?> c, Message m, boolean perRequest, Map<Class<?>, Object> contextValues) {
   Class<?>[] params = c.getParameterTypes();
   Annotation[][] anns = c.getParameterAnnotations();
   Type[] genericTypes = c.getGenericParameterTypes();
   @SuppressWarnings("unchecked")
   MultivaluedMap<String, String> templateValues =
       m == null ? null : (MultivaluedMap<String, String>) m.get(URITemplate.TEMPLATE_PARAMETERS);
   Object[] values = new Object[params.length];
   for (int i = 0; i < params.length; i++) {
     if (AnnotationUtils.getAnnotation(anns[i], Context.class) != null) {
       Object contextValue = contextValues != null ? contextValues.get(params[i]) : null;
       if (contextValue == null) {
         if (perRequest) {
           values[i] = JAXRSUtils.createContextValue(m, genericTypes[i], params[i]);
         } else {
           values[i] = InjectionUtils.createThreadLocalProxy(params[i]);
         }
       } else {
         values[i] = contextValue;
       }
     } else {
       // this branch won't execute for singletons given that the found constructor
       // is guaranteed to have only Context parameters, if any, for singletons
       Parameter p = ResourceUtils.getParameter(i, anns[i], params[i]);
       values[i] =
           JAXRSUtils.createHttpParameterValue(
               p, params[i], genericTypes[i], anns[i], m, templateValues, null);
     }
   }
   return values;
 }
Example #3
0
  private static void checkJaxbType(
      Class<?> serviceClass,
      Class<?> type,
      Type genericType,
      ResourceTypes types,
      Annotation[] anns,
      MessageBodyWriter<?> jaxbWriter) {
    boolean isCollection = false;
    if (InjectionUtils.isSupportedCollectionOrArray(type)) {
      type = InjectionUtils.getActualType(genericType);
      isCollection = true;
    }
    if (type == Object.class && !(genericType instanceof Class)) {
      Type theType =
          InjectionUtils.processGenericTypeIfNeeded(serviceClass, Object.class, genericType);
      type = InjectionUtils.getActualType(theType);
    }
    if (type == null
        || InjectionUtils.isPrimitive(type)
        || JAXBElement.class.isAssignableFrom(type)
        || Response.class.isAssignableFrom(type)
        || type.isInterface()) {
      return;
    }

    MessageBodyWriter<?> writer = jaxbWriter;
    if (writer == null) {
      JAXBElementProvider<Object> defaultWriter = new JAXBElementProvider<Object>();
      defaultWriter.setMarshallAsJaxbElement(true);
      defaultWriter.setXmlTypeAsJaxbElementOnly(true);
      writer = defaultWriter;
    }
    if (writer.isWriteable(type, type, anns, MediaType.APPLICATION_XML_TYPE)) {
      types.getAllTypes().put(type, type);
      Class<?> genCls = InjectionUtils.getActualType(genericType);
      if (genCls != type
          && genCls != null
          && genCls != Object.class
          && !InjectionUtils.isSupportedCollectionOrArray(genCls)) {
        types.getAllTypes().put(genCls, genCls);
      }

      XMLName name = AnnotationUtils.getAnnotation(anns, XMLName.class);
      QName qname = name != null ? JAXRSUtils.convertStringToQName(name.value()) : null;
      if (isCollection) {
        types.getCollectionMap().put(type, qname);
      } else {
        types.getXmlNameMap().put(type, qname);
      }
    }
  }
 /**
  * Access the applyable configuration keys for this field.
  *
  * @return the configuration keys, never null.
  */
 public Collection<String> getConfiguredKeys() {
   return InjectionUtils.getKeys(this.setterMethod);
 }