Example usage for java.net HttpURLConnection getContentEncoding

List of usage examples for java.net HttpURLConnection getContentEncoding

Introduction

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

Prototype

public String getContentEncoding() 

Source Link

Document

Returns the value of the content-encoding header field.

Usage

From source file:org.apache.hyracks.maven.license.DownloadLicensesMojo.java

private void doDownload(int timeoutMillis, int id, String url, String fileName) {
    try {//from w w w .ja  va  2s . c om
        HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
        conn.setConnectTimeout(timeoutMillis);
        conn.setReadTimeout(timeoutMillis);
        conn.setRequestMethod("GET");
        final File outFile = new File(downloadDir, fileName);
        getLog().info("[" + id + "] " + url + " -> " + outFile);
        final InputStream is = conn.getInputStream();
        try (FileOutputStream fos = new FileOutputStream(outFile);
                Writer writer = new OutputStreamWriter(fos, StandardCharsets.UTF_8)) {
            IOUtils.copy(is, writer, conn.getContentEncoding());
        }
        getLog().info("[" + id + "] ...done!");
    } catch (IOException e) {
        getLog().warn("[" + id + "] ...error downloading " + url + ": " + e);
    }
}

From source file:org.sufficientlysecure.keychain.keyimport.HkpKeyServer.java

private String query(String request) throws QueryException, HttpError {
    InetAddress ips[];//  w  ww .  j av  a2  s . c  o  m
    try {
        ips = InetAddress.getAllByName(mHost);
    } catch (UnknownHostException e) {
        throw new QueryException(e.toString());
    }
    for (int i = 0; i < ips.length; ++i) {
        try {
            String url = "http://" + ips[i].getHostAddress() + ":" + mPort + request;
            Log.d(Constants.TAG, "hkp keyserver query: " + url);
            URL realUrl = new URL(url);
            HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
            conn.setConnectTimeout(5000);
            conn.setReadTimeout(25000);
            conn.connect();
            int response = conn.getResponseCode();
            if (response >= 200 && response < 300) {
                return readAll(conn.getInputStream(), conn.getContentEncoding());
            } else {
                String data = readAll(conn.getErrorStream(), conn.getContentEncoding());
                throw new HttpError(response, data);
            }
        } catch (MalformedURLException e) {
            // nothing to do, try next IP
        } catch (IOException e) {
            // nothing to do, try next IP
        }
    }

    throw new QueryException("querying server(s) for '" + mHost + "' failed");
}

From source file:org.thialfihar.android.apg.keyimport.HkpKeyserver.java

private String query(String request) throws QueryFailedException, HttpError {
    InetAddress ips[];//from ww w.j  a va  2  s .c  o  m
    try {
        ips = InetAddress.getAllByName(mHost);
    } catch (UnknownHostException e) {
        throw new QueryFailedException(e.toString());
    }
    for (int i = 0; i < ips.length; ++i) {
        try {
            String url = "http://" + ips[i].getHostAddress() + ":" + mPort + request;
            Log.d(Constants.TAG, "hkp keyserver query: " + url);
            URL realUrl = new URL(url);
            HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
            conn.setConnectTimeout(5000);
            conn.setReadTimeout(25000);
            conn.connect();
            int response = conn.getResponseCode();
            if (response >= 200 && response < 300) {
                return readAll(conn.getInputStream(), conn.getContentEncoding());
            } else {
                String data = readAll(conn.getErrorStream(), conn.getContentEncoding());
                throw new HttpError(response, data);
            }
        } catch (MalformedURLException e) {
            // nothing to do, try next IP
        } catch (IOException e) {
            // nothing to do, try next IP
        }
    }

    throw new QueryFailedException("querying server(s) for '" + mHost + "' failed");
}

From source file:uk.ac.gate.cloud.client.RestClient.java

/**
 * Read an error response from the given connection and throw a
 * suitable {@link RestClientException}. This method always throws an
 * exception, it will never return normally.
 *//*from  ww w .jav a 2 s . co  m*/
private void readError(HttpURLConnection connection) throws RestClientException {
    InputStream stream;
    try {
        String encoding = connection.getContentEncoding();
        if ("gzip".equalsIgnoreCase(encoding)) {
            stream = new GZIPInputStream(connection.getErrorStream());
        } else {
            stream = connection.getErrorStream();
        }

        InputStreamReader reader = new InputStreamReader(stream, "UTF-8");

        try {
            JsonNode errorNode = null;
            errorNode = MAPPER.readTree(stream);

            throw new RestClientException("Server returned response code " + connection.getResponseCode(),
                    errorNode, connection.getResponseCode(), connection.getHeaderFields());

        } finally {
            reader.close();
        }
    } catch (RestClientException e2) {
        throw e2;
    } catch (Exception e2) {
        throw new RestClientException("Error communicating with server", e2);
    }
}

From source file:at.mukprojects.giphy4j.dao.HttpRequestSender.java

private Response send(HttpURLConnection connection) throws IOException {
    connection.connect();/*from   w ww . ja va2s  .c  o  m*/

    if (connection.getResponseCode() != 200) {
        throw new IOException("Bad response! Code: " + connection.getResponseCode());
    }

    Map<String, String> headers = new HashMap<String, String>();
    for (String key : connection.getHeaderFields().keySet()) {
        headers.put(key, connection.getHeaderFields().get(key).get(0));
    }

    String body = null;
    InputStream inputStream = null;

    try {
        inputStream = connection.getInputStream();

        String encoding = connection.getContentEncoding();
        encoding = encoding == null ? Giphy4JConstants.ENCODING : encoding;

        body = IOUtils.toString(inputStream, encoding);
    } catch (IOException e) {
        throw new IOException(e);
    } finally {
        if (inputStream != null) {
            inputStream.close();
        }
    }

    if (body == null) {
        throw new IOException("Unparseable response body! \n {" + body + "}");
    }

    Response response = new Response(headers, body);

    return response;
}

From source file:org.shredzone.flattr4j.connector.impl.FlattrConnection.java

/**
 * Reads the returned HTTP response as string.
 *
 * @param conn//from   w  ww. j a v  a  2 s.com
 *            {@link HttpURLConnection} to read from
 * @return Response read
 */
private String readResponse(HttpURLConnection conn) throws IOException {
    InputStream in = null;

    try {
        in = conn.getErrorStream();
        if (in == null) {
            in = conn.getInputStream();
        }

        if ("gzip".equals(conn.getContentEncoding())) {
            in = new GZIPInputStream(in);
        }

        Charset charset = getCharset(conn.getContentType());
        Reader reader = new InputStreamReader(in, charset);

        // Sadly, the Android API does not offer a JSONTokener for a Reader.
        char[] buffer = new char[1024];
        StringBuilder sb = new StringBuilder();

        int len;
        while ((len = reader.read(buffer)) >= 0) {
            sb.append(buffer, 0, len);
        }

        return sb.toString();
    } finally {
        if (in != null) {
            in.close();
        }
    }
}

From source file:org.sakaiproject.commons.tool.entityprovider.CommonsEntityProvider.java

@EntityCustomAction(action = "getUrlMarkup", viewKey = EntityView.VIEW_LIST)
public ActionReturn getUrlMarkup(OutputStream outputStream, EntityView view, Map<String, Object> params) {

    String userId = getCheckedUser();

    String urlString = (String) params.get("url");

    if (StringUtils.isBlank(urlString)) {
        throw new EntityException("No url supplied", "", HttpServletResponse.SC_BAD_REQUEST);
    }/*from   w w w . java 2s .c om*/

    try {
        CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
        URL url = new URL(urlString);
        URLConnection c = url.openConnection();

        if (c instanceof HttpURLConnection) {
            HttpURLConnection conn = (HttpURLConnection) c;
            conn.setRequestProperty("User-Agent", USER_AGENT);
            conn.setInstanceFollowRedirects(false);
            conn.connect();
            String contentEncoding = conn.getContentEncoding();
            String contentType = conn.getContentType();
            int responseCode = conn.getResponseCode();
            log.debug("Response code: {}", responseCode);

            int redirectCounter = 1;
            while ((responseCode == HttpURLConnection.HTTP_MOVED_PERM
                    || responseCode == HttpURLConnection.HTTP_MOVED_TEMP
                    || responseCode == HttpURLConnection.HTTP_SEE_OTHER) && redirectCounter < 20) {
                String newUri = conn.getHeaderField("Location");
                log.debug("{}. New URI: {}", responseCode, newUri);
                String cookies = conn.getHeaderField("Set-Cookie");
                url = new URL(newUri);
                c = url.openConnection();
                conn = (HttpURLConnection) c;
                conn.setInstanceFollowRedirects(false);
                conn.setRequestProperty("User-Agent", USER_AGENT);
                conn.setRequestProperty("Cookie", cookies);
                conn.connect();
                contentEncoding = conn.getContentEncoding();
                contentType = conn.getContentType();
                responseCode = conn.getResponseCode();
                log.debug("Redirect counter: {}", redirectCounter);
                log.debug("Response code: {}", responseCode);
                redirectCounter += 1;
            }

            if (contentType != null
                    && (contentType.startsWith("text/html") || contentType.startsWith("application/xhtml+xml")
                            || contentType.startsWith("application/xml"))) {
                String mimeType = contentType.split(";")[0].trim();
                log.debug("mimeType: {}", mimeType);
                log.debug("encoding: {}", contentEncoding);

                BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream));

                String line = null;
                while ((line = reader.readLine()) != null) {
                    writer.write(line);
                }

                return new ActionReturn(contentEncoding, mimeType, outputStream);
            } else {
                log.debug("Invalid content type {}. Throwing bad request ...", contentType);
                throw new EntityException("Url content type not supported", "",
                        HttpServletResponse.SC_BAD_REQUEST);
            }
        } else {
            throw new EntityException("Url content type not supported", "", HttpServletResponse.SC_BAD_REQUEST);
        }
    } catch (MalformedURLException mue) {
        throw new EntityException("Invalid url supplied", "", HttpServletResponse.SC_BAD_REQUEST);
    } catch (IOException ioe) {
        throw new EntityException("Failed to download url contents", "",
                HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    }
}

From source file:org.openestate.is24.restapi.DefaultClient.java

/**
 * Retrieve a {@link Response} from a request, that was sent through
 * {@link HttpURLConnection}.//from www . j av a  2  s  . com
 *
 * @param connection
 * {@link HttpURLConnection}, that contains the server response
 *
 * @return
 * {@link Response} of the request
 *
 * @throws IOException
 * if the {@link Response} can't be obtained
 */
protected Response createResponse(HttpURLConnection connection) throws IOException {
    InputStream responseInput = null;
    try {
        //String encoding = StringUtils.trimToNull( connection.getContentEncoding() );
        //if (encoding==null) encoding = getEncoding();
        String encoding = getEncoding();

        // read response body
        responseInput = new BufferedInputStream(connection.getInputStream());

        // possibly decompress response body from gzip
        if ("gzip".equalsIgnoreCase(connection.getContentEncoding()))
            responseInput = new GZIPInputStream(responseInput);

        // create response
        return new Response(connection.getResponseCode(), connection.getResponseMessage(),
                connection.getHeaderField(RESPONSE_HEADER_REQUEST_REFNUM),
                IOUtils.toString(responseInput, encoding));
    } finally {
        IOUtils.closeQuietly(responseInput);
        IOUtils.close(connection);
    }
}

From source file:org.nuxeo.labs.operations.services.HTTPDownloadFile.java

@OperationMethod
public Blob run() throws IOException {

    Blob result = null;//from w ww. j av  a 2 s.  c o  m

    HttpURLConnection http = null;
    String error = "";
    String resultStatus = "";
    boolean isUnknownHost = false;

    try {
        URL theURL = new URL(url);

        http = (HttpURLConnection) theURL.openConnection();
        HTTPUtils.addHeaders(http, headers, headersAsJSON);

        if (http.getResponseCode() == HttpURLConnection.HTTP_OK) {

            String fileName = "";
            String disposition = http.getHeaderField("Content-Disposition");
            String contentType = http.getContentType();
            String encoding = http.getContentEncoding();

            // Try to get a filename
            if (disposition != null) {
                // extracts file name from header field
                int index = disposition.indexOf("filename=");
                if (index > -1) {
                    fileName = disposition.substring(index + 9);
                }
            } else {
                // extracts file name from URL
                fileName = url.substring(url.lastIndexOf("/") + 1, url.length());
            }
            if (StringUtils.isEmpty(fileName)) {
                fileName = "DownloadedFile-" + java.util.UUID.randomUUID().toString();
            }

            File tempFile = new File(
                    getTempFolderPath() + File.separator + java.util.UUID.randomUUID().toString());

            FileOutputStream outputStream = new FileOutputStream(tempFile);
            InputStream inputStream = http.getInputStream();
            int bytesRead = -1;
            byte[] buffer = new byte[BUFFER_SIZE];
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }

            outputStream.close();
            inputStream.close();

            result = new FileBlob(tempFile, contentType, encoding);
            result.setFilename(fileName);
        }

    } catch (Exception e) {

        error = e.getMessage();
        if (e instanceof java.net.UnknownHostException) {
            isUnknownHost = true;
        }

    } finally {
        int status = 0;
        String statusMessage = "";

        if (isUnknownHost) { // can't use our http variable
            status = 0;
            statusMessage = "UnknownHostException";
        } else {
            // Still, other failures _before_ reaching the server may occur
            // => http.getResponseCode() and others would throw an error
            try {
                status = http.getResponseCode();
                statusMessage = http.getResponseMessage();
            } catch (Exception e) {
                statusMessage = "Error getting the status message itself";
            }

        }

        ObjectMapper mapper = new ObjectMapper();
        ObjectNode resultObj = mapper.createObjectNode();
        resultObj.put("status", status);
        resultObj.put("statusMessage", statusMessage);
        resultObj.put("error", error);

        ObjectWriter ow = mapper.writer();// .withDefaultPrettyPrinter();
        resultStatus = ow.writeValueAsString(resultObj);
    }

    ctx.put("httpDownloadFileStatus", resultStatus);

    return result;
}

From source file:com.ontotext.s4.client.HttpClient.java

/**
* Read a response or error message from the given connection, handling any 303 redirect responses
* if <code>followRedirects</code> is true.
*//*w  w  w  .ja  v  a  2  s.  c o  m*/
private <T> T readResponseOrError(HttpURLConnection connection, TypeReference<T> responseType,
        boolean followRedirects) throws HttpClientException {

    InputStream stream = null;
    try {
        int responseCode = connection.getResponseCode();
        if (responseCode == HttpURLConnection.HTTP_NO_CONTENT) {
            // successful response with no content
            return null;
        }
        String encoding = connection.getContentEncoding();
        if ("gzip".equalsIgnoreCase(encoding)) {
            stream = new GZIPInputStream(connection.getInputStream());
        } else {
            stream = connection.getInputStream();
        }

        if (responseCode < 300 || responseCode >= 400 || !followRedirects) {
            try {
                return MAPPER.readValue(stream, responseType);
            } finally {
                stream.close();
            }
        } else {
            // redirect - all redirects we care about from the S4
            // APIs are 303. We have to follow them manually to make
            // authentication work properly.
            String location = connection.getHeaderField("Location");
            // consume body
            IOUtils.copy(stream, new NullOutputStream());
            IOUtils.closeQuietly(stream);
            // follow the redirect
            return get(location, responseType);
        }
    } catch (Exception e) {
        readError(connection);
        return null; // unreachable, as readError always throws exception
    }
}