Example usage for java.net URI toURL

List of usage examples for java.net URI toURL

Introduction

In this page you can find the example usage for java.net URI toURL.

Prototype

public URL toURL() throws MalformedURLException 

Source Link

Document

Constructs a URL from this URI.

Usage

From source file:org.mobicents.servlet.sip.restcomm.fax.InterFaxService.java

private final byte[] toBytes(final URI uri) throws IOException {
    final InputStream input = uri.toURL().openStream();
    final ByteArrayOutputStream output = new ByteArrayOutputStream();
    try {//  w w  w  .ja  va2  s.c  o m
        final byte[] buffer = new byte[1024];
        long totalBytesRead = 0;
        int bytesRead = 0;
        do {
            bytesRead = input.read(buffer, 0, 1024);
            if (bytesRead > 0) {
                output.write(buffer, 0, bytesRead);
                totalBytesRead += bytesRead;
                if (totalBytesRead > maxFileSize) {
                    final StringBuilder exception = new StringBuilder();
                    exception.append("The size of the resource located @ ").append(uri.toString())
                            .append(" is to large.");
                    throw new IOException(exception.toString());
                }
            }
        } while (bytesRead != -1);
        return output.toByteArray();
    } finally {
        input.close();
    }
}

From source file:psiprobe.controllers.certificates.ListCertificatesController.java

/**
 * Tries to open a InputStream the same way as
 * {@link org.apache.tomcat.util.file.ConfigFileLoader.getInputStream(String)}
 * /*w  w w . ja  va2s.  c o  m*/
 * @param path the path of a store file (absolute or relative to CATALINA.BASE), or URI to store
 *        file (absolute or relative to CATALINA.BASE).
 * @return the input stream of the path file
 * @throws IOException if path can not be resolved
 */
private InputStream getStoreInputStream(String path) throws IOException {
    File file = new File(path);
    if (file.exists()) {
        return new FileInputStream(file);
    }

    File catalinaBaseFolder = new File(System.getProperty("catalina.base"));
    file = new File(catalinaBaseFolder, path);

    if (file.exists()) {
        return new FileInputStream(file);
    }

    URI uri = catalinaBaseFolder.toURI().resolve(path);

    URL url = uri.toURL();

    return url.openConnection().getInputStream();
}

From source file:es.gva.cit.gazetteer.idec.drivers.IDECGazetteerServiceDriver.java

public DiscoveryServiceCapabilities getCapabilities(URI uri) {
    Collection nodes = new java.util.ArrayList();
    URL url = null;/*w  w w  . j  a  va2s. c om*/
    try {
        url = uri.toURL();
    } catch (MalformedURLException e) {
        setServerAnswerReady("errorServerNotFound");
        return null;
    }
    nodes = new HTTPGetProtocol().doQuery(url, getMessageCapabilities(), 0);
    IdecCapabilitiesParser parser = new IdecCapabilitiesParser(this);
    parser.parse(nodes);
    return parser.getCapabilities();
}

From source file:com.puppycrawl.tools.checkstyle.checks.header.AbstractHeaderCheck.java

/**
 * Load the header from a file.//  ww  w.  j a  va  2  s . c  om
 * @throws CheckstyleException if the file cannot be loaded
 */
private void loadHeaderFile() throws CheckstyleException {
    checkHeaderNotInitialized();
    Reader headerReader = null;
    try {
        final URI uri = CommonUtils.getUriByFilename(filename);
        headerReader = new InputStreamReader(new BufferedInputStream(uri.toURL().openStream()), charset);
        loadHeader(headerReader);
    } catch (final IOException ex) {
        throw new CheckstyleException("unable to load header file " + filename, ex);
    } finally {
        Closeables.closeQuietly(headerReader);
    }
}

From source file:org.codice.ddf.registry.report.action.provider.RegistryMetacardActionProvider.java

private Action getAction(String metacardId, String sourceId) {

    if (StringUtils.isNotBlank(sourceId)) {
        sourceId = SOURCE_ID_QUERY_PARAM + sourceId;
    }/*  www .j av a  2 s  .c o  m*/

    URL url;
    try {

        URI uri = new URI(SystemBaseUrl
                .constructUrl(REGISTRY_PATH + "/" + metacardId + REPORT_PATH + FORMAT + sourceId, true));
        url = uri.toURL();

    } catch (MalformedURLException e) {
        LOGGER.info("Malformed URL exception", e);
        return null;
    } catch (URISyntaxException e) {
        LOGGER.info("URI Syntax exception", e);
        return null;
    }

    return new ActionImpl(getId(), TITLE, DESCRIPTION, url);
}

From source file:org.kalypso.core.catalog.CatalogManager.java

private ICatalog readCatalog(final URI catalogURI) {
    try (InputStream is = catalogURI.toURL().openStream()) {
        final Unmarshaller unmarshaller = JAX_CONTEXT_CATALOG.createUnmarshaller();

        // REMARK: do not use 'unmarshaller.unmarshal( catalogURL )'
        // It does leave the stream open
        final JAXBElement<Catalog> object = (JAXBElement<Catalog>) unmarshaller.unmarshal(is);
        is.close();//from  w  w  w . j a v  a2  s. co m

        final Catalog catalog = object.getValue();

        final URL catalogURL = catalogURI.toURL();
        return createCatalog(catalogURL, catalog);
    } catch (final Exception e) {
        e.printStackTrace();
        System.err.println(String.format(Messages.getString("CatalogManager.0"), catalogURI)); //$NON-NLS-1$
        return null;
    }
}

From source file:org.jboss.aerogear.test.arquillian.container.NonDeployingContainer.java

private URL buildUrl(URI uri, String contextPath) throws DeploymentException {
    try {//from w  w  w .  ja va 2 s .c o  m
        return new URL(uri.toURL(), contextPath);
    } catch (MalformedURLException e) {
        throw new DeploymentException("Unable to construct URL for deployment", e);
    }
}

From source file:gmusic.api.comm.HttpUrlConnector.java

private URI adjustAddress(URI address) throws MalformedURLException, URISyntaxException {
    if (address.toString().startsWith("https://play.google.com/music/services/")) {
        return address = new URI(address.toURL() + String.format("?u=0&xt=%1$s", cookie));
    }//from  www . j  a va2  s.  c  o  m

    return address;
}

From source file:com.woonoz.proxy.servlet.UrlRewriterImpl.java

private boolean hostIsSameAsServletHost(final URI requestedUrl) throws MalformedURLException {
    return requestedUrl.getHost().equals(getServletHost())
            && getPortOrDefault(requestedUrl.toURL()) == getServletPort();
}

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

/**
 * {@inheritDoc}//  ww w .  j a  v a  2 s .c  om
 */
@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);
}