Example usage for java.net URL toExternalForm

List of usage examples for java.net URL toExternalForm

Introduction

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

Prototype

public String toExternalForm() 

Source Link

Document

Constructs a string representation of this URL .

Usage

From source file:bixo.operations.ProcessRobotsTask.java

private BaseRobotRules getRobotRules(BaseFetcher fetcher, BaseRobotsParser parser, URL robotsUrl) {

    try {//from www.  j av  a2  s . c o  m
        String urlToFetch = robotsUrl.toExternalForm();
        FetchedDatum result = fetcher.get(new ScoredUrlDatum(urlToFetch));

        // HACK! DANGER! Some sites will redirect the request to the top-level domain
        // page, without returning a 404. So look for a response which has a redirect,
        // and the fetched content is not plain text, and assume it's one of these...
        // which is the same as not having a robots.txt file.

        String contentType = result.getContentType();
        boolean isPlainText = (contentType != null) && (contentType.startsWith("text/plain"));
        if ((result.getNumRedirects() > 0) && !isPlainText) {
            return parser.failedFetch(HttpStatus.SC_GONE);
        }

        return parser.parseContent(urlToFetch, result.getContentBytes(), result.getContentType(),
                fetcher.getUserAgent().getAgentName());
    } catch (HttpFetchException e) {
        return parser.failedFetch(e.getHttpStatus());
    } catch (IOFetchException e) {
        return parser.failedFetch(HttpStatus.SC_INTERNAL_SERVER_ERROR);
    } catch (RedirectFetchException e) {
        // Other sites will have circular redirects, so treat this as a missing robots.txt
        return parser.failedFetch(HttpStatus.SC_GONE);
    } catch (Exception e) {
        LOGGER.error("Unexpected exception fetching robots.txt: " + robotsUrl, e);
        return parser.failedFetch(HttpStatus.SC_INTERNAL_SERVER_ERROR);
    }
}

From source file:com.jetbrains.pluginUtils.xml.JDOMXIncluder.java

private static List<Content> resolveXIncludeElement(Element element, Stack<String> bases)
        throws XIncludeException {
    String base = "";
    if (!bases.isEmpty())
        base = bases.peek();//from  w  ww .  j  a v a 2 s . c om

    // These lines are probably unnecessary
    assert isIncludeElement(element);

    String href = element.getAttributeValue(HREF);
    assert href != null : "Missing href attribute";

    Attribute baseAttribute = element.getAttribute(BASE, Namespace.XML_NAMESPACE);
    if (baseAttribute != null) {
        base = baseAttribute.getValue();
    }

    URL remote;
    if (base != null) {
        try {
            URL context = new URL(base);
            remote = new URL(context, href);
        } catch (MalformedURLException ex) {
            throw new XIncludeException(ex);
        }
    } else { // base == null
        try {
            remote = new URL(href);
        } catch (MalformedURLException ex) {
            throw new XIncludeException(ex);
        }
    }

    boolean parse = true;
    final String parseAttribute = element.getAttributeValue(PARSE);

    if (parseAttribute != null) {
        if (parseAttribute.equals(TEXT)) {
            parse = false;
        }

        assert parseAttribute.equals(XML) : parseAttribute + "is not a legal value for the parse attribute";
    }

    if (parse) {
        assert !bases.contains(remote.toExternalForm()) : "Circular XInclude Reference to "
                + remote.toExternalForm();

        final Element fallbackElement = element.getChild("fallback", element.getNamespace());
        List<Content> remoteParsed = parseRemote(bases, remote, fallbackElement);
        if (!remoteParsed.isEmpty()) {
            remoteParsed = extractNeededChildren(element, remoteParsed);
        }

        for (int i = 0; i < remoteParsed.size(); i++) {
            Object o = remoteParsed.get(i);

            if (o instanceof Element) {
                Element e = (Element) o;
                List<? extends Content> nodes = resolve(e, bases);
                remoteParsed.addAll(i, nodes);
                i += nodes.size();
                remoteParsed.remove(i);
                i--;
                e.detach();
            }
        }

        for (Object o : remoteParsed) {
            if (o instanceof Content) {
                Content content = (Content) o;
                content.detach();
            }
        }
        return remoteParsed;
    } else {
        try {
            String encoding = element.getAttributeValue(ENCODING);
            String s = IOUtils.toString(remote, encoding);
            List<Content> resultList = new ArrayList<Content>(1);
            resultList.add(new Text(s));
            return resultList;
        } catch (IOException e) {
            throw new XIncludeException(e);
        }
    }

}

From source file:jp.dip.komusubi.botter.Configuration.java

public String getLastModified(URL url) {
    return getProperty(url.toExternalForm());
}

From source file:net.ontopia.utils.ResourcesDirectoryReader.java

private void findResourcesFromJar(URL jarPath) {
    try {/*from www.  jav  a  2s.  c om*/
        JarURLConnection jarConnection = (JarURLConnection) jarPath.openConnection();
        JarFile jarFile = jarConnection.getJarFile();
        Enumeration<JarEntry> entries = jarFile.entries();
        while (entries.hasMoreElements()) {
            JarEntry entry = entries.nextElement();
            String resourcePath = entry.getName();
            if ((!entry.isDirectory()) && (resourcePath.startsWith(directoryPath))
                    && (searchSubdirectories || !resourcePath.substring(directoryPath.length()).contains("/"))
                    && (filtersApply(resourcePath))) {
                // cannot do new URL(jarPath, resourcePath), somehow leads to duplicated path
                // retest on java 8
                Enumeration<URL> urls = classLoader.getResources(resourcePath);
                while (urls.hasMoreElements()) {
                    URL url = urls.nextElement();
                    if (url.toExternalForm().startsWith(jarPath.toExternalForm())) {
                        resources.add(url);
                    }
                }
            }
        }
    } catch (IOException e) {
    }
}

From source file:ch.ledcom.maven.sitespeed.analyzer.SiteSpeedAnalyzer.java

private ImmutableList<String> constructCommand(URL url) {
    ImmutableList.Builder<String> builder = ImmutableList.builder();
    builder.addAll(baseCommand);//from ww  w  . j a va  2 s.  co  m
    builder.add(url.toExternalForm());
    return builder.build();
}

From source file:org.jasig.portlet.notice.service.ssp.SSPApi.java

/**
 * {@inheritDoc}//from w  ww.  j  a v  a 2 s . c  o m
 */
@Override
public <T> ResponseEntity<T> doRequest(SSPApiRequest<T> request)
        throws MalformedURLException, RestClientException {
    SSPToken token = getAuthenticationToken(false);

    request.setHeader(AUTHORIZATION, token.getTokenType() + " " + token.getAccessToken());

    URL url = getSSPUrl(request.getUrlFragment(), true);
    ResponseEntity<T> response = restTemplate.exchange(url.toExternalForm(), request.getMethod(),
            request.getRequestEntity(), request.getResponseClass(), request.getUriParameters());
    // if we get a 401, the token may have unexpectedly expired (eg. ssp server restart).
    // Clear it, get a new token and replay the request one time.
    if (response.getStatusCode() == HttpStatus.UNAUTHORIZED) {
        token = getAuthenticationToken(true);
        request.setHeader(AUTHORIZATION, token.getTokenType() + " " + token.getAccessToken());
        return restTemplate.exchange(url.toExternalForm(), request.getMethod(), request.getRequestEntity(),
                request.getResponseClass(), request.getUriParameters());
    }

    return response;
}

From source file:com.redhat.tools.kerberos.SunJaasKerberosTicketValidator.java

public void setProperties() throws Exception {
    // if (keyTabLocation instanceof ClassPathResource) {
    // LOG.warn("Your keytab is in the classpath. This file needs special protection and shouldn't be in the classpath. JAAS may also not be able to load this file from classpath.");
    // }/* w  w  w  . j  a  v  a 2 s . co m*/
    URL keytabURL = new URL(this.keyTabLocation);
    LoginConfig loginConfig = new LoginConfig(keytabURL.toExternalForm(), this.servicePrincipal, this.debug);
    Set<Principal> princ = new HashSet<Principal>(1);
    princ.add(new KerberosPrincipal(this.servicePrincipal));
    Subject sub = new Subject(false, princ, new HashSet<Object>(), new HashSet<Object>());
    LoginContext lc = new LoginContext("", sub, null, loginConfig);
    lc.login();
    this.serviceSubject = lc.getSubject();
}

From source file:org.danann.cernunnos.runtime.PojoTask.java

/**
 * This method <em>must</em> be invoked after all POJO properties have been 
 * supplied.  If this <code>Task</code> has been defined using Spring 
 * Dependency Injection, this method will be called for you by the Spring 
 * context.  Additional calls to this method are no-ops.
 *//*  w ww.j  a  v  a2 s. co  m*/
public synchronized void afterPropertiesSet() {

    // Subsequent calls are no-ops...
    if (task == null) {

        // Be sure we have what we need...
        if (location == null) {
            String msg = "Property 'location' not set.  You must specify " + "a Cernunnos XML document.";
            throw new IllegalStateException(msg);
        }

        if (log.isDebugEnabled()) {
            log.debug("Bootstrapping Cernunnos XML [context=" + context + " location=" + location);
        }

        try {
            URL u = ResourceHelper.evaluate(context, location);
            origin = u.toExternalForm();
            task = runner.compileTask(u.toExternalForm());
        } catch (Throwable t) {
            String msg = "Unable to read the specified Cernunnos XML" + "\n\t\tcontext=" + context
                    + "\n\t\tlocation=" + location;
            throw new RuntimeException(msg, t);
        }

    }

}

From source file:gov.nih.nci.ncicb.tcga.dcc.qclive.util.QCliveXMLSchemaValidator.java

protected Source getSource(final Boolean isURL, final String schema, final File xmlFile) throws IOException {

    if (isURL) {/*from  w  w w .j a  va 2s  .c  om*/
        final URL schemaURL = new URL(schema);
        return new StreamSource(schemaURL.toExternalForm());
    } else {
        // get xsd file
        File schemaFile = new File(
                new StringBuilder(xmlFile.getParent()).append(File.separator).append(schema).toString());
        if (!schemaFile.exists()) {
            throw new FileNotFoundException(new StringBuilder("Schema ").append(schema)
                    .append(" was not found in the archive, even though the XML file ")
                    .append(xmlFile.getName()).append(" refers to it").toString());
        }
        return new StreamSource(schemaFile);

    }
}

From source file:com.provenance.cloudprovenance.connector.policy.PolicyEnforcementConnector.java

/** Method returns a policy response based on a repose URI as a string */
@Override/*  ww w. j  av  a2 s  .  c  o m*/
public String policyResponse(String serviceId, URL policyResponseURI) {

    RestTemplate restTemplate = new RestTemplate();

    logger.info("Invoking URI: " + policyResponseURI);

    String policyResponse = restTemplate.getForObject(policyResponseURI.toExternalForm(), String.class);

    return policyResponse;
}