Esempio n. 1
0
  /** Creates the list with link instances necessary for the query list. */
  private void createQueryLinks() {
    NetworkInterface.generateAttributeList();
    StorageLink.generateAttributeList();
    try {
      Link storageLink = new Link(null, null);
      Set<Kind> storageRelated = new HashSet<Kind>();
      storageRelated.add(
          new Kind(
              null,
              null,
              null,
              null,
              "storagelink",
              "storagelink",
              "http://schemas.ogf.org/occi/infrastructure#link",
              StorageLink.attributes));
      storageLink.setKind(
          new Kind(
              null,
              storageRelated,
              null,
              null,
              "storagelink",
              "storagelink",
              "http://schemas.ogf.org/occi/infrastructure#",
              StorageLink.attributes));
      queryLinks.add(storageLink);

      Set<Kind> networkInterfaceRelated = new HashSet<Kind>();
      networkInterfaceRelated.add(
          new Kind(
              null,
              null,
              null,
              null,
              "networkinterface",
              "networkinterface",
              "http://schemas.ogf.org/occi/infrastructure#link",
              NetworkInterface.getAttributes()));
      Link networkInterface = new Link(null, null);
      networkInterface.setKind(
          new Kind(
              null,
              networkInterfaceRelated,
              null,
              null,
              "networkinterface",
              "networkinterface",
              "http://schemas.ogf.org/occi/infrastructure#",
              NetworkInterface.getAttributes()));
      queryLinks.add(networkInterface);
    } catch (SchemaViolationException e) {
      e.printStackTrace();
    } catch (URISyntaxException e) {
      e.printStackTrace();
    }
  }
Esempio n. 2
0
  /**
   * Returns all available resources.
   *
   * @return ComputeResource String
   */
  @Get
  public String getOCCIRequest() {
    // set occi version
    getServerInfo().setAgent(OcciConfig.getInstance().config.getString("occi.version"));
    LOGGER.debug("getReference().getLastSegment(): " + getReference().getLastSegment());

    // if there is something behind -, set bad request
    if (!getReference().getLastSegment().equals("-")) {
      getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
      return "There are no resources at the query interface.";
    }

    createQueryKinds();
    createQueryLinks();
    createQueryMixins();

    StringBuffer buffer = new StringBuffer();
    /*
     * Print all properties of the kind instance
     */
    for (Kind kind : queryKinds) {
      if (kind != null) {
        buffer.append("Category: " + kind.getTerm() + ";");
        buffer.append(" scheme=\"" + kind.getScheme() + "\";");
        // buffer.append("\r\n");
        buffer.append(" class=\"kind\"");
        buffer.append("\r\n");
        // append related scheme to buffer, if kind has a related kind
        if (kind.getRelated() != null) {
          for (Kind related : kind.getRelated()) {
            if (related != null) {
              buffer.append("\t\t rel=").append(related.getScheme()).append(";\n");
            }
          }
        }
        buffer.append("\t\t attributes=\"");
        if (kind.getAttributes() != null) {
          for (String attribute : kind.getAttributes()) {
            if (attribute != null) {
              buffer.append(attribute).append(" ");
            }
          }
        }
        buffer.append("\";\n");
        buffer.append("\t\t actions=");
        for (String actionName : kind.getActionNames()) {
          if (actionName != null) {
            buffer.append(actionName).append(" ");
          }
        }
        buffer.append(";");
        buffer.append("\r\n");
        buffer.append("\t\t location=/").append(kind.getTerm()).append("/;");
        buffer.append("\r\n");
      }
    }

    /*
     * Print all link properties.
     */
    for (Link link : queryLinks) {
      if (link != null) {
        buffer.append("Category: ").append(link.getKind().getTerm()).append(";");
        buffer.append("\t\t scheme=\"").append(link.getKind().getScheme()).append("\";");
        buffer.append("\r\n");
        buffer.append("\t\t class=\"link\";");
        buffer.append("\r\n");
        // append related scheme to buffer, if kind has a related kind
        if (link.getKind().getRelated() != null) {
          for (Kind related : link.getKind().getRelated()) {
            if (related != null) {
              buffer.append("\t\t rel=").append(related.getScheme()).append(";\n");
            }
          }
        }
        buffer.append("\t\t attributes=\"");
        if (link.getKind().getAttributes() != null) {
          for (String attribute : link.getKind().getAttributes()) {
            if (attribute != null) {
              buffer.append(attribute + " ");
            }
          }
        }
        buffer.append("\";\n");
        buffer.append("\t\t actions=");
        for (String actionName : link.getKind().getActionNames()) {
          if (actionName != null) {
            buffer.append(actionName).append(" ");
          }
        }
        buffer.append(";");
        buffer.append("\r\n");
        buffer.append("\t\t location=/").append(link.getKind().getTerm()).append("/;");
        buffer.append("\r\n");
      }
    }

    /*
     * Print all properties of the mixin instance
     */

    if (ipNetworkInterface != null) {
      buffer.append("Category: ").append(ipNetworkInterface.getTitle()).append(";");
      buffer.append("\t\t scheme=\"").append(ipNetworkInterface.getScheme()).append("\";");
      buffer.append("\r\n");
      buffer.append("\t\t class=\"mixin\"");
      buffer.append("\r\n");
      // append related scheme to buffer, if kind has a related
      // kind
      if (ipNetworkInterface.getRelated() != null) {
        for (Mixin related : ipNetworkInterface.getRelated()) {
          if (related != null) {
            buffer.append("\t\t rel=").append(related.getScheme()).append(";\n");
          }
        }
      }
      buffer.append("\t\t attributes=\"");
      if (ipNetworkInterface.getAttributes() != null) {
        for (String attribute : ipNetworkInterface.getAttributes()) {
          if (attribute != null) {
            buffer.append(attribute).append(" ");
          }
        }
      }
      buffer.append("\";\n");
      buffer.append("\t\t location=/").append(ipNetworkInterface.getTerm()).append("/;");
      buffer.append("\r\n");
    }

    /*
     * Print all properties of the mixin instance
     */
    for (Mixin mixin : Mixin.getMixins()) {
      if (mixin != null && !mixin.getTerm().equals("ipnetwork")) {
        buffer.append("Category: " + mixin.getTitle()).append(";");

        buffer.append("\r\n");
        buffer.append("\t\t class=\"mixin\"");
        buffer.append("\r\n");
        // append related scheme to buffer, if kind has a related
        // kind
        if (mixin.getRelated() != null) {
          for (Mixin related : mixin.getRelated()) {
            if (related != null) {
              buffer.append("\t\t rel=").append(related.getScheme()).append(";\n");
            }
          }
        }
        buffer.append("\t\t scheme=").append(mixin.getScheme()).append(";");
        buffer.append("\r\n");
        buffer.append("\t\t location=/").append(mixin.getTerm()).append("/;");
        buffer.append("\r\n");
      }
    }
    buffer.append("\r\n");

    // Generate all action names list
    Network.generateActionNames();
    Storage.generateActionNames();
    Compute.generateActionNames();

    /*
     * Print all action names.
     */
    for (String computeAction : Compute.getActionNames()) {
      if (computeAction.contains("#")) {
        buffer
            .append("Category: " + computeAction.substring(computeAction.lastIndexOf("#") + 1))
            .append(";");
      } else {
        buffer.append("Cagegory: action;");
      }
      buffer
          .append(" scheme=\"" + computeAction.substring(0, computeAction.lastIndexOf("#") + 1))
          .append("\"\n");

      if (computeAction.contains("#start")) {
        // buffer.append(" attributes: ");
      } else if (computeAction.contains("#stop")) {
        // buffer.append(" attributes: "
        // + StopAction.getStopAttributesAsString() + " ");
      } else if (computeAction.contains("#restart")) {
        // buffer.append(" attributes: "
        // + RestartAction.getRestartAttributesAsString()
        // + " ");
      } else if (computeAction.contains("#suspend")) {
        // buffer.append(" attributes: "
        // + SuspendAction.getSuspendAttributesAsString()
        // + " ");
      }
    }

    for (String storageAction : Storage.getActionNames()) {
      if (storageAction.contains("#")) {
        buffer
            .append("Category: ")
            .append(storageAction.substring(storageAction.lastIndexOf("#") + 1))
            .append(";");
      } else {
        buffer.append("Cagegory: action;");
      }
      buffer
          .append("\t\t scheme=\"")
          .append(storageAction.substring(0, storageAction.lastIndexOf("#") + 1))
          .append("\"\n");
    }

    for (String networkAction : Network.getActionNames()) {
      if (networkAction.contains("#")) {
        buffer
            .append("Category: " + networkAction.substring(networkAction.lastIndexOf("#") + 1))
            .append(";");
      } else {
        buffer.append("Cagegory: action;");
      }
      buffer
          .append("\t\t scheme=\"" + networkAction.substring(0, networkAction.lastIndexOf("#") + 1))
          .append("\"\n");
    }

    // access the request headers and get the Accept attribute of the
    // Content-Type
    Form requestHeaders = (Form) getRequest().getAttributes().get("org.restlet.http.headers");
    Representation representation =
        OcciCheck.checkContentType(requestHeaders, buffer, getResponse());
    LOGGER.debug("Raw Request headers: " + requestHeaders.toString());

    /*
     * if content type equals text/occi, render query information in the
     * header and NOT in the body
     */
    if (representation.getMediaType().toString().equals("text/occi")) {
      // Generate the header rendering if media-type equals text/occi
      OcciCheck occiCheck = new OcciCheck();
      occiCheck.setHeaderRendering((LinkedList<Kind>) queryKinds);
      getResponse().setEntity(representation);
      return " ";
    }
    // add content type and status to client response
    getResponse().setEntity(representation);
    return " ";
  }