URLFetcher.java revision 871fe6ed66e9de1369fbc7e4a145f98272b88c0b
1/* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.statementservice.retriever; 18 19import android.util.Log; 20 21import com.android.volley.Cache; 22import com.android.volley.NetworkResponse; 23import com.android.volley.toolbox.HttpHeaderParser; 24 25import java.io.BufferedInputStream; 26import java.io.ByteArrayOutputStream; 27import java.io.IOException; 28import java.io.InputStream; 29import java.net.HttpURLConnection; 30import java.net.URL; 31import java.util.HashMap; 32import java.util.List; 33import java.util.Locale; 34import java.util.Map; 35 36/** 37 * Helper class for fetching HTTP or HTTPS URL. 38 * 39 * Visible for testing. 40 * 41 * @hide 42 */ 43public class URLFetcher { 44 private static final String TAG = URLFetcher.class.getSimpleName(); 45 46 private static final long DO_NOT_CACHE_RESULT = 0L; 47 private static final int INPUT_BUFFER_SIZE_IN_BYTES = 1024; 48 49 /** 50 * Fetches the specified url and returns the content and ttl. 51 * 52 * @throws IOException if it can't retrieve the content due to a network problem. 53 * @throws AssociationServiceException if the URL scheme is not http or https or the content 54 * length exceeds {code fileSizeLimit}. 55 */ 56 public WebContent getWebContentFromUrl(URL url, long fileSizeLimit, int connectionTimeoutMillis) 57 throws AssociationServiceException, IOException { 58 final String scheme = url.getProtocol().toLowerCase(Locale.US); 59 if (!scheme.equals("http") && !scheme.equals("https")) { 60 throw new IllegalArgumentException("The url protocol should be on http or https."); 61 } 62 63 HttpURLConnection connection = null; 64 try { 65 connection = (HttpURLConnection) url.openConnection(); 66 connection.setInstanceFollowRedirects(true); 67 connection.setConnectTimeout(connectionTimeoutMillis); 68 connection.setReadTimeout(connectionTimeoutMillis); 69 connection.setUseCaches(true); 70 connection.setInstanceFollowRedirects(false); 71 connection.addRequestProperty("Cache-Control", "max-stale=60"); 72 73 if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) { 74 Log.e(TAG, "The responses code is not 200 but " + connection.getResponseCode()); 75 return new WebContent("", DO_NOT_CACHE_RESULT); 76 } 77 78 if (connection.getContentLength() > fileSizeLimit) { 79 Log.e(TAG, "The content size of the url is larger than " + fileSizeLimit); 80 return new WebContent("", DO_NOT_CACHE_RESULT); 81 } 82 83 Long expireTimeMillis = getExpirationTimeMillisFromHTTPHeader( 84 connection.getHeaderFields()); 85 86 return new WebContent(inputStreamToString( 87 connection.getInputStream(), connection.getContentLength(), fileSizeLimit), 88 expireTimeMillis); 89 } finally { 90 if (connection != null) { 91 connection.disconnect(); 92 } 93 } 94 } 95 96 /** 97 * Visible for testing. 98 * @hide 99 */ 100 public static String inputStreamToString(InputStream inputStream, int length, long sizeLimit) 101 throws IOException, AssociationServiceException { 102 if (length < 0) { 103 length = 0; 104 } 105 ByteArrayOutputStream baos = new ByteArrayOutputStream(length); 106 BufferedInputStream bis = new BufferedInputStream(inputStream); 107 byte[] buffer = new byte[INPUT_BUFFER_SIZE_IN_BYTES]; 108 int len = 0; 109 while ((len = bis.read(buffer)) != -1) { 110 baos.write(buffer, 0, len); 111 if (baos.size() > sizeLimit) { 112 throw new AssociationServiceException("The content size of the url is larger than " 113 + sizeLimit); 114 } 115 } 116 return baos.toString("UTF-8"); 117 } 118 119 /** 120 * Parses the HTTP headers to compute the ttl. 121 * 122 * @param headers a map that map the header key to the header values. Can be null. 123 * @return the ttl in millisecond or null if the ttl is not specified in the header. 124 */ 125 private Long getExpirationTimeMillisFromHTTPHeader(Map<String, List<String>> headers) { 126 if (headers == null) { 127 return null; 128 } 129 Map<String, String> joinedHeaders = joinHttpHeaders(headers); 130 131 NetworkResponse response = new NetworkResponse(null, joinedHeaders); 132 Cache.Entry cachePolicy = HttpHeaderParser.parseCacheHeaders(response); 133 134 if (cachePolicy == null) { 135 // Cache is disabled, set the expire time to 0. 136 return DO_NOT_CACHE_RESULT; 137 } else if (cachePolicy.ttl == 0) { 138 // Cache policy is not specified, set the expire time to 0. 139 return DO_NOT_CACHE_RESULT; 140 } else { 141 // cachePolicy.ttl is actually the expire timestamp in millisecond. 142 return cachePolicy.ttl; 143 } 144 } 145 146 /** 147 * Converts an HTTP header map of the format provided by {@linkHttpUrlConnection} to a map of 148 * the format accepted by {@link HttpHeaderParser}. It does this by joining all the entries for 149 * a given header key with ", ". 150 */ 151 private Map<String, String> joinHttpHeaders(Map<String, List<String>> headers) { 152 Map<String, String> joinedHeaders = new HashMap<String, String>(); 153 for (Map.Entry<String, List<String>> entry : headers.entrySet()) { 154 List<String> values = entry.getValue(); 155 if (values.size() == 1) { 156 joinedHeaders.put(entry.getKey(), values.get(0)); 157 } else { 158 joinedHeaders.put(entry.getKey(), Utils.joinStrings(", ", values)); 159 } 160 } 161 return joinedHeaders; 162 } 163} 164