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:ch.entwine.weblounge.kernel.site.SiteServlet.java

/**
 * Tries to serve the request as a static resource from the bundle.
 * //from   ww w.java2s .c o m
 * @param request
 *          the http servlet request
 * @param response
 *          the http servlet response
 * @throws ServletException
 *           if serving the request fails
 * @throws IOException
 *           if writing the response back to the client fails
 */
protected void serviceResource(final HttpServletRequest request, final HttpServletResponse response)
        throws ServletException, IOException {

    Http11ResponseType responseType = null;
    String requestPath = request.getPathInfo();

    // There is also a special set of resources that we don't want to expose
    if (isProtected(requestPath)) {
        response.sendError(HttpServletResponse.SC_FORBIDDEN);
        return;
    }

    String bundlePath = UrlUtils.concat("/site", requestPath);

    // Does the resource exist?
    final URL url = bundle.getResource(bundlePath);
    if (url == null) {
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
        return;
    }

    // Load the resource from the bundle
    URLConnection connection = url.openConnection();
    String contentEncoding = connection.getContentEncoding();
    long contentLength = connection.getContentLength();
    long lastModified = connection.getLastModified();

    if (contentLength <= 0) {
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
        return;
    }

    // final Resource resource = Resource.newResource(url);
    // if (!resource.exists()) {
    // response.sendError(HttpServletResponse.SC_NOT_FOUND);
    // return;
    // }

    // We don't allow directory listings
    // if (resource.isDirectory()) {
    // response.sendError(HttpServletResponse.SC_FORBIDDEN);
    // return;
    // }

    String mimeType = tika.detect(bundlePath);

    // Try to get mime type and content encoding from resource
    if (mimeType == null)
        mimeType = connection.getContentType();

    if (mimeType != null) {
        if (contentEncoding != null)
            mimeType += ";" + contentEncoding;
        response.setContentType(mimeType);
    }

    // Send the response back to the client
    InputStream is = connection.getInputStream();
    // InputStream is = resource.getInputStream();
    try {
        logger.debug("Serving {}", url);
        responseType = Http11ProtocolHandler.analyzeRequest(request, lastModified,
                Times.MS_PER_DAY + System.currentTimeMillis(), contentLength);
        if (!Http11ProtocolHandler.generateResponse(response, responseType, is)) {
            logger.warn("I/O error while generating content from {}", url);
        }
    } finally {
        IOUtils.closeQuietly(is);
    }

}

From source file:org.deegree.enterprise.servlet.SimpleProxyServlet.java

/**
 * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest,
 *      javax.servlet.http.HttpServletResponse)
 *///w  ww  .  j  av  a 2s . c om
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    InputStream is = null;
    OutputStream os = null;
    try {
        String query = request.getQueryString();
        Map<String, String> map = KVP2Map.toMap(query);
        if (removeCredentials) {
            map.remove("USER");
            map.remove("PASSWORD");
            map.remove("SESSIONID");
            query = "";
            for (String key : map.keySet()) {
                query += key + "=" + map.get(key) + "&";
            }
            query = query.substring(0, query.length() - 1);
        }
        String service = getService(map);
        String hostAddr = host.get(service);
        String req = hostAddr + "?" + query;
        URL url = new URL(req);
        LOG.logDebug("forward URL: " + url);

        URLConnection con = url.openConnection();
        con.setDoInput(true);
        con.setDoOutput(false);
        is = con.getInputStream();
        response.setContentType(con.getContentType());
        response.setCharacterEncoding(con.getContentEncoding());
        os = response.getOutputStream();
        int read = 0;
        byte[] buf = new byte[16384];
        if (LOG.getLevel() == ILogger.LOG_DEBUG) {
            while ((read = is.read(buf)) > -1) {
                os.write(buf, 0, read);
                LOG.logDebug(new String(buf, 0, read,
                        con.getContentEncoding() == null ? "UTF-8" : con.getContentEncoding()));
            }
        } else {
            while ((read = is.read(buf)) > -1) {
                os.write(buf, 0, read);
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
        response.setContentType("text/plain; charset=" + CharsetUtils.getSystemCharset());
        os.write(StringTools.stackTraceToString(e).getBytes());
    } finally {
        try {
            is.close();
        } catch (Exception e) {
            // try to do what ?
        }
        try {
            os.close();
        } catch (Exception e) {
            // try to do what ?
        }
    }
}

From source file:org.alfresco.dataprep.AlfrescoHttpClient.java

/**
 * Generates an Alfresco Authentication Ticket based on the user name and password passed in.
 * @param username user identifier//from w ww .ja v a2s  .  c  om
 * @param password user password
 * @return String authentication ticket
 */
public String getAlfTicket(String username, String password) {
    if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
        throw new IllegalArgumentException("Username and password are required");
    }
    String targetUrl = apiUrl + "login?u=" + username + "&pw=" + password + "&format=json";
    try {
        URL url = new URL(apiUrl + "login?u=" + username + "&pw=" + password + "&format=json");
        URLConnection con = url.openConnection();
        InputStream in = con.getInputStream();
        String encoding = con.getContentEncoding();
        encoding = encoding == null ? UTF_8_ENCODING : encoding;
        String json = IOUtils.toString(in, encoding);
        JSONParser parser = new JSONParser();
        JSONObject obj = (JSONObject) parser.parse(json);
        JSONObject data = (JSONObject) obj.get("data");
        return (String) data.get("ticket");
    } catch (IOException | ParseException e) {
        logger.error(String.format("Unable to generate ticket, url: %s", targetUrl), e);
    }
    throw new RuntimeException("Unable to get ticket");
}

From source file:com.allblacks.utils.web.HttpUtil.java

/**
 * Gets data from URL as String throws {@link RuntimeException} If anything
 * goes wrong//w  ww.j ava2 s  .  com
 * 
 * @return The content of the URL as a String
 * @throws java.io.IOException
 */
public String getDataAsString(String url) throws IOException {
    URLConnection urlc = null;
    InputStream is = null;
    InputStreamReader re = null;
    BufferedReader rd = null;
    String responseBody = "";

    try {
        urlc = getConnection(new URL(url));
        if (urlc.getContentEncoding() != null && urlc.getContentEncoding().equalsIgnoreCase(HttpUtil.GZIP)) {
            is = new GZIPInputStream(urlc.getInputStream());
        } else {
            is = urlc.getInputStream();
        }

        re = new InputStreamReader(is, Charset.forName(HttpUtil.UTF_8));
        rd = new BufferedReader(re);

        String line = "";
        while ((line = rd.readLine()) != null) {
            responseBody += line;
            responseBody += HttpUtil.NL;
            line = null;
        }
    } catch (IOException exception) {
        throw exception;
    } finally {
        try {
            rd.close();
            re.close();
        } catch (Exception e) {
            // we do not care about this
        }
    }
    return responseBody;
}

From source file:net.solarnetwork.node.io.url.UrlDataCollector.java

@Override
public void collectData() {
    String resolvedUrl = url;/*from  ww w.  ja  va 2s. c o  m*/
    if (urlFactory != null) {
        resolvedUrl = urlFactory.getObject();
    }
    URL dataUrl = null;
    try {
        dataUrl = new URL(resolvedUrl);
    } catch (MalformedURLException e) {
        throw new RuntimeException("Bad url configured: " + resolvedUrl);
    }
    if (log.isDebugEnabled()) {
        log.debug("Connecting to URL [" + resolvedUrl + ']');
    }
    BufferedReader reader = null;
    String data = null;
    String enc = null;
    Pattern pat = Pattern.compile(matchExpression);
    try {
        URLConnection conn = dataUrl.openConnection();
        conn.setConnectTimeout(connectionTimeout);
        conn.setReadTimeout(connectionTimeout);
        conn.setUseCaches(false);
        InputStream in = conn.getInputStream();
        if (this.encoding == null) {
            enc = conn.getContentEncoding();
            if (enc != null) {
                if (log.isTraceEnabled()) {
                    log.trace("Using connection encoding [" + enc + ']');
                }
                this.encoding = enc;
            }
        }
        if (enc == null) {
            enc = getEncodingToUse();
        }
        reader = new BufferedReader(new InputStreamReader(in, enc));
        String lastLine = null;
        boolean keepGoing = true;
        while (keepGoing) {
            String line = reader.readLine();
            if (line == null) {
                keepGoing = false;
                if (skipToLastLine) {
                    line = lastLine;
                }
            }
            Matcher m = pat.matcher(line);
            if (m.find()) {
                if (log.isDebugEnabled()) {
                    log.debug("Found matching data line [" + line + ']');
                }
                data = line;
                keepGoing = false;
            } else {
                lastLine = line;
            }
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                if (log.isWarnEnabled()) {
                    log.warn("IOException closing input stream: " + e);
                }
            }
        }
    }
    if (data == null) {
        log.info("Input stream finished without finding expected data");
    } else {
        if (this.buffer == null) {
            this.buffer = new StringBuilder(data);
        } else {
            this.buffer.append(data);
        }
    }
}

From source file:org.roosster.input.UrlFetcher.java

/**
 * URLs will be fetched a second time, if the entry's lastFetched
 * object is <code>null</code>, when processed the first time.
 *///from  w w  w. ja v  a 2s.  c  om
private Entry[] fetch(URL url) throws IOException, Exception {
    LOG.debug("Opening connection to URL " + url);

    URLConnection con = url.openConnection();

    long modified = con.getLastModified();

    String embeddedContentEnc = null;

    String contentType = con.getContentType();
    if (contentType != null && contentType.indexOf(";") > -1) {
        LOG.debug("Content-type string (" + contentType + ") contains charset; strip it!");
        contentType = contentType.substring(0, contentType.indexOf(";")).trim();

        String cType = con.getContentType();
        if (cType.indexOf("=") > -1) {
            embeddedContentEnc = cType.substring(cType.indexOf("=") + 1).trim();
        }
    }

    String contentEnc = con.getContentEncoding();
    if (contentEnc == null) {
        if (embeddedContentEnc != null)
            contentEnc = embeddedContentEnc;
        else
            contentEnc = defaultEncoding;
    }

    ContentTypeProcessor proc = getProcessor(contentType);
    LOG.debug("ContentType: '" + contentType + "' - ContentEncoding: '" + contentEnc + "'");
    LOG.debug("Using Processor " + proc);

    Entry[] entries = proc.process(url, con.getInputStream(), contentEnc);

    Date modDate = new Date(modified);
    Date now = new Date();

    List returnArr = new ArrayList();
    for (int i = 0; i < entries.length; i++) {
        if (entries[i] == null)
            continue;

        URL entryUrl = entries[i].getUrl();

        String title = entries[i].getTitle();
        if (title == null || "".equals(title))
            entries[i].setTitle(entryUrl.toString());

        if (entries[i].getModified() == null)
            entries[i].setModified(modDate);

        if (entries[i].getIssued() == null)
            entries[i].setIssued(modDate);

        if (entries[i].getAdded() == null)
            entries[i].setAdded(now);

        String fileType = entries[i].getFileType();
        if (fileType == null || "".equals(fileType)) {

            int dotIndex = entryUrl.getPath().lastIndexOf(".");
            if (dotIndex != -1) {
                String type = entryUrl.getPath().substring(dotIndex + 1);
                entries[i].setFileType(type.toLowerCase());
                LOG.debug("Filetype is subsequently set to '" + type + "'");
            }

        }

        returnArr.add(entries[i]);
        entries[i] = null;
    }

    return (Entry[]) returnArr.toArray(new Entry[0]);
}

From source file:ubic.gemma.image.aba.AllenBrainAtlasServiceImpl.java

private void showHeader(URLConnection url) {

    this.infoOut.println("");
    this.infoOut.println("URL              : " + url.getURL().toString());
    this.infoOut.println("Content-Type     : " + url.getContentType());
    this.infoOut.println("Content-Length   : " + url.getContentLength());
    if (url.getContentEncoding() != null)
        this.infoOut.println("Content-Encoding : " + url.getContentEncoding());
}

From source file:com.apache.ivy.BasicURLHandler.java

public InputStream openStream(URL url) throws IOException {
    // Install the IvyAuthenticator
    if ("http".equals(url.getProtocol()) || "https".equals(url.getProtocol())) {
        IvyAuthenticator.install();//from   w  ww .j a va2s  . c o m
    }

    URLConnection conn = null;
    try {
        url = normalizeToURL(url);
        conn = url.openConnection();
        conn.setRequestProperty("User-Agent", "Apache Ivy/1.0");// + Ivy.getIvyVersion());
        conn.setRequestProperty("Accept-Encoding", "gzip,deflate");
        if (conn instanceof HttpURLConnection) {
            HttpURLConnection httpCon = (HttpURLConnection) conn;
            if (!checkStatusCode(url, httpCon)) {
                throw new IOException("The HTTP response code for " + url + " did not indicate a success."
                        + " See log for more detail.");
            }
        }
        InputStream inStream = getDecodingInputStream(conn.getContentEncoding(), conn.getInputStream());
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();

        byte[] buffer = new byte[BUFFER_SIZE];
        int len;
        while ((len = inStream.read(buffer)) > 0) {
            outStream.write(buffer, 0, len);
        }
        return new ByteArrayInputStream(outStream.toByteArray());
    } finally {
        disconnect(conn);
    }
}

From source file:com.spinn3r.api.BaseClient.java

/**
 * Fetch the API with the given FeedConfig
 * //  www  . j  a  va2s  .c o m
 * @throws IOException if there's an error with network transport.
 * @throws ParseException if there's a problem parsing the resulting XML.
 */
public BaseClientResult<ResultType> completeFetch(PartialBaseClientResult<ResultType> partial_result)
        throws IOException, ParseException, InterruptedException {

    Config<ResultType> config = partial_result.getConfig();
    int request_limit = partial_result.getRequestLimit();
    String resource = partial_result.getLastRequestURL();
    boolean has_results_header = partial_result.getHasMoreResultsHeader();
    boolean has_more_results = partial_result.getHasMoreResults();
    String next_request = partial_result.getNextRequestURL();

    BaseClientResult<ResultType> result = new BaseClientResult<ResultType>(config);

    result.setLastRequestURL(resource);
    result.setRequestLimit(request_limit);
    result.setHasMoreResultsHeadder(has_results_header);
    result.setHasMoreResults(has_more_results);
    result.setNextRequestURL(next_request);

    try {

        long before = System.currentTimeMillis();

        long call_before = System.currentTimeMillis();

        URLConnection conn = partial_result.getConnection();

        //TODO: clean up the naming here.  getLocalInputStream actually
        //reads everything into a byte array in memory.
        InputStream localInputStream = getLocalInputStream(conn.getInputStream(), result);

        result.setLocalInputStream(localInputStream);

        if (GZIP_ENCODING.equals(conn.getContentEncoding()))
            result.setIsCompressed(true);

        InputStream is = result.getInputStream();

        long call_after = System.currentTimeMillis();

        result.setCallDuration(call_after - call_before);

        if (!config.getDisableParse()) {

            if (config.getFormat() == Format.PROTOSTREAM)
                result.setResults(protobufParse(doProtoStreamFetch(localInputStream, config), config));
            else if (config.getFormat() == Format.PROTOBUF)
                result.setResults(protobufParse(doProtobufFetch(localInputStream, config), config));

            else {

                Document doc = doXmlFetch(is, config);

                if (doc != null) {
                    result.setResults(xmlParse(doc, config));
                }

            }
        }

        long after = System.currentTimeMillis();

        result.setParseDuration(after - before);

    }

    catch (Exception e) {
        throw new ParseException(e, "Unable to handle request: " + resource);
    }

    if (!result.getHasMoreResultsHeadder())
        result.setHasMoreResults(result.getResults().size() == request_limit);

    return result;
}

From source file:eu.europa.ec.markt.dss.validation.tsp.OnlineTSPSource.java

/**
 * Get timestamp token - communications layer
 * /*  ww w. j  a  v a  2  s .co  m*/
 * @return - byte[] - TSA response, raw bytes (RFC 3161 encoded)
 */
protected byte[] getTSAResponse(byte[] requestBytes) throws IOException {
    // Setup the TSA connection

    URL tspUrl = new URL(tspServer);
    URLConnection tsaConnection = tspUrl.openConnection();

    tsaConnection.setDoInput(true);
    tsaConnection.setDoOutput(true);
    tsaConnection.setUseCaches(false);
    tsaConnection.setRequestProperty("Content-Type", "application/timestamp-query");
    // tsaConnection.setRequestProperty("Content-Transfer-Encoding",
    // "base64");
    tsaConnection.setRequestProperty("Content-Transfer-Encoding", "binary");

    OutputStream out = tsaConnection.getOutputStream();
    out.write(requestBytes);
    out.close();

    // Get TSA response as a byte array
    InputStream inp = tsaConnection.getInputStream();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int bytesRead = 0;
    while ((bytesRead = inp.read(buffer, 0, buffer.length)) >= 0) {
        baos.write(buffer, 0, bytesRead);
    }
    byte[] respBytes = baos.toByteArray();

    String encoding = tsaConnection.getContentEncoding();
    if (encoding != null && encoding.equalsIgnoreCase("base64")) {
        respBytes = new Base64().decode(respBytes);
    }
    return respBytes;
}