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:de.ist.clonto.webwiki.InfoboxParser.java

private String removeExternalAnchors(String text) {
    Pattern eanchorPattern = Pattern.compile("\\[.*?\\]");
    Matcher eanchorMatcher = eanchorPattern.matcher(text);

    while (eanchorMatcher.find()) {
        String anchor = eanchorMatcher.group();
        String manchor = anchor.replaceAll("\\[", "");
        manchor = manchor.replaceAll("\\]", "");
        String[] manchorParts = manchor.split("\\s");
        UrlValidator validator = new UrlValidator();
        if (validator.isValid(manchorParts[0])) {
            text = text.replace(anchor, manchor);
        }//from w ww.  j a  va2  s . c  o  m
    }

    return text;
}

From source file:cz.hobrasoft.pdfmu.operation.signature.TimestampParameters.java

@Override
public void setFromNamespace(Namespace namespace) throws OperationException {
    url = namespace.get("tsa_url");

    if (url != null) {
        UrlValidator urlValidator = new UrlValidator();
        if (!urlValidator.isValid(url)) {
            throw new OperationException(SIGNATURE_ADD_TSA_INVALID_URL);
        }//from  www  . j  a  va2s.c  om
    }

    username = namespace.getString("tsa_username");

    passwordArgs.setFromNamespace(namespace);

    sslKeystore.setFromNamespace(namespace);
    if (sslKeystore.file != null) {
        String password = sslKeystore.getPassword();
        if (password == null || password.isEmpty()) {
            LOGGER.warning(
                    "SSL KeyStore: Location has been set but password has not. Only KeyStores protected by a non-empty password are supported.");
        }
    }
    sslKeystore.setSystemProperties(SslKeystore.PRIVATE);

    sslTruststore.setFromNamespace(namespace);
    sslTruststore.setSystemProperties(SslKeystore.TRUSTSTORE);
}

From source file:com.wisc.cs407project.ImageLoader.ImageLoader.java

private Bitmap getBitmap(String url) {
    File f = fileCache.getFile(url);
    Bitmap b = decodeFile(f);//from w w  w  .  ja v  a  2s.  co  m

    //from Local Directory
    BufferedReader in = null;
    UrlValidator validator = new UrlValidator();
    if (new File(url).exists()) {
        b = decodeFile(new File(url));
        if (b != null)
            return b;
    }

    //from web
    try {
        //check SD cache first
        if (b != null)
            return b;

        Bitmap bitmap = null;
        URL imageUrl = new URL(url);
        HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection();
        conn.setConnectTimeout(30000);
        conn.setReadTimeout(30000);
        conn.setInstanceFollowRedirects(true);
        InputStream is = conn.getInputStream();
        OutputStream os = new FileOutputStream(f);
        Utils.CopyStream(is, os);
        os.close();
        conn.disconnect();
        bitmap = decodeFile(f);
        return bitmap;
    } catch (Throwable ex) {
        ex.printStackTrace();
        if (ex instanceof OutOfMemoryError)
            memoryCache.clear();
        return null;
    }
}

From source file:com.github.benchdoos.weblocopener.weblocOpener.gui.EditDialog.java

private void fillTextFieldWithClipboard() {
    String data = "<empty clipboard>";
    try {//from www .  j a  va2  s  . co m
        data = (String) Toolkit.getDefaultToolkit().getSystemClipboard().getData(DataFlavor.stringFlavor);
        URL url = new URL(data);
        UrlValidator urlValidator = new UrlValidator();
        if (urlValidator.isValid(data)) {
            textField.setText(url.toString());
            setTextFieldFont(textField.getFont(), TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
            textField.setCaretPosition(textField.getText().length());
            textField.selectAll();
            log.debug("Got URL from clipboard: " + url);
        }
    } catch (UnsupportedFlavorException | IllegalStateException | HeadlessException | IOException e) {
        textField.setText("");
        log.warn("Can not read URL from clipboard: [" + data + "]", e);
    }
}

From source file:it.cnr.ilc.soapclient.app.Theservice.java

/**
 * This method does the following:/* w  w  w .java2  s.  c o  m*/
 * <ul>
 * <li>Sets the input parameters as service inputs; </li>
 * <li>Checks if the input string is a URL. If so then executes the corresponding methods; </li>
 * <li>Executes the service. </li>
 * </ul>
 */
public void run() {
    String message = "", routine = "run";
    boolean fromUrl = false;

    service.setInputs(inputs);

    UrlValidator defaultValidator = new UrlValidator(); // default schemes
    if (defaultValidator.isValid(input)) {
        fromUrl = true;
        message = String.format("Routine %s. The inputType supplied -%s- requires to be read from a URL. "
                + "So fromUrl is set to %s", routine, input, fromUrl);
        Logger.getLogger(CLASS_NAME).log(Level.INFO, message);

    } else {
        fromUrl = false;
        message = String.format(
                "The inputType supplied -%s- requires to be read from a String. So fromUrl is set to %s", input,
                fromUrl);
        Logger.getLogger(CLASS_NAME).log(Level.INFO, message);
    }

    // Map inputs = new HashMap();
    //inputs.put("output_format", "tagged");
    //freelingIt.setInputs(inputs);
    message = String.format("Calling service %s with fromUrl %s", service.getSERVICE_NAME(), fromUrl);
    Logger.getLogger(CLASS_NAME).log(Level.INFO, message);

    service.runService(input, inputs, fromUrl);
    if (service.getStatus() == 0 && !service.getOutputUrl().isEmpty()) {
        message = String.format("Executed service %s with status %s and output url %s",
                service.getSERVICE_NAME(), service.getStatus(), service.getOutputUrl());
        Logger.getLogger(CLASS_NAME).log(Level.INFO, message);
    }

}

From source file:com.github.benchdoos.weblocopener.weblocOpener.gui.EditDialog.java

private void initTextField(String pathToEditingFile) {
    textField.addMouseListener(new ClickListener() {
        @Override/*from   w  w w.  java 2s  . c o  m*/
        public void doubleClick(MouseEvent e) {
            textField.selectAll();
        }
    });

    textField.getDocument().addDocumentListener(new DocumentListener() {
        @Override
        public void changedUpdate(DocumentEvent e) {
        }

        @Override
        public void insertUpdate(DocumentEvent e) {
            updateTextFont();
        }

        @Override
        public void removeUpdate(DocumentEvent e) {
            updateTextFont();
        }

        private void updateTextFont() {
            UrlValidator urlValidator = new UrlValidator();
            if (urlValidator.isValid(textField.getText())) {
                if (textField != null) {
                    setTextFieldFont(textField.getFont(), TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
                    textField.setForeground(Color.BLUE);
                }
            } else {
                if (textField != null) {
                    setTextFieldFont(textField.getFont(), TextAttribute.UNDERLINE, -1);
                    textField.setForeground(Color.BLACK);
                }
            }
        }

    });

    UndoManager undoManager = new UndoManager();
    textField.getDocument().addUndoableEditListener(new UndoableEditListener() {

        public void undoableEditHappened(UndoableEditEvent evt) {
            undoManager.addEdit(evt.getEdit());
        }

    });

    textField.getActionMap().put("Undo", new AbstractAction("Undo") {
        public void actionPerformed(ActionEvent evt) {
            try {
                if (undoManager.canUndo()) {
                    undoManager.undo();
                }
            } catch (CannotUndoException e) {
            }
        }
    });

    textField.getInputMap().put(KeyStroke.getKeyStroke("control Z"), "Undo");

    textField.getActionMap().put("Redo", new AbstractAction("Redo") {
        public void actionPerformed(ActionEvent evt) {
            try {
                if (undoManager.canRedo()) {
                    undoManager.redo();
                }
            } catch (CannotRedoException e) {
            }
        }
    });

    textField.getInputMap().put(KeyStroke.getKeyStroke("control shift Z"), "Redo");

    fillTextField(pathToEditingFile);
}

From source file:com.github.benchdoos.weblocopener.updater.gui.UpdateDialog.java

private void createGUI() {
    setContentPane(contentPane);/*from   ww w  . j a  va2  s  .  c  om*/
    getRootPane().setDefaultButton(buttonOK);
    if (IS_WINDOWS_XP) {
        //for windows xp&server 2003
        setIconImage(Toolkit.getDefaultToolkit()
                .getImage(UpdateDialog.class.getResource("/images/updaterIcon64_white.png")));
    } else {
        setIconImage(Toolkit.getDefaultToolkit()
                .getImage(UpdateDialog.class.getResource("/images/updaterIcon64.png")));

    }

    createDefaultActionListeners();

    // call onCancel() when cross is clicked
    setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            onCancel();
        }
    });

    // call onCancel() on ESCAPE
    contentPane.registerKeyboardAction(e -> onCancel(), KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
            JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

    updateInfoButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            onUpdateInfoButton();
        }
    });

    updateInfoButton.setCursor(new Cursor(Cursor.HAND_CURSOR));

    manualDownloadButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            openSetupUrl();
        }

        private void openSetupUrl() {
            log.debug("Calling to download setup manually");
            URL url = null;
            if (updater != null) {
                if (updater.getAppVersion() != null) {
                    try {
                        log.debug("Trying to open [" + updater.getAppVersion().getDownloadUrl() + "]");
                        url = new URL(updater.getAppVersion().getDownloadUrl());
                        UrlValidator urlValidator = new UrlValidator();
                        UserUtils.openWebUrl(url);
                    } catch (MalformedURLException e1) {
                        openWebsite(url);
                    }
                } else
                    UserUtils.openWebUrl(ApplicationConstants.UPDATE_WEB_URL);

            } else
                UserUtils.openWebUrl(ApplicationConstants.UPDATE_WEB_URL);
        }

        private void openWebsite(URL url) {
            log.warn("Could not open setup url: [" + url + "]\n" + "Opening "
                    + ApplicationConstants.UPDATE_WEB_URL);
            UserUtils.openWebUrl(ApplicationConstants.UPDATE_WEB_URL);
        }
    });

    pack();
    setLocation(FrameUtils.getFrameOnCenterLocationPoint(this));
    setSize(new Dimension(400, 170));
    setResizable(false);
}

From source file:com.github.benchdoos.weblocopener.weblocOpener.gui.EditDialog.java

private void onOK() {
    try {//from w w w.  jav  a  2 s.  c om
        URL url = new URL(textField.getText());
        UrlValidator urlValidator = new UrlValidator();
        if (urlValidator.isValid(textField.getText())) {
            UrlsProceed.createWebloc(path, url);
            dispose();
        } else {
            throw new MalformedURLException();
        }
    } catch (MalformedURLException e) {
        log.warn("Can not parse URL: [" + textField.getText() + "]", e);

        String message = incorrectUrlMessage + ": [";
        String incorrectUrl = textField.getText().substring(0, Math.min(textField.getText().length(), 10));
        //Fixes EditDialog long url message showing issue
        message += textField.getText().length() > incorrectUrl.length() ? incorrectUrl + "...]"
                : incorrectUrl + "]";

        UserUtils.showWarningMessageToUser(this, errorTitle, message);
    }

}

From source file:com.bitplan.pdfindex.Pdfindexer.java

/**
 * get the sources to index//from w  ww  . ja  v  a  2s . c  om
 * @param pSource - the source to get the files to index from
 * @return - the list of document sournce
 * @throws MalformedURLException - if there is a wrong url
 */
public List<DocumentSource> getFilesToIndex(String pSource) throws MalformedURLException {
    List<DocumentSource> result = new ArrayList<DocumentSource>();
    if (pSource != null) {
        UrlValidator urlValidator = new UrlValidator();
        if (urlValidator.isValid(pSource)) {
            result.add(new DocumentSource(new URL(pSource)));
        } else {
            File sourceFile = new File(pSource);
            if (sourceFile.isFile()) {
                result.add(new DocumentSource(sourceFile));
            } else if (sourceFile.isDirectory()) {
                for (final File file : sourceFile.listFiles()) {
                    if (file.isDirectory()) {
                        result.addAll(getFilesToIndex(file.getAbsolutePath()));
                    }
                    if (file.isFile()) {
                        if (file.getAbsolutePath().toLowerCase().endsWith(".pdf")) {
                            result.add(new DocumentSource(file));
                        }
                    }
                }
            } else {
                throw new IllegalArgumentException("getFilesToIndex failed for '" + pSource
                        + "' it is neither an URI, nor a file nor a directory");
            }
        }
    }
    return result;
}

From source file:ddf.security.pdp.realm.xacml.XacmlPdp.java

protected String getXacmlDataType(String curPermValue) {
    if ("false".equalsIgnoreCase(curPermValue) || "true".equalsIgnoreCase(curPermValue)) {
        return BOOLEAN_DATA_TYPE;
    } else if (IntegerValidator.getInstance().validate(curPermValue) != null) {
        return INTEGER_DATA_TYPE;
    } else if (DoubleValidator.getInstance().validate(curPermValue, Locale.getDefault()) != null) {
        return DOUBLE_DATA_TYPE;
    } else if (TimeValidator.getInstance().validate(curPermValue, "H:mm:ss") != null
            || TimeValidator.getInstance().validate(curPermValue, "H:mm:ss.SSS") != null
            || TimeValidator.getInstance().validate(curPermValue, "H:mm:ssXXX") != null
            || TimeValidator.getInstance().validate(curPermValue, "H:mm:ss.SSSXXX") != null) {
        return TIME_DATA_TYPE;
    } else if (DateValidator.getInstance().validate(curPermValue, "yyyy-MM-dd") != null
            || DateValidator.getInstance().validate(curPermValue, "yyyy-MM-ddXXX") != null) {
        return DATE_DATA_TYPE;
    } else if (CalendarValidator.getInstance().validate(curPermValue, "yyyy-MM-dd:ss'T'H:mm") != null
            || CalendarValidator.getInstance().validate(curPermValue, "yyyy-MM-dd'T'H:mm:ssXXX") != null
            || CalendarValidator.getInstance().validate(curPermValue, "yyyy-MM-dd'T'H:mm:ss.SSS") != null
            || CalendarValidator.getInstance().validate(curPermValue, "yyyy-MM-dd'T'H:mm:ss.SSSXXX") != null
            || CalendarValidator.getInstance().validate(curPermValue, "yyyy-MM-dd'T'H:mm:ss") != null) {
        return DATE_TIME_DATA_TYPE;
    } else if (EmailValidator.getInstance().isValid(curPermValue)) {
        return RFC822_NAME_DATA_TYPE;
    } else if (new UrlValidator().isValid(curPermValue)) {
        return URI_DATA_TYPE;
    } else if (InetAddresses.isUriInetAddress(curPermValue)) {
        return IP_ADDRESS_DATA_TYPE;
    } else {/*from w ww  . jav  a  2s. co m*/

        try {
            if (new X500Name(curPermValue).getRDNs().length > 0) {
                return X500_NAME_DATA_TYPE;
            }
        } catch (IllegalArgumentException e) {

        }
    }
    return STRING_DATA_TYPE;
}