Example usage for org.apache.commons.httpclient URI getScheme

List of usage examples for org.apache.commons.httpclient URI getScheme

Introduction

In this page you can find the example usage for org.apache.commons.httpclient URI getScheme.

Prototype

public String getScheme() 

Source Link

Document

Get the scheme.

Usage

From source file:org.zaproxy.zap.extension.spider.SpiderAPI.java

/**
 * Starts a spider scan at the given {@code url} and, optionally, with the perspective of the given {@code user}.
 * <p>//  w  ww .  java2s  .co m
 * The {@code scanIdCounter} is used to generate the ID of the started spider scan. The started {@code SpiderScan} is saved
 * in {@code spiderScans} for later access/control, accessible with the returned ID.
 * </p>
 * 
 * @param url the url to start the spider scan
 * @param user the user to scan as, or null if the scan is done without the perspective of any user
 * @param maxChildren Max number of children to scan
 * @param recurse Whether or not to scan recursively
 * @param context the context that will be used during spider process, might be {@code null}
 * @return the ID of the newly started scan
 * @throws ApiException if the {@code url} is not valid
 * @see #scanIdCounter
 * @see #spiderScans
 */
private int scanURL(String url, User user, int maxChildren, boolean recurse, Context context)
        throws ApiException {
    log.debug("API Spider scanning url: " + url);

    URI startURI;
    try {
        // Try to build uri
        startURI = new URI(url, true);
    } catch (URIException e) {
        throw new ApiException(ApiException.Type.ILLEGAL_PARAMETER, PARAM_URL);
    }
    String scheme = startURI.getScheme();
    if (scheme == null || (!scheme.equalsIgnoreCase("http") && !scheme.equalsIgnoreCase("https"))) {
        throw new ApiException(ApiException.Type.ILLEGAL_PARAMETER, PARAM_URL);
    }

    StructuralNode node = null;
    try {
        node = SessionStructure.find(Model.getSingleton().getSession().getSessionId(), new URI(url, false),
                "GET", "");
    } catch (Exception e) {
        throw new ApiException(ApiException.Type.INTERNAL_ERROR);
    }
    Target target = new Target(node);
    target.setRecurse(recurse);
    if (context != null) {
        target.setContext(context);
    }

    List<Object> objs = new ArrayList<>(maxChildren > 0 ? 3 : 1);
    objs.add(startURI);
    if (maxChildren > 0) {
        // Add the filters to filter on maximum number of children
        MaxChildrenFetchFilter maxChildrenFetchFilter = new MaxChildrenFetchFilter();
        maxChildrenFetchFilter.setMaxChildren(maxChildren);
        maxChildrenFetchFilter.setModel(extension.getModel());

        MaxChildrenParseFilter maxChildrenParseFilter = new MaxChildrenParseFilter();
        maxChildrenParseFilter.setMaxChildren(maxChildren);
        maxChildrenParseFilter.setModel(extension.getModel());
        objs.add(maxChildrenFetchFilter);
        objs.add(maxChildrenParseFilter);
    }

    return extension.startScan(target.getDisplayName(), target, user, objs.toArray(new Object[objs.size()]));
}

From source file:org.zaproxy.zap.extension.stats.StatsAPI.java

@Override
public ApiResponse handleApiView(String name, JSONObject params) throws ApiException {
    ApiResponse result = null;/* ww  w  .j a v a2  s.c o  m*/
    InMemoryStats memStats = extension.getInMemoryStats();
    if (memStats == null) {
        throw new ApiException(ApiException.Type.DOES_NOT_EXIST);
    }

    if (VIEW_STATS.equals(name)) {
        Map<String, String> map = new TreeMap<>();

        for (Entry<String, Long> stat : memStats.getStats(this.getParam(params, PARAM_KEY_PREFIX, ""))
                .entrySet()) {
            map.put(stat.getKey(), stat.getValue().toString());
        }
        result = new ApiResponseSet<String>(name, map);

    } else if (VIEW_ALL_SITES_STATS.equals(name)) {
        result = new ApiResponseList(name);
        for (Entry<String, Map<String, Long>> stats : memStats
                .getAllSiteStats(this.getParam(params, PARAM_KEY_PREFIX, "")).entrySet()) {
            ((ApiResponseList) result).addItem(new SiteStatsApiResponse(stats.getKey(), stats.getValue()));
        }

    } else if (VIEW_SITE_STATS.equals(name)) {
        String site = params.getString(PARAM_SITE);
        URI siteURI;

        try {
            siteURI = new URI(site, true);
            site = SessionStructure.getHostName(siteURI);
        } catch (Exception e) {
            throw new ApiException(ApiException.Type.ILLEGAL_PARAMETER, PARAM_SITE);
        }
        String scheme = siteURI.getScheme();
        if (scheme == null || (!scheme.equalsIgnoreCase("http") && !scheme.equalsIgnoreCase("https"))) {
            throw new ApiException(ApiException.Type.ILLEGAL_PARAMETER, PARAM_SITE);
        }

        result = new SiteStatsApiResponse(site,
                memStats.getSiteStats(site, this.getParam(params, PARAM_KEY_PREFIX, "")));

    } else {
        throw new ApiException(ApiException.Type.BAD_VIEW);
    }
    return result;
}

From source file:org.zaproxy.zap.model.SessionStructure.java

public static String getHostName(URI uri) throws URIException {
    StringBuilder host = new StringBuilder();

    String scheme = uri.getScheme().toLowerCase();
    host.append(scheme).append("://").append(uri.getHost());

    int port = uri.getPort();
    if (port != -1 && ((port == 80 && !"http".equals(scheme))
            || (port == 443 && !"https".equals(scheme) || (port != 80 && port != 443)))) {
        host.append(":").append(port);
    }/* ww w  .j a v  a2  s .  c om*/

    return host.toString();
}

From source file:org.zaproxy.zap.spider.filters.DefaultFetchFilter.java

@Override
public FetchStatus checkFilter(URI uri) {

    log.debug("Checking: " + uri);
    // Protocol check
    String scheme = uri.getScheme();
    if (scheme == null || (!scheme.equalsIgnoreCase("http") && !scheme.equalsIgnoreCase("https"))) {
        return FetchStatus.ILLEGAL_PROTOCOL;
    }/*from w w  w.  j  av a 2s  .c  o  m*/

    try {

        // Context check
        if (this.scanContext != null)
            if (!this.scanContext.isInContext(uri.toString()))
                return FetchStatus.OUT_OF_CONTEXT;

        // Scope check
        boolean ok = false;
        String host = uri.getHost();
        for (String scope : scopes) {
            if (host.matches(scope)) {
                ok = true;
                break;
            }
        }
        for (DomainAlwaysInScopeMatcher domainInScope : domainsAlwaysInScope) {
            if (domainInScope.matches(host)) {
                ok = true;
                break;
            }
        }

        if (!ok) {
            return FetchStatus.OUT_OF_SCOPE;
        }

        // Check if any of the exclusion regexes match.
        if (excludeList != null) {
            String uriS = uri.toString();
            for (String ex : excludeList) {
                if (uriS.matches(ex)) {
                    return FetchStatus.USER_RULES;
                }
            }
        }

    } catch (URIException e) {
        log.warn("Error while fetching host for uri: " + uri, e);
        return FetchStatus.OUT_OF_SCOPE;
    }

    return FetchStatus.VALID;
}

From source file:org.zaproxy.zap.spider.Spider.java

/**
 * Adds a new seed for the Spider./*from  w  w  w . j a v a  2s  .  c o m*/
 * 
 * @param uri the uri
 */
public void addSeed(URI uri) {
    // Update the scope of the spidering process
    String host = null;

    try {
        host = uri.getHost();
        defaultFetchFilter.addScopeRegex(host);
    } catch (URIException e) {
        log.error("There was an error while adding seed value: " + uri, e);
        return;
    }
    // Add the seed to the list -- it will be added to the task list only when the spider is
    // started
    this.seedList.add(uri);
    // Add the appropriate 'robots.txt' as a seed
    if (getSpiderParam().isParseRobotsTxt()) {
        try {
            // Build the URI of the robots.txt file
            URI robotsUri;
            // If the port is not 80 or 443, add it to the URI
            if (uri.getPort() == 80 || uri.getPort() == 443) {
                robotsUri = new URI(uri.getScheme() + "://" + host + "/robots.txt", true);
            } else {
                robotsUri = new URI(uri.getScheme() + "://" + host + ":" + uri.getPort() + "/robots.txt", true);
            }
            this.seedList.add(robotsUri);
        } catch (Exception e) {
            log.warn("Error while creating URI for robots.txt file for site " + uri, e);
        }
    }
    // Add the appropriate 'sitemap.xml' as a seed
    if (getSpiderParam().isParseSitemapXml()) {
        try {
            // Build the URI of the sitemap.xml file
            URI sitemapUri;
            // If the port is not 80 or 443, add it to the URI
            if (uri.getPort() == 80 || uri.getPort() == 443) {
                sitemapUri = new URI(uri.getScheme() + "://" + host + "/sitemap.xml", true);
            } else {
                sitemapUri = new URI(uri.getScheme() + "://" + host + ":" + uri.getPort() + "/sitemap.xml",
                        true);
            }
            this.seedList.add(sitemapUri);
        } catch (Exception e) {
            log.warn("Error while creating URI for sitemap.xml file for site " + uri, e);
        }
    }
    // And add '.svn/entries' as a seed, for SVN based spidering
    if (getSpiderParam().isParseSVNEntries()) {
        try {
            URI svnEntriesURI1, svnEntriesURI2;
            // If the port is not 80 or 443, add it to the URI
            // SVN entries can exist in multiple directories, so make sure to add in the full path.
            String fullpath = uri.getPath();
            String name = uri.getName();
            if (fullpath == null)
                fullpath = "";
            if (name == null)
                name = "";

            String pathminusfilename = fullpath.substring(0, fullpath.lastIndexOf(name));
            if (pathminusfilename.equals(""))
                pathminusfilename = "/";

            //if it's not an svn folder, add the seeds.
            Matcher matcherSvnUrl = svnUrlPattern.matcher(pathminusfilename);
            if (!matcherSvnUrl.find()) {
                if (uri.getPort() == 80 || uri.getPort() == 443) {
                    svnEntriesURI1 = new URI(
                            uri.getScheme() + "://" + host + pathminusfilename + ".svn/entries", true);
                    svnEntriesURI2 = new URI(uri.getScheme() + "://" + host + pathminusfilename + ".svn/wc.db",
                            true);
                } else {
                    svnEntriesURI1 = new URI(uri.getScheme() + "://" + host + ":" + uri.getPort()
                            + pathminusfilename + ".svn/entries", true);
                    svnEntriesURI2 = new URI(uri.getScheme() + "://" + host + ":" + uri.getPort()
                            + pathminusfilename + ".svn/wc.db", true);
                }
                this.seedList.add(svnEntriesURI1);
                this.seedList.add(svnEntriesURI2);
            }
        } catch (Exception e) {
            log.warn("Error while creating a seed URI for the SVN files for site " + uri, e);
        }
    }

    // And add '.git/index' as a seed, for Git based spidering
    if (getSpiderParam().isParseGit()) {
        try {
            URI gitEntriesURI;
            // If the port is not 80 or 443, add it to the URI
            // Make sure to add in the full path.
            String fullpath = uri.getPath();
            String name = uri.getName();
            if (fullpath == null)
                fullpath = "";
            if (name == null)
                name = "";

            String pathminusfilename = fullpath.substring(0, fullpath.lastIndexOf(name));
            if (pathminusfilename.equals(""))
                pathminusfilename = "/";

            //if it's not in a Git folder, add the seed.
            Matcher matcherGitUrl = gitUrlPattern.matcher(pathminusfilename);
            if (!matcherGitUrl.find()) {
                if (uri.getPort() == 80 || uri.getPort() == 443) {
                    gitEntriesURI = new URI(uri.getScheme() + "://" + host + pathminusfilename + ".git/index",
                            true);
                } else {
                    gitEntriesURI = new URI(uri.getScheme() + "://" + host + ":" + uri.getPort()
                            + pathminusfilename + ".git/index", true);
                }
                this.seedList.add(gitEntriesURI);
            }
        } catch (Exception e) {
            log.warn("Error while creating a seed URI for the Git files for site " + uri, e);
        }
    }

}

From source file:phex.download.MagnetData.java

public static MagnetData parseFromURI(URI uri) {
    String protocol = uri.getScheme();
    if (!"magnet".equals(protocol)) {
        return null;
    }// w  ww.j  a v a 2  s.  co  m

    MagnetData magnetData = new MagnetData();

    String urlQuery = uri.getEscapedQuery();

    StringTokenizer tokenizer = new StringTokenizer(urlQuery, "&");
    while (tokenizer.hasMoreTokens()) {
        String param = tokenizer.nextToken().trim();
        int seperatorIdx = param.indexOf('=');
        if (seperatorIdx == -1) {// no = found.
            continue;
        }
        String key = param.substring(0, seperatorIdx);
        String value = param.substring(seperatorIdx + 1);
        value = URLCodecUtils.decodeURL(value);

        switch (key) {
        case "xt":
            magnetData.addExactTopic(value);
            break;
        case "xs":
            magnetData.addExactSubstitute(value);
            break;
        case "as":
            magnetData.addAcceptableSubstitute(value);
            break;
        case "dn":
            magnetData.setDisplayName(value);
            break;
        case "kt":
            magnetData.setKeywordTopic(value);
            break;
        }
    }
    return magnetData;
}

From source file:phex.download.swarming.SWDownloadFile.java

/**
 *
 *//*from  w  w w. j  ava 2 s .  c om*/
public SWDownloadFile(URI downloadUri, SwarmingManager swMgr) throws URIException {
    this(swMgr);
    String protocol = downloadUri.getScheme();
    if ("magnet".equals(protocol)) {
        MagnetData magnetData = MagnetData.parseFromURI(downloadUri);

        URN urn = MagnetData.lookupSHA1URN(magnetData);
        String magnetFileName = MagnetData.lookupFileName(magnetData);
        magnetFileName = FileUtils.convertToLocalSystemFilename(magnetFileName);
        String searchTerm;
        if (magnetData.getKeywordTopic() != null) {
            searchTerm = magnetData.getKeywordTopic();
        } else {
            searchTerm = StringUtils.createNaturalSearchTerm(MagnetData.lookupSearchName(magnetData));
        }

        initialize(magnetFileName, urn, UNKNOWN_FILE_SIZE, searchTerm, true);
        try {
            initIncompleteFile();
        } catch (FileHandlingException | ManagedFileException exp) {
            logger.error(exp.toString(), exp);
        }

        List<URI> urlList = MagnetData.lookupHttpURIs(magnetData);
        for (URI uri : urlList) {
            String host = uri.getHost();
            int port = uri.getPort();
            if (port == -1) {
                port = 80;
            }
            DestAddress address = new DefaultDestAddress(host, port);
            SWDownloadCandidate candidate = new SWDownloadCandidate(address, uri, this,
                    mgr.getCandidateLogBuffer());
            addDownloadCandidate(candidate);
        }

        // fire off a search in case this is a magnet download to get sources.
        if (urn != null || getCandidatesCount() == 0) {
            startSearchForCandidates();
        }
    } else {
        String uriFileName = URLUtil.getFileNameFromUri(downloadUri);
        uriFileName = FileUtils.convertToLocalSystemFilename(uriFileName);
        String searchTerm = StringUtils.createNaturalSearchTerm(uriFileName);
        initialize(uriFileName, null, UNKNOWN_FILE_SIZE, searchTerm, true);
        try {
            initIncompleteFile();
        } catch (FileHandlingException | ManagedFileException exp) {
            logger.error(exp.toString(), exp);
        }

        String host = downloadUri.getHost();
        if (host != null) {
            int port = downloadUri.getPort();
            if (port == -1) {
                port = 80;
            }
            DestAddress address = new DefaultDestAddress(host, port);
            SWDownloadCandidate candidate = new SWDownloadCandidate(address, downloadUri, this,
                    mgr.getCandidateLogBuffer());
            addDownloadCandidate(candidate);
        }
    }
}

From source file:phex.gui.dialogs.NewDownloadDialog.java

private void createNewDownload() throws URIException {
    Servent servent = GUIRegistry.getInstance().getServent();
    SwarmingManager swarmingMgr = servent.getDownloadService();
    SharedFilesService shareService = servent.getSharedFilesService();

    String uriStr = uriTF.getText().trim();
    if (uriStr.length() == 0) {
        return;/*from   www.jav a2s . com*/
    }
    URI uri = new URI(uriStr, true);
    String protocol = uri.getScheme();

    // in case this is no magnet we cant determine the file urn and cant
    // check if the download is already running.
    if ("magnet".equals(protocol)) {
        MagnetData magnetData = MagnetData.parseFromURI(uri);
        URN urn = MagnetData.lookupSHA1URN(magnetData);

        if (swarmingMgr.isURNDownloaded(urn)) {
            GUIUtils.showErrorMessage(Localizer.getString("NewDownload_AlreadyDownloadingMessage"),
                    Localizer.getString("NewDownload_AlreadyDownloadingTitle"));
            return;
        }
        if (shareService.isURNShared(urn)) {
            GUIUtils.showErrorMessage(Localizer.getString("NewDownload_AlreadySharedMessage"),
                    Localizer.getString("NewDownload_AlreadySharedTitle"));
            return;
        }
    }
    swarmingMgr.addFileToDownload(uri, true);
}

From source file:ru.org.linux.util.LorURL.java

/**
 * ?? scheme url http  https  ??   secure
 * ?  ? lor ??,    ? ,  ?//from w  w  w .  j  av a2s .  co m
 * @param canonical ? URL ?
 * @return ? url
 * @throws URIException  url
 */
public String canonize(URI canonical) throws URIException {
    if (!_true_lor_url) {
        return toString();
    }

    String host = canonical.getHost();
    int port = canonical.getPort();
    String path = parsed.getPath();
    String query = parsed.getQuery();
    String fragment = parsed.getFragment();

    if (canonical.getScheme().equals("http")) {
        return (new HttpURL(null, host, port, path, query, fragment)).getEscapedURIReference();
    } else {
        return (new HttpsURL(null, host, port, path, query, fragment)).getEscapedURIReference();
    }
}

From source file:ru.org.linux.util.LorURL.java

/**
 *  url ?    ?\/*from   w  w w  .j a v  a2s .c  o m*/
 * @param messageDao ?   ?
 * @param canonical ? URL ?
 * @return url ?   ?? ?
 * @throws MessageNotFoundException ?  ??
 * @throws URIException ? url 
 */
public String formatJump(TopicDao messageDao, URI canonical) throws MessageNotFoundException, URIException {
    if (_topic_id != -1) {
        Topic message = messageDao.getById(_topic_id);

        Group group = messageDao.getGroup(message);

        String scheme = canonical.getScheme();

        String host = canonical.getHost();
        int port = canonical.getPort();
        String path = group.getUrl() + _topic_id;
        String query = "";
        if (_comment_id != -1) {
            query = "cid=" + _comment_id;
        }
        URI jumpUri = new URI(scheme, null, host, port, path, query);
        return jumpUri.getEscapedURI();
    }

    return "";
}