List of usage examples for org.apache.commons.validator.routines UrlValidator isValid
public boolean isValid(String value)
Checks if a field has a valid url address.
From source file:org.apache.ambari.view.capacityscheduler.PropertyValidator.java
private boolean validateUrl(String[] schemas, String urlString) { RegexValidator authorityValidator = new RegexValidator(AUTHORITY_REGEX); UrlValidator validator = new UrlValidator(schemas, authorityValidator, UrlValidator.ALLOW_LOCAL_URLS); return validator.isValid(urlString); }
From source file:org.apache.ambari.view.hive.PropertyValidator.java
private boolean validateURL(String webhdfsUrl, String[] schemes) { RegexValidator authority = new RegexValidator(".*"); UrlValidator urlValidator = new UrlValidator(schemes, authority, UrlValidator.ALLOW_LOCAL_URLS); return urlValidator.isValid(webhdfsUrl); }
From source file:org.apache.ambari.view.utils.ambari.ValidatorUtils.java
public static boolean validateURL(String webhdfsUrl, String[] schemes) { RegexValidator authority = new RegexValidator(".*"); UrlValidator urlValidator = new UrlValidator(schemes, authority, UrlValidator.ALLOW_LOCAL_URLS); return urlValidator.isValid(webhdfsUrl); }
From source file:org.apache.cxf.fediz.service.idp.beans.PassiveRequestorValidator.java
public boolean isValid(RequestContext context, String endpointAddress, String realm) throws Exception { if (endpointAddress == null) { return true; }/*ww w. j a v a2s. c om*/ Idp idpConfig = (Idp) WebUtils.getAttributeFromFlowScope(context, "idpConfig"); Application serviceConfig = idpConfig.findApplication(realm); if (serviceConfig == null) { LOG.warn("No service config found for " + realm); return true; } // The endpointAddress address must match the passive endpoint requestor constraint // (if it is specified) // Also, it must be a valid URL + start with https // Validate it first using commons-validator UrlValidator urlValidator = new UrlValidator( UrlValidator.ALLOW_LOCAL_URLS + UrlValidator.ALLOW_ALL_SCHEMES); if (!urlValidator.isValid(endpointAddress)) { LOG.warn("The given endpointAddress parameter {} is not a valid URL", endpointAddress); return false; } if (serviceConfig.getCompiledPassiveRequestorEndpointConstraint() == null) { LOG.warn("No passive requestor endpoint constraint is configured for the application. " + "This could lead to a malicious redirection attack"); return true; } Matcher matcher = serviceConfig.getCompiledPassiveRequestorEndpointConstraint().matcher(endpointAddress); if (!matcher.matches()) { LOG.error("The endpointAddress value of {} does not match any of the passive requestor values", endpointAddress); return false; } return true; }
From source file:org.apache.cxf.fediz.service.oidc.clients.ClientRegistrationService.java
private boolean isValidURI(String uri, boolean requireHttps) { UrlValidator urlValidator = null; if (requireHttps) { String[] schemes = { "https" }; urlValidator = new UrlValidator(schemes, UrlValidator.ALLOW_LOCAL_URLS); } else {//from w w w . j a va 2 s . c om urlValidator = new UrlValidator(UrlValidator.ALLOW_LOCAL_URLS + UrlValidator.ALLOW_ALL_SCHEMES); } if (!urlValidator.isValid(uri)) { return false; } // Do additional checks on the URI try { URI parsedURI = new URI(uri); // The URI can't have a fragment according to the OAuth 2.0 spec (+ audience spec) if (parsedURI.getFragment() != null) { return false; } } catch (URISyntaxException ex) { return false; } return true; }
From source file:org.apache.maven.report.projectinfo.LicenseReport.java
/** * @param project not null// www . j av a 2 s.c o m * @param url not null * @return a valid URL object from the url string * @throws IOException if any */ protected static URL getLicenseURL(MavenProject project, String url) throws IOException { URL licenseUrl; UrlValidator urlValidator = new UrlValidator(UrlValidator.ALLOW_ALL_SCHEMES); // UrlValidator does not accept file URLs because the file // URLs do not contain a valid authority (no hostname). // As a workaround accept license URLs that start with the // file scheme. if (urlValidator.isValid(url) || StringUtils.defaultString(url).startsWith("file://")) { try { licenseUrl = new URL(url); } catch (MalformedURLException e) { throw new MalformedURLException( "The license url '" + url + "' seems to be invalid: " + e.getMessage()); } } else { File licenseFile = new File(project.getBasedir(), url); if (!licenseFile.exists()) { // Workaround to allow absolute path names while // staying compatible with the way it was... licenseFile = new File(url); } if (!licenseFile.exists()) { throw new IOException("Maven can't find the file '" + licenseFile + "' on the system."); } try { licenseUrl = licenseFile.toURI().toURL(); } catch (MalformedURLException e) { throw new MalformedURLException( "The license url '" + url + "' seems to be invalid: " + e.getMessage()); } } return licenseUrl; }
From source file:org.apache.rave.portal.web.validator.WidgetValidatorTest.java
@Test public void testValidation() throws Exception { RegexValidator regex = new RegexValidator( new String[] { "http", "https", "((localhost)(:[0-9]+))", ".*\\.linux-server(:[0-9]+)" }); UrlValidator validator = new UrlValidator(regex, 0); assertTrue("localhost URL should validate", validator.isValid("http://localhost:8080/demogadgets/CTSSResourcesMapView.xml")); assertTrue("127.0.0.1 should validate", validator.isValid("http://127.0.0.1:8080/demogadgets/CTSSResourcesMapView.xml")); assertTrue("my.linux-server should validate", validator.isValid("http://my.linux-server:8080/demogadgets/CTSSResourcesMapView.xml")); assertFalse("broke.my-test should not validate", validator.isValid("http://broke.my-test/test/index.html")); assertTrue("www.apache.org should still validate", validator.isValid("http://www.apache.org/test/index.html")); }
From source file:org.apache.stratos.adc.mgt.cli.StratosApplication.java
/** * @return {@code true} if required properties are loaded *///from w ww .j a v a2 s . c om private boolean loadRequiredProperties() { if (logger.isDebugEnabled()) { logger.debug("Loading properties..."); } // Load properties String stratosURL = null; String username = null; String password = null; stratosURL = System.getenv(CliConstants.STRATOS_URL_ENV_PROPERTY); username = System.getenv(CliConstants.STRATOS_USERNAME_ENV_PROPERTY); password = System.getenv(CliConstants.STRATOS_PASSWORD_ENV_PROPERTY); if (StringUtils.isBlank(stratosURL)) { if (logger.isDebugEnabled()) { logger.debug("Required configuration not found."); } // Stratos Controller details are not set. System.out.format("Could not find required \"%s\" variable in your environment.%n", CliConstants.STRATOS_URL_ENV_PROPERTY); return false; } else { if (logger.isDebugEnabled()) { logger.debug("Required configuration found. Validating {}", stratosURL); } UrlValidator urlValidator = new UrlValidator(new String[] { "https" }); if (!urlValidator.isValid(stratosURL)) { if (logger.isDebugEnabled()) { logger.debug("Stratos Controller URL {} is not valid", stratosURL); } System.out.format( "The \"%s\" variable in your environment is not a valid URL. You have provided \"%s\".%n" + "Please provide the Stratos Controller URL as follows%nhttps://<host>:<port>%n", CliConstants.STRATOS_URL_ENV_PROPERTY, stratosURL); return false; } if (logger.isDebugEnabled()) { logger.debug("Stratos Controller URL {} is valid.", stratosURL); logger.debug("Adding the values to context."); } context.put(CliConstants.STRATOS_URL_ENV_PROPERTY, stratosURL); context.put(CliConstants.STRATOS_USERNAME_ENV_PROPERTY, username); context.put(CliConstants.STRATOS_PASSWORD_ENV_PROPERTY, password); return true; } }
From source file:org.apache.stratos.cli.StratosApplication.java
/** * @return {@code true} if required properties are loaded */// ww w. j a v a 2 s . c om private boolean loadRequiredProperties() { if (logger.isDebugEnabled()) { logger.debug("Loading properties..."); } // Load properties String stratosURL = null; String username = null; String password = null; stratosURL = System.getenv(CliConstants.STRATOS_URL_ENV_PROPERTY); username = System.getenv(CliConstants.STRATOS_USERNAME_ENV_PROPERTY); password = System.getenv(CliConstants.STRATOS_PASSWORD_ENV_PROPERTY); int slashCount = StringUtils.countMatches(stratosURL, "/"); int colonCount = StringUtils.countMatches(stratosURL, ":"); if (!(colonCount == 2 && (slashCount == 3 || slashCount == 2))) { if (logger.isDebugEnabled()) { logger.debug("Invalid STRATOS_URL"); } System.out.println("Invalid STRATOS_URL. Please enter correct STRATOS_URL"); return false; } if (StringUtils.isBlank(stratosURL)) { if (logger.isDebugEnabled()) { logger.debug("Required configuration not found."); } // Stratos Controller details are not set. System.out.format("Could not find required \"%s\" variable in your environment.%n", CliConstants.STRATOS_URL_ENV_PROPERTY); return false; } else { if (logger.isDebugEnabled()) { logger.debug("Required configuration found. Validating {}", stratosURL); } UrlValidator urlValidator = new UrlValidator(new String[] { "https" }, UrlValidator.ALLOW_LOCAL_URLS); if (!urlValidator.isValid(stratosURL)) { if (logger.isDebugEnabled()) { logger.debug("Stratos Controller URL {} is not valid", stratosURL); } System.out.format( "The \"%s\" variable in your environment is not a valid URL. You have provided \"%s\".%n" + "Please provide the Stratos Controller URL as follows%nhttps://<host>:<port>%n", CliConstants.STRATOS_URL_ENV_PROPERTY, stratosURL); return false; } if (logger.isDebugEnabled()) { logger.debug("Stratos Controller URL {} is valid.", stratosURL); logger.debug("Adding the values to context."); } context.put(CliConstants.STRATOS_URL_ENV_PROPERTY, stratosURL); context.put(CliConstants.STRATOS_USERNAME_ENV_PROPERTY, username); context.put(CliConstants.STRATOS_PASSWORD_ENV_PROPERTY, password); return true; } }
From source file:org.apache.streams.urls.LinkResolver.java
public void unwindLink(String url) { Objects.requireNonNull(linkDetails); Objects.requireNonNull(url);//from w w w . j a v a 2 s . co 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); }