@Override
  public PropertyName findNameForDeserialization(Annotated a) {
    String name;

    // @JsonSetter has precedence over @JsonProperty, being more specific
    // @JsonDeserialize implies that there is a property, but no name
    JsonSetter js = _findAnnotation(a, JsonSetter.class);
    if (js != null) {
      name = js.value();
    } else {
      JsonProperty pann = _findAnnotation(a, JsonProperty.class);
      if (pann != null) {
        name = pann.value();
        /* 22-Apr-2014, tatu: Should figure out a better way to do this, but
         *   it's actually bit tricky to do it more efficiently (meta-annotations
         *   add more lookups; AnnotationMap costs etc)
         */
      } else if (_hasAnnotation(a, JsonDeserialize.class)
          || _hasAnnotation(a, JsonView.class)
          || _hasAnnotation(a, JsonUnwrapped.class) // [#442]
          || _hasAnnotation(a, JsonBackReference.class)
          || _hasAnnotation(a, JsonManagedReference.class)) {
        name = "";
      } else {
        return null;
      }
    }
    return PropertyName.construct(name);
  }
  protected BeanPropertyWriter _constructVirtualProperty(
      JsonAppend.Prop prop, MapperConfig<?> config, AnnotatedClass ac) {
    PropertyMetadata metadata =
        prop.required() ? PropertyMetadata.STD_REQUIRED : PropertyMetadata.STD_OPTIONAL;
    PropertyName propName = _propertyName(prop.name(), prop.namespace());
    JavaType type = config.constructType(prop.type());
    // now, then, we need a placeholder for member (no real Field/Method):
    AnnotatedMember member =
        new VirtualAnnotatedMember(
            ac, ac.getRawType(), propName.getSimpleName(), type.getRawClass());
    // and with that and property definition
    SimpleBeanPropertyDefinition propDef =
        SimpleBeanPropertyDefinition.construct(config, member, propName, metadata, prop.include());

    Class<?> implClass = prop.value();

    HandlerInstantiator hi = config.getHandlerInstantiator();
    VirtualBeanPropertyWriter bpw =
        (hi == null) ? null : hi.virtualPropertyWriterInstance(config, implClass);
    if (bpw == null) {
      bpw =
          (VirtualBeanPropertyWriter)
              ClassUtil.createInstance(implClass, config.canOverrideAccessModifiers());
    }

    // one more thing: give it necessary contextual information
    return bpw.withConfig(config, ac, propDef, type);
  }
 protected PropertyName _propertyName(String localName, String namespace) {
   if (localName.isEmpty()) {
     return PropertyName.USE_DEFAULT;
   }
   if (namespace == null || namespace.isEmpty()) {
     return PropertyName.construct(localName);
   }
   return PropertyName.construct(localName, namespace);
 }
  /**
   * Additional simple tests to ensure we will retain basic namespace information now that it can be
   * included
   *
   * @since 2.1
   */
  public void testNamespaces() throws Exception {
    JaxbAnnotationIntrospector ai = new JaxbAnnotationIntrospector(TypeFactory.defaultInstance());
    AnnotatedClass ac = AnnotatedClass.construct(NamespaceBean.class, ai, null);
    AnnotatedField af = _findField(ac, "string");
    assertNotNull(af);
    PropertyName pn = ai.findNameForDeserialization(af);
    assertNotNull(pn);

    // JAXB seems to assert field name instead of giving "use default"...
    assertEquals("", pn.getSimpleName());
    assertEquals("urn:method", pn.getNamespace());
  }
  protected SettableBeanProperty(
      PropertyName propName,
      JavaType type,
      PropertyName wrapper,
      TypeDeserializer typeDeser,
      Annotations contextAnnotations,
      PropertyMetadata metadata) {
    // 09-Jan-2009, tatu: Intern()ing makes sense since Jackson parsed
    //  field names are (usually) interned too, hence lookups will be faster.
    // 23-Oct-2009, tatu: should this be disabled wrt [JACKSON-180]?
    //   Probably need not, given that namespace of field/method names
    //   is not unbounded, unlike potential JSON names.
    if (propName == null) {
      _propName = PropertyName.NO_NAME;
    } else {
      _propName = propName.internSimpleName();
    }
    _type = type;
    _wrapperName = wrapper;
    _metadata = metadata;
    _contextAnnotations = contextAnnotations;
    _viewMatcher = null;
    _nullProvider = null;

    // 30-Jan-2012, tatu: Important: contextualize TypeDeserializer now...
    if (typeDeser != null) {
      typeDeser = typeDeser.forProperty(this);
    }
    _valueTypeDeserializer = typeDeser;
    _valueDeserializer = MISSING_VALUE_DESERIALIZER;
  }
  public void testRootNameAccess() throws Exception {
    AnnotationIntrospector ai = new JaxbAnnotationIntrospector(TypeFactory.defaultInstance());
    // If no @XmlRootElement, should get null (unless pkg has etc)
    assertNull(ai.findRootName(AnnotatedClass.construct(SimpleBean.class, ai, null)));
    // With @XmlRootElement, but no name, empty String
    PropertyName rootName =
        ai.findRootName(AnnotatedClass.construct(NamespaceBean.class, ai, null));
    assertNotNull(rootName);
    assertEquals("", rootName.getSimpleName());
    assertEquals("urn:class", rootName.getNamespace());

    // and otherwise explicit name
    rootName = ai.findRootName(AnnotatedClass.construct(RootNameBean.class, ai, null));
    assertNotNull(rootName);
    assertEquals("test", rootName.getSimpleName());
    assertNull(rootName.getNamespace());
  }
 @Override
 public ObjectIdInfo findObjectIdInfo(Annotated ann) {
   JsonIdentityInfo info = _findAnnotation(ann, JsonIdentityInfo.class);
   if (info == null || info.generator() == ObjectIdGenerators.None.class) {
     return null;
   }
   // In future may need to allow passing namespace?
   PropertyName name = PropertyName.construct(info.property());
   return new ObjectIdInfo(name, info.scope(), info.generator(), info.resolver());
 }
  protected BeanPropertyWriter _constructVirtualProperty(
      JsonAppend.Attr attr, MapperConfig<?> config, AnnotatedClass ac, JavaType type) {
    PropertyMetadata metadata =
        attr.required() ? PropertyMetadata.STD_REQUIRED : PropertyMetadata.STD_OPTIONAL;
    // could add Index, Description in future, if those matter
    String attrName = attr.value();

    // allow explicit renaming; if none, default to attribute name
    PropertyName propName = _propertyName(attr.propName(), attr.propNamespace());
    if (!propName.hasSimpleName()) {
      propName = PropertyName.construct(attrName);
    }
    // now, then, we need a placeholder for member (no real Field/Method):
    AnnotatedMember member =
        new VirtualAnnotatedMember(ac, ac.getRawType(), attrName, type.getRawClass());
    // and with that and property definition
    SimpleBeanPropertyDefinition propDef =
        SimpleBeanPropertyDefinition.construct(config, member, propName, metadata, attr.include());
    // can construct the property writer
    return AttributePropertyWriter.construct(attrName, propDef, ac.getAnnotations(), type);
  }
 @Override
 public PropertyName findRootName(AnnotatedClass ac) {
   JsonRootName ann = _findAnnotation(ac, JsonRootName.class);
   if (ann == null) {
     return null;
   }
   String ns = ann.namespace();
   if (ns != null && ns.length() == 0) {
     ns = null;
   }
   return PropertyName.construct(ann.value(), ns);
 }
 /**
  * Constructor only used by {@link ObjectIdValueProperty}.
  *
  * @since 2.3
  */
 protected SettableBeanProperty(
     PropertyName propName,
     JavaType type,
     PropertyMetadata metadata,
     JsonDeserializer<Object> valueDeser) {
   // as with above ctor, intern()ing probably fine
   if (propName == null) {
     _propName = PropertyName.NO_NAME;
   } else {
     _propName = propName.internSimpleName();
   }
   _type = type;
   _wrapperName = null;
   _metadata = metadata;
   _contextAnnotations = null;
   _viewMatcher = null;
   _nullProvider = null;
   _valueTypeDeserializer = null;
   _valueDeserializer = valueDeser;
 }
  @Override
  public PropertyName findNameForSerialization(Annotated a) {
    String name = null;

    JsonGetter jg = _findAnnotation(a, JsonGetter.class);
    if (jg != null) {
      name = jg.value();
    } else {
      JsonProperty pann = _findAnnotation(a, JsonProperty.class);
      if (pann != null) {
        name = pann.value();
      } else if (_hasAnnotation(a, JsonSerialize.class)
          || _hasAnnotation(a, JsonView.class)
          || _hasAnnotation(a, JsonRawValue.class)) {
        name = "";
      } else {
        return null;
      }
    }
    return PropertyName.construct(name);
  }
Exemplo n.º 12
0
 public static int getPropertyInt(PropertyName key) {
   return Integer.parseInt(finals.getProperty(key.name()));
 }
Exemplo n.º 13
0
 public static String getProperty(PropertyName key) {
   return finals.getProperty(key.name());
 }
Exemplo n.º 14
0
 public Object getPropertyValue(Object target, String propertyName) {
   return getProperty((Source) target, PropertyName.valueOf(propertyName));
 }
  /**
   * Edit the properties of a resource. The updates must refer to a Document containing a WebDAV
   * propertyupdates element as the document root.
   *
   * @param updates an XML Document containing propertyupdate elements
   * @return the result of making the updates describing the edits to be made.
   * @exception com.ibm.webdav.WebDAVException
   */
  public MultiStatus setProperties(Document propertyUpdates) throws WebDAVException {
    // create a MultiStatus to hold the results. It will hold a MethodResponse
    // for each update, and one for the method as a whole
    MultiStatus multiStatus = new MultiStatus();
    boolean errorsOccurred = false;

    // first, load the properties so they can be edited
    Document propertiesDocument = resource.loadProperties();
    Element properties = (Element) propertiesDocument.getDocumentElement();

    // be sure the updates have at least one update
    Element propertyupdate = (Element) propertyUpdates.getDocumentElement();
    String tagName = propertyupdate.getNamespaceURI() + propertyupdate.getLocalName();

    if (!tagName.equals("DAV:propertyupdate")) {
      throw new WebDAVException(
          WebDAVStatus.SC_UNPROCESSABLE_ENTITY, "missing propertyupdate element");
    }

    NodeList updates = propertyupdate.getChildNodes();

    if (updates.getLength() == 0) {
      throw new WebDAVException(WebDAVStatus.SC_UNPROCESSABLE_ENTITY, "no updates in request");
    }

    Vector propsGood = new Vector(); // a list of properties that

    // were patched correctly or would have been if another
    // property hadn't gone bad.
    // apply the updates
    Node temp = null;

    for (int i = 0; i < updates.getLength(); i++) {
      temp = updates.item(i);

      // skip any ignorable TXText elements
      if (!(temp.getNodeType() == Node.ELEMENT_NODE)) {
        continue;
      }

      Element update = (Element) temp;
      int updateCommand = -1;
      tagName = update.getNamespaceURI() + update.getLocalName();

      if (tagName.equals("DAV:set")) {
        updateCommand = set;
      } else if (tagName.equals("DAV:remove")) {
        updateCommand = remove;
      } else {
        throw new WebDAVException(
            WebDAVStatus.SC_UNPROCESSABLE_ENTITY,
            update.getTagName() + " is not a valid property update request");
      }

      // iterate through the props in the set or remove element and update the
      // properties as directed
      Element prop = (Element) update.getElementsByTagNameNS("DAV:", "prop").item(0);

      if (prop == null) {
        throw new WebDAVException(
            WebDAVStatus.SC_UNPROCESSABLE_ENTITY, "no propeprties in update request");
      }

      NodeList propsToUpdate = prop.getChildNodes();

      for (int j = 0; j < propsToUpdate.getLength(); j++) {
        temp = propsToUpdate.item(j);

        // skip any TXText elements??
        if (!(temp.getNodeType() == Node.ELEMENT_NODE)) {
          continue;
        }

        Element propToUpdate = (Element) temp;

        // find the property in the properties element
        Element property = null;
        PropertyName propertyName = new PropertyName(propToUpdate);

        if (((Element) propToUpdate).getNamespaceURI() != null) {
          property =
              (Element)
                  properties
                      .getElementsByTagNameNS(
                          propToUpdate.getNamespaceURI(), propToUpdate.getLocalName())
                      .item(0);
        } else {
          property = (Element) properties.getElementsByTagName(propToUpdate.getTagName()).item(0);
        }

        boolean liveone = isLive(propertyName.asExpandedString());

        if (liveone) {
          errorsOccurred = true;

          PropertyResponse response = new PropertyResponse(resource.getURL().toString());
          response.addProperty(propertyName, propToUpdate, WebDAVStatus.SC_FORBIDDEN);
          multiStatus.addResponse(response);
        }

        // do the update
        if (updateCommand == set) {
          if (property != null) {
            try {
              properties.removeChild(property);
            } catch (DOMException exc) {
            }
          }

          if (!liveone) {
            // I don't think we're allowed to update live properties
            //    here.  Doing so effects the cache.  A case in
            //    point is the lockdiscoveryproperty.  properties
            //    is actually the properites cache "document" of this
            //    resource.  Even though we don't "save" the request
            //    if it includes live properties, we don't remove
            //    it from the cache after we'd set it here, so it
            //    can affect other queries. (jlc 991002)
            properties.appendChild(propertiesDocument.importNode(propToUpdate, true));

            propsGood.addElement(propToUpdate);
          }
        } else if (updateCommand == remove) {
          try {
            if (property != null) {
              properties.removeChild(property);
              propsGood.addElement(propToUpdate);
            }
          } catch (DOMException exc) {
          }
        }
      }
    }

    {
      Enumeration els = propsGood.elements();

      for (; els.hasMoreElements(); ) {
        Object ob1 = els.nextElement();
        Element elProp = (Element) ob1;
        PropertyName pn = new PropertyName(elProp);
        PropertyResponse response = new PropertyResponse(resource.getURL().toString());
        response.addProperty(
            pn,
            (Element) elProp.cloneNode(false),
            (errorsOccurred ? WebDAVStatus.SC_FAILED_DEPENDENCY : WebDAVStatus.SC_OK));

        // todo: add code for responsedescription
        multiStatus.addResponse(response);
      }
    }

    // write out the properties
    if (!errorsOccurred) {
      resource.saveProperties(propertiesDocument);
    }

    return multiStatus;
  }
 /** @since 2.3 */
 public SettableBeanProperty withSimpleName(String simpleName) {
   PropertyName n =
       (_propName == null) ? new PropertyName(simpleName) : _propName.withSimpleName(simpleName);
   return (n == _propName) ? this : withName(n);
 }
 @Override
 public final String getName() {
   return _propName.getSimpleName();
 }