Example usage for org.apache.commons.validator.routines UrlValidator UrlValidator

List of usage examples for org.apache.commons.validator.routines UrlValidator UrlValidator

Introduction

In this page you can find the example usage for org.apache.commons.validator.routines UrlValidator UrlValidator.

Prototype

public UrlValidator() 

Source Link

Document

Create a UrlValidator with default properties.

Usage

From source file:com.svi.uzabase.logic.ValidationProcess.java

private List<XMLHolder> validateData(List<XMLHolder> xmlBatchHolder) {
    try {//from w  ww . j  a  v  a 2 s  . c o  m
        int totalCounter = 0;
        //Initialize dictionary
        String dictFileName = "file://./dic/english.jar";
        String configFile = "file://./classes/spellCheck.config";
        BasicDictionary dictionary = new BasicDictionary(dictFileName);
        SpellCheckConfiguration configuration = new SpellCheckConfiguration(configFile);
        BasicSuggester suggester = new BasicSuggester(configuration);
        suggester.attach(dictionary);

        // create SpellCheck object based on configuration and specify Suggester
        SpellCheck spellCheck = new SpellCheck(configuration);
        spellCheck.setSuggester(suggester);
        //set for jprogress bar
        for (XMLHolder h : xmlBatchHolder) {
            totalCounter += h.size();

        }
        progress = new AtomicInteger(0);
        total = new AtomicInteger(totalCounter);
        mf.setJprogressValues(total, progress);
        //validation process begins here
        String[] invalidWords = { "corporation", "inc.", "city", "corp.", "st.", "co.", "ltd." };
        String[] invalidBoardWords = { "other", "oth" };
        String[] validWords = { "loc", "to", "ext", "local" };
        String[] invalidCharacters = { ",", "/", "\\", "[", "]", "\"", ":", "^", "{", "}", "%", "+", "#", "(",
                ")" };
        String[] splitter;
        String tempURL;
        SimpleDateFormat sdf = new SimpleDateFormat("YYYY/MM/DD");
        SimpleDateFormat fiscalYear = new SimpleDateFormat("MM/DD");
        sdf.setLenient(false);
        Set<String> officerList = new HashSet<>();
        List<Double> percentOwnership = new ArrayList<>();
        UrlValidator urlValidator = new UrlValidator();
        Date date = null;
        for (XMLHolder h : xmlBatchHolder) {
            for (Field f : h) {
                mf.loader("Validating fields: ", false);
                if (!f.getType().equals("none") && !f.getValue().equals("*N/A")) {
                    switch (f.getType()) {
                    case "city":
                        if (f.getValue().isEmpty() || f.getValue().equals("")) {
                            f.add("Address is empty");
                        } else {
                            if (hasWhiteSpaceTrailing(f.getValue())) {
                                f.add("has trailing white space ");
                            }
                            if (cityList.indexOf(f.getValue()) < 0) {
                                f.add("City not found on list!");
                            }
                        }

                        break;
                    case "province":
                        if (f.getValue().isEmpty() || f.getValue().equals("")) {
                            f.add("Address is empty");
                        } else {
                            if (hasWhiteSpaceTrailing(f.getValue())) {
                                f.add("has trailing white space ");
                            }
                            if (provinceList.indexOf(f.getValue()) < 0) {
                                f.add("Province not found on list!");
                            }
                        }

                        break;
                    case "tel":
                        if (!f.getValue().isEmpty() || !f.getValue().equals("")) {
                            //                                    if (f.getValue().matches("[a-z A-Z]+")) {
                            if (f.getValue().matches(".*[a-zA-Z]+.*")) {
                                for (String s : validWords) {
                                    if (!f.getValue().contains(s)) {
                                        f.add("Invalid telephone number");
                                    }
                                }
                            }

                            if (f.getValue().replace(" ", "").replace("-", "").length() < 7
                                    || f.getValue().replace(" ", "").replace("-", "").length() > 8) {
                                f.add("Invalid telephone number length");
                            }

                            if (hasWhiteSpaceTrailing(f.getValue())) {
                                f.add("has trailing white space ");
                            }
                            if (StringUtils.countMatches(f.getValue(), "-") > 2) {
                                f.add("Invalid telephone number");
                            }

                            for (String c : invalidCharacters) {
                                if (f.getValue().contains(c)) {
                                    f.add("Contains invalid character [ " + c + " ]");
                                    break;
                                }
                            }
                        }
                        break;
                    case "fax":
                        if (!f.getValue().isEmpty() || !f.getValue().equals("")) {
                            //                                    if (f.getValue().matches("[a-z A-Z]+")) {
                            if (f.getValue().matches(".*[a-zA-Z]+.*")) {
                                for (String s : validWords) {
                                    if (!f.getValue().contains(s)) {
                                        f.add("Invalid fax number");
                                    }
                                }
                            }
                            if (f.getValue().replace(" ", "").length() < 6) {
                                f.add("Invalid fax number");
                            }
                            if (StringUtils.countMatches(f.getValue(), "-") > 1) {
                                f.add("Invalid fax number");
                            }
                            if (hasWhiteSpaceTrailing(f.getValue())) {
                                f.add("has trailing white space ");
                            }
                            for (String c : invalidCharacters) {
                                if (f.getValue().contains(c)) {
                                    f.add("Contains invalid character [ " + c + " ]");
                                    break;
                                }
                            }
                        }
                        break;
                    case "person":
                        if (!f.getValue().isEmpty() || !f.getValue().equals("")) {
                            if (!f.getValue().matches("[a-zA-Z\\.,\\- ()]+")) {
                                f.add("Invalid name");
                            }
                            if (f.getValue().matches("[a-z ]+")) {
                                f.add("All small caps");
                            }
                            if (f.getValue().matches("\\w+")) {
                                f.add("Only one word");
                            }
                            if (f.getValue().replace(" ", "").length() > 30) {
                                f.add("More than 30 characters.");
                            }
                            if (f.getValue().replace(" ", "").length() < 2) {
                                f.add("Invalid name.");
                            }
                            if (hasWhiteSpaceTrailing(f.getValue())) {
                                f.add("has trailing white space ");
                            }
                        }
                        break;
                    case "email":
                        if (!f.getValue().isEmpty() || !f.getValue().equals("")) {
                            if (!EmailValidator.getInstance(true).isValid(f.getValue())) {
                                f.add("Invalid email");
                            }
                        }
                        break;
                    case "website":
                        if (!f.getValue().isEmpty() || !f.getValue().equals("")) {
                            if (!f.getValue().contains("http")) {
                                tempURL = "http://" + f.getValue();
                            } else {
                                tempURL = f.getValue();
                            }
                            if (!urlValidator.isValid(tempURL)) {
                                f.add("Invalid website");
                            }
                            if (hasWhiteSpaceTrailing(f.getValue())) {
                                f.add("has trailing white space ");
                            }
                        }
                        break;
                    case "name":
                        officerList.add(f.getValue());
                        if (!f.getValue().isEmpty() || !f.getValue().equals("")) {
                            if (!f.getValue().matches("[a-zA-Z\\.,\\-() ]+")) {
                                f.add("Invalid name");
                            }
                            if (f.getValue().replace(" ", "").length() > 30) {
                                f.add("More than 50 characters.");
                            }
                            if (f.getValue().matches("[a-z ]+")) {
                                f.add("All small caps");
                            }
                            if (f.getValue().matches("\\w+")) {
                                f.add("Only one word");
                            }
                            if (f.getValue().replace(" ", "").length() < 2) {
                                f.add("Invalid name.");
                            }
                            if (hasWhiteSpaceTrailing(f.getValue())) {
                                f.add("has trailing white space ");
                            }
                            for (String s : invalidWords) {
                                if (f.getValue().contains(s)) {
                                    f.add("Contains invalid word: " + s);
                                    break;
                                }
                            }
                        }
                        break;
                    case "stockholder":
                        officerList.add(f.getValue());
                        if (!f.getValue().isEmpty() || !f.getValue().equals("")) {
                            if (!f.getValue().matches("[a-zA-Z\\.,\\-() ]+")) {
                                f.add("Invalid name");
                            }
                            if (f.getValue().replace(" ", "").length() > 30) {
                                f.add("More than 50 characters.");
                            }
                            if (f.getValue().matches("[a-z ]+")) {
                                f.add("All small caps");
                            }
                            if (f.getValue().matches("\\w+")) {
                                f.add("Only one word");
                            }
                            if (f.getValue().replace(" ", "").length() < 2) {
                                f.add("Invalid name.");
                            }
                            if (hasWhiteSpaceTrailing(f.getValue())) {
                                f.add("has trailing white space ");
                            }
                            for (String s : invalidWords) {
                                if (f.getValue().contains(s)) {
                                    f.add("Contains invalid word: " + s);
                                    break;
                                }
                            }
                        }
                        break;
                    case "board":
                        if (!f.getValue().isEmpty() || !f.getValue().equals("")) {
                            if (!f.getValue().matches("[a-zA-Z\\.,\\-() ]+")) {
                                f.add("Invalid position");
                            }
                            for (String c : invalidCharacters) {
                                if (f.getValue().contains(c)) {
                                    f.add("Contains invalid character [ " + c + " ]");
                                    break;
                                }
                            }
                            for (String c : invalidBoardWords) {
                                if (f.getValue().contains(c)) {
                                    f.add("Contains invalid word [ " + c + " ]");
                                    break;
                                }
                            }

                            if (f.getValue().equalsIgnoreCase("N") || f.getValue().equalsIgnoreCase("Y")) {
                                f.add("is letter " + f.getValue() + " only");
                            }
                            if (Character.isLowerCase(f.getValue().charAt(0))) {
                                f.add("starts with a lower case letter");
                            }

                            spellCheck.setText(f.getValue(), Constants.DOC_TYPE_TEXT, "en");
                            spellCheck.check();
                            if (spellCheck.hasMisspelt()) {
                                f.add("word is misspelled.");
                            }

                        }
                        break;
                    case "corporation":
                        if (companyList.indexOf(f.getValue().toUpperCase()) < 0) {
                            f.add("Company name not found on table.");
                        }
                        break;
                    case "sec":
                        if (StringUtils.countMatches(f.getValue(), "-") > 1) {
                            f.add("Invalid SEC number");
                        }
                        if (f.getValue().replace(" ", "").length() > 9) {
                            f.add("SEC number more than 9 digits.");
                        }
                        if (hasWhiteSpaceTrailing(f.getValue())) {
                            f.add("SEC has trailing white space.");
                        }
                        for (String c : invalidCharacters) {
                            if (f.getValue().contains(c)) {
                                f.add("Contains invalid character [ " + c + " ]");
                                break;
                            }
                        }
                        break;
                    case "tin":
                        if (f.getValue().isEmpty() || f.getValue().equals("")) {
                            f.add("TIN is empty");
                        }
                        if (hasWhiteSpaceTrailing(f.getValue())) {
                            f.add("TIN has trailing white space.");
                        }
                        if (!f.getValue().matches("[0-9]+")) {
                            f.add("invalid TIN number");
                        }
                        if (f.getValue().replace(" ", "").replace("-", "").length() > 12
                                || f.getValue().replace(" ", "").replace("-", "").length() < 9) {
                            f.add("TIN number invalid length.");
                        }
                        if (StringUtils.countMatches(f.getValue(), "-") > 1) {
                            f.add("Invalid TIN number");
                        }
                        for (String c : invalidCharacters) {
                            if (f.getValue().contains(c)) {
                                f.add("Contains invalid character [ " + c + " ]");
                                break;
                            }
                        }
                        break;
                    case "nationality":
                        if (!f.getValue().isEmpty() || !f.getValue().equals("")) {
                            if (nationalityList.indexOf(f.getValue()) < 0) {
                                f.add("nationality is misspelled.");
                            }
                        }
                        break;
                    case "purpose":
                        splitter = f.getValue().split(" ");
                        for (int i = 0; i < splitter.length; i++) {
                            spellCheck.setText(splitter[i], Constants.DOC_TYPE_TEXT, "en");
                            spellCheck.check();
                            if (spellCheck.hasMisspelt()) {
                                f.add("word is misspelled. ( " + spellCheck.getMisspelt() + " )");
                            }
                        }
                        break;
                    case "periodCovered":
                        try {
                            date = sdf.parse(f.getValue());
                            if (!f.getValue().equals(sdf.format(date))) {
                                f.add("Invalid date format");
                            }
                        } catch (ParseException ex) {
                            f.add("Invalid date format");
                        }
                        break;
                    case "fiscalYear":
                        try {
                            date = fiscalYear.parse(f.getValue());
                            if (!f.getValue().equals(sdf.format(date))) {
                                f.add("Invalid date format");
                            }
                        } catch (ParseException ex) {
                            f.add("Invalid date format");
                        }
                        break;
                    case "position":
                        if (f.getValue().contains("\\d+")) {
                            f.add("Invalid position/designation");
                        }
                        if (f.getValue().replace(" ", "").length() > 10
                                || f.getValue().replace(" ", "").length() < 3) {
                            f.add("More than 30 characters.");
                        }
                        break;
                    case "shareType":
                        if (f.getValue().toLowerCase().contains("total")) {
                            f.add("Share type contains total.");
                        }

                        if (f.getValue().replace(" ", "").length() > 20) {
                            f.add("Share type More than 20 characters.");
                        }
                        break;
                    case "ownership":
                        percentOwnership.add(Double.parseDouble(f.getValue()));
                        if (Double.parseDouble(f.getValue()) > 100) {
                            f.add("Percent ownership more than 100%");
                        }
                        break;
                    default:
                        break;
                    }
                } else if (f.getType().equals("tin") && f.getValue().equals("*N/A")) {
                    f.add("TIN is N/A");
                }
            }
        }

    } catch (EncryptedDocumentException | SuggesterException ex) {
        Logger.getLogger(ValidationProcess.class.getName()).log(Level.SEVERE, null, ex);
    }
    return xmlBatchHolder;
}

From source file:com._17od.upm.gui.MainWindow.java

private boolean urlIsValid(String urL) {

    UrlValidator urlValidator = new UrlValidator();
    if (urlValidator.isValid(urL)) {
        return true;
    } else {//from  ww  w.  j  av  a2 s.  co  m
        return false;
    }

}

From source file:com._17od.upm.gui.AccountDialog.java

/**
 * Use com.apache.commons.validator library in order to check the validity
 * (proper formating, e.x http://www.url.com) of the given URL.
 * /*from   w  ww. j av a 2 s . c om*/
 * @param urL
 * @return
 */
private boolean urlIsValid(String urL) {

    UrlValidator urlValidator = new UrlValidator();
    if (urlValidator.isValid(urL)) {
        return true;
    } else {
        return false;
    }

}

From source file:com.ut.healthelink.service.impl.transactionInManagerImpl.java

@Override
public boolean isValidURL(String url) {
    UrlValidator urlValidator = new UrlValidator();
    if (urlValidator.isValid(url)) {
        return true;
    } else {//  w ww. j a v  a 2 s.c  om
        return false;
    }
}

From source file:noThreads.ParseLevel2.java

/**
 * @param theUrl/*from ww w  .  ja va  2  s .  c om*/
 * @param conAttempts
 * @return
 * @throws IOException
 */
public boolean validUrl(String theUrl, int conAttempts) throws IOException {
    long total_time = 0, endTime = 0;
    long startTime = System.currentTimeMillis();
    URL link = new URL(theUrl);
    int CONNECT_TIMEOUT = 5000, READ_TIMEOUT = 2000;

    HttpURLConnection huc = (HttpURLConnection) link.openConnection();
    huc.setRequestProperty("User-Agent", userAgent);
    huc.setConnectTimeout(CONNECT_TIMEOUT);
    huc.setReadTimeout(READ_TIMEOUT);
    try {
        huc.connect();

    } catch (java.net.ConnectException e) {
        print(e.getMessage() + "\n");
        if (e.getMessage().equalsIgnoreCase("Connection timed out")) {
            if (conAttempts != MAX_CONNECTION_ATTEMPTS) {
                print("Recurrencing validUrl method...");
                return validUrl(theUrl, conAttempts + 1);
            } else {
                return false;
            }
        } else {
            return false;
        }

    } catch (java.net.SocketTimeoutException e) {
        print(e.getMessage() + "\n");
        if (e.getMessage().equalsIgnoreCase("connect timed out")) {
            if (conAttempts != MAX_CONNECTION_ATTEMPTS) {
                print("Recurrencing validUrl method...");
                return validUrl(theUrl, conAttempts + 1);
            } else {
                return false;
            }
        } else {
            return false;
        }

    } catch (IOException e) {
        print(e.getMessage() + "\n");
        return false;
    }
    UrlValidator urlValidator = new UrlValidator();
    if (urlValidator.isValid(theUrl) == true) {
        print("valid url form");
        if (huc.getContentType() != null) {
            print("Content: " + huc.getContentType());
            if (huc.getContentType().equals("text/html") || huc.getContentType().equals("unknown/unknown")) {
                if (getResposeCode(theUrl, 0) >= java.net.HttpURLConnection.HTTP_BAD_REQUEST) {
                    print("Server Response Code: " + getResposeCode(theUrl, 0));
                    return false;
                }
            }
            print(huc.getContentType());
            endTime = System.currentTimeMillis();
            total_time = total_time + (endTime - startTime);
            print("Total elapsed time is :" + total_time + "\n");
            return true;
        } else {//edw erxetai an den prolavei na diavasei h an einai null to content
            endTime = System.currentTimeMillis();
            total_time = total_time + (endTime - startTime);
            print("Total elapsed time is :" + total_time + "\n");
            if (conAttempts != MAX_CONNECTION_ATTEMPTS) {
                print("Recurrencing validUrl method...");
                return validUrl(theUrl, conAttempts + 1);
            } else {
                return false;
            }
        }
    } else {
        endTime = System.currentTimeMillis();
        total_time = total_time + (endTime - startTime);
        print("Total elapsed time is :" + total_time + "\n");
        return false;
    }
}

From source file:org.ambraproject.action.article.MediaCoverageAction.java

/**
 * Validate the input from the form/*  ww  w.  ja  v  a2 s.  c  o m*/
 * @return true if everything is ok
 */
private boolean validateInput() {
    // TODO handle data better

    boolean isValid = true;

    if (StringUtils.isBlank(uri)) {
        isValid = false;
    }

    UrlValidator urlValidator = new UrlValidator();

    if (StringUtils.isBlank(link)) {
        addFieldError("link", "This field is required.");
        isValid = false;
    } else if (!urlValidator.isValid(link)) {
        addFieldError("link", "Invalid Media link URL");
        isValid = false;
    }

    if (StringUtils.isBlank(name)) {
        addFieldError("name", "This field is required.");
        isValid = false;
    }

    if (StringUtils.isBlank(email)) {
        addFieldError("email", "This field is required.");
        isValid = false;
    } else if (!EmailValidator.getInstance().isValid(email)) {
        addFieldError("email", "Invalid e-mail address");
        isValid = false;
    }

    HttpServletRequest request = ServletActionContext.getRequest();

    if (!captchaService.validateCaptcha(request.getRemoteAddr(), captchaChallenge, captchaResponse)) {
        addFieldError("captcha", "Verification is incorrect. Please try again.");
        isValid = false;
    }

    if (isValid) {
        this.link = this.link.substring(0, Math.min(this.link.length(), MAX_LENGTH));
        this.name = this.name.substring(0, Math.min(this.name.length(), MAX_LENGTH));
        this.email = this.email.substring(0, Math.min(this.email.length(), MAX_LENGTH));

        if (!StringUtils.isBlank(comment)) {
            this.comment = this.comment.substring(0, Math.min(this.comment.length(), MAX_LENGTH));
        }
    }

    return isValid;
}

From source file:org.apache.streams.urls.LinkResolver.java

public void unwindLink(String url) {
    Objects.requireNonNull(linkDetails);
    Objects.requireNonNull(url);//from  www  .  j  a  v  a  2  s. c o  m

    // Check url validity
    UrlValidator urlValidator = new UrlValidator();
    if (!urlValidator.isValid(url)) {
        linkDetails.setLinkStatus(LinkDetails.LinkStatus.MALFORMED_URL);
        return;
    }

    // Check to see if they wound up in a redirect loop,
    // IE: 'A' redirects to 'B', then 'B' redirects to 'A'
    if ((linkDetails.getRedirectCount() != null && linkDetails.getRedirectCount() > 0
            && (linkDetails.getOriginalURL().equals(url) || linkDetails.getRedirects().contains(url)))
            || (linkDetails.getRedirectCount() != null
                    && linkDetails.getRedirectCount() > MAX_ALLOWED_REDIRECTS)) {
        linkDetails.setLinkStatus(LinkDetails.LinkStatus.LOOP);
        return;
    }

    if (!linkDetails.getOriginalURL().equals(url))
        linkDetails.getRedirects().add(url);

    HttpURLConnection connection = null;

    // Store where the redirected link will go (if there is one)
    String reDirectedLink = null;

    try {
        // Turn the string into a URL
        URL thisURL = new URL(url);

        // Be sensitive to overloading domains STREAMS-77
        try {
            String host = thisURL.getHost().toLowerCase();
            if (!domainsSensitiveTo.contains(host)) {
                domainsSensitiveTo.add(host);
                long domainWait = LinkResolverHelperFunctions.waitTimeForDomain(thisURL.getHost());
                if (domainWait > 0) {
                    LOGGER.debug("Waiting for domain: {}", domainWait);
                    Thread.sleep(domainWait);
                }
            }
        } catch (Exception e) {
            // noOp
        }

        connection = (HttpURLConnection) new URL(url).openConnection();

        // now we are going to pretend that we are a browser...
        // This is the way my mac works.
        if (!BOTS_ARE_OK.contains(thisURL.getHost())) {
            connection.addRequestProperty("Host", thisURL.getHost());

            // Bots are not 'ok', so we need to spoof the headers
            for (String k : SPOOF_HTTP_HEADERS.keySet())
                connection.addRequestProperty(k, SPOOF_HTTP_HEADERS.get(k));

            // the test to seattlemamadoc.com prompted this change.
            // they auto detect bots by checking the referrer chain and the 'user-agent'
            // this broke the t.co test. t.co URLs are EXPLICITLY ok with bots
            // there is a list for URLS that behave this way at the top in BOTS_ARE_OK
            // smashew 2013-13-2013
            if (linkDetails.getRedirectCount() > 0 && BOTS_ARE_OK.contains(thisURL.getHost()))
                connection.addRequestProperty("Referrer", linkDetails.getOriginalURL());
        }

        connection.setReadTimeout(DEFAULT_HTTP_TIMEOUT);
        connection.setConnectTimeout(DEFAULT_HTTP_TIMEOUT);

        // we want to follow this behavior on our own to ensure that we are getting to the
        // proper place. This is especially true with links that are wounded by special
        // link winders,
        // IE:
        connection.setInstanceFollowRedirects(false);

        if (linkDetails.getCookies() != null)
            for (String cookie : linkDetails.getCookies())
                connection.addRequestProperty("Cookie", cookie.split(";", 1)[0]);

        connection.connect();

        linkDetails.setFinalResponseCode((long) connection.getResponseCode());

        Map<String, List<String>> headers = createCaseInsensitiveMap(connection.getHeaderFields());
        /*
         * If they want us to set cookies, well, then we will set cookies
         * Example URL:
         * http://nyti.ms/1bCpesx
         *****************************************************************/
        if (headers.containsKey(SET_COOKIE_IDENTIFIER))
            linkDetails.getCookies().add(headers.get(SET_COOKIE_IDENTIFIER).get(0));

        switch (linkDetails.getFinalResponseCode().intValue()) {
        /*
         * W3C HTTP Response Codes:
         * http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
         */
        case 200: // HTTP OK
            linkDetails.setFinalURL(connection.getURL().toString());
            linkDetails.setDomain(new URL(linkDetails.getFinalURL()).getHost());
            linkDetails.setLinkStatus(LinkDetails.LinkStatus.SUCCESS);
            break;
        case 300: // Multiple choices
        case 301: // URI has been moved permanently
        case 302: // Found
        case 303: // Primarily for a HTTP Post
        case 304: // Not Modified
        case 306: // This status code is unused but in the redirect block.
        case 307: // Temporary re-direct
            /*
             * Author:
             * Smashew
             *
             * Date: 2013-11-15
             *
             * Note:
             * It is possible that we have already found our final URL. In
             * the event that we have found our final URL, we are going to
             * save this URL as long as it isn't the original URL.
             * We are still going to ask the browser to re-direct, but in the
             * case of yet another redirect, seen with the redbull test
             * this can be followed by a 304, a browser, by W3C standards would
             * still render the page with it's content, but for us to assert
             * a success, we are really hoping for a 304 message.
             *******************************************************************/
            if (!linkDetails.getOriginalURL().toLowerCase()
                    .equals(connection.getURL().toString().toLowerCase()))
                linkDetails.setFinalURL(connection.getURL().toString());
            if (!headers.containsKey(LOCATION_IDENTIFIER)) {
                LOGGER.info("Headers: {}", headers);
                linkDetails.setLinkStatus(LinkDetails.LinkStatus.REDIRECT_ERROR);
            } else {
                linkDetails.setRedirected(Boolean.TRUE);
                linkDetails.setRedirectCount(linkDetails.getRedirectCount() + 1);
                reDirectedLink = connection.getHeaderField(LOCATION_IDENTIFIER);
            }
            break;
        case 305: // User must use the specified proxy (deprecated by W3C)
            break;
        case 401: // Unauthorized (nothing we can do here)
            linkDetails.setLinkStatus(LinkDetails.LinkStatus.UNAUTHORIZED);
            break;
        case 403: // HTTP Forbidden (Nothing we can do here)
            linkDetails.setLinkStatus(LinkDetails.LinkStatus.FORBIDDEN);
            break;
        case 404: // Not Found (Page is not found, nothing we can do with a 404)
            linkDetails.setLinkStatus(LinkDetails.LinkStatus.NOT_FOUND);
            break;
        case 500: // Internal Server Error
        case 501: // Not Implemented
        case 502: // Bad Gateway
        case 503: // Service Unavailable
        case 504: // Gateway Timeout
        case 505: // Version not supported
            linkDetails.setLinkStatus(LinkDetails.LinkStatus.HTTP_ERROR_STATUS);
            break;
        default:
            LOGGER.info("Unrecognized HTTP Response Code: {}", linkDetails.getFinalResponseCode());
            linkDetails.setLinkStatus(LinkDetails.LinkStatus.NOT_FOUND);
            break;
        }
    } catch (MalformedURLException e) {
        // the URL is trash, so, it can't load it.
        linkDetails.setLinkStatus(LinkDetails.LinkStatus.MALFORMED_URL);
    } catch (IOException ex) {
        // there was an issue we are going to set to error.
        linkDetails.setLinkStatus(LinkDetails.LinkStatus.ERROR);
    } catch (Exception ex) {
        // there was an unknown issue we are going to set to exception.
        linkDetails.setLinkStatus(LinkDetails.LinkStatus.EXCEPTION);
    } finally {
        // if the connection is not null, then we need to disconnect to close any underlying resources
        if (connection != null)
            connection.disconnect();
    }

    // If there was a redirection, then we have to keep going
    // Placing this code here should help to satisfy ensuring that the connection object
    // is closed successfully.
    if (reDirectedLink != null)
        unwindLink(reDirectedLink);

}

From source file:org.jaqpot.core.service.resource.AlgorithmResource.java

@POST
@Produces({ MediaType.APPLICATION_JSON, "text/uri-list" })
@Path("/{id}")
@ApiOperation(value = "Creates Model", notes = "Applies Dataset and Parameters on Algorithm and creates Model.", response = Task.class)
@org.jaqpot.core.service.annotations.Task
public Response trainModel(@ApiParam(name = "title", required = true) @FormParam("title") String title,
        @ApiParam(name = "description", required = true) @FormParam("description") String description,
        @ApiParam(name = "dataset_uri", defaultValue = DEFAULT_DATASET) @FormParam("dataset_uri") String datasetURI,
        @ApiParam(name = "prediction_feature", defaultValue = DEFAULT_PRED_FEATURE) @FormParam("prediction_feature") String predictionFeature,
        @FormParam("parameters") String parameters,
        @ApiParam(name = "transformations", defaultValue = DEFAULT_TRANSFORMATIONS) @FormParam("transformations") String transformations,
        @ApiParam(name = "scaling", defaultValue = STANDARIZATION) @FormParam("scaling") String scaling, //, allowableValues = SCALING + "," + STANDARIZATION
        @ApiParam(name = "doa", defaultValue = DEFAULT_DOA) @FormParam("doa") String doa,
        @PathParam("id") String algorithmId, @HeaderParam("subjectid") String subjectId)
        throws QuotaExceededException, ParameterIsNullException, ParameterInvalidURIException,
        ParameterTypeException, ParameterRangeException, ParameterScopeException {
    UrlValidator urlValidator = new UrlValidator();

    Algorithm algorithm = algorithmHandler.find(algorithmId);
    if (algorithm == null) {
        throw new NotFoundException("Could not find Algorithm with id:" + algorithmId);
    }//from  ww w  .jav a2s  .  c om

    //Dataset validation should happen only in regression and classification algorithms
    if (algorithm.getOntologicalClasses().contains("ot:Regression")
            || algorithm.getOntologicalClasses().contains("ot:Classification")) {

        if (datasetURI == null) {
            throw new ParameterIsNullException("datasetURI");
        }

        if (!urlValidator.isValid(datasetURI)) {
            throw new ParameterInvalidURIException("Not valid Dataset URI.");
        }

        String datasetId = datasetURI.split("dataset/")[1];
        Dataset datasetMeta = datasetHandler.findMeta(datasetId);

        if (datasetMeta.getTotalRows() != null && datasetMeta.getTotalRows() < 2) {
            throw new BadRequestException("Cannot train model on dataset with less than 2 rows.");
        }
    }

    //Prediction validation should not happen in enm:NoTarget  algorithms
    if (algorithm.getOntologicalClasses().contains("enm:NoTarget")) {
        if (predictionFeature == null) {
            throw new ParameterIsNullException("predictionFeature");
        }
        if (!urlValidator.isValid(predictionFeature)) {
            throw new ParameterInvalidURIException("Not valid Prediction Feature URI.");
        }
    }

    if (title == null) {
        throw new ParameterIsNullException("title");
    }
    if (description == null) {
        throw new ParameterIsNullException("description");
    }

    User user = userHandler.find(securityContext.getUserPrincipal().getName());
    long modelCount = modelHandler.countAllOfCreator(user.getId());
    int maxAllowedModels = new UserFacade(user).getMaxModels();

    if (modelCount > maxAllowedModels) {
        LOG.info(String.format("User %s has %d models while maximum is %d", user.getId(), modelCount,
                maxAllowedModels));
        throw new QuotaExceededException("Dear " + user.getId()
                + ", your quota has been exceeded; you already have " + modelCount + " models. "
                + "No more than " + maxAllowedModels + " are allowed with your subscription.");
    }

    Map<String, Object> options = new HashMap<>();
    options.put("title", title);
    options.put("description", description);
    options.put("dataset_uri", datasetURI);
    options.put("prediction_feature", predictionFeature);
    options.put("subjectid", subjectId);
    options.put("algorithmId", algorithmId);
    options.put("parameters", parameters);
    options.put("base_uri", uriInfo.getBaseUri().toString());
    options.put("creator", securityContext.getUserPrincipal().getName());

    Map<String, String> transformationAlgorithms = new LinkedHashMap<>();
    if (transformations != null && !transformations.isEmpty()) {
        transformationAlgorithms.put(uriInfo.getBaseUri().toString() + "algorithm/pmml",
                "{\"transformations\" : \"" + transformations + "\"}");
    }
    if (scaling != null && !scaling.isEmpty()) {
        transformationAlgorithms.put(scaling, "");
    }
    if (doa != null && !doa.isEmpty()) {
        transformationAlgorithms.put(doa, "");
    }
    if (!transformationAlgorithms.isEmpty()) {
        String transformationAlgorithmsString = serializer.write(transformationAlgorithms);
        LOG.log(Level.INFO, "Transformations:{0}", transformationAlgorithmsString);
        options.put("transformations", transformationAlgorithmsString);
    }

    parameterValidator.validate(parameters, algorithm.getParameters());

    //return Response.ok().build();
    Task task = trainingService.initiateTraining(options, securityContext.getUserPrincipal().getName());

    return Response.ok(task).build();
}

From source file:org.jaqpot.core.service.resource.DatasetResource.java

@POST
@Path("/{id}/qprf")
@ApiOperation("Creates QPRF Report")
@Authorize// ww w .jav  a 2s  .c om
public Response createQPRFReport(
        @ApiParam(value = "Authorization token") @HeaderParam("subjectid") String subjectId,
        @PathParam("id") String id, @FormParam("substance_uri") String substanceURI,
        @FormParam("title") String title, @FormParam("description") String description)
        throws QuotaExceededException {

    User user = userHandler.find(securityContext.getUserPrincipal().getName());
    long reportCount = reportHandler.countAllOfCreator(user.getId());
    int maxAllowedReports = new UserFacade(user).getMaxReports();

    if (reportCount > maxAllowedReports) {
        LOG.info(String.format("User %s has %d algorithms while maximum is %d", user.getId(), reportCount,
                maxAllowedReports));
        throw new QuotaExceededException("Dear " + user.getId()
                + ", your quota has been exceeded; you already have " + reportCount + " reports. "
                + "No more than " + maxAllowedReports + " are allowed with your subscription.");
    }

    Dataset ds = datasetHandler.find(id);
    if (ds == null) {
        throw new NotFoundException("Dataset with id:" + id + " was not found on the server.");
    }
    if (ds.getByModel() == null || ds.getByModel().isEmpty()) {
        throw new BadRequestException("Selected dataset was not produced by a valid model.");
    }
    Model model = modelHandler.find(ds.getByModel());
    if (model == null) {
        throw new BadRequestException("Selected dataset was not produced by a valid model.");
    }
    String datasetURI = model.getDatasetUri();
    if (datasetURI == null || datasetURI.isEmpty()) {
        throw new BadRequestException(
                "The model that created this dataset does not point to a valid training dataset.");
    }
    Dataset trainingDS = client.target(datasetURI).request().accept(MediaType.APPLICATION_JSON)
            .header("subjectid", subjectId).get(Dataset.class);
    if (trainingDS == null) {
        throw new BadRequestException(
                "The model that created this dataset does not point to a valid training dataset.");
    }

    if (model.getTransformationModels() != null) {
        for (String transModelURI : model.getTransformationModels()) {
            Model transModel = modelHandler.find(transModelURI.split("model/")[1]);
            if (transModel == null) {
                throw new NotFoundException(
                        "Transformation model with id:" + transModelURI + " was not found.");
            }
            try {
                trainingDS = jpdiClient
                        .predict(trainingDS, transModel, trainingDS.getMeta(), UUID.randomUUID().toString())
                        .get();
            } catch (InterruptedException ex) {
                LOG.log(Level.SEVERE, "JPDI Training procedure interupted", ex);
                throw new InternalServerErrorException("JPDI Training procedure interupted", ex);
            } catch (ExecutionException ex) {
                LOG.log(Level.SEVERE, "Training procedure execution error", ex.getCause());
                throw new InternalServerErrorException("JPDI Training procedure error", ex.getCause());
            } catch (CancellationException ex) {
                throw new InternalServerErrorException("Procedure was cancelled");
            }
        }
    }

    List<String> retainableFeatures = new ArrayList<>(model.getIndependentFeatures());
    retainableFeatures.addAll(model.getDependentFeatures());

    trainingDS.getDataEntry().parallelStream().forEach(dataEntry -> {
        dataEntry.getValues().keySet().retainAll(retainableFeatures);
    });

    DataEntry dataEntry = ds.getDataEntry().stream()
            .filter(de -> de.getCompound().getURI().equals(substanceURI)).findFirst()
            .orElseThrow(() -> new BadRequestException(""));

    trainingDS.getDataEntry().add(dataEntry);
    trainingDS.getMeta().setCreators(new HashSet<>(Arrays.asList(user.getId())));

    Map<String, Object> parameters = new HashMap<>();

    UrlValidator urlValidator = new UrlValidator();
    if (urlValidator.isValid(substanceURI)) {
        Dataset structures = client.target(substanceURI + "/structures").request()
                .accept(MediaType.APPLICATION_JSON).header("subjectid", subjectId).get(Dataset.class);
        List<Map<String, String>> structuresList = structures.getDataEntry().stream().map(de -> {
            String compound = de.getCompound().getURI();
            String casrn = Optional.ofNullable(de.getValues().get(
                    "https://apps.ideaconsult.net/enmtest/feature/http%3A%2F%2Fwww.opentox.org%2Fapi%2F1.1%23CASRNDefault"))
                    .orElse("").toString();
            String einecs = Optional.ofNullable(de.getValues().get(
                    "https://apps.ideaconsult.net/enmtest/feature/http%3A%2F%2Fwww.opentox.org%2Fapi%2F1.1%23EINECSDefault"))
                    .orElse("").toString();
            String iuclid5 = Optional.ofNullable(de.getValues().get(
                    "https://apps.ideaconsult.net/enmtest/feature/http%3A%2F%2Fwww.opentox.org%2Fapi%2F1.1%23IUCLID5_UUIDDefault"))
                    .orElse("").toString();
            String inchi = Optional.ofNullable(de.getValues().get(
                    "https://apps.ideaconsult.net/enmtest/feature/http%3A%2F%2Fwww.opentox.org%2Fapi%2F1.1%23InChI_stdDefault"))
                    .orElse("").toString();
            String reach = Optional.ofNullable(de.getValues().get(
                    "https://apps.ideaconsult.net/enmtest/feature/http%3A%2F%2Fwww.opentox.org%2Fapi%2F1.1%23REACHRegistrationDateDefault"))
                    .orElse("").toString();
            String iupac = Optional.ofNullable(de.getValues().get(
                    "https://apps.ideaconsult.net/enmtest/feature/http%3A%2F%2Fwww.opentox.org%2Fapi%2F1.1%23IUPACNameDefault"))
                    .orElse("").toString();

            Map<String, String> structuresMap = new HashMap<>();
            structuresMap.put("Compound", compound);
            structuresMap.put("CasRN", casrn);
            structuresMap.put("EC number", einecs);
            structuresMap.put("REACH registration date", reach);
            structuresMap.put("IUCLID 5 Reference substance UUID", iuclid5);
            structuresMap.put("Std. InChI", inchi);
            structuresMap.put("IUPAC name", iupac);

            return structuresMap;
        }).collect(Collectors.toList());
        parameters.put("structures", structuresList);
    } else {
        List<Map<String, String>> structuresList = new ArrayList<>();
        Map<String, String> structuresMap = new HashMap<>();
        structuresMap.put("Compound", "");
        structuresMap.put("CasRN", "");
        structuresMap.put("EC number", "");
        structuresMap.put("REACH registration date", "");
        structuresMap.put("IUCLID 5 Reference substance UUID", "");
        structuresMap.put("Std. InChI", "");
        structuresMap.put("IUPAC name", "");
        structuresList.add(structuresMap);
        parameters.put("structures", structuresList);
    }

    parameters.put("predictedFeature", model.getPredictedFeatures().stream().findFirst()
            .orElseThrow(() -> new BadRequestException("Model does not have a valid predicted feature")));

    parameters.put("algorithm", algorithmHandler.find(model.getAlgorithm().getId()));
    parameters.put("substanceURI", substanceURI);
    if (model.getLinkedModels() != null && !model.getLinkedModels().isEmpty()) {
        Model doa = modelHandler.find(model.getLinkedModels().get(0).split("model/")[1]);
        if (doa != null) {
            parameters.put("doaURI", doa.getPredictedFeatures().get(0));
            parameters.put("doaMethod", doa.getAlgorithm().getId());
        }
    }
    TrainingRequest request = new TrainingRequest();

    request.setDataset(trainingDS);
    request.setParameters(parameters);
    request.setPredictionFeature(model.getDependentFeatures().stream().findFirst()
            .orElseThrow(() -> new BadRequestException("Model does not have a valid prediction feature")));

    Report report = client.target("http://147.102.82.32:8094/pws/qprf").request()
            .header("Content-Type", MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON)
            .post(Entity.json(request), Report.class);

    report.setMeta(MetaInfoBuilder.builder().addTitles(title).addDescriptions(description)
            .addCreators(securityContext.getUserPrincipal().getName()).build());
    report.setId(new ROG(true).nextString(15));
    report.setVisible(Boolean.TRUE);
    reportHandler.create(report);

    return Response.ok(report).build();
}

From source file:org.jaqpot.core.service.resource.DatasetResource.java

@POST
@Path("/{id}/qprf-dummy")
@ApiOperation("Creates QPRF Report")
@Authorize/*  w  w w  .  j  a va  2s.  c  o  m*/
public Response createQPRFReportDummy(
        @ApiParam(value = "Authorization token") @HeaderParam("subjectid") String subjectId,
        @PathParam("id") String id, @FormParam("substance_uri") String substanceURI,
        @FormParam("title") String title, @FormParam("description") String description) {

    Dataset ds = datasetHandler.find(id);
    if (ds == null) {
        throw new NotFoundException("Dataset with id:" + id + " was not found on the server.");
    }
    if (ds.getByModel() == null || ds.getByModel().isEmpty()) {
        throw new BadRequestException("Selected dataset was not produced by a valid model.");
    }
    Model model = modelHandler.find(ds.getByModel());
    if (model == null) {
        throw new BadRequestException("Selected dataset was not produced by a valid model.");
    }
    String datasetURI = model.getDatasetUri();
    if (datasetURI == null || datasetURI.isEmpty()) {
        throw new BadRequestException(
                "The model that created this dataset does not point to a valid training dataset.");
    }
    Dataset trainingDS = client.target(datasetURI).request().accept(MediaType.APPLICATION_JSON)
            .header("subjectid", subjectId).get(Dataset.class);
    if (trainingDS == null) {
        throw new BadRequestException(
                "The model that created this dataset does not point to a valid training dataset.");
    }

    if (model.getTransformationModels() != null) {
        for (String transModelURI : model.getTransformationModels()) {
            Model transModel = modelHandler.find(transModelURI.split("model/")[1]);
            if (transModel == null) {
                throw new NotFoundException(
                        "Transformation model with id:" + transModelURI + " was not found.");
            }
            try {
                trainingDS = jpdiClient
                        .predict(trainingDS, transModel, trainingDS.getMeta(), UUID.randomUUID().toString())
                        .get();
            } catch (InterruptedException ex) {
                LOG.log(Level.SEVERE, "JPDI Training procedure interupted", ex);
                throw new InternalServerErrorException("JPDI Training procedure interupted", ex);
            } catch (ExecutionException ex) {
                LOG.log(Level.SEVERE, "Training procedure execution error", ex.getCause());
                throw new InternalServerErrorException("JPDI Training procedure error", ex.getCause());
            } catch (CancellationException ex) {
                throw new InternalServerErrorException("Procedure was cancelled");
            }
        }
    }

    List<String> retainableFeatures = new ArrayList<>(model.getIndependentFeatures());
    retainableFeatures.addAll(model.getDependentFeatures());

    trainingDS.getDataEntry().parallelStream().forEach(dataEntry -> {
        dataEntry.getValues().keySet().retainAll(retainableFeatures);
    });

    DataEntry dataEntry = ds.getDataEntry().stream()
            .filter(de -> de.getCompound().getURI().equals(substanceURI)).findFirst()
            .orElseThrow(() -> new BadRequestException(""));

    trainingDS.getDataEntry().add(dataEntry);

    Map<String, Object> parameters = new HashMap<>();

    UrlValidator urlValidator = new UrlValidator();
    if (urlValidator.isValid(substanceURI)) {
        Dataset structures = client.target(substanceURI + "/structures").request()
                .accept(MediaType.APPLICATION_JSON).header("subjectid", subjectId).get(Dataset.class);
        List<Map<String, String>> structuresList = structures.getDataEntry().stream().map(de -> {
            String compound = de.getCompound().getURI();
            String casrn = Optional.ofNullable(de.getValues().get(
                    "https://apps.ideaconsult.net/enmtest/feature/http%3A%2F%2Fwww.opentox.org%2Fapi%2F1.1%23CASRNDefault"))
                    .orElse("").toString();
            String einecs = Optional.ofNullable(de.getValues().get(
                    "https://apps.ideaconsult.net/enmtest/feature/http%3A%2F%2Fwww.opentox.org%2Fapi%2F1.1%23EINECSDefault"))
                    .orElse("").toString();
            String iuclid5 = Optional.ofNullable(de.getValues().get(
                    "https://apps.ideaconsult.net/enmtest/feature/http%3A%2F%2Fwww.opentox.org%2Fapi%2F1.1%23IUCLID5_UUIDDefault"))
                    .orElse("").toString();
            String inchi = Optional.ofNullable(de.getValues().get(
                    "https://apps.ideaconsult.net/enmtest/feature/http%3A%2F%2Fwww.opentox.org%2Fapi%2F1.1%23InChI_stdDefault"))
                    .orElse("").toString();
            String reach = Optional.ofNullable(de.getValues().get(
                    "https://apps.ideaconsult.net/enmtest/feature/http%3A%2F%2Fwww.opentox.org%2Fapi%2F1.1%23REACHRegistrationDateDefault"))
                    .orElse("").toString();
            String iupac = Optional.ofNullable(de.getValues().get(
                    "https://apps.ideaconsult.net/enmtest/feature/http%3A%2F%2Fwww.opentox.org%2Fapi%2F1.1%23IUPACNameDefault"))
                    .orElse("").toString();

            Map<String, String> structuresMap = new HashMap<>();
            structuresMap.put("Compound", compound);
            structuresMap.put("CasRN", casrn);
            structuresMap.put("EC number", einecs);
            structuresMap.put("REACH registration date", reach);
            structuresMap.put("IUCLID 5 Reference substance UUID", iuclid5);
            structuresMap.put("Std. InChI", inchi);
            structuresMap.put("IUPAC name", iupac);

            return structuresMap;
        }).collect(Collectors.toList());
        parameters.put("structures", structuresList);
    } else {
        List<Map<String, String>> structuresList = new ArrayList<>();
        Map<String, String> structuresMap = new HashMap<>();
        structuresMap.put("Compound", "");
        structuresMap.put("CasRN", "");
        structuresMap.put("EC number", "");
        structuresMap.put("REACH registration date", "");
        structuresMap.put("IUCLID 5 Reference substance UUID", "");
        structuresMap.put("Std. InChI", "");
        structuresMap.put("IUPAC name", "");
        structuresList.add(structuresMap);
        parameters.put("structures", structuresList);
    }

    parameters.put("predictedFeature", model.getPredictedFeatures().stream().findFirst()
            .orElseThrow(() -> new BadRequestException("Model does not have a valid predicted feature")));

    parameters.put("algorithm", algorithmHandler.find(model.getAlgorithm().getId()));
    parameters.put("substanceURI", substanceURI);
    if (model.getLinkedModels() != null && !model.getLinkedModels().isEmpty()) {
        Model doa = modelHandler.find(model.getLinkedModels().get(0).split("model/")[1]);
        if (doa != null) {
            parameters.put("doaURI", doa.getPredictedFeatures().get(0));
            parameters.put("doaMethod", doa.getAlgorithm().getId());
        }
    }
    TrainingRequest request = new TrainingRequest();

    request.setDataset(trainingDS);
    request.setParameters(parameters);
    request.setPredictionFeature(model.getDependentFeatures().stream().findFirst()
            .orElseThrow(() -> new BadRequestException("Model does not have a valid prediction feature")));

    return Response.ok(request).build();
    //        Report report = client.target("http://147.102.82.32:8094/pws/qprf")
    //                .request()
    //                .header("Content-Type", MediaType.APPLICATION_JSON)
    //                .accept(MediaType.APPLICATION_JSON)
    //                .post(Entity.json(request), Report.class);
    //
    //        report.setMeta(MetaInfoBuilder.builder()
    //                .addTitles(title)
    //                .addDescriptions(description)
    //                .addCreators(securityContext.getUserPrincipal().getName())
    //                .build()
    //        );
    //        report.setId(new ROG(true).nextString(15));
    //        report.setVisible(Boolean.TRUE);
    //        reportHandler.create(report);
    //
    //        return Response.ok(report).build();
}