use of org.springframework.web.client.RestTemplate in project OpenClinica by OpenClinica.
the class RandomizeService method retrieveARandomisation.
private JSONObject retrieveARandomisation(String randomiseUrl, StudySubjectBean studySubject, HttpHeaders headers) throws JSONException {
// method : GET
// concatenate
randomiseUrl = randomiseUrl + "/api/randomisation?identifier=" + studySubject.getOid();
// Study_Siubject_oid
RestTemplate rest = new RestTemplate(requestFactory);
ResponseEntity<String> response = null;
String body = null;
JSONObject jsonObject = null;
HttpEntity<String> request = new HttpEntity<String>(headers);
try {
response = rest.exchange(randomiseUrl, HttpMethod.GET, request, String.class);
body = response.getBody();
jsonObject = new JSONObject(body);
// if (!jsonObject.get("error").equals("0"))
// jsonObject= null;
} catch (Exception e) {
System.out.println(e.getMessage());
logger.error(e.getMessage());
logger.error(ExceptionUtils.getStackTrace(e));
}
return jsonObject;
}
use of org.springframework.web.client.RestTemplate in project OpenClinica by OpenClinica.
the class XformMetaDataService method getMediaFileItemFromFormManager.
public FileItem getMediaFileItemFromFormManager(String fileLink, String crfOid, String formLayoutOid) {
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new ByteArrayHttpMessageConverter());
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_OCTET_STREAM));
HttpEntity<String> entity = new HttpEntity<String>(headers);
ResponseEntity<byte[]> response = restTemplate.exchange(fileLink, HttpMethod.GET, entity, byte[].class, "1");
FileItem fileItem = null;
if (response.getStatusCode() == HttpStatus.OK) {
String fileName = "";
FileOutputStream output = null;
try {
String disposition = response.getHeaders().get("Content-Disposition").get(0);
fileName = disposition.replaceFirst("(?i)^.*filename=\"([^\"]+)\".*$", "$1");
String dir = Utils.getCrfMediaFilePath(crfOid, formLayoutOid);
if (!new File(dir).exists()) {
new File(dir).mkdirs();
logger.debug("Made the directory " + dir);
}
File file = new File(dir + fileName);
output = new FileOutputStream(file);
IOUtils.write(response.getBody(), output);
fileItem = new DiskFileItem("media_file", response.getHeaders().get("Content-Type").get(0), false, fileName, 100000000, file);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (output != null) {
output.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
return fileItem;
}
use of org.springframework.web.client.RestTemplate in project OpenClinica by OpenClinica.
the class XformMetaDataService method executeIndividualCrf.
public void executeIndividualCrf(ExecuteIndividualCrfObject eicObject) {
for (OCodmComplexTypeDefinitionFormLayoutDef formLayoutDef : eicObject.formLayoutDefs) {
List<String> fileLinks = null;
String vForm = "";
RestTemplate rest = new RestTemplate();
if (eicObject.form != null) {
List<FormVersion> versions = eicObject.form.getVersions();
for (FormVersion version : versions) {
if (version.getName().equals(formLayoutDef.getOID())) {
fileLinks = version.getFileLinks();
for (String fileLink : fileLinks) {
if (fileLink.endsWith(VERSION)) {
vForm = rest.getForObject(fileLink, String.class);
break;
}
}
if (!eicObject.errors.hasErrors()) {
ObjectMapper mapper = new ObjectMapper();
TypeReference<List<XformGroup>> mapType = new TypeReference<List<XformGroup>>() {
};
List<XformGroup> jsonList = null;
try {
jsonList = mapper.readValue(vForm, mapType);
} catch (JsonParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JsonMappingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
XformContainer xformContainer = new XformContainer();
xformContainer.setGroups(jsonList);
eicObject.setContainer(xformContainer);
if (eicObject.errors.hasErrors()) {
return;
}
// Save meta-data in database
saveFormMetadata(eicObject, version, eicObject.container, formLayoutDef, fileLinks);
}
}
}
}
}
}
use of org.springframework.web.client.RestTemplate in project OpenClinica by OpenClinica.
the class EnketoAPI method getURL.
private EnketoURLResponse getURL(URL url, String crfOID) {
try {
String userPasswdCombo = new String(Base64.encodeBase64((token + ":").getBytes()));
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.add("Authorization", "Basic " + userPasswdCombo);
headers.add("Accept-Charset", "UTF-8");
EnketoURLRequest body = new EnketoURLRequest(ocURL, crfOID);
HttpEntity<EnketoURLRequest> request = new HttpEntity<EnketoURLRequest>(body, headers);
RestTemplate rest = new RestTemplate();
ResponseEntity<EnketoURLResponse> response = rest.postForEntity(url.toString(), request, EnketoURLResponse.class);
if (response != null)
return response.getBody();
else
return null;
} catch (Exception e) {
logger.error(e.getMessage());
logger.error(ExceptionUtils.getStackTrace(e));
}
return null;
}
use of org.springframework.web.client.RestTemplate in project OpenClinica by OpenClinica.
the class RandomizationRegistrar method getRandomizationDTOObject.
// Rest Call to OCUI to get Randomization
public SeRandomizationDTO getRandomizationDTOObject(String studyOid) {
String ocUrl = CoreResources.getField("sysURL.base") + "rest2/openrosa/" + studyOid;
String randomizationUrl = CoreResources.getField("moduleManager") + "/app/rest/oc/se_randomizations?studyoid=" + studyOid + "&instanceurl=" + ocUrl;
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
requestFactory.setReadTimeout(RANDOMIZATION_READ_TIMEOUT);
RestTemplate rest = new RestTemplate(requestFactory);
try {
SeRandomizationDTO response = rest.getForObject(randomizationUrl, SeRandomizationDTO.class);
if (response.getStudyOid() != null) {
return response;
} else {
return null;
}
} catch (Exception e) {
logger.error(e.getMessage());
logger.error(ExceptionUtils.getStackTrace(e));
System.out.println(e.getMessage());
System.out.println(ExceptionUtils.getStackTrace(e));
}
return null;
}
Aggregations