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.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 ww.ja  v  a2 s . c o m*/

    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:com.geocent.owf.openlayers.DataRequestProxy.java

/**
 * Gets the data from the provided remote URL.
 * Note that the data will be returned from the method and not automatically
 * populated into the response object./*ww w.  jav a 2s  .c om*/
 * 
 * @param request       ServletRequest object containing the request data.
 * @param response      ServletResponse object for the response information.
 * @param url           URL from which the data will be retrieved.
 * @return              Data from the provided remote URL.
 * @throws IOException 
 */
public String getData(HttpServletRequest request, HttpServletResponse response, String url) throws IOException {

    if (!allowUrl(url)) {
        throw new UnknownHostException("Request to invalid host not allowed.");
    }

    HttpURLConnection connection = (HttpURLConnection) (new URL(url).openConnection());
    connection.setRequestMethod(request.getMethod());

    // Detects a request with a payload inside the message body
    if (request.getContentLength() > 0) {
        connection.setRequestProperty("Content-Type", request.getContentType());
        connection.setDoOutput(true);
        connection.setDoInput(true);
        byte[] requestPayload = IOUtils.toByteArray(request.getInputStream());
        connection.getOutputStream().write(requestPayload);
        connection.getOutputStream().flush();
    }

    // Create handler for the given content type and proxy the extracted information
    Handler contentHandler = HandlerFactory.getHandler(connection.getContentType());
    return contentHandler.handleContent(response, connection.getInputStream());
}

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

private Resolution proxyArcIMS() throws Exception {

    HttpServletRequest request = getContext().getRequest();

    if (!"POST".equals(request.getMethod())) {
        return new ErrorResolution(HttpServletResponse.SC_FORBIDDEN);
    }//from w ww .jav  a  2s . c om

    Map params = new HashMap(getContext().getRequest().getParameterMap());
    // Only allow these parameters in proxy request
    params.keySet().retainAll(Arrays.asList("ClientVersion", "Encode", "Form", "ServiceName"));
    URL theUrl = new URL(url);
    // Must not allow file / jar etc protocols, only HTTP:
    String path = theUrl.getPath();
    for (Map.Entry<String, String[]> param : (Set<Map.Entry<String, String[]>>) params.entrySet()) {
        if (path.length() == theUrl.getPath().length()) {
            path += "?";
        } else {
            path += "&";
        }
        path += URLEncoder.encode(param.getKey(), "UTF-8") + "="
                + URLEncoder.encode(param.getValue()[0], "UTF-8");
    }
    theUrl = new URL("http", theUrl.getHost(), theUrl.getPort(), path);

    // TODO logging for inspecting malicious proxy use

    ByteArrayOutputStream post = new ByteArrayOutputStream();
    IOUtils.copy(request.getInputStream(), post);

    // This check makes some assumptions on how browsers serialize XML
    // created by OpenLayers' ArcXML.js write() function (whitespace etc.),
    // but all major browsers pass this check
    if (!post.toString("US-ASCII").startsWith("<ARCXML version=\"1.1\"><REQUEST><GET_IMAGE")) {
        return new ErrorResolution(HttpServletResponse.SC_FORBIDDEN);
    }

    final HttpURLConnection connection = (HttpURLConnection) theUrl.openConnection();
    connection.setRequestMethod("POST");
    connection.setDoOutput(true);
    connection.setAllowUserInteraction(false);
    connection.setRequestProperty("X-Forwarded-For", request.getRemoteAddr());

    connection.connect();
    try {
        IOUtils.copy(new ByteArrayInputStream(post.toByteArray()), connection.getOutputStream());
    } finally {
        connection.getOutputStream().flush();
        connection.getOutputStream().close();
    }

    return new StreamingResolution(connection.getContentType()) {
        @Override
        protected void stream(HttpServletResponse response) throws IOException {
            try {
                IOUtils.copy(connection.getInputStream(), response.getOutputStream());
            } finally {
                connection.disconnect();
            }

        }
    };
}

From source file:com.gh4a.utils.HttpImageGetter.java

private Drawable loadImageForUrl(String source) {
    Bitmap bitmap = null;//from   w w  w  .  j  a  va  2  s .  c o m

    if (!mDestroyed) {
        File output = null;
        InputStream is = null;
        HttpURLConnection connection = null;
        try {
            connection = (HttpURLConnection) new URL(source).openConnection();
            is = connection.getInputStream();
            if (is != null) {
                String mime = connection.getContentType();
                if (mime == null) {
                    mime = URLConnection.guessContentTypeFromName(source);
                }
                if (mime == null) {
                    mime = URLConnection.guessContentTypeFromStream(is);
                }
                if (mime != null && mime.startsWith("image/svg")) {
                    bitmap = renderSvgToBitmap(mContext.getResources(), is, mWidth, mHeight);
                } else {
                    boolean isGif = mime != null && mime.startsWith("image/gif");
                    if (!isGif || canLoadGif()) {
                        output = File.createTempFile("image", ".tmp", mCacheDir);
                        if (FileUtils.save(output, is)) {
                            if (isGif) {
                                GifDrawable d = new GifDrawable(output);
                                d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
                                return d;
                            } else {
                                bitmap = getBitmap(output, mWidth, mHeight);
                            }
                        }
                    }
                }
            }
        } catch (IOException e) {
            // fall through to showing the error bitmap
        } finally {
            if (output != null) {
                output.delete();
            }
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    // ignored
                }
            }
            if (connection != null) {
                connection.disconnect();
            }
        }
    }

    synchronized (this) {
        if (mDestroyed && bitmap != null) {
            bitmap.recycle();
            bitmap = null;
        }
    }

    if (bitmap == null) {
        return mErrorDrawable;
    }

    BitmapDrawable drawable = new LoadedBitmapDrawable(mContext.getResources(), bitmap);
    drawable.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight());
    return drawable;
}

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

/**
 * Reads the returned HTTP response as string.
 *
 * @param conn//  w  w  w. ja va  2s .c  o  m
 *            {@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.overlord.rtgov.tests.platforms.jbossas.slamonitor.JBossASSLAMonitorTest.java

/**
 * This method deserializes the events into a list of hashmaps. The
 * actual objects are not deserialized, as this would require the
 * domain objects to be included in all deployments, which would
 * make verifying classloading/isolation difficult.
 * /*from  www .j a v  a 2s . co m*/
 * @return The list of objects representing violations
 * @throws Exception Failed to deserialize the violations
 */
protected java.util.List<?> getViolations() throws Exception {
    java.util.List<?> ret = null;

    String urlStr = "http://localhost:8080/slamonitor/monitor/situations";

    URL getUrl = new URL(urlStr);

    HttpURLConnection connection = (HttpURLConnection) getUrl.openConnection();
    connection.setRequestMethod("GET");
    System.out.println("Content-Type: " + connection.getContentType());

    java.io.InputStream is = connection.getInputStream();

    ret = MAPPER.readValue(is, java.util.List.class);

    return (ret);
}

From source file:org.overlord.rtgov.tests.platforms.jbossas.slamonitor.JBossASSLAMonitorTest.java

/**
 * This method deserializes the events into a list of hashmaps. The
 * actual objects are not deserialized, as this would require the
 * domain objects to be included in all deployments, which would
 * make verifying classloading/isolation difficult.
 * /*from   w  w w . j ava2 s .c o  m*/
 * @param query The query string to supply when requesting response times
 * @return The list of objects representing events
 * @throws Exception Failed to deserialize the events
 */
protected java.util.List<?> getResponseTimes(String query) throws Exception {
    java.util.List<?> ret = null;

    String urlStr = "http://localhost:8080/slamonitor/monitor/responseTimes";

    if (query != null) {
        urlStr += "?" + query;
    }

    URL getUrl = new URL(urlStr);

    HttpURLConnection connection = (HttpURLConnection) getUrl.openConnection();
    connection.setRequestMethod("GET");
    System.out.println("Content-Type: " + connection.getContentType());

    java.io.InputStream is = connection.getInputStream();

    ret = MAPPER.readValue(is, java.util.List.class);

    return (ret);
}

From source file:org.peterbaldwin.vlcremote.sweep.Worker.java

@Override
public void run() {
    // Note: InetAddress#isReachable(int) always returns false for Windows
    // hosts because applications do not have permission to perform ICMP
    // echo requests.
    for (;;) {//  w  w  w.  ja v  a2 s .co  m
        byte[] ipAddress = mManager.pollIpAddress();
        if (ipAddress == null) {
            break;
        }
        try {
            InetAddress address = InetAddress.getByAddress(ipAddress);
            String hostAddress = address.getHostAddress();
            URL url = createUrl("http", hostAddress, mPort, mPath);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setConnectTimeout(1000);
            try {
                int responseCode = connection.getResponseCode();
                String responseMessage = connection.getResponseMessage();
                InputStream inputStream = (responseCode == HttpURLConnection.HTTP_OK)
                        ? connection.getInputStream()
                        : connection.getErrorStream();
                if (inputStream != null) {
                    try {
                        int length = connection.getContentLength();
                        InputStreamEntity entity = new InputStreamEntity(inputStream, length);
                        entity.setContentType(connection.getContentType());

                        // Copy the entire response body into memory
                        // before the HTTP connection is closed:
                        String body = EntityUtils.toString(entity);

                        ProtocolVersion version = new ProtocolVersion("HTTP", 1, 1);
                        String hostname = address.getHostName();
                        StatusLine statusLine = new BasicStatusLine(version, responseCode, responseMessage);
                        HttpResponse response = new BasicHttpResponse(statusLine);
                        response.setHeader(HTTP.TARGET_HOST, hostname + ":" + mPort);
                        response.setEntity(new StringEntity(body));
                        mCallback.onReachable(ipAddress, response);
                    } finally {
                        inputStream.close();
                    }
                } else {
                    Log.w(TAG, "InputStream is null");
                }
            } finally {
                connection.disconnect();
            }
        } catch (IOException e) {
            mCallback.onUnreachable(ipAddress, e);
        }
    }
}

From source file:com.mywork.framework.util.RemoteHttpUtil.java

/**
 *  JDK???/*from   w w  w .  j  a v a 2  s.c  o m*/
 */

private void fetchContentByJDKConnection(HttpServletResponse response, String contentUrl) throws IOException {

    HttpURLConnection connection = (HttpURLConnection) new URL(contentUrl).openConnection();
    // Socket
    connection.setReadTimeout(TIMEOUT_SECONDS * 1000);
    try {
        connection.connect();

        // ?
        InputStream input;
        try {
            input = connection.getInputStream();
        } catch (FileNotFoundException e) {
            response.sendError(HttpServletResponse.SC_NOT_FOUND, contentUrl + " is not found.");
            return;
        }

        // Header
        response.setContentType(connection.getContentType());
        if (connection.getContentLength() > 0) {
            response.setContentLength(connection.getContentLength());
        }

        // 
        OutputStream output = response.getOutputStream();
        try {
            // byte?InputStreamOutputStream, ?4k.
            IOUtils.copy(input, output);
            output.flush();
        } finally {
            // ??InputStream.
            IOUtils.closeQuietly(input);
        }
    } finally {
        connection.disconnect();
    }
}

From source file:org.m2x.rssreader.service.FetcherService.java

private int refreshFeed(String feedId) {
    RssAtomParser handler = null;/*from w  ww . j a v  a  2s  .  c  om*/

    ContentResolver cr = getContentResolver();
    Cursor cursor = cr.query(FeedColumns.CONTENT_URI(feedId), null, null, null, null);

    if (!cursor.moveToFirst()) {
        cursor.close();
        return 0;
    }

    int urlPosition = cursor.getColumnIndex(FeedColumns.URL);
    int titlePosition = cursor.getColumnIndex(FeedColumns.NAME);
    int fetchmodePosition = cursor.getColumnIndex(FeedColumns.FETCH_MODE);
    int realLastUpdatePosition = cursor.getColumnIndex(FeedColumns.REAL_LAST_UPDATE);
    int iconPosition = cursor.getColumnIndex(FeedColumns.ICON);
    int retrieveFullTextPosition = cursor.getColumnIndex(FeedColumns.RETRIEVE_FULLTEXT);

    HttpURLConnection connection = null;
    try {
        String feedUrl = cursor.getString(urlPosition);
        connection = NetworkUtils.setupConnection(feedUrl);
        String contentType = connection.getContentType();
        int fetchMode = cursor.getInt(fetchmodePosition);

        handler = new RssAtomParser(new Date(cursor.getLong(realLastUpdatePosition)), feedId,
                cursor.getString(titlePosition), feedUrl, cursor.getInt(retrieveFullTextPosition) == 1);
        handler.setFetchImages(PrefUtils.getBoolean(PrefUtils.FETCH_PICTURES, true));

        if (fetchMode == 0) {
            if (contentType != null && contentType.startsWith(CONTENT_TYPE_TEXT_HTML)) {
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(NetworkUtils.getConnectionInputStream(connection)));

                String line;
                int posStart = -1;

                while ((line = reader.readLine()) != null) {
                    if (line.contains(HTML_BODY)) {
                        break;
                    } else {
                        Matcher matcher = FEED_LINK_PATTERN.matcher(line);

                        if (matcher.find()) { // not "while" as only one link is needed
                            line = matcher.group();
                            posStart = line.indexOf(HREF);

                            if (posStart > -1) {
                                String url = line.substring(posStart + 6, line.indexOf('"', posStart + 10))
                                        .replace("&amp", "&");

                                ContentValues values = new ContentValues();

                                if (url.startsWith("/")) {
                                    int index = feedUrl.indexOf('/', 8);

                                    if (index > -1) {
                                        url = feedUrl.substring(0, index) + url;
                                    } else {
                                        url = feedUrl + url;
                                    }
                                } else if (!url.startsWith(Constants.HTTP_SCHEME)
                                        && !url.startsWith(Constants.HTTPS_SCHEME)) {
                                    url = feedUrl + '/' + url;
                                }
                                values.put(FeedColumns.URL, url);
                                cr.update(FeedColumns.CONTENT_URI(feedId), values, null, null);
                                connection.disconnect();
                                connection = NetworkUtils.setupConnection(url);
                                contentType = connection.getContentType();
                                break;
                            }
                        }
                    }
                }
                // this indicates a badly configured feed
                if (posStart == -1) {
                    connection.disconnect();
                    connection = NetworkUtils.setupConnection(feedUrl);
                    contentType = connection.getContentType();
                }
            }

            if (contentType != null) {
                int index = contentType.indexOf(CHARSET);

                if (index > -1) {
                    int index2 = contentType.indexOf(';', index);

                    try {
                        Xml.findEncodingByName(index2 > -1 ? contentType.substring(index + 8, index2)
                                : contentType.substring(index + 8));
                        fetchMode = FETCHMODE_DIRECT;
                    } catch (UnsupportedEncodingException usee) {
                        fetchMode = FETCHMODE_REENCODE;
                    }
                } else {
                    fetchMode = FETCHMODE_REENCODE;
                }

            } else {
                BufferedReader bufferedReader = new BufferedReader(
                        new InputStreamReader(NetworkUtils.getConnectionInputStream(connection)));

                char[] chars = new char[20];

                int length = bufferedReader.read(chars);

                String xmlDescription = new String(chars, 0, length);

                connection.disconnect();
                connection = NetworkUtils.setupConnection(connection.getURL());

                int start = xmlDescription != null ? xmlDescription.indexOf(ENCODING) : -1;

                if (start > -1) {
                    try {
                        Xml.findEncodingByName(
                                xmlDescription.substring(start + 10, xmlDescription.indexOf('"', start + 11)));
                        fetchMode = FETCHMODE_DIRECT;
                    } catch (UnsupportedEncodingException usee) {
                        fetchMode = FETCHMODE_REENCODE;
                    }
                } else {
                    // absolutely no encoding information found
                    fetchMode = FETCHMODE_DIRECT;
                }
            }

            ContentValues values = new ContentValues();
            values.put(FeedColumns.FETCH_MODE, fetchMode);
            cr.update(FeedColumns.CONTENT_URI(feedId), values, null, null);
        }

        switch (fetchMode) {
        default:
        case FETCHMODE_DIRECT: {
            if (contentType != null) {
                int index = contentType.indexOf(CHARSET);

                int index2 = contentType.indexOf(';', index);

                InputStream inputStream = NetworkUtils.getConnectionInputStream(connection);
                Xml.parse(inputStream,
                        Xml.findEncodingByName(index2 > -1 ? contentType.substring(index + 8, index2)
                                : contentType.substring(index + 8)),
                        handler);
            } else {
                InputStreamReader reader = new InputStreamReader(
                        NetworkUtils.getConnectionInputStream(connection));
                Xml.parse(reader, handler);
            }
            break;
        }
        case FETCHMODE_REENCODE: {
            ByteArrayOutputStream ouputStream = new ByteArrayOutputStream();
            InputStream inputStream = NetworkUtils.getConnectionInputStream(connection);

            byte[] byteBuffer = new byte[4096];

            int n;
            while ((n = inputStream.read(byteBuffer)) > 0) {
                ouputStream.write(byteBuffer, 0, n);
            }

            String xmlText = ouputStream.toString();

            int start = xmlText != null ? xmlText.indexOf(ENCODING) : -1;

            if (start > -1) {
                Xml.parse(new StringReader(new String(ouputStream.toByteArray(),
                        xmlText.substring(start + 10, xmlText.indexOf('"', start + 11)))), handler);
            } else {
                // use content type
                if (contentType != null) {
                    int index = contentType.indexOf(CHARSET);

                    if (index > -1) {
                        int index2 = contentType.indexOf(';', index);

                        try {
                            StringReader reader = new StringReader(new String(ouputStream.toByteArray(),
                                    index2 > -1 ? contentType.substring(index + 8, index2)
                                            : contentType.substring(index + 8)));
                            Xml.parse(reader, handler);
                        } catch (Exception ignored) {
                        }
                    } else {
                        StringReader reader = new StringReader(new String(ouputStream.toByteArray()));
                        Xml.parse(reader, handler);
                    }
                }
            }
            break;
        }
        }

        connection.disconnect();
    } catch (FileNotFoundException e) {
        if (handler == null || (handler != null && !handler.isDone() && !handler.isCancelled())) {
            ContentValues values = new ContentValues();

            // resets the fetchmode to determine it again later
            values.put(FeedColumns.FETCH_MODE, 0);

            values.put(FeedColumns.ERROR, getString(R.string.error_feed_error));
            cr.update(FeedColumns.CONTENT_URI(feedId), values, null, null);
        }
    } catch (Throwable e) {
        if (handler == null || (handler != null && !handler.isDone() && !handler.isCancelled())) {
            ContentValues values = new ContentValues();

            // resets the fetchmode to determine it again later
            values.put(FeedColumns.FETCH_MODE, 0);

            values.put(FeedColumns.ERROR,
                    e.getMessage() != null ? e.getMessage() : getString(R.string.error_feed_process));
            cr.update(FeedColumns.CONTENT_URI(feedId), values, null, null);
        }
    } finally {

        /* check and optionally find favicon */
        try {
            if (handler != null && cursor.getBlob(iconPosition) == null) {
                String feedLink = handler.getFeedLink();
                if (feedLink != null) {
                    NetworkUtils.retrieveFavicon(this, new URL(feedLink), feedId);
                } else {
                    NetworkUtils.retrieveFavicon(this, connection.getURL(), feedId);
                }
            }
        } catch (Throwable ignored) {
        }

        if (connection != null) {
            connection.disconnect();
        }
    }

    cursor.close();

    return handler != null ? handler.getNewCount() : 0;
}