Example #1
0
 public void testMurmur3_32() {
   HashTestUtils.check2BitAvalanche(Hashing.murmur3_32(), 250, 0.20);
   HashTestUtils.checkAvalanche(Hashing.murmur3_32(), 250, 0.17);
   HashTestUtils.checkNo2BitCharacteristics(Hashing.murmur3_32());
   HashTestUtils.checkNoFunnels(Hashing.murmur3_32());
   HashTestUtils.assertInvariants(Hashing.murmur3_32());
 }
Example #2
0
 public static Inet4Address getCoercedIPv4Address(InetAddress paramInetAddress) {
   if ((paramInetAddress instanceof Inet4Address)) {
     return (Inet4Address) paramInetAddress;
   }
   byte[] arrayOfByte = paramInetAddress.getAddress();
   int i = 1;
   for (int j = 0; j < 15; j++) {
     if (arrayOfByte[j] != 0) {
       i = 0;
       break;
     }
   }
   if ((i != 0) && (arrayOfByte[15] == 1)) {
     return LOOPBACK4;
   }
   if ((i != 0) && (arrayOfByte[15] == 0)) {
     return ANY4;
   }
   Inet6Address localInet6Address = (Inet6Address) paramInetAddress;
   long l = 0L;
   if (hasEmbeddedIPv4ClientAddress(localInet6Address)) {
     l = getEmbeddedIPv4ClientAddress(localInet6Address).hashCode();
   } else {
     l = ByteBuffer.wrap(localInet6Address.getAddress(), 0, 8).getLong();
   }
   int k = Hashing.murmur3_32().hashLong(l).asInt();
   k |= 0xE0000000;
   if (k == -1) {
     k = -2;
   }
   return getInet4Address(Ints.toByteArray(k));
 }
/** Splits input by hashing the key. */
public class HashingSharder implements Sharder {

  private static final long serialVersionUID = 7967187256546710108L;
  private static final HashFunction HASH = Hashing.murmur3_32();
  private int numShards;

  public HashingSharder(int numShards) {
    this.numShards = numShards;
    checkArgument(numShards > 0);
  }

  @Override
  public int getNumShards() {
    return numShards;
  }

  @Override
  public int getShardForKey(ByteBuffer key) {
    byte[] bytes = SerializationUtil.getBytes(key);
    int hash = (HASH.hashBytes(bytes).asInt()) & Integer.MAX_VALUE; // Keeping positive
    // Dividing integer range rather than using modulo so as to avoid rewriting entries if they are
    // re-hashed.
    return hash / (Integer.MAX_VALUE / numShards + 1);
  }
}
Example #4
0
 static {
   HASH_FUNCTIONS.put("md5", Hashing.md5());
   HASH_FUNCTIONS.put("sha1", Hashing.sha1());
   HASH_FUNCTIONS.put("sha256", Hashing.sha256());
   HASH_FUNCTIONS.put("sha512", Hashing.sha512());
   HASH_FUNCTIONS.put("murmur3_32", Hashing.murmur3_32());
   HASH_FUNCTIONS.put("murmur3_128", Hashing.murmur3_128());
 }
Example #5
0
 @Override
 public int hashCode() {
   HashFunction hf = Hashing.murmur3_32();
   Hasher hc = hf.newHasher();
   for (String key : fields.keySet()) {
     hc.putString(key, Charsets.UTF_8);
   }
   return hc.hash().asInt();
 }
  /**
   * If shortURL is valid, creates it and saves it XXX: at the moment, it just ignores unknown
   * emails, with no feedback for users.
   */
  protected ShortURL createAndSaveIfValid(
      String url,
      String custom,
      String hasToken,
      String expireDate,
      String ip,
      String[] emails,
      Principal principal,
      String country) {

    UrlValidator urlValidator = new UrlValidator(new String[] {"http", "https"});
    /*
     * Check if url comes through http or https
     */
    if (urlValidator.isValid(url)) {
      logger.info("Shortening valid url " + url);
      /*
       * Creates a hash from the current date in case this is not custom
       */
      String id;
      String now = new Date().toString();
      if (custom.equals("")) {
        id = Hashing.murmur3_32().hashString(now, StandardCharsets.UTF_8).toString();
      } else {
        id = custom;
      }

      /*
       * Has Token
       */
      String token = null;
      if (hasToken != null && !hasToken.equals("")) {
        token = UUID.randomUUID().toString();
      }
      /*
       * Expire date
       */
      Date expire = null;
      if (!expireDate.equals("")) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        try {
          expire = sdf.parse(expireDate);
        } catch (ParseException e) {
          e.printStackTrace();
          logger.info("Error: badly introduced date.");
        }
      }

      /*
       * Checks every mail inserted by the user, and maintains a list with
       * those corresponding to registered users.
       */
      List<String> trueEmails = new ArrayList<String>();
      for (int i = 0; i < emails.length; i++) {
        if (!emails[i].equals("")) {
          User foundUser = null;

          foundUser = userRepository.findByMail(emails[i]);
          if (foundUser != null) {
            trueEmails.add(foundUser.getMail());
          }
        }
      }

      /*
       * If no valid emails are introduced, link will be public and it
       * wont have an email list.
       */
      boolean isPrivate = false;
      if (trueEmails.size() > 0) {
        isPrivate = true;
      } else {
        trueEmails = null;
      }

      /*
       * Gets email
       */
      String owner = getOwnerMail();

      /*
       * Creates ShortURL object
       */
      ShortURL su =
          new ShortURL(
              id,
              url,
              linkTo(
                      methodOn(UrlShortenerControllerWithLogs.class)
                          .redirectTo(id, null, null, null, null))
                  .toUri(),
              new Date(System.currentTimeMillis()),
              expire,
              owner,
              token,
              HttpStatus.TEMPORARY_REDIRECT.value(),
              ip,
              country,
              isPrivate,
              trueEmails);

      /*
       * Insert to DB
       */
      return shortURLRepository.save(su);
    } else {
      logger.info("Not valid url " + url);
      return null;
    }
  }
Example #7
0
 private static class Murmur3_32Holder {
   static final HashFunction MURMUR3_32 = new Murmur3_32HashFunction(0);
   static final HashFunction GOOD_FAST_HASH_FUNCTION_32 =
       Hashing.murmur3_32(Hashing.GOOD_FAST_HASH_SEED);
 }
  protected ShortURL createAndSaveIfValid(
      String url,
      String sponsor,
      String brand,
      String owner,
      String ip,
      String name,
      String username,
      boolean advert,
      int seconds,
      boolean useConditional) {
    UrlValidator urlValidator = new UrlValidator(new String[] {"http", "https"});
    if (urlValidator.isValid(url) || (url.equals("") && useConditional)) {
      ShortURL su = null;
      ResponseEntity<String> entity =
          new RestTemplate().getForEntity("http://ipinfo.io/" + ip + "/json", String.class);
      String json = entity.getBody().toString();
      String country = "";
      try {
        JSONObject obj = new JSONObject(json);
        country = obj.getString("country");
      } catch (JSONException e) {
      }

      if (!name.equals("")) {
        su =
            new ShortURL(
                name,
                url,
                linkTo(methodOn(UrlShortenerControllerWithLogs.class).redirectTo(name, null))
                    .toUri(),
                sponsor,
                new Date(System.currentTimeMillis()),
                owner,
                HttpStatus.TEMPORARY_REDIRECT.value(),
                true,
                ip,
                country,
                username,
                advert,
                seconds);
      } else {
        String id = Hashing.murmur3_32().hashString(url, StandardCharsets.UTF_8).toString();
        su =
            new ShortURL(
                id,
                url,
                linkTo(methodOn(UrlShortenerController.class).redirectTo(id, null)).toUri(),
                sponsor,
                new Date(System.currentTimeMillis()),
                owner,
                HttpStatus.TEMPORARY_REDIRECT.value(),
                true,
                ip,
                country,
                username,
                advert,
                seconds);
      }
      return shortURLRepositoryExtended.save(su);
    } else {
      return null;
    }
  }
  @RequestMapping(value = "/link", method = RequestMethod.POST)
  public ResponseEntity<?> shortener(
      @RequestParam("url") String url,
      @RequestParam(value = "sponsor", required = false) String sponsor,
      @RequestParam(value = "brand", required = false) String brand,
      @RequestParam(value = "urlName", required = false) String name,
      @RequestParam("username") String username,
      @RequestParam(value = "advert", required = false) String advertisement,
      @RequestParam(value = "max", required = false) String max,
      @RequestParam MultiValueMap<String, String> params,
      HttpServletRequest request,
      Principal currentUser,
      Model model) {
    logger.info("Requested new short for uri " + url);
    sponsor = "http://localhost:8080/advert";

    boolean useConditional = false;
    // Comprobamos si se usan URIs condicionales
    if (params.getFirst("ifurl1") != null
        && !params.getFirst("ifurl1").equals("")
        && params.getFirst("expr1") != null
        && !params.getFirst("expr1").equals("")) {
      useConditional = true;
    }

    logger.info("Name (hash) is " + name);
    // Si se usan URIs condicionales pero no hay url estandar,
    // se crea un hash usando las targets de las condicionales
    if (name.equals("") && url.equals("") && useConditional) {
      logger.info("There are Conditional URIs but no default target" + name);
      String ifurl = "ifurl";
      String expr = "expr";
      String nueva = "a";

      int paramn = 1;

      String firstparam = ifurl + paramn;
      String secondparam = expr + paramn;

      while ((params.getFirst(firstparam) != null && params.getFirst(secondparam) != null)
          && (!params.getFirst(firstparam).equals("")
              && !params.getFirst(secondparam).equals(""))) {
        nueva += params.getFirst(firstparam);

        paramn++;
        firstparam = ifurl + paramn;
        secondparam = expr + paramn;
      }

      name =
          Hashing.murmur3_32()
              .hashString(url + username + nueva, StandardCharsets.UTF_8)
              .toString();
      while (shortURLRepositoryExtended.findByHash(name, username) != null) {
        // seguir creando
        name =
            Hashing.murmur3_32()
                .hashString(url + username + "" + nueva, StandardCharsets.UTF_8)
                .toString();
        nueva += "a";
      }
    }

    // Si no existe una hash (id or name) se crea una
    else if (name.equals("")) {
      name = Hashing.murmur3_32().hashString(url + username, StandardCharsets.UTF_8).toString();
      String nueva = "a";
      while (shortURLRepositoryExtended.findByHash(name, username) != null) {
        // seguir creando
        name =
            Hashing.murmur3_32()
                .hashString(url + username + "" + nueva, StandardCharsets.UTF_8)
                .toString();
        nueva += "a";
      }
    }
    logger.info("After name (hash) is " + name);

    boolean advert = false;
    int seconds = 0;
    if (advertisement != null) {
      advert = true;
      if (!max.equals("")) {
        seconds = Integer.valueOf(max);
        if (seconds < 1) {
          seconds = 5;
        }
      }
    }

    if (shortURLRepositoryExtended.findByHash(name, username) != null
        && url != null
        && !url.equals("")) {
      ArrayList<String> sufijos = new ArrayList<String>(); // contiene todos los sufijos
      sufijos.add("ada");
      sufijos.add("aco");
      sufijos.add("ado");
      JSONArray arr = new JSONArray();
      JSONObject tmp;
      try {
        for (int i = 0; i < sufijos.size(); i++) {
          ShortURL l = shortURLRepositoryExtended.findByHash(name + "" + sufijos.get(i), username);
          if (l == null) {
            // No existe en la BD, luego no esta cogido
            tmp = new JSONObject();
            tmp.put("name", name + "" + sufijos.get(i));
            arr.put(tmp);
          }
        }
      } catch (Exception e) {
      }
      String devueltos = "'{\"names\":" + arr.toString() + "}'";

      RiWordnet wordnet = new RiWordnet();

      // Crea 10 sinonimos
      String[] poss = wordnet.getPos(name);
      /*
       * for (int j = 0; j < poss.length; j++) {
       * System.out.println("\n\nSynonyms for " + name + " (pos: " +
       * poss[j] + ")");
       */
      JSONArray sinonimos = new JSONArray();
      try {
        String[] synonyms = wordnet.getAllSynonyms(name, poss[0], 10);
        Arrays.sort(synonyms);
        JSONObject tmpo;
        for (int i = 0; i < synonyms.length; i++) {
          ShortURL l = shortURLRepositoryExtended.findByHash(synonyms[i], username);
          if (l == null) {
            // No existe en la BD, luego no esta cogido
            tmpo = new JSONObject();
            tmpo.put("synonym", synonyms[i]);
            sinonimos.put(tmpo);
          }
        }
      } catch (Exception e) {
      }
      String synDevueltos = "'{\"synonyms\":" + sinonimos.toString() + "}'";
      String respuesta = devueltos + "separa" + synDevueltos;
      return new ResponseEntity<>(respuesta, HttpStatus.NOT_ACCEPTABLE);
    } else {
      ShortURL su =
          createAndSaveIfValid(
              url,
              sponsor,
              brand,
              UUID.randomUUID().toString(),
              extractIP(request),
              name,
              username,
              advert,
              seconds,
              useConditional);
      if (su != null) {
        HttpHeaders h = new HttpHeaders();
        h.setLocation(su.getUri());

        saveConditionalURIs(name, params);

        return new ResponseEntity<>(su, h, HttpStatus.CREATED);
      } else {
        return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
      }
    }
  }