svn-gvsig-desktop / branches / v2_0_0_prep / extensions / org.gvsig.installer / org.gvsig.installer.lib / org.gvsig.installer.lib.impl / src / main / java / org / gvsig / installer / lib / impl / DefaultInstallerProviderServices.java @ 34444
History | View | Annotate | Download (9.23 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
*/
|
22 |
|
23 |
/*
|
24 |
* AUTHORS (In addition to CIT):
|
25 |
* 2010 {Prodevelop} {Task}
|
26 |
*/
|
27 |
|
28 |
package org.gvsig.installer.lib.impl; |
29 |
|
30 |
import java.io.BufferedInputStream; |
31 |
import java.io.BufferedOutputStream; |
32 |
import java.io.File; |
33 |
import java.io.FileOutputStream; |
34 |
import java.io.IOException; |
35 |
import java.io.InputStream; |
36 |
import java.io.OutputStream; |
37 |
import java.net.URL; |
38 |
import java.net.URLConnection; |
39 |
import java.util.Date; |
40 |
import java.util.List; |
41 |
import java.util.Map; |
42 |
import java.util.StringTokenizer; |
43 |
|
44 |
import org.slf4j.Logger; |
45 |
import org.slf4j.LoggerFactory; |
46 |
|
47 |
import org.gvsig.installer.lib.api.PackageInfo; |
48 |
import org.gvsig.installer.lib.api.creation.MakePluginPackageServiceException; |
49 |
import org.gvsig.installer.lib.api.execution.InstallPackageServiceException; |
50 |
import org.gvsig.installer.lib.impl.info.InstallerInfoFileReader; |
51 |
import org.gvsig.installer.lib.impl.info.InstallerInfoFileWriter; |
52 |
import org.gvsig.installer.lib.impl.utils.Compress; |
53 |
import org.gvsig.installer.lib.impl.utils.Decompress; |
54 |
import org.gvsig.installer.lib.spi.InstallPackageProviderServices; |
55 |
import org.gvsig.installer.lib.spi.InstallerInfoFileException; |
56 |
import org.gvsig.installer.lib.spi.InstallerProviderLocator; |
57 |
import org.gvsig.tools.service.spi.AbstractProviderServices; |
58 |
|
59 |
/**
|
60 |
* @author <a href="mailto:jpiera@gvsig.org">Jorge Piera Llodrá</a>
|
61 |
*/
|
62 |
public class DefaultInstallerProviderServices extends AbstractProviderServices |
63 |
implements InstallPackageProviderServices {
|
64 |
|
65 |
private static final Logger LOG = LoggerFactory |
66 |
.getLogger(DefaultInstallerProviderServices.class); |
67 |
|
68 |
public void decompress(InputStream is, File outputDirectory) |
69 |
throws InstallPackageServiceException {
|
70 |
Decompress decompress = new Decompress();
|
71 |
decompress.decompressPlugin(is, outputDirectory); |
72 |
} |
73 |
|
74 |
public PackageInfo readCompressedPackageInfo(InputStream is) |
75 |
throws InstallPackageServiceException {
|
76 |
Decompress decompress = new Decompress();
|
77 |
try {
|
78 |
return decompress.readInstallerInfo(is);
|
79 |
} catch (InstallerInfoFileException e) {
|
80 |
throw new InstallPackageServiceException( |
81 |
"Exception reading a compressed file", e);
|
82 |
} |
83 |
} |
84 |
|
85 |
public void compressPackageSet(File folder, String fileName, OutputStream os) |
86 |
throws MakePluginPackageServiceException {
|
87 |
Compress compress = new Compress();
|
88 |
compress.compressPluginAsPackageSet(folder, fileName, os); |
89 |
} |
90 |
|
91 |
public void compressPackage(File folder, OutputStream os) |
92 |
throws MakePluginPackageServiceException {
|
93 |
Compress compress = new Compress();
|
94 |
compress.compressPluginAsPackage(folder, os); |
95 |
} |
96 |
|
97 |
public void compressPackageIndex(File folder, OutputStream os) |
98 |
throws MakePluginPackageServiceException {
|
99 |
Compress compress = new Compress();
|
100 |
compress.compressPluginAsPackageIndex(folder, os); |
101 |
} |
102 |
|
103 |
public void readPackageInfo(File directory, PackageInfo installerInfo) |
104 |
throws InstallerInfoFileException {
|
105 |
File installInfoFile = new File(directory + File.separator |
106 |
+ getPackageInfoFileName()); |
107 |
if (installInfoFile.exists()) {
|
108 |
InstallerInfoFileReader reader = new InstallerInfoFileReader();
|
109 |
reader.read(installerInfo, installInfoFile.getAbsolutePath()); |
110 |
} |
111 |
|
112 |
if (installerInfo.getCode() == null) { |
113 |
installerInfo.setCode(directory.getName()); |
114 |
} |
115 |
if (installerInfo.getName() == null) { |
116 |
installerInfo.setName(directory.getName()); |
117 |
} |
118 |
} |
119 |
|
120 |
private String getPackageInfoFileName() { |
121 |
return InstallerProviderLocator.getProviderManager()
|
122 |
.getPackageInfoFileName(); |
123 |
} |
124 |
|
125 |
public void writePackageInfo(File directory, PackageInfo packageInfo) |
126 |
throws InstallerInfoFileException {
|
127 |
writePackageInfoFile(directory, getPackageInfoFileName(), packageInfo); |
128 |
} |
129 |
|
130 |
public void writePackageInfoForIndex(File directory, PackageInfo packageInfo) |
131 |
throws InstallerInfoFileException {
|
132 |
writePackageInfoFile(directory, getPackageInfoFileName() + ".index",
|
133 |
packageInfo); |
134 |
} |
135 |
|
136 |
private void writePackageInfoFile(File directory, String fileName, |
137 |
PackageInfo installerInfo) throws InstallerInfoFileException {
|
138 |
if (!directory.exists()) {
|
139 |
throw new InstallerInfoFileException("The directory doesn't exist"); |
140 |
} |
141 |
InstallerInfoFileWriter installerInfoFileWriter = |
142 |
new InstallerInfoFileWriter();
|
143 |
installerInfoFileWriter.write(installerInfo, directory + File.separator
|
144 |
+ fileName); |
145 |
} |
146 |
|
147 |
public InputStream searchPackage(InputStream is, String zipEntry) |
148 |
throws InstallPackageServiceException {
|
149 |
Decompress decompress = new Decompress();
|
150 |
return decompress.searchPlugin(is, zipEntry);
|
151 |
} |
152 |
|
153 |
public void readPackageSetInfo(InputStream is, |
154 |
List<PackageInfo> installerInfos,
|
155 |
Map<PackageInfo, String> zipEntriesMap) |
156 |
throws InstallPackageServiceException {
|
157 |
Decompress decompress = new Decompress();
|
158 |
decompress |
159 |
.readPackageSetInstallInfos(is, installerInfos, zipEntriesMap); |
160 |
} |
161 |
|
162 |
public void readPackageInfo(InputStream is, |
163 |
List<PackageInfo> installerInfos,
|
164 |
Map<PackageInfo, String> zipEntriesMap, String name) |
165 |
throws InstallPackageServiceException {
|
166 |
|
167 |
Decompress decompress = new Decompress();
|
168 |
decompress.readPackageInstallInfo(is, installerInfos, zipEntriesMap, |
169 |
name); |
170 |
} |
171 |
|
172 |
public File downloadFile(URL bundleURL, String defaultFileName) |
173 |
throws IOException { |
174 |
|
175 |
URL downloadURL = bundleURL;
|
176 |
|
177 |
// check if the URL ends with '/' and append the file name
|
178 |
if (defaultFileName != null) { |
179 |
String urlStr = bundleURL.toString();
|
180 |
if (urlStr.endsWith("/")) { |
181 |
urlStr = urlStr.concat(defaultFileName); |
182 |
downloadURL = new URL(urlStr); |
183 |
} |
184 |
} |
185 |
|
186 |
URLConnection connection = downloadURL.openConnection();
|
187 |
String fileName = getFileName(connection);
|
188 |
|
189 |
if (LOG.isDebugEnabled()) {
|
190 |
Date date = new Date(connection.getLastModified()); |
191 |
LOG.debug( |
192 |
"Downloading file {} from URL {}, with last modified date: {}",
|
193 |
new Object[] { fileName, downloadURL, date }); |
194 |
} |
195 |
|
196 |
String fileNamePrefix = fileName;
|
197 |
String fileNameSuffix = "zip"; |
198 |
int dotPosition = fileName.lastIndexOf('.'); |
199 |
if (dotPosition > -1 && dotPosition < fileName.length() - 1) { |
200 |
fileNamePrefix = fileName.substring(0, dotPosition);
|
201 |
fileNameSuffix = fileName.substring(dotPosition); |
202 |
} |
203 |
|
204 |
BufferedInputStream bis = new BufferedInputStream( |
205 |
downloadURL.openStream()); |
206 |
|
207 |
File localFile = File.createTempFile(fileNamePrefix, fileNameSuffix); |
208 |
BufferedOutputStream bos = new BufferedOutputStream( |
209 |
new FileOutputStream(localFile)); |
210 |
|
211 |
byte[] data = new byte[1024]; |
212 |
int count = 0; |
213 |
while ((count = bis.read(data, 0, 1024)) >= 0) { |
214 |
bos.write(data, 0, count);
|
215 |
} |
216 |
|
217 |
bis.close(); |
218 |
bos.flush(); |
219 |
bos.close(); |
220 |
|
221 |
return localFile;
|
222 |
} |
223 |
|
224 |
/**
|
225 |
* Returns the file name associated to an url connection.<br />
|
226 |
* The result is not a path but just a file name.
|
227 |
*
|
228 |
* @param urlConnection
|
229 |
* - the url connection
|
230 |
* @return the file name
|
231 |
*
|
232 |
* @throws IOException
|
233 |
* Signals that an I/O exception has occurred.
|
234 |
*/
|
235 |
private String getFileName(URLConnection urlConnection) throws IOException { |
236 |
String fileName = null; |
237 |
|
238 |
String contentDisposition = urlConnection
|
239 |
.getHeaderField("content-disposition");
|
240 |
|
241 |
if (contentDisposition != null) { |
242 |
fileName = extractFileNameFromContentDisposition(contentDisposition); |
243 |
} |
244 |
|
245 |
// if the file name cannot be extracted from the content-disposition
|
246 |
// header, using the url.getFilename() method
|
247 |
if (fileName == null) { |
248 |
StringTokenizer st = new StringTokenizer(urlConnection.getURL() |
249 |
.getFile(), "/");
|
250 |
while (st.hasMoreTokens())
|
251 |
fileName = st.nextToken(); |
252 |
} |
253 |
|
254 |
return fileName;
|
255 |
} |
256 |
|
257 |
/**
|
258 |
* Extract the file name from the content disposition header.
|
259 |
* <p>
|
260 |
* See <a
|
261 |
* href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html">http:
|
262 |
* //www.w3.org/Protocols/rfc2616/rfc2616-sec19.html</a> for detailled
|
263 |
* information regarding the headers in HTML.
|
264 |
*
|
265 |
* @param contentDisposition
|
266 |
* - the content-disposition header. Cannot be <code>null>/code>.
|
267 |
* @return the file name, or <code>null</code> if the content-disposition
|
268 |
* header does not contain the filename attribute.
|
269 |
*/
|
270 |
private String extractFileNameFromContentDisposition( |
271 |
String contentDisposition) {
|
272 |
String[] attributes = contentDisposition.split(";"); |
273 |
|
274 |
for (String a : attributes) { |
275 |
if (a.toLowerCase().contains("filename")) { |
276 |
// The attribute is the file name. The filename is between
|
277 |
// quotes.
|
278 |
return a.substring(a.indexOf('\"') + 1, a.lastIndexOf('\"')); |
279 |
} |
280 |
} |
281 |
|
282 |
// not found
|
283 |
return null; |
284 |
} |
285 |
} |