Esempio n. 1
0
  /**
   * Add allowed URL protocols for an element's URL attribute. This restricts the possible values of
   * the attribute to URLs with the defined protocol.
   *
   * <p>E.g.: <code>addProtocols("a", "href", "ftp", "http", "https")</code>
   *
   * @param tag Tag the URL protocol is for
   * @param key Attribute key
   * @param protocols List of valid protocols
   * @return this, for chaining
   */
  public Whitelist addProtocols(String tag, String key, String... protocols) {
    Validate.notEmpty(tag);
    Validate.notEmpty(key);
    Validate.notNull(protocols);

    TagName tagName = TagName.valueOf(tag);
    AttributeKey attrKey = AttributeKey.valueOf(key);
    Map<AttributeKey, Set<Protocol>> attrMap;
    Set<Protocol> protSet;

    if (this.protocols.containsKey(tagName)) {
      attrMap = this.protocols.get(tagName);
    } else {
      attrMap = new HashMap<AttributeKey, Set<Protocol>>();
      this.protocols.put(tagName, attrMap);
    }
    if (attrMap.containsKey(attrKey)) {
      protSet = attrMap.get(attrKey);
    } else {
      protSet = new HashSet<Protocol>();
      attrMap.put(attrKey, protSet);
    }
    for (String protocol : protocols) {
      Validate.notEmpty(protocol);
      Protocol prot = Protocol.valueOf(protocol);
      protSet.add(prot);
    }
    return this;
  }
Esempio n. 2
0
 public static Protocol fromString(final String value) {
   try {
     return Protocol.valueOf(value);
   } catch (IllegalArgumentException e) {
     final Integer protocolNumber = Ints.tryParse(value);
     if (protocolNumber != null)
       for (final Protocol protocol : values()) {
         if (protocolNumber == protocol.number) return protocol;
       }
     throw e;
   }
 }
Esempio n. 3
0
  /**
   * Returns a Classifier object from a byte array
   *
   * @param data - the data to parse
   * @return - the object or null if cannot be parsed TODO - make me more robust as exceptions can
   *     be swallowed here.
   */
  public static Classifier parse(final byte[] data) {
    final List<Byte> bytes = Bytes.asList(data);

    try {
      final byte[] srcAddrBytes = Bytes.toArray(bytes.subList(4, 8));
      final byte[] dstAddrBytes = Bytes.toArray(bytes.subList(8, 12));
      return new Classifier(
          Protocol.valueOf(COPSMsgParser.bytesToShort(data[0], data[1])),
          data[2],
          data[3],
          (Inet4Address) InetAddress.getByAddress(srcAddrBytes),
          (Inet4Address) InetAddress.getByAddress(dstAddrBytes),
          COPSMsgParser.bytesToShort(data[12], data[13]),
          COPSMsgParser.bytesToShort(data[14], data[15]),
          data[16]);
    } catch (UnknownHostException e) {
      return null;
    }
  }
 public void setProtocol(String protocol) {
   this.protocol = Protocol.valueOf(protocol.toUpperCase());
 }