Skip to content
Snippets Groups Projects
Commit 1af0f261 authored by Jan Kožusznik's avatar Jan Kožusznik
Browse files

feat: iss1143

allow open BDV only for job with available data
parent 517f5b2e
No related branches found
No related tags found
No related merge requests found
package cz.it4i.fiji.commons;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
final public class WebRoutines {
private static final Logger log = LoggerFactory.getLogger(
cz.it4i.fiji.commons.WebRoutines.class);
public static boolean doesURLExist(final URL url) {
// We want to check the current URL
HttpURLConnection.setFollowRedirects(false);
// We don't need to get data
try {
final HttpURLConnection httpURLConnection = (HttpURLConnection) url
.openConnection();
httpURLConnection.setRequestMethod("HEAD");
// Some websites don't like programmatic access so pretend to be a browser
httpURLConnection.setRequestProperty("User-Agent",
"Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 (.NET CLR 3.5.30729)");
final int responseCode = httpURLConnection.getResponseCode();
return responseCode == HttpURLConnection.HTTP_OK;
// We only accept response code 200
}
catch (final IOException exc) {
log.error(exc.getMessage(), exc);
return false;
}
}
private WebRoutines() {}
}
/**
*
*/
/**
* @author koz01
*
*/
package cz.it4i.fiji.commons;
......@@ -13,10 +13,16 @@ import java.io.Closeable;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collection;
......@@ -51,6 +57,7 @@ import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import cz.it4i.fiji.commons.WebRoutines;
import cz.it4i.fiji.haas.HaaSOutputHolder;
import cz.it4i.fiji.haas.HaaSOutputHolderImpl;
import cz.it4i.fiji.haas.Job;
......@@ -84,6 +91,8 @@ public class BenchmarkJobManager implements Closeable {
private CompletableFuture<JobState> running;
private ProgressNotifier downloadNotifier;
private boolean visibleInBDV;
public BenchmarkJob(Job job) {
this.job = job;
tasks = new LinkedList<>();
......@@ -142,8 +151,46 @@ public class BenchmarkJobManager implements Closeable {
public void update() {
job.updateInfo();
try {
visibleInBDV = Files.exists(getLocalPathToResultXML()) || WebRoutines
.doesURLExist(new URL(getPathToToResultXMLOnBDS()));
if (log.isDebugEnabled()) {
log.debug("job #" + getId() + " is visible in BDV " + visibleInBDV);
}
}
catch (MalformedURLException exc) {
log.info(exc.getMessage(), exc);
visibleInBDV = false;
}
}
public Path getLocalPathToResultXML() {
return getOutputDirectory().resolve(getResultXML());
}
public String getPathToToResultXMLOnBDS() {
String changed = job.getId() + "/" + getResultXML();
MessageDigest digest;
try {
digest = MessageDigest.getInstance("SHA-1");
digest.reset();
digest.update(changed.getBytes("utf8"));
String sha1 = String.format("%040x", new BigInteger(1, digest.digest()));
String result = Constants.BDS_ADDRESS + sha1 + "/";
if (log.isDebugEnabled()) {
log.debug("getBDSPathForData changed={} path={}",result, result);
}
return result;
}
catch (NoSuchAlgorithmException | UnsupportedEncodingException exc) {
throw new RuntimeException(exc);
}
}
public boolean isVisibleInBDV() {
return visibleInBDV;
}
public void startUpload() {
job.startUploadData();
}
......
......@@ -5,12 +5,8 @@ import static cz.it4i.fiji.haas_spim_benchmark.core.Constants.CONFIG_YAML;
import java.awt.Window;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.LinkedList;
......@@ -164,7 +160,8 @@ public class BenchmarkSPIMControl extends BorderPane implements
menu.addItem("Open job subdirectory", j -> openJobSubdirectory(j
.getValue()), x -> JavaFXRoutines.notNullValue(x, j -> true));
menu.addItem("Open in BigDataViewer", j -> openBigDataViewer(j.getValue()),
x -> JavaFXRoutines.notNullValue(x, j -> true));
x -> JavaFXRoutines.notNullValue(x, j -> j
.getState() == JobState.Finished && j.isVisibleInBDV()));
menu.addSeparator();
menu.addItem("Upload data", job -> executeWSCallAsync("Uploading data",
......@@ -414,14 +411,13 @@ public class BenchmarkSPIMControl extends BorderPane implements
}
private void openBigDataViewer(BenchmarkJob job) {
String resultXML = job.getResultXML();
Path localPathToResultXML = job.getOutputDirectory().resolve(resultXML);
Path localPathToResultXML = job.getLocalPathToResultXML();
String openFile;
if (Files.exists(localPathToResultXML)) {
openFile = localPathToResultXML.toString();
}
else {
openFile = getPathToBDSForData(job, resultXML);
openFile = job.getPathToToResultXMLOnBDS();
}
try {
BigDataViewer.open(openFile, "Result of job " + job.getId(),
......@@ -432,24 +428,7 @@ public class BenchmarkSPIMControl extends BorderPane implements
}
}
private String getPathToBDSForData(BenchmarkJob job, String resultXML) {
String changed = job.getId() + "/" + resultXML;
MessageDigest digest;
try {
digest = MessageDigest.getInstance("SHA-1");
digest.reset();
digest.update(changed.getBytes("utf8"));
String sha1 = String.format("%040x", new BigInteger(1, digest.digest()));
String result = Constants.BDS_ADDRESS + sha1 + "/";
if (log.isDebugEnabled()) {
log.debug("getBDSPathForData changed={} path={}",changed, result);
}
return result;
}
catch (NoSuchAlgorithmException | UnsupportedEncodingException exc) {
throw new RuntimeException(exc);
}
}
private interface P_JobAction {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment