root / 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 @ 34005
History | View | Annotate | Download (8.87 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, |
87 |
OutputStream os)
|
88 |
throws MakePluginPackageServiceException {
|
89 |
Compress compress = new Compress();
|
90 |
compress.compressPluginAsPackageSet(folder, fileName, os); |
91 |
} |
92 |
|
93 |
public void compressPackage(File folder, OutputStream os) |
94 |
throws MakePluginPackageServiceException {
|
95 |
Compress compress = new Compress();
|
96 |
compress.compressPluginAsPackage(folder, os); |
97 |
} |
98 |
|
99 |
public void readPackageInfo(File directory, PackageInfo installerInfo) |
100 |
throws InstallerInfoFileException {
|
101 |
File installInfoFile =
|
102 |
new File(directory + File.separator + INSTALL_INFO_FILE); |
103 |
if (installInfoFile.exists()) {
|
104 |
InstallerInfoFileReader reader = new InstallerInfoFileReader();
|
105 |
reader.read(installerInfo, installInfoFile.getAbsolutePath()); |
106 |
} else {
|
107 |
installerInfo.setCode(directory.getName()); |
108 |
installerInfo.setName(directory.getName()); |
109 |
} |
110 |
} |
111 |
|
112 |
public void writePackageInfo(File directory, PackageInfo installerInfo) |
113 |
throws InstallerInfoFileException {
|
114 |
if (!directory.exists()) {
|
115 |
throw new InstallerInfoFileException("The directory doesn't exist"); |
116 |
} |
117 |
InstallerInfoFileWriter installerInfoFileWriter = |
118 |
new InstallerInfoFileWriter();
|
119 |
installerInfoFileWriter.write(installerInfo, directory + File.separator
|
120 |
+ INSTALL_INFO_FILE); |
121 |
} |
122 |
|
123 |
public InputStream searchPackage(InputStream is, String zipEntry) |
124 |
throws InstallPackageServiceException {
|
125 |
Decompress decompress = new Decompress();
|
126 |
return decompress.searchPlugin(is, zipEntry);
|
127 |
} |
128 |
|
129 |
public void readPackageInfo(InputStream is, |
130 |
List<PackageInfo> installerInfos, Map<PackageInfo, String> zipEntriesMap) |
131 |
throws InstallPackageServiceException {
|
132 |
Decompress decompress = new Decompress();
|
133 |
decompress.readInstallInfo(is, installerInfos, zipEntriesMap); |
134 |
} |
135 |
|
136 |
public File downloadFile(URL bundleURL, String defaultFileName) |
137 |
throws IOException { |
138 |
|
139 |
URL downloadURL = bundleURL;
|
140 |
|
141 |
// check if the URL ends with '/' and append the file name
|
142 |
if (defaultFileName != null) { |
143 |
String urlStr = bundleURL.toString();
|
144 |
if (urlStr.endsWith("/")) { |
145 |
urlStr = urlStr.concat(defaultFileName); |
146 |
downloadURL = new URL(urlStr); |
147 |
} |
148 |
} |
149 |
|
150 |
URLConnection connection = downloadURL.openConnection();
|
151 |
String fileName = getFileName(connection);
|
152 |
|
153 |
if (LOG.isDebugEnabled()) {
|
154 |
Date date = new Date(connection.getLastModified()); |
155 |
LOG.debug( |
156 |
"Downloading file {} from URL {}, with last modified date: {}",
|
157 |
new Object[] { fileName, downloadURL, date }); |
158 |
} |
159 |
|
160 |
String fileNamePrefix = fileName;
|
161 |
String fileNameSuffix = "zip"; |
162 |
int dotPosition = fileName.lastIndexOf('.'); |
163 |
if (dotPosition > -1 && dotPosition < fileName.length() - 1) { |
164 |
fileNamePrefix = fileName.substring(0, dotPosition);
|
165 |
fileNameSuffix = fileName.substring(dotPosition); |
166 |
} |
167 |
|
168 |
BufferedInputStream bis =
|
169 |
new BufferedInputStream(downloadURL.openStream()); |
170 |
|
171 |
File localFile = File.createTempFile(fileNamePrefix, fileNameSuffix); |
172 |
BufferedOutputStream bos =
|
173 |
new BufferedOutputStream(new FileOutputStream(localFile)); |
174 |
|
175 |
byte[] data = new byte[1024]; |
176 |
int count = 0; |
177 |
while ((count = bis.read(data, 0, 1024)) >= 0) { |
178 |
bos.write(data, 0, count);
|
179 |
} |
180 |
|
181 |
bis.close(); |
182 |
bos.flush(); |
183 |
bos.close(); |
184 |
|
185 |
return localFile;
|
186 |
} |
187 |
|
188 |
/**
|
189 |
* Returns the file name associated to an url connection.<br />
|
190 |
* The result is not a path but just a file name.
|
191 |
*
|
192 |
* @param urlConnection
|
193 |
* - the url connection
|
194 |
* @return the file name
|
195 |
*
|
196 |
* @throws IOException
|
197 |
* Signals that an I/O exception has occurred.
|
198 |
*/
|
199 |
private String getFileName(URLConnection urlConnection) throws IOException { |
200 |
String fileName = null; |
201 |
|
202 |
String contentDisposition =
|
203 |
urlConnection.getHeaderField("content-disposition");
|
204 |
|
205 |
if (contentDisposition != null) { |
206 |
fileName = |
207 |
extractFileNameFromContentDisposition(contentDisposition); |
208 |
} |
209 |
|
210 |
// if the file name cannot be extracted from the content-disposition
|
211 |
// header, using the url.getFilename() method
|
212 |
if (fileName == null) { |
213 |
StringTokenizer st =
|
214 |
new StringTokenizer(urlConnection.getURL().getFile(), "/"); |
215 |
while (st.hasMoreTokens())
|
216 |
fileName = st.nextToken(); |
217 |
} |
218 |
|
219 |
return fileName;
|
220 |
} |
221 |
|
222 |
/**
|
223 |
* Extract the file name from the content disposition header.
|
224 |
* <p>
|
225 |
* See <a
|
226 |
* href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html">http:
|
227 |
* //www.w3.org/Protocols/rfc2616/rfc2616-sec19.html</a> for detailled
|
228 |
* information regarding the headers in HTML.
|
229 |
*
|
230 |
* @param contentDisposition
|
231 |
* - the content-disposition header. Cannot be <code>null>/code>.
|
232 |
* @return the file name, or <code>null</code> if the content-disposition
|
233 |
* header does not contain the filename attribute.
|
234 |
*/
|
235 |
private String extractFileNameFromContentDisposition( |
236 |
String contentDisposition) {
|
237 |
String[] attributes = contentDisposition.split(";"); |
238 |
|
239 |
for (String a : attributes) { |
240 |
if (a.toLowerCase().contains("filename")) { |
241 |
// The attribute is the file name. The filename is between
|
242 |
// quotes.
|
243 |
return a.substring(a.indexOf('\"') + 1, a.lastIndexOf('\"')); |
244 |
} |
245 |
} |
246 |
|
247 |
// not found
|
248 |
return null; |
249 |
} |
250 |
|
251 |
} |