Example usage for java.net HttpURLConnection getContentType

List of usage examples for java.net HttpURLConnection getContentType

Introduction

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

Prototype

public String getContentType() 

Source Link

Document

Returns the value of the content-type header field.

Usage

From source file:org.apache.roller.weblogger.util.MediacastUtil.java

/**
 * Validate a Mediacast resource.//  w  w w.  j av a  2 s  .  c  om
 */
public static final MediacastResource lookupResource(String url) throws MediacastException {

    if (url == null || url.trim().length() == 0) {
        return null;
    }

    MediacastResource resource = null;
    try {
        HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection();
        con.setRequestMethod("HEAD");
        int response = con.getResponseCode();
        String message = con.getResponseMessage();

        if (response != 200) {
            log.debug("Mediacast error " + response + ":" + message + " from url " + url);
            throw new MediacastException(BAD_RESPONSE, "weblogEdit.mediaCastResponseError");
        } else {
            String contentType = con.getContentType();
            long length = con.getContentLength();

            if (contentType == null || length == -1) {
                log.debug("Response valid, but contentType or length is invalid");
                throw new MediacastException(INCOMPLETE, "weblogEdit.mediaCastLacksContentTypeOrLength");
            }

            resource = new MediacastResource(url, contentType, length);
            log.debug("Valid mediacast resource = " + resource.toString());

        }
    } catch (MalformedURLException mfue) {
        log.debug("Malformed MediaCast url: " + url);
        throw new MediacastException(BAD_URL, "weblogEdit.mediaCastUrlMalformed", mfue);
    } catch (Exception e) {
        log.error("ERROR while checking MediaCast URL: " + url + ": " + e.getMessage());
        throw new MediacastException(CHECK_FAILED, "weblogEdit.mediaCastFailedFetchingInfo", e);
    }
    return resource;
}

From source file:com.eTilbudsavis.etasdk.network.impl.HttpURLNetwork.java

private static HttpEntity getEntity(HttpURLConnection connection) {
    BasicHttpEntity entity = new BasicHttpEntity();
    InputStream inputStream;//ww w .j  av a  2  s .  co  m
    try {
        inputStream = connection.getInputStream();
    } catch (IOException ioe) {
        inputStream = connection.getErrorStream();
    }
    entity.setContent(inputStream);
    entity.setContentLength(connection.getContentLength());
    entity.setContentEncoding(connection.getContentEncoding());
    entity.setContentType(connection.getContentType());
    return entity;
}

From source file:org.bibsonomy.util.WebUtils.java

/** Extracts the charset ID of a web page as returned by the server.
 * //from w w  w .ja va 2 s  . co m
 * @param urlConn
 * @return
 */
private static String getCharset(final HttpURLConnection urlConn) {
    return extractCharset(urlConn.getContentType());
}

From source file:org.javaweb.utils.HttpRequestUtils.java

/**
 * HTTP????HttpResponse/*from  w w w.j ava  2s . c  o m*/
 *
 * @param httpURLConnection
 * @param response
 * @throws IOException
 */
public static void setResponse(HttpURLConnection httpURLConnection, HttpResponse response) throws IOException {
    response.setStatusCode(httpURLConnection.getResponseCode());
    response.setStatusMessage(httpURLConnection.getResponseMessage());
    response.setContentType(httpURLConnection.getContentType());
    response.setHeader(new CaseInsensitiveMap(httpURLConnection.getHeaderFields()));
    response.setLastModified(httpURLConnection.getLastModified());

    // ?Cookie
    setCookies(response);
}

From source file:pt.aptoide.backupapps.util.NetworkUtils.java

public static int checkServerConnection(String string, String username, String password) throws IOException {
    HttpURLConnection client = (HttpURLConnection) new URL(string).openConnection();
    if (username != null && password != null) {
        String basicAuth = "Basic "
                + new String(Base64.encode((username + ":" + password).getBytes(), Base64.NO_WRAP));
        client.setRequestProperty("Authorization", basicAuth);
    }//from   w w w  .j a  v  a2 s  .com
    client.setConnectTimeout(TIME_OUT);
    client.setReadTimeout(TIME_OUT);

    if (client.getContentType().equals("application/xml")) {

        client.disconnect();

        return 0;
    } else {

        int responseCode = client.getResponseCode();
        client.disconnect();

        return responseCode;
    }

}

From source file:com.grosscommerce.ICEcat.utilities.Downloader.java

public static void download(String urlFrom, String login, String pwd, OutputStream destStream)
        throws Exception {
    try {/*from  w  w  w  .ja v  a2 s .  c o m*/
        HttpURLConnection uc = prepareConnection(urlFrom, login, pwd);

        uc.connect();

        if (uc.getResponseCode() != HttpURLConnection.HTTP_OK) {
            Logger.getLogger(Downloader.class.getName()).log(Level.INFO, "Error, code: {0}, message {1}",
                    new Object[] { uc.getResponseCode(), uc.getResponseMessage() });

            return;
        }

        BufferedInputStream is = null;

        if ((uc.getContentEncoding() != null && uc.getContentEncoding().toLowerCase().equals("gzip"))
                || uc.getContentType() != null && uc.getContentType().toLowerCase().contains("gzip")) {
            is = new BufferedInputStream(new GZIPInputStream(uc.getInputStream()));

            Logger.getLogger(Downloader.class.getName()).log(Level.INFO, "Will download gzip data from: {0}",
                    urlFrom);
        } else {
            is = new BufferedInputStream(uc.getInputStream());

            Logger.getLogger(Downloader.class.getName()).log(Level.INFO,
                    "Will download not compressed data from:{0}", urlFrom);
        }

        StreamsHelper.copy(is, destStream);
        destStream.flush();

    } catch (Exception ex) {
        Logger.getLogger(Downloader.class.getName()).log(Level.SEVERE, "URL: " + urlFrom, ex);

        throw ex;
    }
}

From source file:cn.garymb.wechatmoments.common.OkHttpStack.java

@SuppressWarnings("deprecation")
private static HttpEntity entityFromConnection(HttpURLConnection connection) {
    BasicHttpEntity entity = new BasicHttpEntity();
    InputStream inputStream;/*from  w  w w  .  j a  v  a2s  .com*/
    try {
        inputStream = connection.getInputStream();
    } catch (IOException ioe) {
        inputStream = connection.getErrorStream();
    }
    entity.setContent(inputStream);
    entity.setContentLength(connection.getContentLength());
    entity.setContentEncoding(connection.getContentEncoding());
    entity.setContentType(connection.getContentType());
    return entity;
}

From source file:com.kubeiwu.commontool.khttp.toolbox.HurlStack.java

/**
 * Initializes an {@link HttpEntity} from the given {@link HttpURLConnection}.
 * /*from www .jav  a  2 s.  c  o  m*/
 * @param connection
 * @return an HttpEntity populated with data from <code>connection</code>.
 */
private static HttpEntity entityFromConnection(HttpURLConnection connection) {

    BasicHttpEntity entity = new BasicHttpEntity();
    InputStream inputStream;
    try {
        inputStream = connection.getInputStream();
    } catch (IOException ioe) {
        inputStream = connection.getErrorStream();
    }
    entity.setContent(inputStream);
    entity.setContentLength(connection.getContentLength());
    entity.setContentEncoding(connection.getContentEncoding());
    entity.setContentType(connection.getContentType());
    return entity;
}

From source file:com.android.volley.toolbox.HurlStack.java

/**
 * Initializes an {@link HttpEntity} from the given {@link HttpURLConnection}.
 * @param connection//w ww  . ja  v  a2s  .c om
 * @return an HttpEntity populated with data from <code>connection</code>.
 */
private static HttpEntity entityFromConnection(HttpURLConnection connection) {
    BasicHttpEntity entity = new BasicHttpEntity();
    InputStream inputStream;
    try {
        inputStream = connection.getInputStream();
    } catch (IOException ioe) {
        inputStream = connection.getErrorStream();
    }
    entity.setContent(inputStream);
    entity.setContentLength(connection.getContentLength());
    entity.setContentEncoding(connection.getContentEncoding());
    entity.setContentType(connection.getContentType());
    return entity;
}

From source file:com.nxt.zyl.data.volley.toolbox.HurlStack.java

/**
 * Initializes an {@link org.apache.http.HttpEntity} from the given {@link java.net.HttpURLConnection}.
 * @param connection/*from   w  w w  . j a  v  a2s .co  m*/
 * @return an HttpEntity populated with data from <code>connection</code>.
 */
private static HttpEntity entityFromConnection(HttpURLConnection connection) {
    BasicHttpEntity entity = new BasicHttpEntity();
    InputStream inputStream;
    try {
        inputStream = connection.getInputStream();
    } catch (IOException ioe) {
        inputStream = connection.getErrorStream();
    }
    entity.setContent(inputStream);
    entity.setContentLength(connection.getContentLength());
    entity.setContentEncoding(connection.getContentEncoding());
    entity.setContentType(connection.getContentType());

    return entity;
}