author | herrick |
Fri, 12 Oct 2018 19:00:51 -0400 | |
branch | JDK-8200758-branch |
changeset 56963 | eaca4369b068 |
permissions | -rw-r--r-- |
56963
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
1 |
/* |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
2 |
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
4 |
* |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
8 |
* |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
13 |
* accompanied this code). |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
14 |
* |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
18 |
* |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
21 |
* questions. |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
22 |
*/ |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
23 |
|
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
24 |
package jnlp.converter; |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
25 |
|
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
26 |
import java.io.File; |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
27 |
import java.io.FileInputStream; |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
28 |
import java.io.FileNotFoundException; |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
29 |
import java.io.FileOutputStream; |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
30 |
import java.io.IOException; |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
31 |
import java.io.InputStream; |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
32 |
import java.io.OutputStream; |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
33 |
import java.net.HttpURLConnection; |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
34 |
import java.net.MalformedURLException; |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
35 |
import java.net.URI; |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
36 |
import java.net.URL; |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
37 |
import java.nio.file.Files; |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
38 |
import java.nio.file.Path; |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
39 |
import java.nio.file.Paths; |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
40 |
import jnlp.converter.parser.GeneralUtil; |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
41 |
|
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
42 |
public class HTTPHelper { |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
43 |
|
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
44 |
public static final int BUFFER_SIZE = 4096; |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
45 |
|
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
46 |
public static String downloadFile(String url, String destFolder, String destFileName) throws MalformedURLException, IOException { |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
47 |
HttpURLConnection connection = null; |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
48 |
String destFile = null; |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
49 |
|
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
50 |
try { |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
51 |
if (url.contains(" ")) { |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
52 |
url = url.replace(" ", "%20"); |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
53 |
} |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
54 |
if (url.contains("\\")) { |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
55 |
url = url.replace("\\", "/"); |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
56 |
} |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
57 |
|
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
58 |
URL resource = new URL(url); |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
59 |
connection = (HttpURLConnection) resource.openConnection(); |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
60 |
|
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
61 |
int responseCode = connection.getResponseCode(); |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
62 |
if (responseCode == HttpURLConnection.HTTP_OK) { |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
63 |
destFile = destFolder + File.separator + destFileName; |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
64 |
Log.verbose("Downloading " + url + " to " + destFile); |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
65 |
|
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
66 |
try (InputStream inputStream = connection.getInputStream(); |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
67 |
OutputStream outputStream = new FileOutputStream(destFile)) { |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
68 |
byte[] buffer = new byte[BUFFER_SIZE]; |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
69 |
|
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
70 |
int length; |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
71 |
do { |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
72 |
length = inputStream.read(buffer); |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
73 |
if (length > 0) { |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
74 |
outputStream.write(buffer, 0, length); |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
75 |
} |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
76 |
} while (length > 0); |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
77 |
} |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
78 |
} else { |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
79 |
HTTPHelperException e = new HTTPHelperException("Error: Cannot download " + url + ". Server response code: " + responseCode); |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
80 |
e.setResponseCode(responseCode); |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
81 |
throw e; |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
82 |
} |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
83 |
} catch (IOException e) { |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
84 |
if (e instanceof HTTPHelperException) { |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
85 |
throw e; |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
86 |
} else { |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
87 |
throw new HTTPHelperException("Error: Cannot download " + url + ". " + e.getClass().getSimpleName() + ": " + e.getMessage()); |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
88 |
} |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
89 |
} finally { |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
90 |
if (connection != null) { |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
91 |
connection.disconnect(); |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
92 |
} |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
93 |
} |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
94 |
|
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
95 |
return destFile; |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
96 |
} |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
97 |
|
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
98 |
public static String copyFile(String url, String destFolder, String destFileName) throws Exception { |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
99 |
if (url.contains(" ")) { |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
100 |
url = url.replace(" ", "%20"); |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
101 |
} |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
102 |
|
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
103 |
URI sourceURI = new URI(url); |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
104 |
|
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
105 |
String sourceFile = sourceURI.getPath(); |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
106 |
File file = new File(sourceFile); |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
107 |
if (!file.exists()) { |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
108 |
throw new FileNotFoundException("Error: " + sourceFile + " does not exist."); |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
109 |
} |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
110 |
|
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
111 |
String destFile = destFolder + File.separator + destFileName; |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
112 |
file = new File(destFile); |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
113 |
if (file.exists()) { |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
114 |
file.delete(); |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
115 |
} |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
116 |
|
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
117 |
Path sourcePath = Paths.get(sourceURI); |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
118 |
Path destPath = Paths.get(destFile); |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
119 |
Log.verbose("Copying " + url + " to " + destFile); |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
120 |
Files.copy(sourcePath, destPath); |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
121 |
|
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
122 |
return destFile; |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
123 |
} |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
124 |
|
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
125 |
public static boolean isHTTPUrl(String url) { |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
126 |
return (url.startsWith("http://") || url.startsWith("https://")); |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
127 |
} |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
128 |
|
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
129 |
public static byte[] getJNLPBits(String versionedJNLP, String jnlp) throws Exception { |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
130 |
String jnlpFilePath = null; |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
131 |
byte[] bits = null; |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
132 |
|
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
133 |
if (isHTTPUrl(jnlp)) { |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
134 |
try { |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
135 |
jnlpFilePath = downloadFile(versionedJNLP, JNLPConverter.getJnlpDownloadFolderStatic(), getFileNameFromURL(jnlp)); |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
136 |
} catch (HTTPHelperException ex) { |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
137 |
if (ex.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND && |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
138 |
!versionedJNLP.equals(jnlp)) { |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
139 |
Log.warning("Downloading versioned JNLP from " + versionedJNLP + " failed."); |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
140 |
Log.warning(ex.getMessage()); |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
141 |
Log.warning("Downloading " + jnlp + " instead."); |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
142 |
jnlpFilePath = downloadFile(jnlp, JNLPConverter.getJnlpDownloadFolderStatic(), getFileNameFromURL(jnlp)); |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
143 |
} else { |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
144 |
throw ex; |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
145 |
} |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
146 |
} |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
147 |
JNLPConverter.markFileToDelete(jnlpFilePath); |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
148 |
} else { |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
149 |
try { |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
150 |
jnlpFilePath = copyFile(versionedJNLP, JNLPConverter.getJnlpDownloadFolderStatic(), getFileNameFromURL(jnlp)); |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
151 |
} catch (FileNotFoundException ex) { |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
152 |
System.out.println("Error copying versioned JNLP from " + versionedJNLP); |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
153 |
System.out.println(ex.getMessage()); |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
154 |
System.out.println("Copying " + jnlp + " instead."); |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
155 |
jnlpFilePath = HTTPHelper.copyFile(jnlp, JNLPConverter.getJnlpDownloadFolderStatic(), getFileNameFromURL(jnlp)); |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
156 |
} |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
157 |
JNLPConverter.markFileToDelete(jnlpFilePath); |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
158 |
} |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
159 |
|
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
160 |
File jnlpFile = new File(jnlpFilePath); |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
161 |
if (jnlpFile.exists()) { |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
162 |
bits = GeneralUtil.readBytes(new FileInputStream(jnlpFile), jnlpFile.length()); |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
163 |
} |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
164 |
|
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
165 |
return bits; |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
166 |
} |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
167 |
|
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
168 |
public static String getFileNameFromURL(String url) throws IOException { |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
169 |
int index; |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
170 |
int index1 = url.lastIndexOf('/'); |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
171 |
int index2 = url.lastIndexOf('\\'); |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
172 |
|
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
173 |
if (index1 >= index2) { |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
174 |
index = index1; |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
175 |
} else { |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
176 |
index = index2; |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
177 |
} |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
178 |
|
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
179 |
if (index != -1) { |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
180 |
String name = url.substring(index + 1, url.length()); |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
181 |
name = name.replace("%20", " "); |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
182 |
if (name.endsWith(".jnlp") || name.endsWith(".jar")) { // JNLP or JAR |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
183 |
return name; |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
184 |
} else if (name.endsWith(".ico")) { // Icons |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
185 |
return name; |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
186 |
} else { |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
187 |
throw new IOException("Error: Unsupported file extension for " + url); |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
188 |
} |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
189 |
} else { |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
190 |
throw new IOException("Error: URL (" + url + ") should end with file name."); |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
191 |
} |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
192 |
} |
eaca4369b068
8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff
changeset
|
193 |
} |