try {
DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
if (submissionEndPoint == null) {
throw new SubmissionException("No submission endpoint configured. Please check your submission.properties file.");
}
if (submissionData == null || submissionData.size() == 0) {
throw new SubmissionException("No submission data set.");
}
if (accountName == null || dropBox == null || authKey == null) {
throw new SubmissionException("An accountName, dropBox and authKey must be supplied!");
}
if (centreName == null) {
throw new SubmissionException("No centreName configured. Please check your submission.properties file and specify your Center Name as given by the SRA.");
}
String curl = "curl -k ";
StringBuilder sb = new StringBuilder();
sb.append(curl);
String proxyHost = submissionProperties.getProperty("submission.proxyHost");
String proxyUser = submissionProperties.getProperty("submission.proxyUser");
String proxyPass = submissionProperties.getProperty("submission.proxyPass");
if (proxyHost != null && !proxyHost.equals("")) {
sb.append("-x ").append(proxyHost);
if (proxyUser != null && !proxyUser.equals("")) {
sb.append("-U ").append(proxyUser);
if (proxyPass != null && !proxyPass.equals("")) {
sb.append(":").append(proxyPass);
}
}
}
//submit via REST to endpoint
try {
Map<String, List<Submittable<Document>>> map = new HashMap<String, List<Submittable<Document>>>();
map.put("study", new ArrayList<Submittable<Document>>());
map.put("sample", new ArrayList<Submittable<Document>>());
map.put("experiment", new ArrayList<Submittable<Document>>());
map.put("run", new ArrayList<Submittable<Document>>());
Document submissionXml = docBuilder.newDocument();
String subName = null;
String d = df.format(new Date());
submissionProperties.put("submissionDate", d);
for (Submittable<Document> s : submissionData) {
if (s instanceof Submission) {
//s.buildSubmission();
ERASubmissionFactory.generateParentSubmissionXML(submissionXml, (Submission) s, submissionProperties);
subName = ((Submission) s).getName();
}
else if (s instanceof Study) {
map.get("study").add(s);
}
else if (s instanceof Sample) {
map.get("sample").add(s);
}
else if (s instanceof Experiment) {
map.get("experiment").add(s);
}
else if (s instanceof SequencerPoolPartition) {
map.get("run").add(s);
}
}
if (submissionXml != null && subName != null) {
String url = getSubmissionEndPoint() + "?auth=ERA%20" + dropBox + "%20" + authKey;
HttpClient httpclient = getEvilTrustingTrustManager(new DefaultHttpClient());
HttpPost httppost = new HttpPost(url);
MultipartEntity reqEntity = new MultipartEntity();
String submissionXmlFileName = subName + File.separator + subName + "_submission_"+d+".xml";
File subtmp = new File(submissionStoragePath + submissionXmlFileName);
SubmissionUtils.transform(submissionXml, subtmp, true);
reqEntity.addPart("SUBMISSION", new FileBody(subtmp));
for (String key : map.keySet()) {
List<Submittable<Document>> submittables = map.get(key);
String submittableXmlFileName = subName
+ File.separator
+ subName
+ "_"
+ key.toLowerCase()
+ "_"
+ d
+ ".xml";
File elementTmp = new File(submissionStoragePath + submittableXmlFileName);
Document submissionDocument = docBuilder.newDocument();
ERASubmissionFactory.generateSubmissionXML(submissionDocument, submittables, key, submissionProperties);
SubmissionUtils.transform(submissionDocument, elementTmp, true);
reqEntity.addPart(key.toUpperCase(), new FileBody(elementTmp));
}
httppost.setEntity(reqEntity);
HttpResponse response = httpclient.execute(httppost);
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity resEntity = response.getEntity();
try {
Document submissionReport = docBuilder.newDocument();
SubmissionUtils.transform(resEntity.getContent(), submissionReport);
File savedReport = new File(submissionStoragePath
+ subName
+ File.separator
+ "report_"
+ d
+ ".xml");
SubmissionUtils.transform(submissionReport, savedReport);
return submissionReport;
}
catch (IOException e) {
e.printStackTrace();
}
catch (TransformerException e) {
e.printStackTrace();
}
finally {
submissionProperties.remove("submissionDate");
}
}
else {
throw new SubmissionException("Response from submission endpoint (" + url + ") was not OK (200). Was: " + response.getStatusLine().getStatusCode());
}
}
else {
throw new SubmissionException("Could not find a Submission in the supplied set of Submittables");
}
}
catch (IOException e) {
e.printStackTrace();
}