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.apache.ode.bpel.elang.XslRuntimeUriResolver.java

/**
 * Given a URI this function will attempt to retrieve the resource declared at that URI location
 * as a stream. This URI can be defined as being relative to the executing process instance's 
 * physical file location or can point to an HTTP(S) resource.
 *
 * @param docUri - the URI to resolve/* ww w  .  ja v  a2  s.com*/
 * @return stream - the resource contents, or null if none found.
 */
private InputStream getResourceAsStream(URI docUri) {
    URI resolvedURI = _baseResourceURI.resolve(docUri);
    InputStream is = null;

    try {
        // treat URI as URL and try to load it.
        URL url = resolvedURI.toURL();
        is = url.openStream();

        // and read it to a buffer.
        return is;
    } catch (Exception e) {
        __log.warn("Couldn't load XSL resource " + docUri, e);
    }
    return null;
}

From source file:com.googlecode.jsonschema2pojo.ContentResolver.java

/**
 * Resolve a given URI to read its contents and parse the result as JSON.
 * <p>/*  w w  w.j a  v  a2s  . c o m*/
 * Supported protocols:
 * <ul>
 * <li>http/https
 * <li>file
 * <li>classpath/resource/java (all synonymous, used to resolve a schema
 * from the classpath)
 * </ul>
 * 
 * @param uri
 *            the URI to read schema content from
 * @return the JSON tree found at the given URI
 */
public JsonNode resolve(URI uri) {

    if (CLASSPATH_SCHEMES.contains(uri.getScheme())) {
        return resolveFromClasspath(uri);
    }

    try {
        return OBJECT_MAPPER.readTree(uri.toURL());
    } catch (JsonProcessingException e) {
        throw new IllegalArgumentException("Error parsing document: " + uri, e);
    } catch (MalformedURLException e) {
        throw new IllegalArgumentException("Unrecognised URI, can't resolve this: " + uri, e);
    } catch (IOException e) {
        throw new IllegalArgumentException("Unrecognised URI, can't resolve this: " + uri, e);
    }

}

From source file:brooklyn.util.ResourceUtils.java

public static URL tidy(URL url) {
    // File class has helpful methods for URIs but not URLs. So we convert.
    URI in;//  ww w .  j a  v a 2s.  c  o m
    try {
        in = url.toURI();
    } catch (URISyntaxException e) {
        throw Exceptions.propagate(e);
    }
    URI out;

    Matcher matcher = pattern.matcher(in.toString());
    if (matcher.matches()) {
        // home-relative
        File home = new File(Os.home());
        File file = new File(home, matcher.group(1));
        out = file.toURI();
    } else if (in.getScheme().equals("file:")) {
        // some other file, so canonicalize
        File file = new File(in);
        out = file.toURI();
    } else {
        // some other scheme, so no-op
        out = in;
    }

    URL urlOut;
    try {
        urlOut = out.toURL();
    } catch (MalformedURLException e) {
        throw Exceptions.propagate(e);
    }
    if (!urlOut.equals(url) && log.isDebugEnabled()) {
        log.debug("quietly changing " + url + " to " + urlOut);
    }
    return urlOut;
}

From source file:cat.calidos.morfeu.utils.injection.DataFetcherModule.java

@Produces
@Named("fileData")
public InputStream fetchFileData(URI uri) throws FetchingException {

    try {//from   www. j  a  v  a 2 s  . c  o m

        log.trace("Fetching local data from {}", uri);

        return FileUtils.openInputStream(FileUtils.toFile(uri.toURL()));

    } catch (Exception e) {
        throw new FetchingException("Problem fetching local data at '" + uri + "'", e);
    }

}

From source file:org.apache.ode.axis2.ODEAxis2Server.java

/**
 * Creates and deploys an Axis service based on a provided MessageReceiver. The receiver
 * will be invoked for all invocations of that service.
 *///  w  ww.j av a  2s  .  c  o m
protected void deployService(String bundleName, String defFile, QName serviceName, String port,
        MessageReceiver receiver) throws WSDLException, IOException, URISyntaxException {
    URI wsdlUri = new File(getResource(bundleName) + "/" + defFile).toURI();

    InputStream is = wsdlUri.toURL().openStream();
    WSDL11ToAxisServiceBuilder serviceBuilder = new ODEAxisService.WSDL11ToAxisPatchedBuilder(is, serviceName,
            port);
    serviceBuilder.setBaseUri(wsdlUri.toString());
    serviceBuilder.setCustomResolver(new Axis2UriResolver());
    serviceBuilder.setCustomWSDLResolver(new Axis2WSDLLocator(wsdlUri));
    serviceBuilder.setServerSide(true);

    AxisService axisService = serviceBuilder.populateService();
    axisService.setName(serviceName.getLocalPart());
    axisService.setWsdlFound(true);
    axisService.setCustomWsdl(true);
    axisService.setClassLoader(getConfigurationContext().getAxisConfiguration().getServiceClassLoader());

    Iterator<AxisOperation> operations = axisService.getOperations();
    while (operations.hasNext()) {
        AxisOperation operation = operations.next();
        if (operation.getMessageReceiver() == null) {
            operation.setMessageReceiver(receiver);
        }
    }
    getConfigurationContext().getAxisConfiguration().addService(axisService);
}

From source file:com.reprezen.swagedit.json.references.JsonDocumentManager.java

public JsonNode getDocument(URI uri) {
    final IFile file = getFile(uri);
    if (file == null || !file.exists()) {
        return null;
    }/*ww w . j  av a2 s. c  o m*/

    try {
        return getDocument(uri.toURL());
    } catch (MalformedURLException e) {
        return null;
    }
}

From source file:org.jrecruiter.service.impl.DataServiceImpl.java

/** {@inheritDoc} */
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public Image getGoogleMapImage(final BigDecimal latitude, final BigDecimal longitude, final Integer zoomLevel) {

    if (longitude == null) {
        throw new IllegalArgumentException("Longitude cannot be null.");
    }//from  w  ww.j a  v a 2 s .  c o m

    if (latitude == null) {
        throw new IllegalArgumentException("Latitude cannot be null.");
    }

    if (zoomLevel == null) {
        throw new IllegalArgumentException("ZoomLevel cannot be null.");
    }

    final URI url = GoogleMapsUtils.buildGoogleMapsStaticUrl(latitude, longitude, zoomLevel);

    BufferedImage img;
    try {
        URLConnection conn = url.toURL().openConnection();
        img = ImageIO.read(conn.getInputStream());
    } catch (UnknownHostException e) {
        LOGGER.error("Google static MAPS web service is not reachable (UnknownHostException).", e);
        img = new BufferedImage(GoogleMapsUtils.defaultWidth, 100, BufferedImage.TYPE_INT_RGB);

        final Graphics2D graphics = img.createGraphics();

        final Map<Object, Object> renderingHints = CollectionUtils.getHashMap();
        renderingHints.put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        graphics.addRenderingHints(renderingHints);
        graphics.setBackground(Color.WHITE);
        graphics.setColor(Color.GRAY);
        graphics.clearRect(0, 0, GoogleMapsUtils.defaultWidth, 100);

        graphics.drawString("Not Available", 30, 30);

    } catch (IOException e) {
        throw new IllegalStateException(e);
    }

    return img;
}

From source file:ar.com.aleatoria.ue.rest.SecureSimpleClientHttpRequestFactory.java

public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException {
    HttpURLConnection connection = openConnection(uri.toURL(), this.proxy);
    prepareConnection(connection, httpMethod.name());
    prepareSecureConnection(connection);
    if (this.bufferRequestBody) {
        return new SimpleBufferingClientHttpRequest(connection);
    } else {/*from  w w w .j  a v a  2s .co m*/
        return new SimpleStreamingClientHttpRequest(connection, this.chunkSize);
    }
}

From source file:com.smartitengineering.util.simple.reflection.DefaultClassScannerImpl.java

private ClassReader getClassReader(URI classFileUri) {
    InputStream is = null;/*from   w  w  w  .  jav a 2 s .c  o m*/
    try {
        is = classFileUri.toURL().openStream();
        ClassReader cr = new ClassReader(is);
        return cr;
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    } finally {
        try {
            if (is != null) {
                is.close();
            }
        } catch (IOException ex) {
        }
    }
}