Search in sources :

Example 41 with RestTemplate

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;
}
Also used : JSONObject(org.json.JSONObject) HttpEntity(org.springframework.http.HttpEntity) RestTemplate(org.springframework.web.client.RestTemplate) JSONException(org.json.JSONException)

Example 42 with RestTemplate

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;
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) FileItem(org.apache.commons.fileupload.FileItem) DiskFileItem(org.apache.commons.fileupload.disk.DiskFileItem) HttpEntity(org.springframework.http.HttpEntity) FileOutputStream(java.io.FileOutputStream) RestTemplate(org.springframework.web.client.RestTemplate) ByteArrayHttpMessageConverter(org.springframework.http.converter.ByteArrayHttpMessageConverter) IOException(java.io.IOException) File(java.io.File) DiskFileItem(org.apache.commons.fileupload.disk.DiskFileItem)

Example 43 with RestTemplate

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);
                    }
                }
            }
        }
    }
}
Also used : OCodmComplexTypeDefinitionFormLayoutDef(org.openclinica.ns.odm_ext_v130.v31.OCodmComplexTypeDefinitionFormLayoutDef) XformGroup(org.akaza.openclinica.domain.xform.XformGroup) IOException(java.io.IOException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) XformContainer(org.akaza.openclinica.domain.xform.XformContainer) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) RestTemplate(org.springframework.web.client.RestTemplate) List(java.util.List) ArrayList(java.util.ArrayList) TypeReference(com.fasterxml.jackson.core.type.TypeReference) FormVersion(org.akaza.openclinica.service.dto.FormVersion) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 44 with RestTemplate

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;
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) RestTemplate(org.springframework.web.client.RestTemplate)

Example 45 with RestTemplate

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;
}
Also used : RestTemplate(org.springframework.web.client.RestTemplate) HttpComponentsClientHttpRequestFactory(org.springframework.http.client.HttpComponentsClientHttpRequestFactory) MessagingException(javax.mail.MessagingException) OpenClinicaSystemException(org.akaza.openclinica.exception.OpenClinicaSystemException) MailException(org.springframework.mail.MailException)

Aggregations

RestTemplate (org.springframework.web.client.RestTemplate)206 Test (org.junit.Test)107 URI (java.net.URI)39 HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)29 ArrayList (java.util.ArrayList)24 HashMap (java.util.HashMap)22 HttpComponentsClientHttpRequestFactory (org.springframework.http.client.HttpComponentsClientHttpRequestFactory)22 HttpHeaders (org.springframework.http.HttpHeaders)19 HttpEntity (org.springframework.http.HttpEntity)16 MapPropertySource (org.springframework.core.env.MapPropertySource)15 StandardEnvironment (org.springframework.core.env.StandardEnvironment)15 StringHttpMessageConverter (org.springframework.http.converter.StringHttpMessageConverter)15 AnnotationConfigWebApplicationContext (org.springframework.web.context.support.AnnotationConfigWebApplicationContext)15 SerDeHttpMessageConverter (com.kixeye.chassis.transport.http.SerDeHttpMessageConverter)13 MessageSerDe (com.kixeye.chassis.transport.serde.MessageSerDe)13 IOException (java.io.IOException)12 JSONObject (org.json.JSONObject)12 Before (org.junit.Before)11 ClientHttpResponse (org.springframework.http.client.ClientHttpResponse)11 MappingJackson2HttpMessageConverter (org.springframework.http.converter.json.MappingJackson2HttpMessageConverter)11