use of org.springframework.http.HttpEntity in project uPortal by Jasig.
the class DefaultTinCanAPIProvider method sendRequest.
/**
* Send a request to the LRS.
*
* @param pathFragment the URL. Should be relative to the xAPI API root
* @param method the HTTP method
* @param getParams the set of GET params
* @param postData the post data.
* @param returnType the type of object to expect in the response
* @param <T> The type of object to expect in the response
* @return The response object.
*/
protected <T> ResponseEntity<T> sendRequest(String pathFragment, HttpMethod method, List<? extends NameValuePair> getParams, Object postData, Class<T> returnType) {
HttpHeaders headers = new HttpHeaders();
headers.add(XAPI_VERSION_HEADER, XAPI_VERSION_VALUE);
// make multipart data is handled correctly.
if (postData instanceof MultiValueMap) {
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
}
URI fullURI = buildRequestURI(pathFragment, getParams);
HttpEntity<?> entity = new HttpEntity<>(postData, headers);
ResponseEntity<T> response = restTemplate.exchange(fullURI, method, entity, returnType);
return response;
}
use of org.springframework.http.HttpEntity in project ArachneCentralAPI by OHDSI.
the class StudyFileServiceImpl method getInputStream.
private InputStream getInputStream(AbstractStudyFile studyFile) {
HttpHeaders headers = new HttpHeaders();
headers.setAccept(singletonList(MediaType.APPLICATION_OCTET_STREAM));
HttpEntity<String> entity = new HttpEntity<>(headers);
ResponseEntity<byte[]> response = restTemplate.exchange(studyFile.getLink(), HttpMethod.GET, entity, byte[].class);
if (response.getStatusCode() == HttpStatus.OK) {
return new ByteArrayInputStream(response.getBody());
}
return null;
}
use of org.springframework.http.HttpEntity in project ArachneCentralAPI by OHDSI.
the class BaseStudyServiceImpl method saveFile.
@Override
@PreAuthorize("hasPermission(#studyId, 'Study', " + "T(com.odysseusinc.arachne.portal.security.ArachnePermission).UPLOAD_FILES)")
public String saveFile(String link, Long studyId, String label, IUser user) throws IOException {
Study study = studyRepository.findOne(studyId);
String fileNameLowerCase = UUID.randomUUID().toString();
try {
if (link == null) {
throw new IORuntimeException("wrong url");
}
HttpHeaders headers = new HttpHeaders();
headers.setAccept(singletonList(MediaType.APPLICATION_OCTET_STREAM));
HttpEntity<String> entity = new HttpEntity<>(headers);
URL url = new URL(link);
String fileName = FilenameUtils.getName(url.getPath());
ResponseEntity<byte[]> response = restTemplate.exchange(link.toString(), HttpMethod.GET, entity, byte[].class);
if (response.getStatusCode() == HttpStatus.OK) {
String contentType = CommonFileUtils.TYPE_LINK;
StudyFile studyFile = new StudyFile();
studyFile.setUuid(fileNameLowerCase);
studyFile.setContentType(contentType);
studyFile.setStudy(study);
studyFile.setLabel(label);
studyFile.setRealName(fileName);
studyFile.setLink(link);
Date created = new Date();
studyFile.setCreated(created);
studyFile.setUpdated(created);
studyFile.setAuthor(user);
studyFile.setAntivirusStatus(AntivirusStatus.WILL_NOT_SCAN);
studyFile.setAntivirusDescription("External links are not scanned");
studyFileRepository.save(studyFile);
return fileNameLowerCase;
}
return null;
} catch (IOException | RuntimeException ex) {
String message = "error save file to disk, filename=" + fileNameLowerCase + " ex=" + ex.toString();
LOGGER.debug(message, ex);
throw new IOException(message);
}
}
use of org.springframework.http.HttpEntity in project OpenClinica by OpenClinica.
the class CreateXformCRFVersionServlet method getFormArtifactsFromFM.
private FormArtifactTransferObj getFormArtifactsFromFM(List<FileItem> files, String studyOid, String crfName) {
LinkedMultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
ArrayList<ByteArrayResource> byteArrayResources = new ArrayList<>();
RestTemplate restTemplate = new RestTemplate();
String uploadFilesUrl = FM_BASEURL + studyOid + "/forms/" + crfName + "/artifacts";
map.add("file", byteArrayResources);
for (FileItem file : files) {
String filename = file.getName();
if (!file.isFormField()) {
ByteArrayResource contentsAsResource = new ByteArrayResource(file.get()) {
@Override
public String getFilename() {
return filename;
}
};
map.get("file").add(contentsAsResource);
}
}
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
HttpEntity<LinkedMultiValueMap<String, Object>> requestEntity = new HttpEntity<>(map, headers);
// TODO: replace with Crf object instead of String object
FormArtifactTransferObj response = restTemplate.postForObject(uploadFilesUrl, requestEntity, FormArtifactTransferObj.class);
return response;
}
use of org.springframework.http.HttpEntity in project OpenClinica by OpenClinica.
the class RandomizeService method randomiseSubject.
private JSONObject randomiseSubject(String randomiseUrl, StudySubjectBean studySubject, StudyBean studyBean, HttpHeaders headers, String user, List<StratificationFactorBean> stratificationFactorBeans, EventCRFBean eventCrfBean, RuleSetBean ruleSet) {
// method : POST
int i = 1;
String exp = "";
randomiseUrl = randomiseUrl + "/api/randomise";
RestTemplate rest = new RestTemplate(requestFactory);
ResponseEntity<String> response = null;
MultiValueMap<String, String> subjectMap = new LinkedMultiValueMap<String, String>();
subjectMap.add("identifier", String.valueOf(studySubject.getOid()));
subjectMap.add("siteIdentifier", studyBean.getOid());
subjectMap.add("user", user);
for (StratificationFactorBean stratificationFactorBean : stratificationFactorBeans) {
i++;
exp = stratificationFactorBean.getStratificationFactor().getValue();
if (exp.startsWith("SS.")) {
subjectMap.add("question" + i, getStudySubjectAttrValue(exp, eventCrfBean, ruleSet));
} else {
String output = getExpressionValue(exp, eventCrfBean, ruleSet);
subjectMap.add("question" + i, output);
}
}
String body = null;
JSONObject jsonObject = null;
HttpEntity<MultiValueMap<String, String>> subjectRequest = new HttpEntity<MultiValueMap<String, String>>(subjectMap, headers);
try {
response = rest.exchange(randomiseUrl, HttpMethod.POST, subjectRequest, String.class);
body = response.getBody();
jsonObject = new JSONObject(body);
} catch (Exception e) {
System.out.println(e.getMessage());
logger.error(e.getMessage());
System.out.println(ExceptionUtils.getStackTrace(e));
logger.error(ExceptionUtils.getStackTrace(e));
}
return jsonObject;
}
Aggregations