1 /* 2 * Copyright 2022 Google LLC 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 package com.google.android.libraries.mobiledatadownload.file.openers; 17 18 import android.net.Uri; 19 import com.google.android.libraries.mobiledatadownload.file.OpenContext; 20 import com.google.android.libraries.mobiledatadownload.file.Opener; 21 import com.google.android.libraries.mobiledatadownload.file.behaviors.UriComputingBehavior; 22 import com.google.android.libraries.mobiledatadownload.file.transforms.IntegrityTransform; 23 import com.google.android.libraries.mobiledatadownload.file.transforms.TransformProtoFragments; 24 import com.google.common.io.ByteStreams; 25 import com.google.mobiledatadownload.TransformProto; 26 import java.io.IOException; 27 import java.io.InputStream; 28 import java.util.concurrent.ExecutionException; 29 30 /** An opener that produces a URI augumented with the IntegrityUriComputing of the input URI. */ 31 public final class IntegrityUriComputingOpener implements Opener<Uri> { 32 33 private static final TransformProto.Transform DEFAULT_SPEC = 34 TransformProto.Transform.newBuilder() 35 .setIntegrity(TransformProto.IntegrityTransform.getDefaultInstance()) 36 .build(); 37 IntegrityUriComputingOpener()38 private IntegrityUriComputingOpener() {} 39 create()40 public static IntegrityUriComputingOpener create() { 41 return new IntegrityUriComputingOpener(); 42 } 43 44 @Override open(OpenContext openContext)45 public Uri open(OpenContext openContext) throws IOException { 46 Uri uri = openContext.originalUri(); 47 48 uri = TransformProtoFragments.addOrReplaceTransform(uri, DEFAULT_SPEC); 49 UriComputingBehavior uriComputer = new UriComputingBehavior(uri); 50 try (InputStream stream = 51 openContext.storage().open(uri, ReadStreamOpener.create().withBehaviors(uriComputer))) { 52 ByteStreams.exhaust(stream); 53 Uri uriWithDigest; 54 try { 55 uriWithDigest = uriComputer.uriFuture().get(); 56 } catch (InterruptedException e) { 57 Thread.currentThread().interrupt(); // per <internal> 58 throw new IOException(e); 59 } catch (ExecutionException e) { 60 if (e.getCause() instanceof IOException) { 61 throw (IOException) e.getCause(); 62 } 63 throw new IOException(e); 64 } 65 String base64Digest = IntegrityTransform.getDigestIfPresent(uriWithDigest); 66 TransformProto.Transform newSpec = 67 TransformProto.Transform.newBuilder() 68 .setIntegrity(TransformProto.IntegrityTransform.newBuilder().setSha256(base64Digest)) 69 .build(); 70 return TransformProtoFragments.addOrReplaceTransform(uriWithDigest, newSpec); 71 } 72 } 73 } 74