Package org.b3log.latke.urlfetch

Examples of org.b3log.latke.urlfetch.HTTPResponse


            httpRequest.setRequestMethod(HTTPRequestMethod.POST);
            httpRequest.setPayload(broadcastRequest.toString().getBytes("UTF-8"));

            @SuppressWarnings("unchecked")
            final Future<HTTPResponse> future = (Future<HTTPResponse>) urlFetchService.fetchAsync(httpRequest);
            final HTTPResponse result = future.get();

            if (HttpServletResponse.SC_OK == result.getResponseCode()) {
                ret.put(Keys.STATUS_CODE, true);

                optionMgmtService.removeOption(Option.ID_C_BROADCAST_CHANCE_EXPIRATION_TIME);

                LOGGER.info("Submits broadcast successfully");
View Full Code Here


                new URL("http://www.gravatar.com/avatar/" + hashedEmail + "?s="
                        + size + "&r=G");
        try {
            final HTTPRequest request = new HTTPRequest();
            request.setURL(gravatarURL);
            final HTTPResponse response = urlFetchService.fetch(request);
            final int statusCode = response.getResponseCode();

            if (HttpServletResponse.SC_OK == statusCode) {
                final List<HTTPHeader> headers = response.getHeaders();
                boolean defaultFileLengthMatched = false;
                for (final HTTPHeader httpHeader : headers) {
                    if ("Content-Length".equalsIgnoreCase(httpHeader.getName())) {
                        if (httpHeader.getValue().equals("2147")) {
                            defaultFileLengthMatched = true;
View Full Code Here

        final HTTPRequest request = new HTTPRequest();
        request.setURL(new URL(url));

        try {
            final HTTPResponse response = urlFetchService.fetch(request);
            if (HttpServletResponse.SC_OK != response.getResponseCode()) {
                System.err.println("HttpGet Method failed: "
                                   + response.getResponseCode());
            }
            ret = new String(response.getContent(), "UTF-8");

        } catch (Exception e) {
            throw new Exception(e);
        }
View Full Code Here

        if (queryString != null && !queryString.equals("")) {
            request.setPayload(queryString.getBytes("UTF-8"));
        }

        try {
            final HTTPResponse response = urlFetchService.fetch(request);
            if (HttpServletResponse.SC_OK != response.getResponseCode()) {
                System.err.println("HttpPost Method failed: "
                                   + response.getResponseCode());
            }
            ret = new String(response.getContent(), "UTF-8");
        } catch (Exception e) {
            throw new Exception(e);
        }

        return ret;
View Full Code Here

                    new URL(Google.GOOGLE_PROFILE_RETRIEVAL.replace("{userId}",
                                                                    id));
            try {
                final HTTPRequest request = new HTTPRequest();
                request.setURL(googleProfileURL);
                final HTTPResponse response = urlFetchService.fetch(request);
                final int statusCode = response.getResponseCode();

                if (HttpServletResponse.SC_OK == statusCode) {
                    final byte[] content = response.getContent();
                    final String profileJSONString =
                            new String(content, "UTF-8");
                    LOGGER.log(Level.FINEST, "Google profile[jsonString={0}]",
                               profileJSONString);
                    final JSONObject profile = new JSONObject(profileJSONString);
                    final JSONObject profileData = profile.getJSONObject("data");
                    thumbnailURL = profileData.getString("thumbnailUrl");
                    comment.put(Comment.COMMENT_THUMBNAIL_URL, thumbnailURL);
                    LOGGER.log(Level.FINEST, "Comment thumbnail[URL={0}]",
                               thumbnailURL);

                    return;
                } else {
                    LOGGER.log(Level.WARNING,
                               "Can not fetch google profile[userId={0}, statusCode={1}]",
                               new Object[]{id, statusCode});
                }
            } catch (final Exception e) {
                LOGGER.log(Level.WARNING,
                           "Can not fetch google profile[userId=" + id + "", e);
            }
        }

        // Try to set thumbnail URL using Gravatar service
        final String hashedEmail = MD5.hash(commentEmail.toLowerCase());
        final int size = 60;
        final URL gravatarURL =
                new URL("http://www.gravatar.com/avatar/" + hashedEmail + "?s="
                        + size + "&r=G");

        try {
            final HTTPRequest request = new HTTPRequest();
            request.setURL(gravatarURL);
            final HTTPResponse response = urlFetchService.fetch(request);
            final int statusCode = response.getResponseCode();

            if (HttpServletResponse.SC_OK == statusCode) {
                final List<HTTPHeader> headers = response.getHeaders();
                boolean defaultFileLengthMatched = false;
                for (final HTTPHeader httpHeader : headers) {
                    if ("Content-Length".equalsIgnoreCase(httpHeader.getName())) {
                        if (httpHeader.getValue().equals("2147")) {
                            defaultFileLengthMatched = true;
View Full Code Here

                    new URL(Google.GOOGLE_PROFILE_RETRIEVAL.replace("{userId}",
                                                                    id));
            try {
                final HTTPRequest request = new HTTPRequest();
                request.setURL(googleProfileURL);
                final HTTPResponse response = urlFetchService.fetch(request);
                final int statusCode = response.getResponseCode();

                if (HttpServletResponse.SC_OK == statusCode) {
                    final byte[] content = response.getContent();
                    final String profileJSONString =
                            new String(content, "UTF-8");
                    LOGGER.log(Level.FINEST, "Google profile[jsonString={0}]",
                               profileJSONString);
                    final JSONObject profile = new JSONObject(profileJSONString);
                    final JSONObject profileData = profile.getJSONObject("data");
                    thumbnailURL = profileData.getString("thumbnailUrl");
                    comment.put(Comment.COMMENT_THUMBNAIL_URL, thumbnailURL);
                    LOGGER.log(Level.FINEST, "Comment thumbnail[URL={0}]",
                               thumbnailURL);

                    return;
                } else {
                    LOGGER.log(Level.WARNING,
                               "Can not fetch google profile[userId={0}, statusCode={1}]",
                               new Object[]{id, statusCode});
                }
            } catch (final Exception e) {
                LOGGER.log(Level.WARNING,
                           "Can not fetch google profile[userId=" + id + "", e);
            }
        }

        // Try to set thumbnail URL using Gravatar service
        final String hashedEmail = MD5.hash(commentEmail.toLowerCase());
        final int size = 60;
        final URL gravatarURL =
                new URL("http://www.gravatar.com/avatar/" + hashedEmail + "?s="
                        + size + "&r=G");
        try {
            final HTTPRequest request = new HTTPRequest();
            request.setURL(gravatarURL);
            final HTTPResponse response = urlFetchService.fetch(request);
            final int statusCode = response.getResponseCode();

            if (HttpServletResponse.SC_OK == statusCode) {
                final List<HTTPHeader> headers = response.getHeaders();
                boolean defaultFileLengthMatched = false;
                for (final HTTPHeader httpHeader : headers) {
                    if ("Content-Length".equalsIgnoreCase(httpHeader.getName())) {
                        if (httpHeader.getValue().equals("2147")) {
                            defaultFileLengthMatched = true;
View Full Code Here

                    new URL(Google.GOOGLE_PROFILE_RETRIEVAL.replace("{userId}",
                                                                    id));
            try {
                final HTTPRequest request = new HTTPRequest();
                request.setURL(googleProfileURL);
                final HTTPResponse response = urlFetchService.fetch(request);
                final int statusCode = response.getResponseCode();

                if (HttpServletResponse.SC_OK == statusCode) {
                    final byte[] content = response.getContent();
                    final String profileJSONString =
                            new String(content, "UTF-8");
                    LOGGER.log(Level.FINEST, "Google profile[jsonString={0}]",
                               profileJSONString);
                    final JSONObject profile = new JSONObject(profileJSONString);
                    final JSONObject profileData = profile.getJSONObject("data");
                    thumbnailURL = profileData.getString("thumbnailUrl");
                    comment.put(Comment.COMMENT_THUMBNAIL_URL, thumbnailURL);
                    LOGGER.log(Level.FINEST, "Comment thumbnail[URL={0}]",
                               thumbnailURL);

                    return;
                } else {
                    LOGGER.log(Level.WARNING,
                               "Can not fetch google profile[userId={0}, statusCode={1}]",
                               new Object[]{id, statusCode});
                }
            } catch (final Exception e) {
                LOGGER.log(Level.WARNING,
                           "Can not fetch google profile[userId=" + id + "", e);
            }
        }

        // Try to set thumbnail URL using Gravatar service
        final String hashedEmail = MD5.hash(commentEmail.toLowerCase());
        final int size = 60;
        final URL gravatarURL =
                new URL("http://www.gravatar.com/avatar/" + hashedEmail + "?s="
                        + size + "&r=G");
        try {
            final HTTPRequest request = new HTTPRequest();
            request.setURL(gravatarURL);
            final HTTPResponse response = urlFetchService.fetch(request);
            final int statusCode = response.getResponseCode();

            if (HttpServletResponse.SC_OK == statusCode) {
                final List<HTTPHeader> headers = response.getHeaders();
                boolean defaultFileLengthMatched = false;
                for (final HTTPHeader httpHeader : headers) {
                    if ("Content-Length".equalsIgnoreCase(httpHeader.getName())) {
                        if (httpHeader.getValue().equals("2147")) {
                            defaultFileLengthMatched = true;
View Full Code Here

     * @param httpRequest {@link HTTPRequest}
     * @return isSuccess
     */
    private boolean doUrlFetch(final HTTPRequest httpRequest) {

        HTTPResponse httpResponse = null;
        try {
            httpResponse = urlFetchService.fetch(httpRequest);
        } catch (final IOException e) {
            LOGGER.log(Level.INFO, "The task[{0}] throw exception {1}", new Object[]{task.getURL(), e.getMessage()});
            return false;
        }

        /**
         * <p> Quote GAE:"
         * If a push task request handler returns an HTTP status code within the range 200–299,
         * App Engine considers the task to have completed successfully.
         * If the task returns a status code outside of this range"
         *</p>
         */
        final Integer beginCode = 200;
        final Integer endCode = 299;

        if (httpResponse.getResponseCode() >= beginCode && httpResponse.getResponseCode() <= endCode) {
            return true;
        }
        LOGGER.log(Level.INFO, "The task[{0}] not success ,the return code is [{1}]",
                   new Object[]{task.getURL(), httpResponse.getResponseCode()});

        return false;
    }
View Full Code Here

    protected HTTPResponse doFetch(final HTTPRequest request) throws IOException {

        final HttpURLConnection httpURLConnection = prepareConnection(request);
        configConnection(httpURLConnection, request);
        httpURLConnection.connect();
        final HTTPResponse ret = resultConnection(httpURLConnection);
        httpURLConnection.disconnect();

        return ret;
    }
View Full Code Here

     * @param httpURLConnection {@link HttpURLConnection}
     * @return HTTPResponse the http response
     * @throws IOException IOException from java.net
     */
    protected HTTPResponse resultConnection(final HttpURLConnection httpURLConnection) throws IOException {
        final HTTPResponse ret = new HTTPResponse();

        ret.setResponseCode(httpURLConnection.getResponseCode());
        ret.setFinalURL(httpURLConnection.getURL());

        InputStream retStream;
        if (HttpServletResponse.SC_OK == ret.getResponseCode()) {
            retStream = httpURLConnection.getInputStream();
        } else {
            retStream = httpURLConnection.getErrorStream();
        }
        ret.setContent(inputStreamToByte(retStream));

        fillHttpResponseHeader(ret, httpURLConnection.getHeaderFields());

        return ret;
    }
View Full Code Here

TOP

Related Classes of org.b3log.latke.urlfetch.HTTPResponse

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.