List of usage examples for java.net MalformedURLException MalformedURLException
public MalformedURLException(String msg)
From source file:com.jaeksoft.searchlib.crawler.web.spider.Crawl.java
/** * Download the file and extract content informations * // www .j ava 2 s . c o m * @param httpDownloader */ public DownloadItem download(HttpDownloader httpDownloader) { synchronized (this) { InputStream is = null; DownloadItem downloadItem = null; try { URL url = urlItem.getURL(); if (url == null) throw new MalformedURLException("Malformed URL: " + urlItem.getUrl()); // URL normalisation URI uri = url.toURI(); url = uri.toURL(); credentialItem = credentialManager == null ? null : credentialManager.matchCredential(url); List<CookieItem> cookieList = cookieManager.getCookies(url.toExternalForm()); downloadItem = ClientCatalog.getCrawlCacheManager().loadCache(uri); boolean fromCache = (downloadItem != null); if (!fromCache) downloadItem = httpDownloader.get(uri, credentialItem, null, cookieList); else if (Logging.isDebug) Logging.debug("Crawl cache deliver: " + uri); urlItem.setContentDispositionFilename(downloadItem.getContentDispositionFilename()); urlItem.setContentBaseType(downloadItem.getContentBaseType()); urlItem.setContentTypeCharset(downloadItem.getContentTypeCharset()); urlItem.setContentEncoding(downloadItem.getContentEncoding()); urlItem.setContentLength(downloadItem.getContentLength()); urlItem.setLastModifiedDate(downloadItem.getLastModified()); urlItem.setFetchStatus(FetchStatus.FETCHED); urlItem.setHeaders(downloadItem.getHeaders()); Integer code = downloadItem.getStatusCode(); if (code == null) throw new IOException("Http status is null"); urlItem.setResponseCode(code); redirectUrlLocation = downloadItem.getRedirectLocation(); if (redirectUrlLocation != null) urlItem.setRedirectionUrl(redirectUrlLocation.toURL().toExternalForm()); urlItem.setBacklinkCount(config.getUrlManager().countBackLinks(urlItem.getUrl())); if (code >= 200 && code < 300) { if (!fromCache) is = ClientCatalog.getCrawlCacheManager().storeCache(downloadItem); else is = downloadItem.getContentInputStream(); parseContent(is); } else if (code == 301) { urlItem.setFetchStatus(FetchStatus.REDIR_PERM); } else if (code > 301 && code < 400) { urlItem.setFetchStatus(FetchStatus.REDIR_TEMP); } else if (code >= 400 && code < 500) { urlItem.setFetchStatus(FetchStatus.GONE); } else if (code >= 500 && code < 600) { urlItem.setFetchStatus(FetchStatus.HTTP_ERROR); } } catch (FileNotFoundException e) { Logging.info("FileNotFound: " + urlItem.getUrl()); urlItem.setFetchStatus(FetchStatus.GONE); setError("FileNotFound: " + urlItem.getUrl()); } catch (LimitException e) { Logging.warn(e.toString() + " (" + urlItem.getUrl() + ")"); urlItem.setFetchStatus(FetchStatus.SIZE_EXCEED); setError(e.getMessage()); } catch (InstantiationException e) { Logging.error(e.getMessage(), e); urlItem.setParserStatus(ParserStatus.PARSER_ERROR); setError(e.getMessage()); } catch (IllegalAccessException e) { Logging.error(e.getMessage(), e); urlItem.setParserStatus(ParserStatus.PARSER_ERROR); setError(e.getMessage()); } catch (ClassNotFoundException e) { Logging.error(e.getMessage(), e); urlItem.setParserStatus(ParserStatus.PARSER_ERROR); setError(e.getMessage()); } catch (URISyntaxException e) { Logging.warn(e.getMessage(), e); urlItem.setFetchStatus(FetchStatus.URL_ERROR); setError(e.getMessage()); } catch (MalformedURLException e) { Logging.warn(e.getMessage(), e); urlItem.setFetchStatus(FetchStatus.URL_ERROR); setError(e.getMessage()); } catch (IOException e) { Logging.error(e.getMessage(), e); urlItem.setFetchStatus(FetchStatus.ERROR); setError(e.getMessage()); } catch (IllegalArgumentException e) { Logging.error(e.getMessage(), e); urlItem.setFetchStatus(FetchStatus.ERROR); setError(e.getMessage()); } catch (Exception e) { Logging.error(e.getMessage(), e); urlItem.setFetchStatus(FetchStatus.ERROR); setError(e.getMessage()); } finally { IOUtils.close(is); } return downloadItem; } }
From source file:net.yacy.cora.document.id.MultiProtocolURL.java
/** * Create MultiProtocolURL/* ww w . j av a 2 s . co m*/ * * decoding exception: if url string contains http url with char '%' the url string must be url encoded (percent-escaped) before * as internal encoding is skipped if url string contains '%'. * * @param url '%' char url encoded before * @throws MalformedURLException */ public MultiProtocolURL(String url) throws MalformedURLException { if (url == null) throw new MalformedURLException("url string is null"); this.hostAddress = null; this.contentDomain = null; // identify protocol url = url.trim(); if (url.startsWith("//")) { // patch for urls starting with "//" which can be found in the wild url = "http:" + url; } if (url.startsWith("\\\\")) { url = "smb://" + CommonPattern.BACKSLASH.matcher(url.substring(2)).replaceAll("/"); } if (url.length() > 1 && (url.charAt(1) == ':' && Character.isLetter(url.charAt(0)))) { // maybe a DOS drive path ( A: to z: ) url = "file://" + url; } if (url.length() > 0 && url.charAt(0) == '/') { // maybe a unix/linux absolute path url = "file://" + url; } int p = url.lastIndexOf("://", 5); // lastindexof to look only at the begin of url, up to "https://", if (p < 0) { if (url.length() > 7 && url.substring(0, 7).equalsIgnoreCase("mailto:")) { p = 6; } else { url = "http://" + url; p = 4; } } this.protocol = url.substring(0, p).toLowerCase(Locale.ROOT).trim().intern(); if (url.length() < p + 4) throw new MalformedURLException("URL not parseable: '" + url + "'"); if (!this.protocol.equals("file") && url.substring(p + 1, p + 3).equals("//")) { // identify host, userInfo and file for http and ftp protocol int q = url.indexOf('/', p + 3); if (q < 0) { // check for www.test.com?searchpart q = url.indexOf("?", p + 3); } else { // check that '/' was not in searchpart (example http://test.com?data=1/2/3) if (url.lastIndexOf("?", q) >= 0) { q = url.indexOf("?", p + 3); } } if (q < 0) { // check for www.test.com#fragment q = url.indexOf("#", p + 3); } int r; if (q < 0) { if ((r = url.indexOf('@', p + 3)) < 0) { this.host = url.substring(p + 3).intern(); this.userInfo = null; } else { this.host = url.substring(r + 1).intern(); this.userInfo = url.substring(p + 3, r); } this.path = "/"; } else { this.host = url.substring(p + 3, q).trim().intern(); if ((r = this.host.indexOf('@')) < 0) { this.userInfo = null; } else { this.userInfo = this.host.substring(0, r); this.host = this.host.substring(r + 1).intern(); } this.path = url.substring(q); // may result in "?searchpart" (resolveBackpath prepends a "/" ) } if (this.host.length() < 4 && !this.protocol.equals("file")) throw new MalformedURLException("host too short: '" + this.host + "', url = " + url); if (this.host.indexOf('&') >= 0) throw new MalformedURLException("invalid '&' in host"); this.path = resolveBackpath(this.path); // adds "/" if missing identPort(url, (isHTTP() ? 80 : (isHTTPS() ? 443 : (isFTP() ? 21 : (isSMB() ? 445 : -1))))); if (this.port < 0) { // none of known protocols (above) = unknown throw new MalformedURLException("unknown protocol: " + url); } identAnchor(); identSearchpart(); escape(); } else { url = UTF8.decodeURL(url); // normalization here // this is not a http or ftp url if (this.protocol.equals("mailto")) { // parse email url final int q = url.indexOf('@', p + 3); if (q < 0) { throw new MalformedURLException("wrong email address: " + url); } this.userInfo = url.substring(p + 1, q); this.host = url.substring(q + 1); this.path = ""; // TODO: quick fix, as not always checked for path != null this.port = -1; this.searchpart = null; this.anchor = null; } else if (this.protocol.equals("file")) { // parse file url (RFC 1738 file://host.domain/path file://localhost/path file:///path) // example unix file://localhost/etc/fstab // file:///etc/fstab // example windows file://localhost/c|/WINDOWS/clock.avi // file:///c|/WINDOWS/clock.avi // file://localhost/c:/WINDOWS/clock.avi // network file://hostname/path/to/the%20file.txt // local file:///c:/path/to/the%20file.txt String h = url.substring(p + 1); this.host = null; // host is ignored on file: protocol if (h.startsWith("///")) { //absolute local file path // no host given this.path = h.substring(2); // "/path" or "/c:/path" } else if (h.startsWith("//")) { // "//host/path" or "//host/c:/path" if (h.length() > 4 && h.charAt(3) == ':' && h.charAt(4) != '/' && h.charAt(4) != '\\') { // wrong windows path, after the doublepoint there should be a backslash. Let's add a slash, as it will be slash in the normal form h = h.substring(0, 4) + '/' + h.substring(4); } int q = h.indexOf('/', 2); if (q < 0 || h.length() > 3 && h.charAt(3) == ':') { // Missing root slash such as "path" or "c:/path" accepted, but the path attribute must by after all start with it this.path = "/" + h.substring(2); } else { this.host = h.substring(2, q); // TODO: handle "c:" ? if (this.host.equalsIgnoreCase(Domains.LOCALHOST)) this.host = null; this.path = h.substring(q); // "/path" } } else if (h.startsWith("/")) { // "/host/path" or "/host/c:/path" this.path = h; } this.userInfo = null; this.port = -1; this.searchpart = null; this.anchor = null; } else { throw new MalformedURLException("unknown protocol: " + url); } } // handle international domains if (!Punycode.isBasic(this.host)) try { this.host = toPunycode(this.host); } catch (final PunycodeException e) { } }
From source file:com.netflix.genie.web.services.impl.HttpFileTransferImplTest.java
/** * Make sure can get the last update time of a file. * * @throws GenieException On error//from w w w . ja va2 s. c o m */ @Test(expected = GenieServerException.class) public void cantGetLastModifiedTimeIfNotURL() throws GenieException { try { this.httpFileTransfer.getLastModifiedTime(UUID.randomUUID().toString()); } catch (final GenieServerException e) { Assert.assertTrue(e.getCause() instanceof MalformedURLException); throw e; } finally { Mockito.verify(this.metadataTimerId, Mockito.times(1)) .withTags(MetricsUtils.newFailureTagsMapForException(new MalformedURLException("test"))); Mockito.verify(this.metadataTimer, Mockito.times(1)).record(Mockito.anyLong(), Mockito.eq(TimeUnit.NANOSECONDS)); } }
From source file:de.jetwick.snacktory.HtmlFetcher.java
public String fetchAsString(String urlAsString, int timeout, boolean includeSomeGooseOptions) throws MalformedURLException, IOException { urlAsString = urlAsString.replace("https", "http"); CloseableHttpResponse response = createUrlConnection(urlAsString, timeout, includeSomeGooseOptions, false); if (response.getStatusLine().getStatusCode() > 399) { throw new MalformedURLException(response.getStatusLine().toString()); }//w w w. ja v a 2s. c o m Header header = response.getFirstHeader("Content-Type"); String encoding = null; if (header == null) { encoding = "utf-8"; } else { encoding = header.getValue(); if (encoding == null || !encoding.startsWith("text")) { throw new MalformedURLException("Not an HTML content!"); } } String res = null; try { final HttpEntity body = response.getEntity(); InputStream is; if (encoding != null && encoding.equalsIgnoreCase("gzip")) { is = new GZIPInputStream(body.getContent()); } else if (encoding != null && encoding.equalsIgnoreCase("deflate")) { is = new InflaterInputStream(body.getContent(), new Inflater(true)); } else { is = body.getContent(); } String enc = Converter.extractEncoding(encoding); res = createConverter(urlAsString).streamToString(is, enc); EntityUtils.consume(body); if (logger.isDebugEnabled()) logger.debug(res.length() + " FetchAsString:" + urlAsString); } finally { response.close(); } return res; }
From source file:org.kuali.rice.devtools.jpa.eclipselink.conv.ojb.OjbUtil.java
private static URL buildURL(String repositoryFileName) throws MalformedURLException { //j2ee compliant lookup of resources URL url = ClassHelper.getResource(repositoryFileName); // don't be too strict: if resource is not on the classpath, try ordinary file lookup if (url == null) { try {// w w w. jav a2s . c o m url = new File(repositoryFileName).toURL(); } catch (MalformedURLException ignore) { } } if (url != null) { LOG.info("OJB Descriptor Repository: " + url); } else { throw new MalformedURLException("did not find resource " + repositoryFileName); } return url; }
From source file:org.lockss.util.UrlUtil.java
/** Normalize URL to a canonical form for the specified AU. First * applies any plugin-specific normalization, then generic normalization. * Disallows changes to protocol, host or port on the theory that such * changes constitute more than "normalization". This might be too * strict; transformations such as <code>publisher.com -> * www.publisher.com</code> might fall within the scope of normalization. * @param url the url to normalize//from w w w. j a v a 2 s.c o m * @param au the AU in which to look for a plugin-specific normalizer, or * null for no plugin-specific normalization * @throws MalformedURLException if the plugin's normalizer throws, or if * the URL it returns is malformed. * @throws PluginBehaviorException if the plugin changes the URL in a way * it shouldn't (the protocol, host or port) */ public static String normalizeUrl(String url, ArchivalUnit au) throws MalformedURLException, PluginBehaviorException { String site; if (au == null) { site = url; } else { try { site = au.siteNormalizeUrl(url); } catch (RuntimeException w) { throw new MalformedURLException(url); } if (site != url) { String normSite = normalizeUrl(site); if (normSite.equals(url)) { // ensure return arg if equivalent return normSite; } else { // check whether stem was changed URL origUrl = new URL(url); URL siteUrl = new URL(normSite); if (origUrl.getProtocol().equals(siteUrl.getProtocol()) && origUrl.getHost().equals(siteUrl.getHost()) && isEquivalentPort(origUrl.getProtocol(), origUrl.getPort(), siteUrl.getPort())) { return normSite; } else if (allowSiteNormalizeChangeStem) { // Illegal normalization if either the original stem or the new // one aren't known stems for this AU. // (This constraint is required by the stam -> candidate-AUs // mapping in PluginManager. If the pre-normalized stem isn't // in the map, the AU won't be found. checkFrom: { String origStem = UrlUtil.getUrlPrefix(url); for (String stem : au.getUrlStems()) { if (origStem.equalsIgnoreCase(stem)) { break checkFrom; } } throw new PluginBehaviorException("siteNormalizeUrl(" + url + ") changed stem from " + "one not known for AU: " + url); } checkTo: { String siteStem = UrlUtil.getUrlPrefix(normSite); for (String stem : au.getUrlStems()) { if (siteStem.equalsIgnoreCase(stem)) { break checkTo; } } throw new PluginBehaviorException("siteNormalizeUrl(" + url + ") changed stem to " + "one not known for AU: " + site); } } else { throw new PluginBehaviorException("siteNormalizeUrl(" + url + ") changed stem to " + site); } } } } return normalizeUrl(site); }
From source file:org.openlaszlo.data.HTTPDataSource.java
/** * @param since last modified time to use * @param req/*from w w w . j av a 2 s . c o m*/ * @param url if null, ignored * @param redirCount number of redirs we've done */ public static HttpData getDataOnce(HttpServletRequest req, HttpServletResponse res, long since, String surl, int redirCount, int timeout) throws IOException, HttpException, DataSourceException, MalformedURLException { HttpMethodBase request = null; HostConfiguration hcfg = new HostConfiguration(); /* [todo hqm 2006-02-01] Anyone know why this code was here? It is setting the mime type to something which just confuses the DHTML parser. if (res != null) { res.setContentType("application/x-www-form-urlencoded;charset=UTF-8"); } */ try { // TODO: [2002-01-09 bloch] cope with cache-control // response headers (no-store, no-cache, must-revalidate, // proxy-revalidate). if (surl == null) { surl = getURL(req); } if (surl == null || surl.equals("")) { throw new MalformedURLException( /* (non-Javadoc) * @i18n.test * @org-mes="url is empty or null" */ org.openlaszlo.i18n.LaszloMessages.getMessage(HTTPDataSource.class.getName(), "051018-312")); } String reqType = ""; String headers = ""; if (req != null) { reqType = req.getParameter("reqtype"); headers = req.getParameter("headers"); } boolean isPost = false; mLogger.debug("reqtype = " + reqType); if (reqType != null && reqType.equals("POST")) { request = new LZPostMethod(); request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8"); isPost = true; mLogger.debug("setting POST req method"); } else if (reqType != null && reqType.equals("PUT")) { request = new LZPutMethod(); // todo [hqm 2007] treat PUT like POST? isPost = true; mLogger.debug("setting PUT req method"); } else if (reqType != null && reqType.equals("DELETE")) { request = new LZDeleteMethod(); mLogger.debug("setting DELETE req method"); } else { mLogger.debug("setting GET (default) req method"); request = new LZGetMethod(); } request.getParams().setVersion(mUseHttp11 ? HttpVersion.HTTP_1_1 : HttpVersion.HTTP_1_0); // Proxy the request headers if (req != null) { LZHttpUtils.proxyRequestHeaders(req, request); } // Set headers from query string if (headers != null && headers.length() > 0) { StringTokenizer st = new StringTokenizer(headers, "\n"); while (st.hasMoreTokens()) { String h = st.nextToken(); int i = h.indexOf(":"); if (i > -1) { String n = h.substring(0, i); String v = h.substring(i + 2, h.length()); request.setRequestHeader(n, v); mLogger.debug( /* (non-Javadoc) * @i18n.test * @org-mes="setting header " + p[0] + "=" + p[1] */ org.openlaszlo.i18n.LaszloMessages.getMessage(HTTPDataSource.class.getName(), "051018-359", new Object[] { n, v })); } } } mLogger.debug("Parsing url"); URI uri = LZHttpUtils.newURI(surl); try { hcfg.setHost(uri); } catch (Exception e) { throw new MalformedURLException( /* (non-Javadoc) * @i18n.test * @org-mes="can't form uri from " + p[0] */ org.openlaszlo.i18n.LaszloMessages.getMessage(HTTPDataSource.class.getName(), "051018-376", new Object[] { surl })); } // This gets us the url-encoded (escaped) path and query string String path = uri.getEscapedPath(); String query = uri.getEscapedQuery(); mLogger.debug( /* (non-Javadoc) * @i18n.test * @org-mes="encoded path: " + p[0] */ org.openlaszlo.i18n.LaszloMessages.getMessage(HTTPDataSource.class.getName(), "051018-389", new Object[] { path })); mLogger.debug( /* (non-Javadoc) * @i18n.test * @org-mes="encoded query: " + p[0] */ org.openlaszlo.i18n.LaszloMessages.getMessage(HTTPDataSource.class.getName(), "051018-397", new Object[] { query })); // This call takes a decoded (unescaped) path request.setPath(path); boolean hasQuery = (query != null && query.length() > 0); String rawcontent = null; // Newer rawpost protocol puts lzpostbody as a separate // top level query arg in the request. rawcontent = req.getParameter("lzpostbody"); if (isPost) { // Older rawpost protocol put the "lzpostbody" arg // embedded in the "url" args's query args if (rawcontent == null && hasQuery) { rawcontent = findQueryArg("lzpostbody", query); } if (rawcontent != null) { // Get the unescaped query string ((EntityEnclosingMethod) request).setRequestEntity(new StringRequestEntity(rawcontent)); } else if (hasQuery) { StringTokenizer st = new StringTokenizer(query, "&"); while (st.hasMoreTokens()) { String it = st.nextToken(); int i = it.indexOf("="); if (i > 0) { String n = it.substring(0, i); String v = it.substring(i + 1, it.length()); // POST encodes values during request ((PostMethod) request).addParameter(n, URLDecoder.decode(v, "UTF-8")); } else { mLogger.warn( /* (non-Javadoc) * @i18n.test * @org-mes="ignoring bad token (missing '=' char) in query string: " + p[0] */ org.openlaszlo.i18n.LaszloMessages.getMessage(HTTPDataSource.class.getName(), "051018-429", new Object[] { it })); } } } } else { // This call takes an encoded (escaped) query string request.setQueryString(query); } // Put in the If-Modified-Since headers if (since != -1) { String lms = LZHttpUtils.getDateString(since); request.setRequestHeader(LZHttpUtils.IF_MODIFIED_SINCE, lms); mLogger.debug( /* (non-Javadoc) * @i18n.test * @org-mes="proxying lms: " + p[0] */ org.openlaszlo.i18n.LaszloMessages.getMessage(HTTPDataSource.class.getName(), "051018-450", new Object[] { lms })); } mLogger.debug( /* (non-Javadoc) * @i18n.test * @org-mes="setting up http client" */ org.openlaszlo.i18n.LaszloMessages.getMessage(HTTPDataSource.class.getName(), "051018-460")); HttpClient htc = null; if (mConnectionMgr != null) { htc = new HttpClient(mConnectionMgr); } else { htc = new HttpClient(); } htc.setHostConfiguration(hcfg); // This is the data timeout mLogger.debug( /* (non-Javadoc) * @i18n.test * @org-mes="timeout set to " + p[0] */ org.openlaszlo.i18n.LaszloMessages.getMessage(HTTPDataSource.class.getName(), "051018-478", new Object[] { timeout })); htc.getParams().setSoTimeout(timeout); // Set connection timeout the same htc.getHttpConnectionManager().getParams().setConnectionTimeout(mConnectionTimeout); // Set timeout for getting a connection htc.getParams().setConnectionManagerTimeout(mConnectionPoolTimeout); // TODO: [2003-03-05 bloch] this should be more configurable (per app?) if (!isPost) { request.setFollowRedirects(mFollowRedirects > 0); } long t1 = System.currentTimeMillis(); mLogger.debug("starting remote request"); int rc = htc.executeMethod(hcfg, request); String status = HttpStatus.getStatusText(rc); if (status == null) { status = "" + rc; } mLogger.debug( /* (non-Javadoc) * @i18n.test * @org-mes="remote response status: " + p[0] */ org.openlaszlo.i18n.LaszloMessages.getMessage(HTTPDataSource.class.getName(), "051018-504", new Object[] { status })); HttpData data = null; if (isRedirect(rc) && mFollowRedirects > redirCount) { String loc = request.getResponseHeader("Location").toString(); String hostURI = loc.substring(loc.indexOf(": ") + 2, loc.length()); mLogger.info( /* (non-Javadoc) * @i18n.test * @org-mes="Following URL from redirect: " + p[0] */ org.openlaszlo.i18n.LaszloMessages.getMessage(HTTPDataSource.class.getName(), "051018-517", new Object[] { hostURI })); long t2 = System.currentTimeMillis(); if (timeout > 0) { timeout -= (t2 - t1); if (timeout < 0) { throw new InterruptedIOException( /* (non-Javadoc) * @i18n.test * @org-mes=p[0] + " timed out after redirecting to " + p[1] */ org.openlaszlo.i18n.LaszloMessages.getMessage(HTTPDataSource.class.getName(), "051018-529", new Object[] { surl, loc })); } } data = getDataOnce(req, res, since, hostURI, redirCount++, timeout); } else { data = new HttpData(request, rc); } if (req != null && res != null) { // proxy response headers LZHttpUtils.proxyResponseHeaders(request, res, req.isSecure()); } return data; } catch (ConnectTimeoutException ce) { // Transduce to an InterrupedIOException, since lps takes these to be timeouts. if (request != null) { request.releaseConnection(); } throw new InterruptedIOException( /* (non-Javadoc) * @i18n.test * @org-mes="connecting to " + p[0] + ":" + p[1] + " timed out beyond " + p[2] + " msecs." */ org.openlaszlo.i18n.LaszloMessages.getMessage(HTTPDataSource.class.getName(), "051018-557", new Object[] { hcfg.getHost(), hcfg.getPort(), mConnectionTimeout })); } catch (HttpRecoverableException hre) { if (request != null) { request.releaseConnection(); } throw hre; } catch (HttpException e) { if (request != null) { request.releaseConnection(); } throw e; } catch (IOException ie) { if (request != null) { request.releaseConnection(); } throw ie; } catch (RuntimeException e) { if (request != null) { request.releaseConnection(); } throw e; } }
From source file:com.serli.maven.plugin.quality.mojo.LicenseMojo.java
/** * @param project// www . j av a 2s.c o m * not null * @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 = null; 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.grycap.gpf4med.util.URLUtils.java
/** * Parses a URL from a String. This method supports file-system paths * (e.g. /foo/bar)./*from www . j a va 2 s . co m*/ * @param str String representation of an URL. * @return an URL. * @throws IOException If an input/output error occurs. */ public static URL parseURL(final String str) throws MalformedURLException { URL url = null; if (StringUtils.isNotBlank(str)) { try { url = new URL(str); } catch (MalformedURLException e) { url = null; if (!str.matches("^[a-zA-Z]+[/]*:[^\\\\]")) { // convert path to UNIX path String path = FilenameUtils.separatorsToUnix(str.trim()); final Pattern pattern = Pattern.compile("^([a-zA-Z]:/)"); final Matcher matcher = pattern.matcher(path); path = matcher.replaceFirst("/"); // convert relative paths to absolute paths if (!path.startsWith("/")) { path = path.startsWith("~") ? path.replaceFirst("~", System.getProperty("user.home")) : FilenameUtils.concat(System.getProperty("user.dir"), path); } // normalize path path = FilenameUtils.normalize(path, true); if (StringUtils.isNotBlank(path)) { url = new File(path).toURI().toURL(); } else { throw new MalformedURLException("Invalid path: " + path); } } else { throw e; } } } return url; }
From source file:org.openlaszlo.data.DataSource.java
/** * Get the URL query paramter for this request. * * @param req servlet request object to retrieve URL parameter. * @return the 'URL' for the request./* w ww . j a v a 2 s . co m*/ * @throws MalformedURLException if the url parameter is missing from the request. */ static public String getURL(HttpServletRequest req) throws MalformedURLException { String surl = req.getParameter("url"); if (surl == null) { throw new MalformedURLException( /* (non-Javadoc) * @i18n.test * @org-mes="'url' parameter missing from request" */ org.openlaszlo.i18n.LaszloMessages.getMessage(DataSource.class.getName(), "051018-362")); } return getURL(req, surl); }