com.threadswarm.imagefeedarchiver.FeedUtils.java Source code

Java tutorial

Introduction

Here is the source code for com.threadswarm.imagefeedarchiver.FeedUtils.java

Source

/*
 * Copyright 2014 steve(at)threadswarm.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.threadswarm.imagefeedarchiver;

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

import org.apache.http.Header;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;

import com.threadswarm.imagefeedarchiver.model.RssMediaContent;

/**
 * Utility class containing static methods that are generally useful throughout the 
 * application.
 * <p>
 * This class has a private constructor to prevent instantiation.
 * 
 * @author steve(at)threadswarm.com
 *
 */
public class FeedUtils {

    private FeedUtils() {
    } //prevent instantiation

    /**
     * Returns a <code>long</code> with the best expected content-length value or -1 if no value could be calculated. 
     * The value of the 'Content-Length' header, if available from the <code>HttpResponse</code> object is preferred 
     * over the file-size specified by the <code>RssMediaContent</code> object argument.  However, if the former is 
     * unavailable than the latter is used.  If no value can be calculated than -1 is returned to the caller.
     * 
     * @param httpResponse the <code>HttpResponse</code> object from which the 'Content-Length' header will be examined
     * @param mediaContent the <code>RssMediaContent</code> object from which the <code>getFileSize()</code> method will be called
     * @return the best content-length value given the two argument sources
     */
    public static long calculateBestExpectedContentLength(HttpResponse httpResponse, RssMediaContent mediaContent) {
        Long mediaContentFileSize = mediaContent.getFileSize();
        long expectedContentLength = (mediaContentFileSize != null) ? mediaContent.getFileSize() : -1;
        Header contentLengthHeader = httpResponse.getFirstHeader(HttpHeaders.CONTENT_LENGTH);
        if (contentLengthHeader != null) {
            long headerContentLength = Long.valueOf(contentLengthHeader.getValue());
            if (expectedContentLength == -1 || headerContentLength != expectedContentLength) {
                expectedContentLength = headerContentLength;
            }
        }

        return expectedContentLength;
    }

    /**
     * Returns a hierarchical {@code URI} constructed from individual components 
     * of the supplied {@code urlString} argument.
     * <p>
     * The {@code urlString} argument is first used to instantiate a {@code URL} 
     * which in turn is used to construct a {@code URI} based on the individual 
     * components of the former.  This more robust then simply calling {@code URL.toURI()}.
     * 
     * @param urlString the {@code String} based representation of a URL
     * @return a {@code URI} constructed from the individual URL components
     * @throws URISyntaxException if a valid {@code URI} cannot be constructed from the supplied {@code urlString} argument
     * @throws MalformedURLException if the {@code urlString} cannot be used to instantiate a {@code URL}
     */
    public static URI getUriFromUrlString(String urlString) throws URISyntaxException, MalformedURLException {
        URL url = new URL(urlString);
        return new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(),
                url.getQuery(), url.getRef());
    }

    /**
     * Returns a {@code String} where "http://" has been replaced with "https://" (if present).
     * <p>
     * The match on "http://" is case-insensitive but does stipulate that the aforementioned 
     * pattern be located at the beginning of the {@code urlString} argument.
     * 
     * @param urlString a {@code String} based representation of a URL
     * @return a {@code String} where "http://" has been replaced with "https://"
     */
    public static String rewriteUrlStringToHttps(String urlString) {
        return urlString.replaceFirst("(?s)(?i:^http://)", "https://");
    }

}