/**
   * Set a property of a resource to a value.
   *
   * @param name the property name
   * @param value the property value
   * @exception com.ibm.webdav.WebDAVException
   */
  public void setProperty(String name, Element value) throws WebDAVException {
    // load the properties
    Document propertiesDocument = resource.loadProperties();
    Element properties = propertiesDocument.getDocumentElement();
    String ns = value.getNamespaceURI();

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

    if (property != null) {
      try {
        properties.removeChild(property);
      } catch (DOMException exc) {
      }
    }

    properties.appendChild(propertiesDocument.importNode(value, true));

    // write out the properties
    resource.saveProperties(propertiesDocument);
  }
Esempio n. 2
0
 public Resource getResource(String id) {
   for (Iterator i = getResourceList().iterator(); i.hasNext(); ) {
     ResourceImpl resource = (ResourceImpl) i.next();
     if (resource.getId().equals(id)) return resource;
   }
   return null;
 }
  /**
   * Get the named properties for this resource and (potentially) its children.
   *
   * @param names an arrary of property names to retrieve
   * @return a MultiStatus of PropertyResponses
   * @exception com.ibm.webdav.WebDAVException
   */
  public MultiStatus getProperties(PropertyName[] names) throws WebDAVException {
    MultiStatus multiStatus = resource.getProperties(resource.getContext());
    MultiStatus newMultiStatus = new MultiStatus();

    Enumeration responses = multiStatus.getResponses();

    while (responses.hasMoreElements()) {
      PropertyResponse response = (PropertyResponse) responses.nextElement();
      PropertyResponse newResponse = new PropertyResponse(response.getResource());
      newResponse.setDescription(response.getDescription());
      newMultiStatus.addResponse(newResponse);

      Hashtable properties = (Hashtable) response.getPropertiesByPropName();

      // Hashtable newProperties = (Hashtable) newResponse.getProperties();
      for (int i = 0; i < names.length; i++) {
        if (properties.containsKey(names[i])) {
          PropertyValue srcval = response.getProperty(names[i]);
          newResponse.setProperty(names[i], srcval);

          // newProperties.put(names[i], properties.get(names[i]));
        } else {
          Document factory = null;

          try {
            factory = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
          } catch (Exception e) {
            throw new WebDAVException(WebDAVStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
          }

          // we'll create an xml element with no value because that's
          //    what webdav will need to return for most methods... even if the
          //    property doesn't exist.   That's because the
          //    distinction between a propertyname and propertyvalue
          //    is fuzzy in WebDAV xml. A property name is
          //    essentially an empty property value because
          //    all property values have their property
          //    name stuck on.
          // if we decide to set reviewStatus to null instead (as
          //    we did previously, then the code for MultiStatus.asXML()
          //    needs to be updated to expect null values.
          // (jlc 990520)
          Element elTm = factory.createElementNS("X", "X:" + names[i].getLocal());

          elTm.setAttribute("xmlns:X", names[i].getNamespace());

          newResponse.addProperty(names[i], elTm, WebDAVStatus.SC_NOT_FOUND);
        }
      }
    }

    return newMultiStatus;
  }
  /**
   * Get all the properties of this resource. This implementation stores properties in an XML
   * document containing a properties root element. The properties file name is derived from the URI
   * by adding the extension PropertiesManager.propertiesSuffix. This applies to collections as well
   * as other resources.
   *
   * <p>Since the properties are stored in a file, and all methods that query and update the
   * properties must read the XML file into memory and parse it, many of the other property methods
   * are implemented by calling this method. Subclasses of ResourceImpl may want to use other
   * techniques depending on how the properties are stored.
   *
   * @return a MultiStatus containing response elements with prop elements containing the properties
   *     and their statuses.
   * @exception com.ibm.webdav.WebDAVException
   */
  public MultiStatus getProperties() throws WebDAVException {
    Document propertiesDocument = resource.loadProperties();

    // create a MultiStatus to hold the results
    MultiStatus results = new MultiStatus();

    // create a response element to hold the properties for this resource
    PropertyResponse response = null;
    String urlstring;

    if (false) {
      // I consider this to be the more correct way and the
      //    way used in the examples in the spec... but it is
      //    redundant and creates the possibility of the two
      //    redundant parts being out of synch.
      urlstring = resource.getURL().toString();
    } else {
      // this is the way that mod_dav and a few others do it. This
      //    way also makes it easier to debug clients even if
      //    redirecting through a dedicated proxy.  Without this
      //    it's inconvenient to debug IE5.  It gets confused if
      //    the host:port (if provided) don't match who it thinks
      //    it's connecting to.
      urlstring = resource.getURL().getFile();
    }

    response = new PropertyResponse(urlstring);

    // add the properties to the response
    NodeList properties = propertiesDocument.getDocumentElement().getChildNodes();
    Node temp = null;

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

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

      Element property = (Element) temp;
      PropertyName pn = new PropertyName(property);
      response.addProperty(pn, property, WebDAVStatus.SC_OK);
    }

    results.addResponse(response);

    return results;
  }
  /**
   * Get the names of all properties for this resource. This implementation reads all the properties
   * and then extracts their names.
   *
   * @return a MultiStatus of PropertyResponses (PropertyValue.value is always null,
   *     PropertyValue.status contains the status)
   * @exception com.ibm.webdav.WebDAVException
   */
  public MultiStatus getPropertyNames() throws WebDAVException {
    MultiStatus multiStatus = resource.getProperties(resource.getContext());
    Enumeration responses = multiStatus.getResponses();

    // we have the result, but all of the properties in our structure contain
    //    values.  We don't want to include values.  Just names.  The following
    //    code strips out the content of these elements.
    while (responses.hasMoreElements()) {
      PropertyResponse response = (PropertyResponse) responses.nextElement();
      Dictionary properties = response.getPropertiesByPropName();
      Enumeration keys = properties.keys();

      while (keys.hasMoreElements()) {
        PropertyName key = (PropertyName) keys.nextElement();
        Element value = (Element) response.getProperty(key).getValue();
        response.setProperty(
            key, new PropertyValue((Element) value.cloneNode(false), WebDAVStatus.SC_OK));
      }
    }

    return multiStatus;
  }
Esempio n. 6
0
  /**
   * Parse an old style OBR repository.
   *
   * <p><dtd-version>1.0</dtd-version> <repository> <name>Oscar Bundle Repository</name>
   * <url>http://oscar-osgi.sourceforge.net/</url> <date>Fri May 07 16:45:07 CEST 2004</date>
   * <extern-repositories>
   * <!--
   * Stefano Lenzi ([email protected]) -->
   * <url>http://domoware.isti.cnr.it/osgi-obr/niche-osgi-obr.xml</url>
   * <!--Manuel Palencia ([email protected]) -->
   * <!--
   * <url>http://jmood.forge.os4os.org/repository.xml</url> -->
   * <!-- Enrique
   * Rodriguez ([email protected]) -->
   * <url>http://update.cainenable.org/repository.xml</url> </extern-repositories> </repository>
   * <bundle> <bundle-name>Bundle Repository</bundle-name> <bundle-description> A bundle repository
   * service for Oscar. </bundle-description> <bundle-updatelocation>
   * http://oscar-osgi.sf.net/repo/bundlerepository/bundlerepository.jar </bundle-updatelocation>
   * <bundle-sourceurl> http://oscar-osgi.sf.net/repo/bundlerepository/bundlerepository-src.jar
   * </bundle-sourceurl> <bundle-version>1.1.3</bundle-version> <bundle-docurl>
   * http://oscar-osgi.sf.net/repo/bundlerepository/ </bundle-docurl>
   * <bundle-category>General</bundle-category> <import-package package="org.osgi.framework"/>
   * <export-package package="org.ungoverned.osgi.service.bundlerepository"
   * specification-version="1.1.0"/> </bundle> *
   */
  private void parseOscar(XmlPullParser parser) throws Exception {
    parser.require(XmlPullParser.START_TAG, null, "bundles");
    while (true) {
      int event = parser.next();

      // Error ..
      if (event == XmlPullParser.TEXT) event = parser.next();

      if (event != XmlPullParser.START_TAG) break;

      ResourceImpl resource = new ResourceImpl(this);

      if (parser.getName().equals("bundle")) {
        while (parser.nextTag() == XmlPullParser.START_TAG) {
          String key = parser.getName();
          if (key.equals("import-package")) {
            RequirementImpl requirement = new RequirementImpl("package");

            requirement.setOptional(false);
            requirement.setMultiple(false);

            String p = parser.getAttributeValue(null, "package");
            StringBuffer sb = new StringBuffer();
            sb.append("(&(package=");
            sb.append(p);
            sb.append(")");
            String version = parser.getAttributeValue(null, "specification-version");
            VersionRange v = new VersionRange("0");
            if (version != null) {
              sb.append("(version=");
              sb.append(v = new VersionRange(version));
              sb.append(")");
            }
            sb.append(")");
            requirement.setFilter(sb.toString());
            requirement.setComment("Import-Package: " + p + ";" + v);
            resource.addRequirement(requirement);

            parser.nextTag();
          } else if (key.equals("export-package")) {
            CapabilityImpl capability = new CapabilityImpl("package");
            capability.addProperty("package", parser.getAttributeValue(null, "package"));
            String version = parser.getAttributeValue(null, "specification-version");
            if (version != null) {
              capability.addProperty("version", new VersionRange(version));
            }
            resource.addCapability(capability);
            parser.nextTag();
          } else {
            String value = parser.nextText().trim();
            if (key.equals("bundle-sourceurl")) resource.setSource(new URL(value));
            else if (key.equals("bundle-docurl")) resource.setDocumentation(new URL(value));
            else if (key.equals("bundle-updatelocation")) resource.setURL(new URL(value));
            else if (key.equals("bundle-description")) resource.setDescription(value);
            else if (key.equals("bundle-category")) resource.addCategory(value);
            else if (key.equals("bundle-name")) {
              resource.setName(value);
              resource.setPresentationName(value);
            } else if (key.equals("bundle-version")) resource.setVersion(new VersionRange(value));
            else {
              resource.put(key, value);
            }
          }
        }
        resources.add(resource);
        parser.require(XmlPullParser.END_TAG, null, "bundle");
      } else if (parser.getName().equals("repository")) {
        parser.require(XmlPullParser.START_TAG, null, "repository");
        while (parser.nextTag() == XmlPullParser.START_TAG) {
          String tag = parser.getName();
          if (tag.equals("name")) {
            String name = parser.nextText();
            if (this.name == null) this.name = name.trim();
          } else if (tag.equals("url")) parser.nextText().trim();
          else if (tag.equals("date")) parser.nextText().trim();
          else if (tag.equals("extern-repositories")) {
            parser.require(XmlPullParser.START_TAG, null, "extern-repositories");
            while (parser.nextTag() == XmlPullParser.START_TAG) {
              if (parser.getName().equals("url")) parseDocument(new URL(parser.nextText().trim()));
              else
                throw new IllegalArgumentException(
                    "Invalid tag in repository while parsing extern repositories: "
                        + url
                        + " "
                        + parser.getName());
            }
            parser.require(XmlPullParser.END_TAG, null, "extern-repositories");
          } else
            throw new IllegalArgumentException(
                "Invalid tag in repository: " + url + " " + parser.getName());
        }
        parser.require(XmlPullParser.END_TAG, null, "repository");
      } else if (parser.getName().equals("dtd-version")) {
        parser.nextText();
      } else
        throw new IllegalArgumentException(
            "Invalid tag in repository: " + url + " " + parser.getName());
    }
    parser.require(XmlPullParser.END_TAG, null, "bundles");
  }
  /**
   * 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;
  }