Example usage for java.net URLConnection getContentType

List of usage examples for java.net URLConnection getContentType

Introduction

In this page you can find the example usage for java.net URLConnection getContentType.

Prototype

public String getContentType() 

Source Link

Document

Returns the value of the content-type header field.

Usage

From source file:nl.b3p.viewer.stripes.ProxyActionBean.java

private Resolution proxyWMS() throws IOException {

    HttpServletRequest request = getContext().getRequest();

    if (!"GET".equals(request.getMethod())) {
        return new ErrorResolution(HttpServletResponse.SC_FORBIDDEN);
    }/*  w w  w . j  a  v  a  2 s.c  om*/

    List allowedParams = new ArrayList<String>();
    allowedParams.add("VERSION");
    allowedParams.add("SERVICE");
    allowedParams.add("REQUEST");
    allowedParams.add("UPDATESEQUENCE");
    allowedParams.add("LAYERS");
    allowedParams.add("LAYER");
    allowedParams.add("STYLES");
    allowedParams.add("SRS");
    allowedParams.add("BBOX");
    allowedParams.add("FORMAT");
    allowedParams.add("WIDTH");
    allowedParams.add("HEIGHT");
    allowedParams.add("TRANSPARENT");
    allowedParams.add("BGCOLOR");
    allowedParams.add("EXCEPTIONS");
    allowedParams.add("TIME");
    allowedParams.add("ELEVATION");
    allowedParams.add("QUERY_LAYERS");
    allowedParams.add("X");
    allowedParams.add("Y");
    allowedParams.add("INFO_FORMAT");
    allowedParams.add("FEATURE_COUNT");
    allowedParams.add("SLD");
    allowedParams.add("SLD_BODY");
    //vendor
    allowedParams.add("MAP");

    URL theUrl = new URL(url);

    String query = theUrl.getQuery();
    //only WMS request param's allowed
    String[] params = query.split("&");
    StringBuilder sb = new StringBuilder();
    for (String param : params) {
        if (allowedParams.contains((param.split("=")[0]).toUpperCase())) {
            sb.append(param + "&");
        }
    }
    theUrl = new URL("http", theUrl.getHost(), theUrl.getPort(), theUrl.getPath() + "?" + sb.toString());

    //TODO: Check if response is a getFeatureInfo response.
    final URLConnection connection = theUrl.openConnection();
    return new StreamingResolution(connection.getContentType()) {
        @Override
        protected void stream(HttpServletResponse response) throws IOException {
            IOUtils.copy(connection.getInputStream(), response.getOutputStream());
        }
    };
}

From source file:org.apache.roller.weblogger.ui.rendering.velocity.deprecated.NewsfeedCache.java

/**
 * Returns a Channel object for the supplied RSS newsfeed URL.
 *
 * @param feedUrl RSS newsfeed URL./*from   w  ww .  ja v a  2s.com*/
 * @return FlockFeedI for specified RSS newsfeed URL.
 */
public SyndFeed getChannel(String feedUrl) {

    SyndFeed feed = null;
    try {
        // If aggregator has been disable return null
        if (!aggregator_enabled) {
            return null;
        }

        if (aggregator_cache_enabled) {
            if (log.isDebugEnabled()) {
                log.debug("Newsfeed: use Cache for " + feedUrl);
            }

            // Get pre-parsed feed from the cache
            feed = (SyndFeed) mCache.get(feedUrl);
            if (log.isDebugEnabled()) {
                log.debug("Newsfeed: got from Cache");
            }

            if (feed == null) {
                try {
                    // Parse the feed
                    SyndFeedInput feedInput = new SyndFeedInput();
                    feed = feedInput.build(new InputStreamReader(new URL(feedUrl).openStream()));
                } catch (Exception e1) {
                    log.info("Error parsing RSS: " + feedUrl);
                }
            }
            // Store parsed feed in the cache
            mCache.put(feedUrl, feed);
            log.debug("Newsfeed: not in Cache");

        } else {
            if (log.isDebugEnabled()) {
                log.debug("Newsfeed: not using Cache for " + feedUrl);
            }
            try {
                // charset fix from Jason Rumney (see ROL-766)
                URLConnection connection = new URL(feedUrl).openConnection();
                connection.connect();
                String contentType = connection.getContentType();
                // Default charset to UTF-8, since we are expecting XML
                String charset = "UTF-8";
                if (contentType != null) {
                    int charsetStart = contentType.indexOf("charset=");
                    if (charsetStart >= 0) {
                        int charsetEnd = contentType.indexOf(";", charsetStart);
                        if (charsetEnd == -1)
                            charsetEnd = contentType.length();
                        charsetStart += "charset=".length();
                        charset = contentType.substring(charsetStart, charsetEnd);
                        // Check that charset is recognized by Java
                        try {
                            byte[] test = "test".getBytes(charset);
                        } catch (UnsupportedEncodingException codingEx) {
                            // default to UTF-8
                            charset = "UTF-8";
                        }
                    }
                }
                // Parse the feed
                SyndFeedInput feedInput = new SyndFeedInput();
                feed = feedInput.build(new InputStreamReader(connection.getInputStream(), charset));
            } catch (Exception e1) {
                log.info("Error parsing RSS: " + feedUrl);
            }
        }

    } catch (Exception ioe) {
        if (log.isDebugEnabled()) {
            log.debug("Newsfeed: Unexpected exception", ioe);
        }
    }

    return feed;
}

From source file:org.apache.pulsar.client.impl.auth.AuthenticationAthenz.java

private PrivateKey loadPrivateKey(String privateKeyURL) {
    PrivateKey privateKey = null;
    try {//  w w  w  .  ja va  2s. c  o  m
        URLConnection urlConnection = new URL(privateKeyURL).openConnection();
        String protocol = urlConnection.getURL().getProtocol();
        if ("data".equals(protocol) && !APPLICATION_X_PEM_FILE.equals(urlConnection.getContentType())) {
            throw new IllegalArgumentException(
                    "Unsupported media type or encoding format: " + urlConnection.getContentType());
        }
        String keyData = CharStreams.toString(new InputStreamReader((InputStream) urlConnection.getContent()));
        privateKey = Crypto.loadPrivateKey(keyData);
    } catch (URISyntaxException e) {
        throw new IllegalArgumentException("Invalid privateKey format", e);
    } catch (CryptoException | InstantiationException | IllegalAccessException | IOException e) {
        privateKey = null;
    }
    return privateKey;
}

From source file:org.gradle.internal.resource.UriResource.java

private Reader getInputStream(URI url) throws IOException {
    final URLConnection urlConnection = url.toURL().openConnection();
    urlConnection.setRequestProperty("User-Agent", getUserAgentString());
    urlConnection.connect();//w w  w . j  a  va 2s  .c  o  m
    String charset = extractCharacterEncoding(urlConnection.getContentType(), "utf-8");
    return new InputStreamReader(urlConnection.getInputStream(), charset);
}

From source file:com.aptana.webserver.internal.core.builtin.LocalWebServer.java

private void testConnection(URL url) throws CoreException {
    CoreException exception = null;/*w w  w.  j  a  v  a  2 s.  c  o  m*/
    for (int trial = 0; trial < 3; ++trial) {
        try {
            URLConnection connection = url.openConnection(); // $codepro.audit.disable variableDeclaredInLoop
            connection.connect();
            connection.getContentType();
            return;
        } catch (IOException e) {
            exception = new CoreException(new Status(IStatus.ERROR, WebServerCorePlugin.PLUGIN_ID,
                    "Testing WebServer connection failed", e)); //$NON-NLS-1$
        }
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            break;
        }
    }
    if (exception != null) {
        throw exception;
    }
}

From source file:org.trancecode.xproc.binding.DataPortBinding.java

private void writeContent(final SaxonBuilder builder) {
    final URI uri = URI.create(href);
    if (uri.getScheme() != null && !StringUtils.equals("file", uri.getScheme())
            && !StringUtils.equals("http", uri.getScheme())) {
        throw XProcExceptions.xd0012(this.getLocation(), uri.toASCIIString());
    }/*from  ww w  .  j  ava  2s  .c o  m*/
    try {
        final URL url;
        if (uri.isAbsolute()) {
            url = uri.toURL();
        } else {
            url = node.getBaseURI().resolve(uri).toURL();
        }
        final QName contentTypeAtt = (wrapper == null) ? XProcXmlModel.Attributes.CONTENT_TYPE
                : XProcXmlModel.Attributes.C_CONTENT_TYPE;
        final QName encodingAtt = (wrapper == null) ? XProcXmlModel.Attributes.ENCODING
                : XProcXmlModel.Attributes.C_ENCODING;
        final URLConnection urlConnection = url.openConnection();
        final ContentType guessContentType;
        if (StringUtils.equals("http", url.getProtocol())) {
            guessContentType = Steps.getContentType(urlConnection.getContentType(), node);
        } else {
            if (contentType != null) {
                guessContentType = contentType;
            } else {
                guessContentType = Steps
                        .getContentType("application/octet-stream ; encoding=" + Steps.ENCODING_BASE64, node);
            }
        }
        final Charset charset;
        if (contentType != null && contentType.getParameter("charset") != null) {
            charset = Charset.forName(contentType.getParameter("charset"));
        } else {
            charset = Charset.forName("UTF-8");
        }

        final InputStream stream = urlConnection.getInputStream();
        builder.attribute(contentTypeAtt, Steps.contentTypeToString(guessContentType));
        if (StringUtils.equals("text", guessContentType.getPrimaryType())
                || StringUtils.contains(guessContentType.getSubType(), "xml")) {
            if (guessContentType.getParameter("encoding") != null) {
                builder.attribute(encodingAtt, guessContentType.getParameter("encoding"));
            }
            builder.startContent();
            builder.text(IOUtils.toString(stream, charset.name()));
        } else {
            builder.attribute(encodingAtt, Steps.ENCODING_BASE64);
            builder.startContent();
            builder.text(Base64.encodeBytes(IOUtils.toByteArray(stream), Base64.DO_BREAK_LINES));
        }
    } catch (final IOException ioe) {
        throw XProcExceptions.xd0029(this.getLocation());
    }
}

From source file:no.get.cms.plugin.resourcecompressor.ContentLoader.java

public String load(String srcUrl) {
    try {//from   w  w  w  .j a  v a  2  s.c  o m
        URL url = new URL(srcUrl);
        URLConnection connection = url.openConnection();
        connection.setRequestProperty("Accept-Charset", "UTF-8");
        connection.setConnectTimeout(2000);
        connection.setReadTimeout(10000);
        connection.connect();
        InputStream inputStream = connection.getInputStream();
        String charset = extractCharsetFromContentType(connection.getContentType());
        String body = IOUtils.toString(inputStream, charset);
        IOUtils.close(connection);
        return body;
    } catch (MalformedURLException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.bah.applefox.main.plugins.fulltextindex.FTLoader.java

/** This method is used to get the page source from the given URL
 * @param url - the url from which to get the contents
 * @return - the page contents//from w w w .  ja  v a2s .  c  om
 */
private static String getPageContents(URL url) {

    String pageContents = "";
    try {

        // Open the URL Connection
        URLConnection con = url.openConnection();

        // Get the file path, and eliminate unreadable documents
        String filePath = url.toString();

        // Reads content only if it is a valid format

        if (!(filePath.endsWith(".pdf") || filePath.endsWith(".doc") || filePath.endsWith(".jsp")
                || filePath.endsWith("rss") || filePath.endsWith(".css"))) {
            // Sets the connection timeout (in milliseconds)
            con.setConnectTimeout(1000);

            // Tries to match the character set of the Web Page
            String charset = "utf-8";
            try {
                Matcher m = Pattern.compile("\\s+charset=([^\\s]+)\\s*").matcher(con.getContentType());
                charset = m.matches() ? m.group(1) : "utf-8";
            } catch (Exception e) {
                log.error("Page had no specified charset");
            }

            // Reader derived from the URL Connection's input stream, with
            // the
            // given character set
            Reader r = new InputStreamReader(con.getInputStream(), charset);

            // String Buffer used to append each chunk of Web Page data
            StringBuffer buf = new StringBuffer();

            // Tries to get an estimate of bytes available
            int BUFFER_SIZE = con.getInputStream().available();

            // If BUFFER_SIZE is too small, increases the size
            if (BUFFER_SIZE <= 1000) {
                BUFFER_SIZE = 1000;
            }

            // Character array to hold each chunk of Web Page data
            char[] ch = new char[BUFFER_SIZE];

            // Read the first chunk of Web Page data
            int len = r.read(ch);

            // Loops until end of the Web Page is reached
            while (len != -1) {

                // Appends the data chunk to the string buffer and gets the
                // next chunk
                buf.append(ch, 0, len);
                len = r.read(ch, 0, BUFFER_SIZE);
            }

            // Sets the pageContents to the newly created string
            pageContents = buf.toString();
        }
    } catch (UnsupportedEncodingException e) {
        if (e.getMessage() != null) {
            log.error(e.getMessage());
        } else {
            log.error(e.getStackTrace());
        }

        // Assume the body contents are blank if the character encoding is
        // not supported
        pageContents = "";
    } catch (IOException e) {
        if (e.getMessage() != null) {
            log.error(e.getMessage());
        } else {
            log.error(e.getStackTrace());
        }

        // Assume the body contents are blank if the Web Page could not be
        // accessed
        pageContents = "";
    }

    return pageContents;
}

From source file:nl.imvertor.common.Runner.java

/**
 * Try to access the internet. If this is not possible, try proxy. If not possible, record the internet as unavailable.
 * @throws ConfiguratorException //w  ww  . j ava 2  s.com
 * @throws IOException 
 * @throws Exception
 */
public boolean activateInternet() throws IOException, ConfiguratorException {
    if (!internetAvailable) {
        debug(logger, "Try internet connection");

        String proxyTestUrl = Configurator.getInstance().getParm("cli", "proxyurl");

        URL address = new URL(proxyTestUrl);
        try {
            URLConnection con = address.openConnection();
            con.getContentType();
        } catch (Exception e) {
            internetAvailable = false;
            debug(logger, "No accessible internet connection detected");
        }
        internetAvailable = true;
    }
    return internetAvailable;
}

From source file:wicket.util.resource.UrlResourceStream.java

/**
 * Private constructor to force use of static factory methods.
 * //from w w w  . java2s .c  o m
 * @param url
 *            URL of resource
 */
public UrlResourceStream(final URL url) {
    // Save URL
    this.url = url;
    URLConnection connection = null;
    try {
        connection = url.openConnection();
        contentLength = connection.getContentLength();
        contentType = connection.getContentType();
        lastModified = connection.getLastModified();
        try {
            file = new File(new URI(url.toExternalForm()));
        } catch (Exception ex) {
            log.debug("cannot convert url: " + url + " to file (" + ex.getMessage()
                    + "), falling back to the inputstream for polling");
        }
        if (file != null && !file.exists()) {
            file = null;
        }
    } catch (IOException ex) {
        // It should be impossible to get here or the original URL
        // couldn't have been constructed. But we re-throw with details
        // anyway.
        final IllegalArgumentException illegalArgumentException = new IllegalArgumentException(
                "Invalid URL parameter " + url);
        illegalArgumentException.initCause(ex);
        throw illegalArgumentException;
    } finally {
        // if applicable, disconnect
        if (connection != null) {
            if (connection instanceof HttpURLConnection) {
                ((HttpURLConnection) connection).disconnect();
            } else {
                try {
                    connection.getInputStream().close();
                } catch (Exception ex) {
                    // ignore
                }
            }
        }
    }
}