Example usage for java.net URL equals

List of usage examples for java.net URL equals

Introduction

In this page you can find the example usage for java.net URL equals.

Prototype

public boolean equals(Object obj) 

Source Link

Document

Compares this URL for equality with another object.

If the given object is not a URL then this method immediately returns false .

Two URL objects are equal if they have the same protocol, reference equivalent hosts, have the same port number on the host, and the same file and fragment of the file.

Two hosts are considered equivalent if both host names can be resolved into the same IP addresses; else if either host name can't be resolved, the host names must be equal without regard to case; or both host names equal to null.

Since hosts comparison requires name resolution, this operation is a blocking operation.

Usage

From source file:misc.TextBatchPrintingDemo.java

/**
 * Called when the print list selection state is changed.  This is the 
 * {@code ListSelectionListener} method.
 *//* ww w.  j  a  va  2s. c om*/
public void valueChanged(ListSelectionEvent e) {
    if (!e.getValueIsAdjusting()) {
        int index = ((JList) e.getSource()).getSelectedIndex();
        if (index >= 0) {
            // Load the currently selected URL into the page browser.
            PageItem item = (PageItem) selectedPages.getModel().getElementAt(index);
            URL page = item.getPage();
            if (!page.equals(pageItem.getPage())) {
                setPage(page);
            }
        }
    }
}

From source file:dip.world.variant.VariantManager.java

/** Returns the URLClassLoader for a given URL, or creates a new one.... */
private static URLClassLoader getClassLoader(URL packageURL) {
    // WARNING: this method is not (itself) threadsafe
    if (packageURL == null) {
        throw new IllegalArgumentException();
    }//from  ww  w . ja  va 2  s .c o  m

    // see if a classloader for this url already exists (cache of 1)
    if (packageURL.equals(vm.currentPackageURL)) {
        return vm.currentUCL;
    }

    vm.currentUCL = new URLClassLoader(new URL[] { packageURL });
    vm.currentPackageURL = packageURL;
    return vm.currentUCL;
}

From source file:org.echocat.nodoodle.server.BaseUrlsDiscovery.java

public List<URL> createBaseUrlsFor(String id, URL preferredBaseUrl) {
    final List<URL> baseUrls = new ArrayList<URL>();
    for (URL baseUrl : _baseUrls) {
        final URL baseUrlForId;
        try {/*ww w  . j  a v a2s  .  c o  m*/
            baseUrlForId = new URL(baseUrl + id);
        } catch (MalformedURLException e) {
            throw new RuntimeException("Could not build the new url from " + baseUrl + " and " + id + ".", e);
        }
        if (!isAvailable(baseUrlForId)) {
            if (baseUrlForId.equals(preferredBaseUrl)) {
                throw new IllegalBaseUrlException(
                        "The preferredBaseUrl '" + preferredBaseUrl + "' is not available.");
            }
        } else if (baseUrlForId.equals(preferredBaseUrl)) {
            baseUrls.add(0, baseUrlForId);
        } else {
            baseUrls.add(baseUrlForId);
        }
    }
    return baseUrls;
}

From source file:org.trianacode.taskgraph.tool.ToolTableImpl.java

/**
 * Deletes the specified tool.//from  www .j  a  v a 2 s  . c  om
 *
 * @param tool tool to be deleted
 */
public void deleteTool(Tool tool, boolean files) {
    URL path = tool.getDefinitionPath();

    if ((path != null) && (!path.equals(""))) {

        File file = UrlUtils.getFile(path);
        if (files) {
            if (file.exists()) {
                file.delete();
            }
            purgeTool(tool);
        } else {
            //Env.addExcludedTool(path);
            purgeToolRef(tool);
        }

    }
}

From source file:misc.TextBatchPrintingDemo.java

/**
 * Synchronize the selection in the print list with the current page item.
 * If the current page item isn't in the print list, clear selection.
 *///from  ww w  .jav  a  2s . com
void updateSelectedPages() {
    ListModel pages = selectedPages.getModel();
    int n = pages.getSize();
    if (n > 0) {
        URL page = pageItem.getPage();
        int index = selectedPages.getSelectedIndex();
        if (index >= 0) {
            PageItem selected = (PageItem) pages.getElementAt(index);
            if (page.equals(selected.getPage())) {
                // Currently displayed page is selected in the print list.
                return;
            }
        }
        for (int i = 0; i < n; i++) {
            PageItem pi = (PageItem) pages.getElementAt(i);
            if (page.equals(pi.getPage())) {
                // Currently displayed page is in the print list, select it.
                selectedPages.setSelectedIndex(i);
                return;
            }
        }
        // Currently displayed page is not in the print list. 
        selectedPages.clearSelection();
    }
}

From source file:com.limegroup.gnutella.licenses.CCLicense.java

/**
 * Adds the given a work with the appropriate details to allWorks.
 *//*from   w w  w  . j a  va 2s. c o m*/
private void addWork(URN urn, String licenseURL) {
    URL url = null;
    try {
        url = new URL(licenseURL);
    } catch (MalformedURLException murl) {
        LOG.warn("Unable to make licenseURL out of: " + licenseURL, murl);
    }

    //See if we can refocus an existing licenseURL.
    Details details = getDetails(urn);
    if (details != null) {
        if (LOG.isDebugEnabled())
            LOG.debug("Found existing details item for URN: " + urn);
        if (url != null) {
            URL guessed = guessLicenseDeed();
            if (guessed != null && guessed.equals(url)) {
                if (LOG.isDebugEnabled())
                    LOG.debug("Updating license URL to be: " + url);
                details.licenseURL = url;
            }
        }

        // Otherwise, not much else we can do.
        // We already have a Details for this URN and it has
        // a licenseURL already.
        return;
    }

    // There's no existing details for this item, so lets add one.
    details = new Details(url);
    if (LOG.isDebugEnabled())
        LOG.debug("Adding new " + details + " for urn: " + urn);

    if (allWorks == null)
        allWorks = new HashMap<URN, Details>(1); // assume it's small.
    allWorks.put(urn, details); // it is fine if urn is null.
}

From source file:com.jaeksoft.searchlib.crawler.web.spider.HtmlArchiver.java

final private String downloadObject(URL parentUrl, String src, String contentType)
        throws ClientProtocolException, IllegalStateException, IOException, SearchLibException,
        URISyntaxException {//  w  ww.j  a v a2 s .  c o  m
    RecursiveEntry recursiveEntry = recursiveSecurity.enter();
    if (recursiveEntry == null) {
        Logging.warn("Max recursion reached - " + recursiveSecurity + " src: " + src + " url: " + parentUrl);
        return src;
    }
    try {
        src = StringEscapeUtils.unescapeXml(src);
        URL objectURL = LinkUtils.getLink(parentUrl, src, null, false);
        if (objectURL == null)
            return src;
        if (objectURL.equals(pageUrl)) {
            return "index.html";
        }
        String urlString = objectURL.toExternalForm();
        String fileName = urlFileMap.get(urlString);
        if (fileName != null)
            return getLocalPath(parentUrl, fileName);
        DownloadItem downloadItem = null;
        try {
            downloadItem = downloader.get(objectURL.toURI(), null);
        } catch (IOException e) {
            Logging.warn("IO Exception on " + objectURL.toURI(), e);
            return src;
        }
        fileName = downloadItem.getFileName();
        if (fileName == null || fileName.length() == 0)
            return src;
        downloadItem.checkNoErrorRange(200, 300);
        String baseName = FilenameUtils.getBaseName(fileName);
        String extension = FilenameUtils.getExtension(fileName);
        if (contentType == null)
            contentType = downloadItem.getContentBaseType();
        if ("text/html".equalsIgnoreCase(contentType))
            extension = "html";
        else if ("text/javascript".equalsIgnoreCase(contentType))
            extension = "js";
        else if ("text/css".equalsIgnoreCase(contentType))
            extension = "css";
        else if ("application/x-shockwave-flash".equalsIgnoreCase(contentType))
            extension = "swf";
        else if ("image/png".equalsIgnoreCase(contentType))
            extension = "png";
        else if ("image/gif".equalsIgnoreCase(contentType))
            extension = "gif";
        else if ("image/jpeg".equalsIgnoreCase(contentType))
            extension = "jpg";
        else if ("image/jpg".equalsIgnoreCase(contentType))
            extension = "jpg";
        File destFile = getAndRegisterDestFile(urlString, baseName, extension);
        if ("css".equals(extension)) {
            String cssContent = downloadItem.getContentAsString();
            StringBuffer sb = checkCSSContent(objectURL, cssContent);
            if (sb != null && sb.length() > 0)
                cssContent = sb.toString();
            FileUtils.write(destFile, cssContent);
        } else
            downloadItem.writeToFile(destFile);

        return getLocalPath(parentUrl, destFile.getName());
    } catch (HttpHostConnectException e) {
        Logging.warn(e);
        return src;
    } catch (UnknownHostException e) {
        Logging.warn(e);
        return src;
    } catch (WrongStatusCodeException e) {
        Logging.warn(e);
        return src;
    } finally {
        recursiveEntry.release();
    }
}

From source file:org.ambraproject.service.trackback.PingbackServiceImpl.java

/**
 * {@inheritDoc}/*from ww w  .  j a  va2s .c  o m*/
 */
@Override
public Long createPingback(URI sourceUri, URI targetUri, String pbServerHost) throws XmlRpcException {
    final URL targetUrl;
    try {
        targetUrl = targetUri.toURL();
    } catch (MalformedURLException e) {
        throw PingbackFault.TARGET_DNE.getException();
    }
    Article target = getArticleFromTargetUri(targetUrl, pbServerHost);

    LinkValidator matchTarget = new LinkValidator() {
        @Override
        public boolean isValid(URL link) {
            // They give us the URL they claim to use, so just check for that without worrying about cross-published URLs
            return targetUrl.equals(link);
        }
    };
    BlogLinkDigest blogInfo;
    try {
        blogInfo = examineBlogPage(sourceUri.toURL(), matchTarget);
    } catch (IOException e) {
        // Generally means that the source page can't be accessed or parsed
        throw PingbackFault.SOURCE_DNE.getException(e);
    }
    if (blogInfo.getLink() == null) {
        throw PingbackFault.NO_LINK_TO_TARGET.getException();
    }

    Long preexisting = (Long) hibernateTemplate.findByCriteria(DetachedCriteria.forClass(Pingback.class)
            .setProjection(Projections.rowCount()).add(Restrictions.eq("url", sourceUri.toString()))
            .add(Restrictions.eq("articleID", target.getID()))).get(0);
    if (preexisting > 0) {
        throw PingbackFault.ALREADY_REGISTERED.getException();
    }

    Pingback pb = new Pingback();
    pb.setUrl(sourceUri.toString());
    pb.setTitle(blogInfo.getTitle());
    pb.setArticleID(target.getID());

    return (Long) hibernateTemplate.save(pb);
}

From source file:org.osaf.cosmo.dav.caldav.report.MultigetReport.java

/**
 * <p>/*w w  w.ja v a 2s .  c  o  m*/
 * Parses the report info, extracting the properties and output filter.
 * </p>
 * <pre>
 * <!ELEMENT calendar-multiget ((DAV:allprop |
 *                              DAV:propname |
 *                              DAV:prop)?, DAV:href+)>
 * </pre>
 *
 * @throws DavException if the report info is not of the correct type
 * @throws BadRequestException if the report info is invalid
 */
protected void parseReport(ReportInfo info) throws DavException {
    if (!getType().isRequestedReportType(info))
        throw new DavException("Report not of type " + getType().getReportName());

    setPropFindProps(info.getPropertyNameSet());
    if (info.containsContentElement(XML_ALLPROP, NAMESPACE)) {
        setPropFindType(PROPFIND_ALL_PROP);
    } else if (info.containsContentElement(XML_PROPNAME, NAMESPACE)) {
        setPropFindType(PROPFIND_PROPERTY_NAMES);
    } else {
        setPropFindType(PROPFIND_BY_PROPERTY);
        setOutputFilter(findOutputFilter(info));
    }

    List<Element> hrefElements = info.getContentElements(XML_HREF, NAMESPACE);
    if (hrefElements.size() == 0)
        throw new BadRequestException("Expected at least one " + QN_HREF);
    if (getResource() instanceof DavContent && hrefElements.size() > 1)
        throw new BadRequestException("Expected at most one " + QN_HREF);

    URL resourceUrl = ((DavResource) getResource()).getResourceLocator().getUrl(true,
            getResource().isCollection());

    hrefs = new HashSet<String>();
    for (Element element : hrefElements) {
        String href = DomUtil.getTextTrim(element);
        // validate and absolutize submitted href
        URL memberUrl = normalizeHref(resourceUrl, href);

        // check if the href refers to the targeted resource (or to a
        // descendent if the target is a collection)
        if (getResource() instanceof DavCollection) {
            if (!isDescendentOrEqual(resourceUrl, memberUrl))
                throw new BadRequestException("Href " + href + " does not refer to the requested collection "
                        + resourceUrl + " or a descendent");
        } else {
            if (!memberUrl.equals(resourceUrl))
                throw new BadRequestException(
                        "Href " + href + " does not refer to the requested resource " + resourceUrl);
        }

        // use the absolute path of our normalized URL as the href
        hrefs.add(memberUrl.getPath());
    }
}

From source file:com.limegroup.gnutella.licenses.CCLicense.java

/**
 * Locates all details that use the given License URL.
 *//*from  w  ww  .  j a  va 2 s . co  m*/
private List<Details> getDetailsForLicenseURL(URL url) {
    if (allWorks == null || url == null)
        return Collections.emptyList();

    List<Details> details = new LinkedList<Details>();
    for (Details detail : allWorks.values()) {
        if (detail.licenseURL != null && url.equals(detail.licenseURL))
            details.add(detail);
    }
    return details;
}