Search in sources :

Example 56 with HttpEntity

use of org.springframework.http.HttpEntity in project OpenClinica by OpenClinica.

the class RandomizeService method addOrUpdateASite.

private void addOrUpdateASite(String randomiseUrl, StudyBean studyBean, HttpHeaders headers, String timezone) {
    // mehtod : POST
    randomiseUrl = randomiseUrl + "/api/sites";
    RestTemplate rest = new RestTemplate(requestFactory);
    ResponseEntity<String> response = null;
    MultiValueMap<String, String> siteMap = new LinkedMultiValueMap<String, String>();
    siteMap.add("siteIdentifier", studyBean.getOid());
    siteMap.add("name", studyBean.getName());
    siteMap.add("timezone", timezone);
    HttpEntity<MultiValueMap<String, String>> siteRequest = new HttpEntity<MultiValueMap<String, String>>(siteMap, headers);
    try {
        response = rest.exchange(randomiseUrl, HttpMethod.POST, siteRequest, String.class);
    } catch (Exception e) {
        logger.error(e.getMessage());
        logger.error(ExceptionUtils.getStackTrace(e));
    }
}
Also used : HttpEntity(org.springframework.http.HttpEntity) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) RestTemplate(org.springframework.web.client.RestTemplate) MultiValueMap(org.springframework.util.MultiValueMap) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) JSONException(org.json.JSONException)

Example 57 with HttpEntity

use of org.springframework.http.HttpEntity in project OpenClinica by OpenClinica.

the class XformMetaDataService method saveAttachedFiles.

public void saveAttachedFiles(String uri, String dir, String fileName) throws IOException {
    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(uri, HttpMethod.GET, entity, byte[].class, "1");
    if (response.getStatusCode().equals(HttpStatus.OK)) {
        FileOutputStream output = new FileOutputStream(new File(dir + File.separator + fileName));
        IOUtils.write(response.getBody(), output);
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) FileOutputStream(java.io.FileOutputStream) RestTemplate(org.springframework.web.client.RestTemplate) ByteArrayHttpMessageConverter(org.springframework.http.converter.ByteArrayHttpMessageConverter) File(java.io.File)

Example 58 with HttpEntity

use of org.springframework.http.HttpEntity in project disconf by knightliao.

the class ConfigFetcherController method downloadDspBill.

/**
     * 下载
     *
     * @param fileName
     *
     * @return
     */
public HttpEntity<byte[]> downloadDspBill(String fileName, String value) {
    HttpHeaders header = new HttpHeaders();
    byte[] res = value.getBytes();
    String name = null;
    try {
        name = URLEncoder.encode(fileName, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    header.set("Content-Disposition", "attachment; filename=" + name);
    header.setContentLength(res.length);
    return new HttpEntity<byte[]>(res, header);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 59 with HttpEntity

use of org.springframework.http.HttpEntity in project disconf by knightliao.

the class ConfigReadController method downloadDspBill.

/**
     * 下载
     *
     * @param configId
     *
     * @return
     */
@RequestMapping(value = "/download/{configId}", method = RequestMethod.GET)
public HttpEntity<byte[]> downloadDspBill(@PathVariable long configId) {
    // 业务校验
    configValidator.valideConfigExist(configId);
    ConfListVo config = configMgr.getConfVo(configId);
    HttpHeaders header = new HttpHeaders();
    byte[] res = config.getValue().getBytes();
    if (res == null) {
        throw new DocumentNotFoundException(config.getKey());
    }
    String name = null;
    try {
        name = URLEncoder.encode(config.getKey(), "UTF-8");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    header.set("Content-Disposition", "attachment; filename=" + name);
    header.setContentLength(res.length);
    return new HttpEntity<byte[]>(res, header);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ConfListVo(com.baidu.disconf.web.service.config.vo.ConfListVo) DocumentNotFoundException(com.baidu.dsp.common.exception.DocumentNotFoundException) HttpEntity(org.springframework.http.HttpEntity) UnsupportedEncodingException(java.io.UnsupportedEncodingException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 60 with HttpEntity

use of org.springframework.http.HttpEntity in project disconf by knightliao.

the class ConfigReadController method download2.

/**
     * 批量下载配置文件
     *
     * @param confListForm
     *
     * @return
     */
@RequestMapping(value = "/downloadfilebatch", method = RequestMethod.GET)
public HttpEntity<byte[]> download2(@Valid ConfListForm confListForm) {
    LOG.info(confListForm.toString());
    //
    // get files
    //
    List<File> fileList = configMgr.getDisconfFileList(confListForm);
    //
    // prefix
    //
    String prefixString = "APP" + confListForm.getAppId() + "_" + "ENV" + confListForm.getEnvId() + "_" + "VERSION" + confListForm.getVersion();
    HttpHeaders header = new HttpHeaders();
    String targetFileString = "";
    File targetFile = null;
    byte[] res = null;
    try {
        targetFileString = TarUtils.tarFiles("tmp", prefixString, fileList);
        targetFile = new File(targetFileString);
        res = IOUtils.toByteArray(new FileInputStream(targetFile));
    } catch (Exception e) {
        throw new DocumentNotFoundException("");
    }
    header.set("Content-Disposition", "attachment; filename=" + targetFile.getName());
    header.setContentLength(res.length);
    return new HttpEntity<byte[]>(res, header);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) DocumentNotFoundException(com.baidu.dsp.common.exception.DocumentNotFoundException) HttpEntity(org.springframework.http.HttpEntity) File(java.io.File) FileInputStream(java.io.FileInputStream) DocumentNotFoundException(com.baidu.dsp.common.exception.DocumentNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

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