Пример #1
1
 /**
  * Validates a given primitive tag against a given validator.
  *
  * @param p The OSM primitive to test
  * @param k The key to validate
  * @param validator The validator to run
  * @param code The error code to set if the validation fails
  * @return The error if the validation fails, {@code null} otherwise
  * @since 7824
  */
 public TestError validateTag(OsmPrimitive p, String k, AbstractValidator validator, int code) {
   TestError error = doValidateTag(p, k, null, validator, code);
   if (error != null) {
     // Workaround to https://issues.apache.org/jira/browse/VALIDATOR-290
     // Apache Commons Validator 1.4.1-SNAPSHOT does not support yet IDN URLs
     // To remove if it gets fixed on Apache side
     String v = p.get(k);
     if (!ASCII_PATTERN.matcher(v).matches()) {
       try {
         String protocol = "";
         if (v.contains("://")) {
           protocol = v.substring(0, v.indexOf("://") + 3);
         }
         String domain = !protocol.isEmpty() ? v.substring(protocol.length(), v.length()) : v;
         String ending = "";
         if (domain.contains("/")) {
           int idx = domain.indexOf("/");
           ending = domain.substring(idx, domain.length());
           domain = domain.substring(0, idx);
         }
         // Try to apply ToASCII algorithm
         error = doValidateTag(p, k, protocol + IDN.toASCII(domain) + ending, validator, code);
       } catch (IllegalArgumentException e) {
         error.setMessage(
             error.getMessage() + tr(" URL cannot be converted to ASCII: {0}", e.getMessage()));
       }
     }
   }
   return error;
 }
Пример #2
0
 @Override
 public boolean isValid(CharSequence value, ConstraintValidatorContext context) {
   // fixing a bug with hibernate throwing exception with emails > 63 chars
   // because of IDN
   if (value == null || value.length() > 60) {
     return false;
   }
   String asciiString = IDN.toASCII(value.toString());
   Matcher m = pattern.matcher(asciiString);
   return m.matches();
 }
Пример #3
0
 /**
  * Serialize a domain name under IDN rules.
  *
  * @param name The domain name.
  * @return The binary domain name representation.
  */
 public static byte[] toByteArray(String name) {
   ByteArrayOutputStream baos = new ByteArrayOutputStream(64);
   if (!name.isEmpty()) {
     // See RFC-3490 section 3.1 https://www.ietf.org/rfc/rfc3490.txt
     for (String s : name.split("[.\u3002\uFF0E\uFF61]")) {
       byte[] buffer = IDN.toASCII(s).getBytes();
       baos.write(buffer.length);
       baos.write(buffer, 0, buffer.length);
     }
   }
   baos.write(0);
   return baos.toByteArray();
 }
Пример #4
0
  public SocksCmdRequest(SocksCmdType var1, SocksAddressType var2, String var3, int var4) {
    super(SocksRequestType.CMD);
    if (var1 == null) {
      throw new NullPointerException("cmdType");
    } else if (var2 == null) {
      throw new NullPointerException("addressType");
    } else if (var3 == null) {
      throw new NullPointerException("host");
    } else {
      switch (SocksCmdRequest.SyntheticClass_1
          .$SwitchMap$io$netty$handler$codec$socks$SocksAddressType[var2.ordinal()]) {
        case 1:
          if (!NetUtil.isValidIpV4Address(var3)) {
            throw new IllegalArgumentException(var3 + " is not a valid IPv4 address");
          }
          break;
        case 2:
          if (IDN.toASCII(var3).length() > 255) {
            throw new IllegalArgumentException(
                var3 + " IDN: " + IDN.toASCII(var3) + " exceeds 255 char limit");
          }
          break;
        case 3:
          if (!NetUtil.isValidIpV6Address(var3)) {
            throw new IllegalArgumentException(var3 + " is not a valid IPv6 address");
          }
        case 4:
      }

      if (var4 > 0 && var4 < 65536) {
        this.cmdType = var1;
        this.addressType = var2;
        this.host = IDN.toASCII(var3);
        this.port = var4;
      } else {
        throw new IllegalArgumentException(var4 + " is not in bounds 0 < x < 65536");
      }
    }
  }
Пример #5
0
 /**
  * Parse a domain name starting at the current offset and moving the input stream pointer past
  * this domain name (even if cross references occure).
  *
  * @param dis The input stream.
  * @param data The raw data (for cross references).
  * @return The domain name string.
  * @throws IOException Should never happen.
  */
 public static String parse(DataInputStream dis, byte data[]) throws IOException {
   int c = dis.readUnsignedByte();
   if ((c & 0xc0) == 0xc0) {
     c = ((c & 0x3f) << 8) + dis.readUnsignedByte();
     HashSet<Integer> jumps = new HashSet<Integer>();
     jumps.add(c);
     return parse(data, c, jumps);
   }
   if (c == 0) {
     return "";
   }
   byte b[] = new byte[c];
   dis.readFully(b);
   String s = IDN.toUnicode(new String(b));
   String t = parse(dis, data);
   if (t.length() > 0) {
     s = s + "." + t;
   }
   return s;
 }
Пример #6
0
  /**
   * Creates a new origin from the specified URI string.
   *
   * <p>Note: Applies IDNA algorigthm to host portion (RFC 3490).
   *
   * @param uriSpec The URI string representing the origin, {@code "null"} or {@code null} if
   *     unknown/unspecified.
   * @throws OriginException On bad URI syntax or unexpected scheme/ protocol.
   */
  public Origin(final String uriSpec) throws OriginException {

    if (uriSpec == null || uriSpec.equals("null")) {
      // Leave all fields at their default null
      return;
    }

    URI uri = null;

    try {
      uri = new URI(uriSpec);

    } catch (URISyntaxException e) {

      throw new OriginException("Bad origin URI: " + e.getMessage());
    }

    scheme = uri.getScheme();
    host = uri.getHost();
    port = uri.getPort();
    path = uri.getPath();

    if (scheme == null)
      throw new OriginException("Bad origin URI: Missing scheme, must be http, https or file");

    scheme = scheme.toLowerCase();

    if (!scheme.equals("http") && !scheme.equals("https") && !scheme.equals("file"))
      throw new OriginException("Bad origin URI: Scheme must be http, https or file");

    if (scheme.equals("http") || scheme.equals("https")) {

      if (host == null) throw new OriginException("Bad origin URI: Missing host name / IP address");

      // Apply the IDNA ToASCII algorithm [RFC3490] to /host/
      host = IDN.toASCII(host, IDN.ALLOW_UNASSIGNED | IDN.USE_STD3_ASCII_RULES);

      // Finally, convert to lower case
      host = host.toLowerCase();
    }
  }
Пример #7
0
  /**
   * Perform external link processing.
   *
   * <p>
   *
   * <p>According to {@link <a
   * href="http://download.oracle.com/otn-pub/jcp/jaxrs-2_0-fr-eval-spec/jsr339-jaxrs-2.0-final-spec.pdf">
   * JAX-RS specification section 3.7.3</a>}: "4. If the resulting string ends with ‘/’ then remove
   * the final character". To differentiate links the trailing slash is checked manually.
   */
  @GET
  @Path("link/{spec:(Http|Https|Ftp|Smb|Sftp|Scp)://.*}")
  @Produces("text/html")
  public Redirect externalLink(
      @PathParam("spec") String spec,
      @Context HttpServletRequest req,
      @Context HttpServletResponse resp)
      throws Exception {

    if (req.getRequestURI().endsWith("/") && !spec.endsWith("/")) {
      spec += "/";
    }

    Matcher matcher = EXT_LINK_REGEXP.matcher(spec);
    if (!matcher.matches()) {
      throw new BadRequestException();
    }

    URL url;
    try {
      url = new URL(spec);
    } catch (MalformedURLException ignored) {
      throw new BadRequestException();
    }

    Matcher cematcher = CHECK_ENCODED_REGEXP.matcher(url.getPath());
    String path = cematcher.matches() ? Encoder.encodeUrl(url.getPath()) : url.getPath();

    return new Redirect(
        new URL(
                url.getProtocol(),
                IDN.toASCII(url.getHost()),
                url.getPort(),
                path + (!StringUtils.isBlank(url.getQuery()) ? "?" + url.getQuery() : ""))
            .toURI());
  }
Пример #8
0
 /**
  * Retrieve the binary length of a string.
  *
  * @param name The name string.
  * @return The binary size of the string.
  */
 public static int size(String name) {
   if (name.isEmpty()) return 1;
   return IDN.toASCII(name).length() + 2;
 }
  @FilterWith({XSRFFilter.class, AdminFilter.class})
  public Result addTarget(
      Context context,
      @Param("target-radio") String targetType,
      @Params("name[]") String[] names,
      @Params("pattern[]") String[] patterns) {
    FlashScope flash = context.getFlashScope();
    Group group = context.getAttribute("group", Group.class);

    if (targetType == null
        || names == null
        || names.length == 0
        || patterns == null
        || patterns.length == 0
        || names.length != patterns.length) {
      flash.error("error.invalidParameters");
      return Results.redirect(
          router.getReverseRoute(GoogleGroupController.class, "view", "groupId", group.getId()));
    }

    Set<GoogleTarget> targets = new HashSet<>();
    for (int i = 0; i < names.length; i++) {
      String name = names[i];
      String pattern = patterns[i];

      if (name != null) {
        name = name.replaceAll("(^\\s+)|(\\s+$)", "");
      }

      if (pattern != null) {
        pattern = pattern.replaceAll("(^\\s+)|(\\s+$)", "");
      }

      if (Validator.isEmpty(name)) {
        flash.error("error.invalidName");
        return Results.redirect(
            router.getReverseRoute(GoogleGroupController.class, "view", "groupId", group.getId()));
      }

      PatternType type = null;
      try {
        type = PatternType.valueOf(targetType);
      } catch (Exception ex) {
        flash.error("error.invalidTargetType");
        return Results.redirect(
            router.getReverseRoute(GoogleGroupController.class, "view", "groupId", group.getId()));
      }

      if (PatternType.DOMAIN.equals(type) || PatternType.SUBDOMAIN.equals(type)) {
        try {
          pattern = IDN.toASCII(pattern);
        } catch (Exception ex) {
          pattern = null;
        }
      }

      if (!GoogleTarget.isValidPattern(type, pattern)) {
        flash.error("error.invalidPattern");
        return Results.redirect(
            router.getReverseRoute(GoogleGroupController.class, "view", "groupId", group.getId()));
      }

      targets.add(new GoogleTarget(group.getId(), name, type, pattern));
    }

    if (googleDB.target.insert(targets) < 1) {
      flash.error("error.internalError");
      return Results.redirect(
          router.getReverseRoute(GoogleGroupController.class, "view", "groupId", group.getId()));
    }
    googleDB.serpRescan.rescan(null, targets, getSearches(context), true);

    Run runningGoogleTask = taskManager.getRunningGoogleTask();
    if (runningGoogleTask != null) {
      flash.put(
          "warning",
          msg.get(
                  "google.group.websiteInsertedWhileRun",
                  context,
                  Optional.absent(),
                  runningGoogleTask.getId())
              .or(""));
    } else {
      flash.success("google.group.websiteInserted");
    }

    return Results.redirect(
        router.getReverseRoute(GoogleGroupController.class, "view", "groupId", group.getId()));
  }
Пример #10
0
 public String host() {
   return IDN.toUnicode(this.host);
 }