Example usage for com.google.common.net InternetDomainName isValid

List of usage examples for com.google.common.net InternetDomainName isValid

Introduction

In this page you can find the example usage for com.google.common.net InternetDomainName isValid.

Prototype

public static boolean isValid(String name) 

Source Link

Document

Indicates whether the argument is a syntactically valid domain name using lenient validation.

Usage

From source file:com.eucalyptus.compute.vpc.VpcManager.java

public CreateDhcpOptionsResponseType createDhcpOptions(final CreateDhcpOptionsType request)
        throws EucalyptusCloudException {
    final CreateDhcpOptionsResponseType reply = request.getReply();
    final Context ctx = Contexts.lookup();
    final Supplier<DhcpOptionSet> allocator = new Supplier<DhcpOptionSet>() {
        @Override/*w  w  w  .  j ava 2  s .c o m*/
        public DhcpOptionSet get() {
            try {
                final DhcpOptionSet dhcpOptionSet = DhcpOptionSet.create(ctx.getUserFullName(),
                        Identifier.dopt.generate());
                for (final DhcpConfigurationItemType item : request.getDhcpConfigurationSet().getItem()) {
                    final List<String> values = item.values();
                    boolean validValue = false;
                    out: switch (item.getKey()) {
                    case DhcpOptionSets.DHCP_OPTION_DOMAIN_NAME:
                        validValue = values.size() == 1 && InternetDomainName.isValid(values.get(0));
                        break;
                    case DhcpOptionSets.DHCP_OPTION_DOMAIN_NAME_SERVERS:
                        validValue = values.size() == 1 && "AmazonProvidedDNS".equals(values.get(0));
                        if (validValue)
                            break; // else fallthrough
                    case DhcpOptionSets.DHCP_OPTION_NTP_SERVERS: // fallthrough
                    case DhcpOptionSets.DHCP_OPTION_NETBIOS_NAME_SERVERS:
                        for (final String value : values) {
                            validValue = InetAddresses.isInetAddress(value);
                            if (!validValue)
                                break out;
                        }
                        break;
                    case DhcpOptionSets.DHCP_OPTION_NETBIOS_NODE_TYPE:
                        validValue = values.size() == 1 && Optional.fromNullable(Ints.tryParse(values.get(0)))
                                .transform(
                                        Functions.forPredicate(Predicates.in(Lists.newArrayList(1, 2, 4, 8))))
                                .or(false);
                        break;
                    default:
                        throw new ClientComputeException("InvalidParameterValue", "Value (" + item.getKey()
                                + ") for parameter name is invalid. Unknown DHCP option");
                    }
                    if (!validValue || values.isEmpty()) {
                        throw new ClientComputeException("InvalidParameterValue",
                                "Value (" + Joiner.on(',').join(values)
                                        + ") for parameter value is invalid. Invalid DHCP option value.");
                    }
                    dhcpOptionSet.getDhcpOptions()
                            .add(DhcpOption.create(dhcpOptionSet, item.getKey(), item.values()));
                }
                return dhcpOptionSets.save(dhcpOptionSet);
            } catch (Exception ex) {
                throw Exceptions.toUndeclared(ex);
            }
        }
    };
    reply.setDhcpOptions(allocate(allocator, DhcpOptionSet.class, DhcpOptionsType.class));
    return reply;
}

From source file:SeedGenerator.WorkerProfilerQueue.java

private void insertSeedLink(String url, String searchEngineName, String text, int resultOrder,
        int pageContentId) {
    try {//from   w w w  . j ava 2 s . c om
        URL u = new URL(url);
        String host = u.getHost();
        if (InternetDomainName.isValid(host) || com.google.common.net.HostSpecifier.isValid(host)) {

            //InternetDomainName.from(host).topPrivateDomain().toString();
            PreparedStatement pstmt = con.prepareStatement(
                    "INSERT INTO seedurlraw (url, searchEngine,resultOrder,pageContentId) VALUES (?,?,?,?);");
            pstmt.setString(1, url);
            pstmt.setString(2, searchEngineName);
            pstmt.setString(3, String.valueOf(resultOrder));
            pstmt.setInt(4, pageContentId);

            pstmt.executeUpdate();
            pstmt.close();

            DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
            Object[] row = { searchEngineName, text, url, dateFormat.format(new Date()) };
            target.dispatchEvent(new EventSearchWorker(target, row, count, ""));

        }
    } catch (Exception ex) {
        String a = "";
        String b = "";

    }
}

From source file:SeedGenerator.MainForm.java

private void btnSameAsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSameAsActionPerformed

    try {/*from   w ww . j  a  v a 2s .c o  m*/
        PreparedStatement pstmtendpoints = con
                .prepareStatement("select id, endpointurl from endpoints where outLinksCount is null;");
        ResultSet rs1 = pstmtendpoints.executeQuery();
        int endpointid = 0;
        int queryid = 23;//sameas ext
        while (rs1.next()) {
            endpointid = rs1.getInt("id");
            int hashTotalCount = 0;
            PreparedStatement pstmt = con
                    .prepareStatement("select s,p,o from endpointtriples where queryid=? and endpointid=?;");
            pstmt.setInt(1, queryid);
            pstmt.setInt(2, endpointid);

            // execute the query, and get a java resultset
            ResultSet rs = pstmt.executeQuery();
            ;

            // iterate through the java resultset
            HashMap<String, Integer> hash = new HashMap();
            int totalNumberOfWords = 0;
            while (rs.next()) {
                String s = rs.getString("s");
                String p = rs.getString("p");
                String o = rs.getString("o");
                if (o != null) {
                    String host = "";
                    try {
                        URL u = new URL(o);
                        host = u.getHost();
                        if (InternetDomainName.isValid(host)) {
                            host = InternetDomainName.from(host).topPrivateDomain().toString();
                        } else {
                            if (com.google.common.net.HostSpecifier.isValid(host)) {
                                host = com.google.common.net.HostSpecifier.from(host).toString();
                            } else {
                                host = "";
                            }
                        }
                    } catch (Exception ex) {
                    }

                    if (hash.containsKey(host)) {
                        hash.replace(host, hash.get(host) + 1);
                    } else {
                        hash.put(host, 1);
                    }

                }
                //String firstName = rs.getString("url");
            }
            pstmt.close();
            rs.close();

            Iterator it = hash.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry pair = (Map.Entry) it.next();
                //<>
                if (Integer.parseInt(pair.getValue().toString()) > 1) {
                    PreparedStatement insertpstmt = con.prepareStatement(
                            "insert into recommender_sameas_pldcount (pld,endpointid,count,queryid) values(?,?,?,?);");
                    if (pair.getKey().toString().length() > 44) {
                        insertpstmt.setString(1, pair.getKey().toString().substring(0, 44));
                    } else {
                        insertpstmt.setString(1, pair.getKey().toString());
                    }
                    insertpstmt.setInt(3, Integer.parseInt(pair.getValue().toString()));
                    insertpstmt.setInt(2, endpointid);
                    insertpstmt.setInt(4, queryid);
                    insertpstmt.executeUpdate();
                }
                it.remove(); // avoids a ConcurrentModificationException
            }
            PreparedStatement updatepstmt = con
                    .prepareStatement("update endpoints set outLinksCount=? where id=?;");
            updatepstmt.setInt(1, hash.size());
            updatepstmt.setInt(2, endpointid);
            updatepstmt.executeUpdate();
        }
    } catch (Exception e) {
        //System.err.println("Got an exception! ");
        System.err.println(e.getMessage());
    }
    // TODO add your handling code here:
}

From source file:SeedGenerator.MainForm.java

private void updateDomainsOfEndpoints() {
    try {//from  w w  w .j a  va 2s.  co m

        PreparedStatement pstmtendpoints = con.prepareStatement("select id, endpointUrl from endpoints;");
        ResultSet rs1 = pstmtendpoints.executeQuery();

        while (rs1.next()) {
            String endpointUrl = rs1.getString("endpointUrl");
            int id = rs1.getInt("id");
            URL u = new URL(endpointUrl);
            String host = u.getHost();
            if (InternetDomainName.isValid(host)) {
                host = InternetDomainName.from(host).topPrivateDomain().toString();
            } else {
                if (com.google.common.net.HostSpecifier.isValid(host)) {
                    host = com.google.common.net.HostSpecifier.from(host).toString();
                } else {
                    host = "";
                }
            }
            PreparedStatement updatepstmt = con
                    .prepareStatement("update endpoints set domain = ? where id = ?");
            updatepstmt.setString(1, host);
            updatepstmt.setInt(2, id);
            updatepstmt.executeUpdate();
        }
        pstmtendpoints.close();
        rs1.close();

    } catch (Exception ex) {

    }
    try {

        PreparedStatement pstmtendpoints = con
                .prepareStatement("select domain from endpoints group by domain;");
        ResultSet rs1 = pstmtendpoints.executeQuery();

        while (rs1.next()) {
            String domain = rs1.getString("domain");

            PreparedStatement pstmtendpointsdomains = con
                    .prepareStatement("select domain,endpointUrl,source from endpoints where domain=?;");
            pstmtendpointsdomains.setString(1, domain);
            ResultSet rs2 = pstmtendpointsdomains.executeQuery();

            String sourceDomain = "";
            while (rs2.next()) {
                String source = rs2.getString("source");
                String[] sources = source.split("-");
                for (String s : sources) {
                    if (!sourceDomain.contains(s)) {
                        if (sourceDomain != "") {
                            sourceDomain += "-" + s;
                        } else {
                            sourceDomain += s;
                        }
                    }
                }
            }
            PreparedStatement updatepstmt = con
                    .prepareStatement("update endpoints set domainSource = ? where domain = ?");
            updatepstmt.setString(1, sourceDomain);
            updatepstmt.setString(2, domain);
            updatepstmt.executeUpdate();
        }
        pstmtendpoints.close();
        rs1.close();

    } catch (Exception ex) {
        System.out.println(ex.getMessage());
    }
}

From source file:SeedGenerator.MainForm.java

private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
    try {//from w  ww.  jav  a 2 s.c om
        String host = "";
        String SQL = "SELECT * FROM profiler.tumkayit where domain is null;";
        PreparedStatement selectstmt = con.prepareStatement(SQL);
        ResultSet rs = selectstmt.executeQuery();
        int i = 0;
        String SQLu = "UPDATE profiler.tumkayit SET domain = ? where idtumkayit = ?;";
        PreparedStatement selectstmtu = con.prepareStatement(SQLu);

        while (rs.next()) {

            try {
                host = rs.getString("url");
                URL u = new URL("http://" + rs.getString("url"));
                host = u.getHost();
                if (InternetDomainName.isValid(host)) {
                    host = InternetDomainName.from(host).topPrivateDomain().toString();
                } else {
                    if (com.google.common.net.HostSpecifier.isValid(host)) {
                        host = com.google.common.net.HostSpecifier.from(host).toString();
                    } else {
                        host = "";
                    }
                }
            } catch (Exception ex) {

            }
            selectstmtu.setString(1, host);
            selectstmtu.setInt(2, rs.getInt("idtumkayit"));
            selectstmtu.addBatch();
            if (i > 10000) {
                selectstmtu.executeBatch();
                selectstmtu = con.prepareStatement(SQLu);

                i = 0;
            }
            i++;
            //String query = txtSearchTexts.getText().split("\\n")[0] + " site:" + host;
        }
        selectstmtu.executeBatch();

    } catch (Exception ex) {
        System.out.println(ex.getMessage());
    }

    // TODO add your handling code here:
}

From source file:SeedGenerator.MainForm.java

private void btnCreateSearchQueueFromPreviousUrlsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCreateSearchQueueFromPreviousUrlsActionPerformed
    try {/* www. j a  v a 2s.  c  om*/
        String SQLurl = "SELECT url FROM distinct_all_urls";
        Statement stmturl = con1.createStatement();
        ResultSet rsurl = stmturl.executeQuery(SQLurl);
        HashSet<String> previousQueries = new HashSet<String>();
        while (rsurl.next()) {

            try {
                URL u = new URL(rsurl.getString("url"));
                String host = u.getHost();
                if (InternetDomainName.isValid(host)) {
                    host = InternetDomainName.from(host).topPrivateDomain().toString();
                } else {
                    if (com.google.common.net.HostSpecifier.isValid(host)) {
                        host = com.google.common.net.HostSpecifier.from(host).toString();
                    } else {
                        host = "";
                    }
                }
                String query = txtSearchTexts.getText().split("\\n")[0] + " site:" + host;
                if (!previousQueries.contains(query) && !host.equals("")) {
                    previousQueries.add(query);
                    //System.out.println(host);
                    //  System.out.println(query);
                    int maxSearchPage = Integer.valueOf(txtMaxPage.getText());
                    if (!isQueryExistInQueryQueue(query)) {
                        try {
                            //   System.out.println(host);
                            for (Object searchEngineName : lstSearchEngines.getSelectedValuesList()) {

                                //                                    String url = rsurl.getString(1);
                                //                                    String SQLi = "INSERT INTO searchqueue (searchText,searchEngineName, maxSearchPage,disabled) VALUES (?,?,?,0)";
                                //                                    PreparedStatement pstmt = con2.prepareStatement(SQLi);
                                //                                    pstmt.setString(1, query);
                                //                                    pstmt.setString(2, searchEngineName.toString());
                                //                                    pstmt.setInt(3, maxSearchPage);
                                //                                    pstmt.executeUpdate();
                                //                                    pstmt.close();
                            }
                        } catch (Exception ex) {
                            //   System.out.println(ex.getMessage());
                        }
                    }
                }
            } catch (Exception ex) {
                //System.out.println(ex.getMessage());
            }
        }
        stmturl.close();
        rsurl.close();

        // return id + 1;
    } catch (Exception ex) {
        System.out.println(ex.getMessage());
    }

    // TODO add your handling code here:
}