示例#1
0
  /** @param args */
  public static void main(String[] args) {
    // TODO Auto-generated method stub
    if (args.length < 2 || args.length > 3) {
      StartMessage();
    }

    if (args[0].equals("client") && args.length != 2) {
      StartMessage();
    }

    try {
      if (args[0].equals("client")) {
        ContentName _name = ContentName.fromURI(args[1]);
        new RelayClient(_name);
      }

      if (args[0].equals("server")) {
        ContentName _name = ContentName.fromURI(args[1]);
        if (args.length == 3) new RelayServer(_name, args[2]);
        else new RelayServer(_name);
      }
    } catch (MalformedContentNameStringException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
示例#2
0
 public void parseWithException(String input) {
   System.out.println("ContentName: parsing illegal name string \"" + input + "\"");
   try {
     ContentName.fromURI(input);
     fail("Illegal string parsed without error");
   } catch (MalformedContentNameStringException ex) {
     // expected
     System.out.println("Exception expected msg: " + ex.getMessage());
   }
 }
示例#3
0
  /**
   * Applies policy changes
   *
   * @param pxml policy data
   * @return
   * @throws XMLStreamException
   */
  public void update(PolicyXML pxml, boolean fromNet) throws RepositoryException {
    Log.info(Log.FAC_REPO, "Updating policy");
    if (pxml._version == null) throw new RepositoryException("No version in policy file");
    if (!pxml._version.equals(POLICY_VERSION)) {
      Log.warning(Log.FAC_REPO, "Bad version in policy file: {0}", pxml._version);
      throw new RepositoryException("Bad version in policy file");
    }

    if (null == pxml._localName) throw new RepositoryException("No local name in policy file");
    if (fromNet) {
      if (!pxml._localName.equals(_pxml.getLocalName())) {
        Log.warning(
            Log.FAC_REPO, "Repository local name doesn't match: request = {0}", pxml._localName);
        throw new RepositoryException("Repository local name doesn't match policy file");
      }
    } else {
      try {
        setLocalName(pxml._localName);
      } catch (MalformedContentNameStringException e) {
        throw new RepositoryException(e.getMessage());
      }
    }

    if (null == pxml._globalPrefix) throw new RepositoryException("No globalPrefix in policy file");

    if (fromNet) {
      if (!pxml.getGlobalPrefix().equals(_pxml.getGlobalPrefix())) {
        Log.warning("Repository globalPrefix doesn't match: request = {0}", pxml._globalPrefix);
        throw new RepositoryException("Repository global prefix doesn't match policy file");
      }
    } else {
      _pxml.setGlobalPrefixOnly(pxml._globalPrefix);
    }

    _pxml.setNamespace(pxml.getNamespace());
    if (null != pxml.getNamespace()) {
      String message = "";
      for (ContentName name : pxml.getNamespace()) {
        message += name.toString() + ':';
      }
      Log.info(Log.FAC_REPO, "Policy has been updated. New namespace is: " + message);
    }
  }
示例#4
0
  /** @param args */
  public void write(String[] args) {
    Log.setDefaultLevel(Level.WARNING);

    for (int i = 0; i < args.length - 2; i++) {
      if (args[i].equals("-local")) {
        CommonParameters.local = true;
      } else if (args[i].equals(("-raw"))) {
        CommonParameters.rawMode = true;
      } else {
        if (!CommonArguments.parseArguments(args, i, ccnputfile)) {
          usage();
        }
        if (CommonParameters.startArg > i + 1) i = CommonParameters.startArg - 1;
      }
      if (CommonParameters.startArg <= i) CommonParameters.startArg = i + 1;
    }

    if (args.length < CommonParameters.startArg + 2) {
      usage();
    }

    long starttime = System.currentTimeMillis();
    try {
      // If we get one file name, put as the specific name given.
      // If we get more than one, put underneath the first as parent.
      // Ideally want to use newVersion to get latest version. Start
      // with random version.
      ContentName argName = ContentName.fromURI(args[CommonParameters.startArg]);

      CCNHandle handle = CCNHandle.open();

      if (args.length == (CommonParameters.startArg + 2)) {
        if (CommonParameters.verbose)
          Log.info("ccnputfile: putting file " + args[CommonParameters.startArg + 1]);

        doPut(handle, args[CommonParameters.startArg + 1], argName);
        System.out.println("Inserted file " + args[CommonParameters.startArg + 1] + ".");
        if (CommonParameters.verbose)
          System.out.println(
              "ccnputfile took: " + (System.currentTimeMillis() - starttime) + " ms");
        System.exit(0);
      } else {
        for (int i = CommonParameters.startArg + 1; i < args.length; ++i) {

          // put as child of name
          ContentName nodeName = ContentName.fromURI(argName, args[i]);

          doPut(handle, args[i], nodeName);
          // leave this one as always printing for now
          System.out.println("Inserted file " + args[i] + ".");
        }
        if (CommonParameters.verbose)
          System.out.println(
              "ccnputfile took: " + (System.currentTimeMillis() - starttime) + " ms");
        System.exit(0);
      }
    } catch (ConfigurationException e) {
      System.out.println("Configuration exception in put: " + e.getMessage());
      e.printStackTrace();
    } catch (MalformedContentNameStringException e) {
      System.out.println(
          "Malformed name: " + args[CommonParameters.startArg] + " " + e.getMessage());
      e.printStackTrace();
    } catch (IOException e) {
      System.out.println("Cannot put file. " + e.getMessage());
      e.printStackTrace();
    } catch (InvalidKeyException e) {
      System.out.println("Cannot publish invalid key: " + e.getMessage());
      e.printStackTrace();
    }
    System.exit(1);
  }
示例#5
0
  @Test
  public void testContentNameString() {
    ContentName name;

    // ----------------------------- Simple strings: identical as URI-encoded and native Java

    // simple string: /test/briggs/test.txt is legal as both URI-encoded and native Java
    String testString =
        ContentName.SEPARATOR
            + baseName
            + ContentName.SEPARATOR
            + subName1
            + ContentName.SEPARATOR
            + document1;

    System.out.println("ContentName: parsing name string \"" + testString + "\"");
    // test URI-encoded (the canonical interpretation)
    try {
      name = ContentName.fromURI(testString);
    } catch (MalformedContentNameStringException e) {
      System.out.println("Exception " + e.getClass().getName() + ", message: " + e.getMessage());
      e.printStackTrace();
      name = null;
    }
    assertNotNull(name);
    System.out.println("Name: " + name);
    assertEquals(name.toString(), testString);

    // test as native Java String
    try {
      name = ContentName.fromNative(testString);
    } catch (MalformedContentNameStringException e) {
      System.out.println(
          "Exception on native " + e.getClass().getName() + ", message: " + e.getMessage());
      e.printStackTrace();
      name = null;
    }
    assertNotNull(name);
    System.out.println("Name (native): " + name);
    assertEquals(name.toString(), testString);

    // alternate simple string: / only, also legal as both URI-encoded and native Java
    String testString2 = ContentName.SEPARATOR;
    ContentName name2 = null;

    // test as URI-encoded (canonical)
    System.out.println("ContentName: parsing name string \"" + testString2 + "\"");
    try {
      name2 = ContentName.fromURI(testString2);
    } catch (MalformedContentNameStringException e) {
      System.out.println("Exception " + e.getClass().getName() + ", message: " + e.getMessage());
      e.printStackTrace();
      name2 = null;
    }
    assertNotNull(name2);
    System.out.println("Name: " + name2);
    assertEquals(name2.toString(), testString2);

    // test as native Java String
    System.out.println("ContentName: parsing name string \"" + testString2 + "\"");
    try {
      name2 = ContentName.fromNative(testString2);
    } catch (MalformedContentNameStringException e) {
      System.out.println("Exception " + e.getClass().getName() + ", message: " + e.getMessage());
      e.printStackTrace();
      name2 = null;
    }
    assertNotNull(name2);
    System.out.println("Name: " + name2);
    assertEquals(name2.toString(), testString2);

    // ----------------------------- tests specific to URI-encoded cases

    // string with ccnx: scheme on front
    ContentName name3 = null;
    System.out.println("ContentName: parsing name string \"" + withScheme + "\"");
    try {
      name3 = ContentName.fromURI(withScheme);
    } catch (MalformedContentNameStringException e) {
      System.out.println("Exception " + e.getClass().getName() + ", message: " + e.getMessage());
      e.printStackTrace();
      name3 = null;
    }
    assertNotNull(name3);
    System.out.println("Name: " + name3);
    assertEquals(name3.toString(), withScheme.substring(withScheme.indexOf(":") + 1));

    ContentName input3 = null;
    try {
      input3 = ContentName.fromURI(name3.toString());
    } catch (MalformedContentNameStringException e) {
      System.out.println("Exception " + e.getClass().getName() + ", message: " + e.getMessage());
      e.printStackTrace();
      input3 = null;
    }
    assertEquals(input3, name3);

    // string with dots and slashes
    ContentName name4 = null;
    System.out.println("ContentName: parsing name string \"" + dotSlash + "\"");
    try {
      name4 = ContentName.fromURI(dotSlash);
    } catch (MalformedContentNameStringException e) {
      System.out.println("Exception " + e.getClass().getName() + ", message: " + e.getMessage());
      e.printStackTrace();
      name4 = null;
    }
    assertNotNull(name4);
    System.out.println("Name: " + name4);
    assertEquals(name4.toString(), dotSlashResolved.substring(dotSlashResolved.indexOf(":") + 1));

    // empty name
    System.out.println("ContentName: testing empty name round trip: /");
    ContentName name5 = null;
    try {
      name5 = ContentName.fromURI("/");
    } catch (MalformedContentNameStringException e) {
      System.out.println("Exception " + e.getClass().getName() + ", message: " + e.getMessage());
      e.printStackTrace();
      name5 = null;
    }
    assertNotNull(name5);
    assertEquals(name5.count(), 0);
    assertEquals(name5.components().size(), 0);
    assertEquals(name5.toString(), "/");

    // empty name with scheme
    System.out.println("ContentName: testing empty name round trip: /");
    ContentName name6 = null;
    try {
      name6 = ContentName.fromURI("ccnx:/");
    } catch (MalformedContentNameStringException e) {
      System.out.println("Exception " + e.getClass().getName() + ", message: " + e.getMessage());
      e.printStackTrace();
      name6 = null;
    }
    assertNotNull(name6);
    assertEquals(name6.count(), 0);
    assertEquals(name6.components().size(), 0);
    assertEquals(name6.toString(), "/");

    // query and fragment parts
    try {
      assertEquals(ContentName.fromURI(withQuery).toString(), withQuery.split("\\?")[0]);
      assertEquals(ContentName.fromURI(withFragment).toString(), withFragment.split("\\#")[0]);
      assertEquals(
          ContentName.fromURI(withQueryAndFragment).toString(),
          withQueryAndFragment.split("\\?")[0]);
    } catch (MalformedContentNameStringException e) {
      System.out.println("Exception " + e.getClass().getName() + ", message: " + e.getMessage());
      e.printStackTrace();
      fail(e.getMessage());
    }
  }