1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package io.wcm.tooling.commons.packmgr.download;
21
22 import java.io.Closeable;
23 import java.io.File;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.OutputStream;
27 import java.nio.file.Files;
28
29 import org.apache.commons.io.IOUtils;
30 import org.apache.http.HttpStatus;
31 import org.apache.http.client.methods.CloseableHttpResponse;
32 import org.apache.http.client.methods.HttpGet;
33 import org.apache.http.client.methods.HttpPost;
34 import org.apache.http.client.protocol.HttpClientContext;
35 import org.apache.http.impl.client.CloseableHttpClient;
36 import org.apache.http.util.EntityUtils;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 import io.wcm.tooling.commons.packmgr.PackageManagerException;
41 import io.wcm.tooling.commons.packmgr.PackageManagerHelper;
42 import io.wcm.tooling.commons.packmgr.PackageManagerProperties;
43 import io.wcm.tooling.commons.packmgr.install.VendorInstallerFactory;
44
45
46
47
48 public final class PackageDownloader implements Closeable {
49
50 private final PackageManagerProperties props;
51 private final PackageManagerHelper pkgmgr;
52 private final CloseableHttpClient httpClient;
53
54 private static final Logger log = LoggerFactory.getLogger(PackageDownloader.class);
55
56
57
58
59 public PackageDownloader(PackageManagerProperties props) {
60 this.props = props;
61 this.pkgmgr = new PackageManagerHelper(props);
62 this.httpClient = pkgmgr.getHttpClient();
63 }
64
65
66
67
68
69
70 public String uploadPackageDefinition(File file) {
71
72 if (!file.exists()) {
73 throw new PackageManagerException("File not found: " + file.getAbsolutePath());
74 }
75 String packageManagerUrl = props.getPackageManagerUrl();
76
77
78 log.info("Upload package definition for {} to {} ...", file.getName(), packageManagerUrl);
79 VendorPackageDownloader downloader = VendorInstallerFactory.getPackageDownloader(packageManagerUrl);
80 return downloader.uploadPackageDefinition(packageManagerUrl, file, pkgmgr);
81 }
82
83
84
85
86
87
88
89
90
91 public File downloadContentPackage(String packagePath, String ouputFilePath, boolean rebuildPackage) {
92 try {
93 HttpClientContext httpClientContext = pkgmgr.getPackageManagerHttpClientContext();
94 String packageManagerUrl = props.getPackageManagerUrl();
95 VendorPackageDownloader downloader = VendorInstallerFactory.getPackageDownloader(packageManagerUrl);
96
97
98 if (rebuildPackage) {
99 log.info("Rebuilding package {} ...", packagePath);
100 HttpPost buildMethod = downloader.createRebuildMethod(packagePath, packageManagerUrl);
101 pkgmgr.executePackageManagerMethodHtmlOutputResponse(httpClient, httpClientContext, buildMethod);
102 }
103
104
105 String baseUrl = downloader.createDownloadZipBaseUrl(packageManagerUrl);
106 log.info("Downloading package {} from {} ...", packagePath, baseUrl);
107
108 HttpGet downloadMethod = new HttpGet(baseUrl + packagePath);
109
110
111 CloseableHttpResponse response = httpClient.execute(downloadMethod, httpClientContext);
112 try (response) {
113 if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
114
115
116 InputStream responseStream = response.getEntity().getContent();
117
118
119 File outputFileObject = new File(ouputFilePath);
120 Files.deleteIfExists(outputFileObject.toPath());
121
122
123 try (OutputStream fos = Files.newOutputStream(outputFileObject.toPath())) {
124 IOUtils.copy(responseStream, fos);
125 fos.flush();
126 responseStream.close();
127 }
128
129 log.info("Package downloaded to {}", outputFileObject.getAbsolutePath());
130
131 return outputFileObject;
132 }
133 else {
134 throw new PackageManagerException("Package download failed:\n"
135 + EntityUtils.toString(response.getEntity()));
136 }
137 }
138 finally {
139 EntityUtils.consumeQuietly(response.getEntity());
140 }
141 }
142 catch (IOException ex) {
143 throw new PackageManagerException("Download operation failed.", ex);
144 }
145 }
146
147 @Override
148 public void close() throws IOException {
149 httpClient.close();
150 }
151
152 }