Skip to content
Snippets Groups Projects
Commit 12c95e0d authored by Mirco Nasuti's avatar Mirco Nasuti
Browse files

refactoring

parent ad9a4db3
No related branches found
No related tags found
No related merge requests found
...@@ -19,11 +19,11 @@ import org.springframework.http.HttpStatus; ...@@ -19,11 +19,11 @@ import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.io.*; import java.io.BufferedReader;
import java.net.HttpURLConnection; import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
...@@ -260,110 +260,7 @@ public class ExperimentApi { ...@@ -260,110 +260,7 @@ public class ExperimentApi {
return new ResponseEntity<>(new Gson().toJson(catalog), HttpStatus.valueOf(code)); return new ResponseEntity<>(new Gson().toJson(catalog), HttpStatus.valueOf(code));
} }
private void sendExperiment(Experiment experiment) throws MalformedURLException { private ResponseEntity<String> doListExperiments(
URL obj = new URL(experimentUrl);
// this runs in the background. For future optimization: use a thread pool
new Thread() {
@Override
public void run() {
try {
String query = experiment.computeQuery();
HttpURLConnection con = createConnection(obj, query);
writeQueryBody(con, query);
String response = readResponse(con);
// write to experiment
experiment.setResult(response.replace("\0", ""));
experiment.setHasError(con.getResponseCode() >= 400);
experiment.setHasServerError(con.getResponseCode() >= 500);
} catch (ProtocolException pe) {
LOGGER.trace(pe);
} catch (IOException ioe) {
// write error to
LOGGER.trace(ioe);
LOGGER.warn("Experiment failed to run properly !");
experiment.setHasError(true);
experiment.setHasServerError(true);
experiment.setResult(ioe.getMessage());
}
experiment.finish();
}
}.start();
}
private void sendExaremeExperiment(Experiment experiment) {
new Thread() {
@Override
public void run() {
try {
String query = experiment.computeExaremeQuery();
String url = miningExaremeQueryUrl + "/" + EXAREME_LR_ALGO;
StringBuilder results = new StringBuilder();
int code = HTTPUtil.sendPost(url, query, results);
experiment.setResult(results.toString().replace("\0", ""));
experiment.setHasError(code >= 400);
experiment.setHasServerError(code >= 500);
if(!JSONUtil.isJSONValid(experiment.getResult()))
{
experiment.setResult("Unsupported variables !");
}
} catch (Exception e) {
LOGGER.trace(e);
LOGGER.warn("Failed to run Exareme algorithm !");
experiment.setHasError(true);
experiment.setHasServerError(true);
experiment.setResult(e.getMessage());
}
experiment.finish();
}
}.start();
}
public ResponseEntity<String> doMarkExperimentAsShared(String uuid, boolean shared) {
Experiment experiment;
UUID experimentUuid;
User user = mipApplication.getUser();
try {
experimentUuid = UUID.fromString(uuid);
} catch (IllegalArgumentException iae) {
LOGGER.trace(iae);
LOGGER.warn("An invalid Experiment UUID was received !");
return ResponseEntity.badRequest().body("Invalid Experiment UUID");
}
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
Transaction transaction = null;
try {
transaction = session.beginTransaction();
Query hibernateQuery = session.createQuery("from Experiment as experiment where experiment.uuid = :uuid");
hibernateQuery.setParameter("uuid", experimentUuid);
experiment = (Experiment) hibernateQuery.uniqueResult();
if (!experiment.getCreatedBy().getUsername().equals(user.getUsername()))
return new ResponseEntity<>("You're not the owner of this experiment", HttpStatus.BAD_REQUEST);
experiment.setShared(shared);
session.update(experiment);
transaction.commit();
} catch (Exception e) {
// 404 here probably
if(transaction != null)
{
transaction.rollback();
}
throw e;
}
return new ResponseEntity<>(gson.toJson(experiment), HttpStatus.OK);
}
public ResponseEntity<String> doListExperiments(
boolean mine, boolean mine,
int maxResultCount, int maxResultCount,
String modelSlug String modelSlug
...@@ -419,34 +316,107 @@ public class ExperimentApi { ...@@ -419,34 +316,107 @@ public class ExperimentApi {
return new ResponseEntity<>(gson.toJson(experiments), HttpStatus.OK); return new ResponseEntity<>(gson.toJson(experiments), HttpStatus.OK);
} }
private static String readResponse(HttpURLConnection con) throws IOException { private ResponseEntity<String> doMarkExperimentAsShared(String uuid, boolean shared) {
InputStream stream = con.getResponseCode() < 400 ? con.getInputStream() : con.getErrorStream(); Experiment experiment;
BufferedReader in = new BufferedReader(new InputStreamReader(stream)); UUID experimentUuid;
String inputLine; User user = mipApplication.getUser();
StringBuilder response = new StringBuilder(); try {
while ((inputLine = in.readLine()) != null) { experimentUuid = UUID.fromString(uuid);
response.append(inputLine + '\n'); } catch (IllegalArgumentException iae) {
LOGGER.trace(iae);
LOGGER.warn("An invalid Experiment UUID was received !");
return ResponseEntity.badRequest().body("Invalid Experiment UUID");
}
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
Transaction transaction = null;
try {
transaction = session.beginTransaction();
Query hibernateQuery = session.createQuery("from Experiment as experiment where experiment.uuid = :uuid");
hibernateQuery.setParameter("uuid", experimentUuid);
experiment = (Experiment) hibernateQuery.uniqueResult();
if (!experiment.getCreatedBy().getUsername().equals(user.getUsername()))
return new ResponseEntity<>("You're not the owner of this experiment", HttpStatus.BAD_REQUEST);
experiment.setShared(shared);
session.update(experiment);
transaction.commit();
} catch (Exception e) {
// 404 here probably
if(transaction != null)
{
transaction.rollback();
}
throw e;
} }
in.close();
return response.toString(); return new ResponseEntity<>(gson.toJson(experiment), HttpStatus.OK);
}
private void sendExperiment(Experiment experiment) throws MalformedURLException {
// this runs in the background. For future optimization: use a thread pool
new Thread() {
@Override
public void run() {
String url = experimentUrl;
String query = experiment.computeQuery();
// Results are stored in the experiment object
try {
executeExperiment(url, query, experiment);
} catch (IOException e) {
LOGGER.trace(e);
LOGGER.warn("Experiment failed to run properly !");
setExperimentError(e, experiment);
}
experiment.finish();
}
}.start();
}
private void sendExaremeExperiment(Experiment experiment) {
// this runs in the background. For future optimization: use a thread pool
new Thread() {
@Override
public void run() {
String query = experiment.computeExaremeQuery();
String url = miningExaremeQueryUrl + "/" + EXAREME_LR_ALGO;
// Results are stored in the experiment object
try {
executeExperiment(url, query, experiment);
} catch (IOException e) {
LOGGER.trace(e);
LOGGER.warn("Exareme experiment failed to run properly !");
setExperimentError(e, experiment);
}
if(!JSONUtil.isJSONValid(experiment.getResult()))
{
experiment.setResult("Unsupported variables !");
}
experiment.finish();
}
}.start();
} }
private static void writeQueryBody(HttpURLConnection con, String query) throws IOException { private static void executeExperiment(String url, String query, Experiment experiment) throws IOException {
DataOutputStream wr = new DataOutputStream(con.getOutputStream()); StringBuilder results = new StringBuilder();
wr.write(query.getBytes("UTF8")); int code = HTTPUtil.sendPost(url, query, results);
wr.flush(); experiment.setResult(results.toString().replace("\0", ""));
wr.close(); experiment.setHasError(code >= 400);
experiment.setHasServerError(code >= 500);
} }
private static HttpURLConnection createConnection(URL url, String query) throws IOException { private static void setExperimentError(IOException e, Experiment experiment) {
HttpURLConnection con = (HttpURLConnection) url.openConnection(); experiment.setHasError(true);
con.setRequestMethod("POST"); experiment.setHasServerError(true);
con.addRequestProperty("Content-Type", "application/json"); experiment.setResult(e.getMessage());
con.setRequestProperty("Content-Length", Integer.toString(query.length()));
con.setInstanceFollowRedirects(true);
con.setReadTimeout(3600000); // 1 hour: 60*60*1000 ms
con.setDoOutput(true);
return con;
} }
private static boolean isExaremeAlgo(Experiment experiment) { private static boolean isExaremeAlgo(Experiment experiment) {
......
...@@ -11,6 +11,11 @@ public class JSONUtil { ...@@ -11,6 +11,11 @@ public class JSONUtil {
private static final Logger LOGGER = Logger.getLogger(JSONUtil.class); private static final Logger LOGGER = Logger.getLogger(JSONUtil.class);
private JSONUtil() {
/* Hide implicit public constructor */
throw new IllegalAccessError("JSONUtil class");
}
public static boolean isJSONValid(String test) { public static boolean isJSONValid(String test) {
try { try {
new JsonParser().parse(test); new JsonParser().parse(test);
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment