1 /*
2  * Copyright (C) 2019 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 package com.android.tradefed.config.remote;
17 
18 import com.android.annotations.VisibleForTesting;
19 import com.android.tradefed.build.BuildRetrievalError;
20 import com.android.tradefed.config.DynamicRemoteFileResolver;
21 import com.android.tradefed.util.FileUtil;
22 import com.android.tradefed.util.MultiMap;
23 import com.android.tradefed.util.net.HttpHelper;
24 import com.android.tradefed.util.net.IHttpHelper;
25 
26 import java.io.File;
27 import java.io.FileOutputStream;
28 import java.io.IOException;
29 
30 import javax.annotation.Nonnull;
31 
32 /** Implementation of {@link IRemoteFileResolver} that allows downloading remote file via http */
33 public class HttpRemoteFileResolver implements IRemoteFileResolver {
34 
35     public static final String PROTOCOL_HTTP = "http";
36 
37     @Override
resolveRemoteFile(RemoteFileResolverArgs args)38     public ResolvedFile resolveRemoteFile(RemoteFileResolverArgs args) throws BuildRetrievalError {
39         File consideredFile = args.getConsideredFile();
40         // Don't use absolute path as it would not start with gs:
41         String path = consideredFile.getPath();
42         // Replace the very first / by // to be http:// again.
43         path = path.replaceFirst(":/", "://");
44 
45         IHttpHelper downloader = getDownloader();
46         File downloadedFile = null;
47         try {
48             downloadedFile =
49                     FileUtil.createTempFile(
50                             FileUtil.getBaseName(consideredFile.getName()),
51                             FileUtil.getExtension(consideredFile.getName()));
52             downloader.doGet(downloader.buildUrl(path, new MultiMap<>(args.getQueryArgs())),
53                     new FileOutputStream(downloadedFile));
54             return new ResolvedFile(
55                     DynamicRemoteFileResolver.unzipIfRequired(downloadedFile, args.getQueryArgs()));
56         } catch (IOException | RuntimeException e) {
57             FileUtil.deleteFile(downloadedFile);
58             throw new BuildRetrievalError(
59                     String.format("Failed to download %s due to: %s", path, e.getMessage()), e);
60         }
61     }
62 
63     @Override
getSupportedProtocol()64     public @Nonnull String getSupportedProtocol() {
65         return PROTOCOL_HTTP;
66     }
67 
68     @VisibleForTesting
getDownloader()69     protected IHttpHelper getDownloader() {
70         return new HttpHelper();
71     }
72 }
73