This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package example; | |
import java.io.File; | |
import java.io.InputStream; | |
import java.net.HttpURLConnection; | |
import java.net.URI; | |
import java.net.URL; | |
import java.util.Base64; | |
import arc.mf.plugin.PluginService; | |
import arc.mf.plugin.ServiceExecutor; | |
import arc.streams.StreamCopy; | |
import arc.xml.XmlDocMaker; | |
import arc.xml.XmlWriter; | |
public class CreateDatasetFromURL { | |
public static interface ResponseInputStreamHandler { | |
void handleResponseInputStream(InputStream in) throws Throwable; | |
} | |
/** | |
* Do HTTP GET for the specified URL (and user credentials). | |
* | |
* @param uri | |
* @param username | |
* @param password | |
* @param rh | |
* @throws Throwable | |
*/ | |
public static void httpGet(String uri, String username, String password, final ResponseInputStreamHandler rh) | |
throws Throwable { | |
URL url = URI.create(uri).toURL(); | |
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); | |
try { | |
conn.setDoOutput(false); | |
conn.setDoInput(true); | |
conn.setRequestMethod("GET"); | |
String authorization = "Basic " | |
+ Base64.getEncoder().encodeToString((username + ":" + password).getBytes()); | |
conn.setRequestProperty("Authorization", authorization); | |
int responseCode = conn.getResponseCode(); | |
String responseMessage = conn.getResponseMessage(); | |
InputStream responseInputStream = null; | |
try { | |
if (responseCode != HttpURLConnection.HTTP_OK) { | |
throw new Exception("Unexpected HTTP response: " + responseCode + " " + responseMessage); | |
} | |
responseInputStream = conn.getInputStream(); | |
rh.handleResponseInputStream(responseInputStream); | |
} finally { | |
if (responseInputStream != null) { | |
responseInputStream.close(); | |
} | |
} | |
} finally { | |
conn.disconnect(); | |
} | |
} | |
/** | |
* Download from the HTTP URL and save to the specified file. | |
* | |
* @param httpUrl | |
* @param httpUsername | |
* @param httpPassword | |
* @param dstFile | |
* @throws Throwable | |
*/ | |
public static void download(String httpUrl, String httpUsername, String httpPassword, final File dstFile) | |
throws Throwable { | |
httpGet(httpUrl, httpUsername, httpPassword, new ResponseInputStreamHandler() { | |
@Override | |
public void handleResponseInputStream(InputStream in) throws Throwable { | |
StreamCopy.copy(in, dstFile); | |
} | |
}); | |
} | |
/** | |
* Create a dataset from HTTP URL (and basic http auth). | |
* | |
* @param cxn | |
* @param studyCid | |
* @param httpUrl | |
* @param httpUsername | |
* @param httpPassword | |
* @throws Throwable | |
*/ | |
public static void createDataset(final ServiceExecutor executor, final String studyCid, final String httpUrl, | |
String httpUsername, String httpPassword, final XmlWriter w) throws Throwable { | |
httpGet(httpUrl, httpUsername, httpPassword, new ResponseInputStreamHandler() { | |
@Override | |
public void handleResponseInputStream(InputStream in) throws Throwable { | |
XmlDocMaker dm = new XmlDocMaker("args"); | |
dm.add("pid", studyCid); | |
// TODO: add other metadata if needed | |
String fileName = extractFileNameFromUrl(httpUrl); | |
String fileExt = extractFileExtension(fileName); | |
String fileMimeType = mimeTypeFromExt(fileExt); | |
PluginService.Input input = new PluginService.Input(in, -1, fileMimeType, fileName); | |
executor.execute("om.pssd.dataset.derivation.create", dm.root(), new PluginService.Inputs(input), null); | |
} | |
}); | |
} | |
private static String extractFileExtension(String fileName) { | |
if (fileName != null) { | |
int idx = fileName.lastIndexOf('.'); | |
if (idx > 0) { | |
return fileName.substring(idx + 1); | |
} | |
} | |
return null; | |
} | |
private static String extractFileNameFromUrl(String url) { | |
if (url != null) { | |
int idx = url.lastIndexOf('/'); | |
if (idx > 0) { | |
return url.substring(idx + 1); | |
} | |
} | |
return null; | |
} | |
private static String mimeTypeFromExt(String ext) { | |
if (ext != null) { | |
if (ext.equalsIgnoreCase("gz")) { | |
return "application/x-gzip"; | |
} | |
if (ext.equalsIgnoreCase("zip")) { | |
return "application/zip"; | |
} | |
// The above just example. | |
// You add other know mime types here. | |
} | |
return null; | |
} | |
} |