svn-gvsig-desktop / tags / v2_0_0_Build_2021 / extensions / org.gvsig.installer / org.gvsig.installer.lib / org.gvsig.installer.lib.impl / src / main / java / org / gvsig / installer / lib / impl / DefaultInstallerProviderServices.java @ 34107
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.tools.service.spi.AbstractProviderServices; |
57 |
|
58 |
/**
|
59 |
* @author <a href="mailto:jpiera@gvsig.org">Jorge Piera Llodrá</a>
|
60 |
*/
|
61 |
public class DefaultInstallerProviderServices extends AbstractProviderServices |
62 |
implements InstallPackageProviderServices {
|
63 |
|
64 |
private static final Logger LOG = LoggerFactory |
65 |
.getLogger(DefaultInstallerProviderServices.class); |
66 |
|
67 |
private static final String INSTALL_INFO_FILE = "package.info"; |
68 |
|
69 |
public void decompress(InputStream is, File outputDirectory) |
70 |
throws InstallPackageServiceException {
|
71 |
Decompress decompress = new Decompress();
|
72 |
decompress.decompressPlugin(is, outputDirectory); |
73 |
} |
74 |
|
75 |
public PackageInfo readCompressedPackageInfo(InputStream is) |
76 |
throws InstallPackageServiceException {
|
77 |
Decompress decompress = new Decompress();
|
78 |
try {
|
79 |
return decompress.readInstallerInfo(is);
|
80 |
} catch (InstallerInfoFileException e) {
|
81 |
throw new InstallPackageServiceException( |
82 |
"Exception reading a compressed file", e);
|
83 |
} |
84 |
} |
85 |
|
86 |
public void compressPackageSet(File folder, String fileName, OutputStream os) |
87 |
throws MakePluginPackageServiceException {
|
88 |
Compress compress = new Compress();
|
89 |
compress.compressPluginAsPackageSet(folder, fileName, os); |
90 |
} |
91 |
|
92 |
public void compressPackage(File folder, OutputStream os) |
93 |
throws MakePluginPackageServiceException {
|
94 |
Compress compress = new Compress();
|
95 |
compress.compressPluginAsPackage(folder, os); |
96 |
} |
97 |
|
98 |
public void readPackageInfo(File directory, PackageInfo installerInfo) |
99 |
throws InstallerInfoFileException {
|
100 |
File installInfoFile =
|
101 |
new File(directory + File.separator + INSTALL_INFO_FILE); |
102 |
if (installInfoFile.exists()) {
|
103 |
InstallerInfoFileReader reader = new InstallerInfoFileReader();
|
104 |
reader.read(installerInfo, installInfoFile.getAbsolutePath()); |
105 |
} else {
|
106 |
installerInfo.setCode(directory.getName()); |
107 |
installerInfo.setName(directory.getName()); |
108 |
} |
109 |
} |
110 |
|
111 |
public void writePackageInfo(File directory, PackageInfo installerInfo) |
112 |
throws InstallerInfoFileException {
|
113 |
if (!directory.exists()) {
|
114 |
throw new InstallerInfoFileException("The directory doesn't exist"); |
115 |
} |
116 |
InstallerInfoFileWriter installerInfoFileWriter = |
117 |
new InstallerInfoFileWriter();
|
118 |
installerInfoFileWriter.write(installerInfo, directory + File.separator
|
119 |
+ INSTALL_INFO_FILE); |
120 |
} |
121 |
|
122 |
public InputStream searchPackage(InputStream is, String zipEntry) |
123 |
throws InstallPackageServiceException {
|
124 |
Decompress decompress = new Decompress();
|
125 |
return decompress.searchPlugin(is, zipEntry);
|
126 |
} |
127 |
|
128 |
public void readPackageSetInfo(InputStream is, |
129 |
List<PackageInfo> installerInfos, Map<PackageInfo, String> zipEntriesMap) |
130 |
throws InstallPackageServiceException {
|
131 |
Decompress decompress = new Decompress();
|
132 |
decompress |
133 |
.readPackageSetInstallInfos(is, installerInfos, zipEntriesMap); |
134 |
} |
135 |
|
136 |
public void readPackageInfo(InputStream is, |
137 |
List<PackageInfo> installerInfos,
|
138 |
Map<PackageInfo, String> zipEntriesMap, String name) |
139 |
throws InstallPackageServiceException {
|
140 |
|
141 |
Decompress decompress = new Decompress();
|
142 |
decompress.readPackageInstallInfo(is, installerInfos, zipEntriesMap, |
143 |
name); |
144 |
} |
145 |
|
146 |
public File downloadFile(URL bundleURL, String defaultFileName) |
147 |
throws IOException { |
148 |
|
149 |
URL downloadURL = bundleURL;
|
150 |
|
151 |
// check if the URL ends with '/' and append the file name
|
152 |
if (defaultFileName != null) { |
153 |
String urlStr = bundleURL.toString();
|
154 |
if (urlStr.endsWith("/")) { |
155 |
urlStr = urlStr.concat(defaultFileName); |
156 |
downloadURL = new URL(urlStr); |
157 |
} |
158 |
} |
159 |
|
160 |
URLConnection connection = downloadURL.openConnection();
|
161 |
String fileName = getFileName(connection);
|
162 |
|
163 |
if (LOG.isDebugEnabled()) {
|
164 |
Date date = new Date(connection.getLastModified()); |
165 |
LOG.debug( |
166 |
"Downloading file {} from URL {}, with last modified date: {}",
|
167 |
new Object[] { fileName, downloadURL, date }); |
168 |
} |
169 |
|
170 |
String fileNamePrefix = fileName;
|
171 |
String fileNameSuffix = "zip"; |
172 |
int dotPosition = fileName.lastIndexOf('.'); |
173 |
if (dotPosition > -1 && dotPosition < fileName.length() - 1) { |
174 |
fileNamePrefix = fileName.substring(0, dotPosition);
|
175 |
fileNameSuffix = fileName.substring(dotPosition); |
176 |
} |
177 |
|
178 |
BufferedInputStream bis =
|
179 |
new BufferedInputStream(downloadURL.openStream()); |
180 |
|
181 |
File localFile = File.createTempFile(fileNamePrefix, fileNameSuffix); |
182 |
BufferedOutputStream bos =
|
183 |
new BufferedOutputStream(new FileOutputStream(localFile)); |
184 |
|
185 |
byte[] data = new byte[1024]; |
186 |
int count = 0; |
187 |
while ((count = bis.read(data, 0, 1024)) >= 0) { |
188 |
bos.write(data, 0, count);
|
189 |
} |
190 |
|
191 |
bis.close(); |
192 |
bos.flush(); |
193 |
bos.close(); |
194 |
|
195 |
return localFile;
|
196 |
} |
197 |
|
198 |
/**
|
199 |
* Returns the file name associated to an url connection.<br />
|
200 |
* The result is not a path but just a file name.
|
201 |
*
|
202 |
* @param urlConnection
|
203 |
* - the url connection
|
204 |
* @return the file name
|
205 |
*
|
206 |
* @throws IOException
|
207 |
* Signals that an I/O exception has occurred.
|
208 |
*/
|
209 |
private String getFileName(URLConnection urlConnection) throws IOException { |
210 |
String fileName = null; |
211 |
|
212 |
String contentDisposition =
|
213 |
urlConnection.getHeaderField("content-disposition");
|
214 |
|
215 |
if (contentDisposition != null) { |
216 |
fileName = |
217 |
extractFileNameFromContentDisposition(contentDisposition); |
218 |
} |
219 |
|
220 |
// if the file name cannot be extracted from the content-disposition
|
221 |
// header, using the url.getFilename() method
|
222 |
if (fileName == null) { |
223 |
StringTokenizer st =
|
224 |
new StringTokenizer(urlConnection.getURL().getFile(), "/"); |
225 |
while (st.hasMoreTokens())
|
226 |
fileName = st.nextToken(); |
227 |
} |
228 |
|
229 |
return fileName;
|
230 |
} |
231 |
|
232 |
/**
|
233 |
* Extract the file name from the content disposition header.
|
234 |
* <p>
|
235 |
* See <a
|
236 |
* href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html">http:
|
237 |
* //www.w3.org/Protocols/rfc2616/rfc2616-sec19.html</a> for detailled
|
238 |
* information regarding the headers in HTML.
|
239 |
*
|
240 |
* @param contentDisposition
|
241 |
* - the content-disposition header. Cannot be <code>null>/code>.
|
242 |
* @return the file name, or <code>null</code> if the content-disposition
|
243 |
* header does not contain the filename attribute.
|
244 |
*/
|
245 |
private String extractFileNameFromContentDisposition( |
246 |
String contentDisposition) {
|
247 |
String[] attributes = contentDisposition.split(";"); |
248 |
|
249 |
for (String a : attributes) { |
250 |
if (a.toLowerCase().contains("filename")) { |
251 |
// The attribute is the file name. The filename is between
|
252 |
// quotes.
|
253 |
return a.substring(a.indexOf('\"') + 1, a.lastIndexOf('\"')); |
254 |
} |
255 |
} |
256 |
|
257 |
// not found
|
258 |
return null; |
259 |
} |
260 |
|
261 |
} |