Search in sources :

Example 51 with HttpEntity

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;
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) URI(java.net.URI) MultiValueMap(org.springframework.util.MultiValueMap) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap)

Example 52 with HttpEntity

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;
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 53 with HttpEntity

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);
    }
}
Also used : UserStudy(com.odysseusinc.arachne.portal.model.UserStudy) Study(com.odysseusinc.arachne.portal.model.Study) FavouriteStudy(com.odysseusinc.arachne.portal.model.FavouriteStudy) HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) StudyFile(com.odysseusinc.arachne.portal.model.StudyFile) IOException(java.io.IOException) URL(java.net.URL) Date(java.util.Date) IORuntimeException(com.odysseusinc.arachne.portal.exception.IORuntimeException) IORuntimeException(com.odysseusinc.arachne.portal.exception.IORuntimeException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 54 with HttpEntity

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;
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) ArrayList(java.util.ArrayList) ByteArrayResource(org.springframework.core.io.ByteArrayResource) FileItem(org.apache.commons.fileupload.FileItem) FormArtifactTransferObj(org.akaza.openclinica.service.crfdata.FormArtifactTransferObj) RestTemplate(org.springframework.web.client.RestTemplate) ExecuteIndividualCrfObject(org.akaza.openclinica.service.crfdata.ExecuteIndividualCrfObject)

Example 55 with HttpEntity

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;
}
Also used : JSONObject(org.json.JSONObject) HttpEntity(org.springframework.http.HttpEntity) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) StratificationFactorBean(org.akaza.openclinica.domain.rule.action.StratificationFactorBean) RestTemplate(org.springframework.web.client.RestTemplate) MultiValueMap(org.springframework.util.MultiValueMap) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) JSONException(org.json.JSONException)

Aggregations

HttpEntity (org.springframework.http.HttpEntity)104 HttpHeaders (org.springframework.http.HttpHeaders)81 Test (org.junit.Test)46 RestTemplate (org.springframework.web.client.RestTemplate)17 URI (java.net.URI)15 ArrayList (java.util.ArrayList)13 ResponseEntity (org.springframework.http.ResponseEntity)12 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)12 Map (java.util.Map)11 MediaType (org.springframework.http.MediaType)11 HashMap (java.util.HashMap)10 List (java.util.List)10 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)10 MultiValueMap (org.springframework.util.MultiValueMap)9 ByteArrayInputStream (java.io.ByteArrayInputStream)7 ByteArrayHttpMessageConverter (org.springframework.http.converter.ByteArrayHttpMessageConverter)7 MappingJackson2HttpMessageConverter (org.springframework.http.converter.json.MappingJackson2HttpMessageConverter)7 HttpStatus (org.springframework.http.HttpStatus)6 HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)6 StringHttpMessageConverter (org.springframework.http.converter.StringHttpMessageConverter)6