Example usage for java.net URLConnection getContentEncoding

List of usage examples for java.net URLConnection getContentEncoding

Introduction

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

Prototype

public String getContentEncoding() 

Source Link

Document

Returns the value of the content-encoding header field.

Usage

From source file:org.culturegraph.mf.stream.pipe.TripleObjectRetriever.java

@Override
public void process(final Triple triple) {
    assert !isClosed();

    if (triple.getObjectType() != ObjectType.STRING) {
        return;/*from  www  . j  a  v a 2 s  .c om*/
    }

    final String objectValue;
    try {
        final URL url = new URL(triple.getObject());
        final URLConnection con = url.openConnection();
        String enc = con.getContentEncoding();
        if (enc == null) {
            enc = defaultEncoding;
        }
        objectValue = IOUtils.toString(con.getInputStream(), enc);
    } catch (IOException e) {
        throw new MetafactureException(e);
    }

    getReceiver().process(new Triple(triple.getSubject(), triple.getPredicate(), objectValue));
}

From source file:league.stat.checker.nameToID.java

public String nameToID(String name) throws IOException {
    //HTTP response input code taken WhiteFang34 on Stack overflow ; http://stackoverflow.com/questions/5769717/how-can-i-get-an-http-response-body-as-a-string-in-java
    URL url = new URL("https://na.api.pvp.net/api/lol/na/v1.4/summoner/by-name/" + name
            + "?api_key=RGAPI-f2ba678c-7c42-4265-8c36-ae1364c3e9b9");
    URLConnection con = url.openConnection();
    InputStream in = con.getInputStream();
    String encoding = con.getContentEncoding();
    encoding = encoding == null ? "UTF-8" : encoding;
    String body = IOUtils.toString(in, encoding);

    //Make this code more efficient by using String slicing
    int index = body.indexOf("id") + 4;
    String sID = "";

    while (body.charAt(index) != ',') {
        sID += body.charAt(index);/*w w  w.  ja  va2 s . c  o  m*/
        index++;
    }

    return sID;
}

From source file:league.stat.checker.nameToID.java

public String[] findInfo(String input, String name) throws IOException {
    String[] output = new String[5];
    output[0] = name;//www  .j  a v a 2s  . com
    output[1] = input;

    URL url = new URL("https://na.api.pvp.net/api/lol/na/v1.3/stats/by-summoner/" + input
            + "/summary?season=SEASON2017&api_key=RGAPI-f2ba678c-7c42-4265-8c36-ae1364c3e9b9");
    URLConnection con = url.openConnection();
    InputStream in = con.getInputStream();
    String encoding = con.getContentEncoding();
    encoding = encoding == null ? "UTF-8" : encoding;
    String body = IOUtils.toString(in, encoding);
    String rankedInfo = body.substring(body.indexOf("RankedSolo5x5"), body.lastIndexOf("}"));

    //Wins Number
    output[2] = rankedInfo.substring(rankedInfo.indexOf("wins") + 6, rankedInfo.length()).substring(0,
            rankedInfo.substring(rankedInfo.indexOf("wins") + 6, rankedInfo.length()).indexOf(","));
    //Loss Number
    output[3] = rankedInfo.substring(rankedInfo.indexOf("losses") + 8, rankedInfo.length()).substring(0,
            rankedInfo.substring(rankedInfo.indexOf("losses") + 8, rankedInfo.length()).indexOf(","));
    //Win percent
    output[4] = (String.format("%.2f",
            (Double.parseDouble(output[2]) / (Double.parseDouble(output[2]) + Double.parseDouble(output[3])))
                    * 100));

    return output;
}

From source file:org.n52.wps.server.request.strategy.WCS111XMLEmbeddedBase64OutputReferenceStrategy.java

private InputStream retrievingZippedContent(URLConnection conn) throws IOException {
    String contentType = conn.getContentEncoding();
    if (contentType != null && contentType.equals("gzip")) {
        return new GZIPInputStream(conn.getInputStream());
    } else {//from   www . j av  a 2  s .  c o  m
        return conn.getInputStream();
    }
}

From source file:com.esofthead.mycollab.shell.view.CommunitySliderContent.java

public SyndFeed getSyndFeedForUrl(String url) throws IOException, IllegalArgumentException, FeedException {
    SyndFeed feed = null;//w  w  w . j  a  v  a2  s  .  co  m
    InputStream is = null;

    try {
        URLConnection openConnection = new URL(url).openConnection();
        is = new URL(url).openConnection().getInputStream();
        if ("gzip".equals(openConnection.getContentEncoding())) {
            is = new GZIPInputStream(is);
        }
        InputSource source = new InputSource(is);
        SyndFeedInput input = new SyndFeedInput();
        feed = input.build(source);

    } catch (Exception e) {
        LOG.error("Exception occured when building the feed object out of the url", e);
    } finally {
        if (is != null)
            is.close();
    }

    return feed;
}

From source file:com.nominanuda.web.mvc.URLStreamer.java

public HttpResponse handle(HttpRequest request) throws IOException {
    URL url = getURL(request);//w w w.  ja  v a 2 s .  co  m
    URLConnection conn = url.openConnection();
    conn.connect();
    int len = conn.getContentLength();
    InputStream is = conn.getInputStream();
    String ce = conn.getContentEncoding();
    String ct = determineContentType(url, conn);
    if (len < 0) {
        byte[] content = ioHelper.readAndClose(is);
        is = new ByteArrayInputStream(content);
        len = content.length;
    }
    StatusLine statusline = httpCoreHelper.statusLine(SC_OK);
    HttpResponse resp = new BasicHttpResponse(statusline);
    resp.setEntity(new InputStreamEntity(is, len));
    httpCoreHelper.setContentType(resp, ct);
    httpCoreHelper.setContentLength(resp, len);//TODO not needed ??
    if (ce != null) {
        httpCoreHelper.setContentEncoding(resp, ce);
    }
    return resp;
}

From source file:org.norvelle.addressdiscoverer.parse.structured.StructuredPageWebContactLink.java

/**
  * Fetches the web page specified by the contact weblink and extracts
  * an email from it. The email gets stored in the address field for retrieval
  * by the Individual extractor. Note that we fetch the first such email found
  * and discard others./*w ww .  j av a2  s. com*/
  * 
  * @return 
  * @throws org.norvelle.addressdiscoverer.exceptions.DoesNotContainContactLinkException 
  */
public String fetchEmailFromWeblink() throws DoesNotContainContactLinkException {
    String body;

    if (this.address.startsWith("javascript:"))
        throw new DoesNotContainContactLinkException();

    // Try to fetch the webpage linked to
    try {
        String addr = StructuredPageContactLinkLocator.resolveAddress(this.address);
        URL u = new URL(addr);
        u.toURI();
        URLConnection con = u.openConnection();
        InputStream in = con.getInputStream();
        String encoding = con.getContentEncoding();
        encoding = encoding == null ? "UTF-8" : encoding;
        String html = IOUtils.toString(in, encoding);
        Document soup = Jsoup.parse(html);
        Element bodyElement = soup.select("body").first();
        body = bodyElement.html();
    } catch (URISyntaxException | IOException ex) {
        throw new DoesNotContainContactLinkException();
    }

    // Now, extract the email if we can.
    String matchFound = this.findEmail(body);
    if (matchFound.isEmpty()) {
        throw new DoesNotContainContactLinkException();
    }
    return matchFound;
}

From source file:me.mast3rplan.phantombot.twitch.TwitchAPI.java

public JSONObject getObject(String url) throws IOException {
    URLConnection connection = new URL(url).openConnection();
    connection.setUseCaches(false);//from  w  w  w .  j  a va  2 s  .c  o  m
    connection.setDefaultUseCaches(false);
    String content = IOUtils.toString(connection.getInputStream(), connection.getContentEncoding());
    return new JSONObject(content);
}

From source file:org.sonar.fortify.crawler.Main.java

@VisibleForTesting
String download(URL url) throws IOException {
    LOG.info("Download: " + url);
    URLConnection openConnection = url.openConnection();
    return IOUtils.toString(openConnection.getInputStream(), openConnection.getContentEncoding());
}

From source file:org.theospi.portfolio.presentation.export.StreamedPage.java

public InputStream getStream() throws IOException {
    URLConnection conn = Access.getAccess().openConnection(link);

    // fetch and store final redirected URL and response headers
    InputStream returned = conn.getInputStream();

    this.setContentEncoding(conn.getContentEncoding());
    this.setContentType(conn.getContentType());
    this.setExpiration(conn.getExpiration());
    this.setLastModified(conn.getLastModified());

    return returned;
}