Example usage for com.lowagie.text Utilities toURL

List of usage examples for com.lowagie.text Utilities toURL

Introduction

In this page you can find the example usage for com.lowagie.text Utilities toURL.

Prototype

public static URL toURL(String filename) throws MalformedURLException 

Source Link

Document

This method makes a valid URL from a given filename.

Usage

From source file:org.geomajas.plugin.print.component.impl.LegendViaUrlComponentImpl.java

License:Open Source License

private boolean hasInVisibleResponse() {
    boolean inVisibleResponse = false;

    URL url = null;//w w  w  . jav  a  2s .  c om
    try {
        url = Utilities.toURL(getLegendImageServiceUrl());
    } catch (MalformedURLException e1) {
        // Should never happen!
        log.error("Exception in  Utilities.toURL() for URL " + getLegendImageServiceUrl(), e1);
        e1.printStackTrace();
    }
    if (null == url) {
        log.error("Error in  Utilities.toURL() for URL " + getLegendImageServiceUrl());
        return false; // Unexpected error, ABORT
    }

    HttpURLConnection connection = null;
    try {
        connection = (HttpURLConnection) url.openConnection();
    } catch (IOException e2) {
        log.error("Exception in url.openConnection() for URL " + getLegendImageServiceUrl(), e2);
        e2.printStackTrace();
    }
    if (null == connection) {
        log.error("Error in  url.openConnection() for URL " + getLegendImageServiceUrl());
        return false; // Unexpected error, ABORT
    }

    try {
        connection.connect();
    } catch (IOException e1) {
        log.error("Exception in connection.connect for URL " + getLegendImageServiceUrl(), e1);
        e1.printStackTrace();
        return false; // ABORT
    }

    String contentType = connection.getContentType();
    if (contentType.startsWith("text/")) {
        // Something went wrong (normally the URL returns an image
        // Read the response text
        InputStream is = null;
        try {
            is = url.openStream();
            byte[] messageByteArray = new byte[19];
            is.read(messageByteArray, 0, 19);

            is.close();
            is = null;

            String message = new String(messageByteArray);
            if (NOT_VISIBLE_MSG.equals(message)) {
                inVisibleResponse = true;
            }

        } catch (IOException e1) {
            e1.printStackTrace();
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }
    return inVisibleResponse;
}

From source file:org.geomajas.plugin.print.component.PdfContext.java

License:Open Source License

public Image getImage(String path) {
    try {/*from   w w w .j  av a 2 s.  c  om*/
        URL url = getClass().getResource(path);
        if (url == null) {
            url = Utilities.toURL(path);
        }
        return Image.getInstance(url);
    } catch (Exception e) { // NOSONAR
        log.warn("could not fetch image", e);
        return null;
    }
}