Example usage for java.net URL getAuthority

List of usage examples for java.net URL getAuthority

Introduction

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

Prototype

public String getAuthority() 

Source Link

Document

Gets the authority part of this URL .

Usage

From source file:org.commonjava.maven.galley.transport.htcli.internal.HttpListing.java

static boolean isSameServer(final URL url, final String linkHref) {
    String linkProtocol = null;/*  w w  w  .j a  v  a2  s  . c  om*/
    String linkAuthority = null;
    Logger logger = LoggerFactory.getLogger(HttpListing.class);
    try {
        URL linkUrl = new URL(linkHref);
        linkProtocol = linkUrl.getProtocol();
        linkAuthority = linkUrl.getAuthority();

        logger.debug("Absolute URL: {} is on the same server.", linkHref);
    } catch (MalformedURLException ex) {
        // linkHref is a relative path on the same server
        logger.debug("URL is relative, must be on the same server.");
    }

    boolean valid = (linkProtocol == null || linkProtocol.equals(url.getProtocol()))
            && (linkAuthority == null || linkAuthority.equals(url.getAuthority()));

    logger.debug("URL: {} has same protocol and authority? {}", linkHref, valid);

    return valid;
}

From source file:ca.hoogit.garagepi.Utils.Helpers.java

/**
 * Parse the url path, and rebuild it with supplied paths
 *
 * @param oldUrl Original url/*from   w  w w  .  j a v  a2s.  c o  m*/
 * @param paths  Paths to add to the url
 * @return String Built url
 * @throws MalformedURLException
 */
public static String urlBuilder(String oldUrl, String... paths) throws MalformedURLException {
    URL url = new URL(oldUrl);
    Uri.Builder builder = new Uri.Builder();
    builder.scheme(url.getProtocol()).authority(url.getAuthority()).path(url.getPath());
    for (String path : paths) {
        builder.appendPath(path);
    }
    return builder.build().toString();
}

From source file:org.getobjects.appserver.core.WOCORSConfig.java

private static final String removePathFromURL(final String _url) {
    if (_url == null)
        return null;
    if (_url.length() == 0)
        return "";
    try {// ww  w.  ja va 2s .  c o  m
        // yeah, not perfect
        StringBuilder sb = new StringBuilder(32);
        String s;
        URL url = new URL(_url);
        if (UObject.isNotEmpty((s = url.getProtocol()))) {
            sb.append(s);
            sb.append("://");
        }
        if (UObject.isNotEmpty((s = url.getAuthority())))
            sb.append(s);

        return sb.toString();
    } catch (MalformedURLException e) {
        log.warn("could not parse URL: " + _url);
        return _url; // keep as-is
    }
}

From source file:org.sakaiproject.blti.LessonsFacade.java

public static String doImportTool(String siteId, String launchUrl, String bltiTitle, String strXml,
        String custom) throws Exception {
    if (ltiService == null)
        return null;

    String toolUrl = launchUrl;//www.  j ava2 s. c o  m
    int pos = toolUrl.indexOf("?");
    if (pos > 0) {
        toolUrl = toolUrl.substring(0, pos);
    }

    String toolName = toolUrl;
    try {
        URL launch = new URL(launchUrl);
        toolName = launch.getProtocol() + "://" + launch.getAuthority();
    } catch (Exception e) {
        toolName = toolUrl;
    }

    // Check for global tool configurations (prefer global)
    Map<String, Object> theTool = null;
    List<Map<String, Object>> tools = ltiService.getToolsDao(null, null, 0, 0, "!admin");
    String lastLaunch = "";
    for (Map<String, Object> tool : tools) {
        String toolLaunch = (String) tool.get(LTIService.LTI_LAUNCH);
        // Prefer the longest match
        if (toolUrl.startsWith(toolLaunch) && toolLaunch.length() > lastLaunch.length()) {
            theTool = tool;
            lastLaunch = toolLaunch;
        }
    }

    // Check for within-site tool configurations (prefer global)
    if (theTool == null) {
        tools = ltiService.getToolsDao(null, null, 0, 0, siteId);
        lastLaunch = "";
        for (Map<String, Object> tool : tools) {
            String toolLaunch = (String) tool.get(LTIService.LTI_LAUNCH);
            // Prefer the longest match
            if (toolUrl.startsWith(toolLaunch) && toolLaunch.length() > lastLaunch.length()) {
                theTool = tool;
                lastLaunch = toolLaunch;
            }
        }
    }

    // If we still do not have a tool configuration throw an error
    if (theTool == null) {
        M_log.error("LORI Launch configuration not found- " + toolUrl);
        throw new Exception("LORI Launch configuration not found");
    }

    // Found a tool - time to insert content
    Map<String, Object> theContent = null;
    Long contentKey = null;
    if (theTool != null) {
        Properties props = new Properties();
        String toolId = foorm.getLong(theTool.get(LTIService.LTI_ID)).toString();
        props.setProperty(LTIService.LTI_TOOL_ID, toolId);
        props.setProperty(LTIService.LTI_PLACEMENTSECRET, UUID.randomUUID().toString());
        props.setProperty(LTIService.LTI_TITLE, bltiTitle);
        props.setProperty(LTIService.LTI_PAGETITLE, bltiTitle);
        props.setProperty(LTIService.LTI_LAUNCH, launchUrl);
        props.setProperty(LTIService.LTI_SITE_ID, siteId);

        if (strXml != null)
            props.setProperty(LTIService.LTI_XMLIMPORT, strXml);
        if (custom != null)
            props.setProperty(LTIService.LTI_CUSTOM, custom);

        M_log.debug("Inserting content associted with toolId=" + toolId);

        // Insert as admin into siteId, on error throw upwards
        Object result = ltiService.insertContentDao(props, "!admin");
        if (result instanceof String) {
            M_log.error("Could not insert content - " + result);
        } else {
            M_log.debug("Adding LTI tool " + result);
        }
        if (result instanceof Long)
            theContent = ltiService.getContentDao((Long) result, siteId);
    }

    String sakaiId = null;
    if (theContent != null) {
        sakaiId = "/blti/" + theContent.get(LTIService.LTI_ID);
    }
    return sakaiId;
}

From source file:org.bahmni.feed.openelis.feed.job.OpenELISFeedReaderJob.java

private static String getURLPrefix(String authenticationURI) {
    URL openMRSAuthURL;
    try {//from   ww w.j av  a2 s.com
        openMRSAuthURL = new URL(authenticationURI);
    } catch (MalformedURLException e) {
        throw new RuntimeException("Is not a valid URI - " + authenticationURI);
    }
    return String.format("%s://%s", openMRSAuthURL.getProtocol(), openMRSAuthURL.getAuthority());
}

From source file:at.beris.virtualfile.util.UrlUtils.java

/**
 * Masks sensitive information in an url (e.g. for logging)
 *
 * @param url/*  w  ww  .  j  av a 2 s  .  c o  m*/
 * @return
 */
public static String maskedUrlString(URL url) {
    StringBuilder stringBuilder = new StringBuilder("");

    stringBuilder.append(url.getProtocol());
    stringBuilder.append(':');
    if (!url.getProtocol().toLowerCase().equals("file"))
        stringBuilder.append("//");

    String authority = url.getAuthority();
    if (!StringUtils.isEmpty(authority)) {
        String[] authorityParts = authority.split("@");

        if (authorityParts.length > 1) {
            String[] userInfoParts = authorityParts[0].split(":");
            stringBuilder.append(userInfoParts[0]);

            if (userInfoParts.length > 1) {
                stringBuilder.append(":***");
            }
            stringBuilder.append('@');
            stringBuilder.append(authorityParts[1]);
        } else {
            stringBuilder.append(authorityParts[0]);
        }
    }

    stringBuilder.append(url.getPath());

    return stringBuilder.toString();
}

From source file:org.artifactory.util.HttpUtils.java

public static String adjustRefererValue(Map<String, String> headersMap, String headerVal) {
    //Append the artifactory user agent to the referer
    if (headerVal == null) {
        //Fallback to host
        headerVal = headersMap.get("HOST");
        if (headerVal == null) {
            //Fallback to unknown
            headerVal = "UNKNOWN";
        }//from  w  w w  . j  a  v  a  2  s . c o  m
    }
    if (!headerVal.startsWith("http")) {
        headerVal = "http://" + headerVal;
    }
    try {
        java.net.URL uri = new java.net.URL(headerVal);
        //Only use the uri up to the path part
        headerVal = uri.getProtocol() + "://" + uri.getAuthority();
    } catch (MalformedURLException e) {
        //Nothing
    }
    headerVal += "/" + HttpUtils.getArtifactoryUserAgent();
    return headerVal;
}

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

private static URL normalizeHref(URL context, String href) throws DavException {
    URL url = null;
    try {/*from  w  w w.  j  av  a2  s  . c  om*/
        url = new URL(context, href);
        // check that the URL is escaped. it's questionable whether or
        // not we should all unescaped URLs, but at least as of
        // 10/02/2007, iCal 3.0 generates them
        url.toURI();
        return url;
    } catch (URISyntaxException e) {
        try {
            URI escaped = new URI(url.getProtocol(), url.getAuthority(), url.getPath(), url.getQuery(),
                    url.getRef());
            return new URL(escaped.toASCIIString());
        } catch (Exception e2) {
            throw new BadRequestException("Malformed unescaped href " + href + ": " + e.getMessage());
        }
    } catch (MalformedURLException e) {
        throw new BadRequestException("Malformed href " + href + ": " + e.getMessage());
    }
}

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

private static URL normalizeHref(URL context, String href) throws CosmoDavException {
    URL url = null;//w  ww  .j a  va  2 s .  co m
    try {
        url = new URL(context, href);
        // check that the URL is escaped. it's questionable whether or
        // not we should all unescaped URLs, but at least as of
        // 10/02/2007, iCal 3.0 generates them
        url.toURI();
        return url;
    } catch (URISyntaxException e) {
        try {
            URI escaped = new URI(url.getProtocol(), url.getAuthority(), url.getPath(), url.getQuery(),
                    url.getRef());
            return new URL(escaped.toString());
        } catch (URISyntaxException | MalformedURLException e2) {
            throw new BadRequestException("Malformed unescaped href " + href + ": " + e.getMessage());
        }
    } catch (MalformedURLException e) {
        throw new BadRequestException("Malformed href " + href + ": " + e.getMessage());
    }
}

From source file:net.antidot.semantic.rdf.rdb2rdf.r2rml.tools.R2RMLToolkit.java

/**
 * The URL-safe version of a string is obtained by an URL encoding to a
 * string value./*from  w  w w . j  av  a2  s  . co m*/
 * 
 * @throws R2RMLDataError
 * @throws MalformedURLException
 */
public static String getURLSafeVersion(String value) throws R2RMLDataError {
    if (value == null)
        return null;
    URL url = null;
    try {
        url = new URL(value);
    } catch (MalformedURLException mue) {
        // This template should be not a url : no encoding
        return value;
    }
    // No exception raised, this template is a valid url : perform
    // percent-encoding
    try {
        java.net.URI uri = new java.net.URI(url.getProtocol(), url.getAuthority(), url.getPath(),
                url.getQuery(), url.getRef());
        String result = uri.toURL().toString();
        // Percent encoding : complete with no supported char in this
        // treatment
        result = result.replaceAll("\\,", "%2C");
        return result;
    } catch (URISyntaxException e) {
        throw new R2RMLDataError("[R2RMLToolkit:getIRISafeVersion] This value " + value
                + " can not be percent-encoded because " + e.getMessage());
    } catch (MalformedURLException e) {
        throw new R2RMLDataError("[R2RMLToolkit:getIRISafeVersion] This value " + value
                + " can not be percent-encoded because " + e.getMessage());
    }
}