Search in sources :

Example 1 with SiScolRestException

use of fr.univlorraine.ecandidat.services.siscol.SiScolRestUtils.SiScolRestException in project esup-ecandidat by EsupPortail.

the class SiScolRestServiceInterface method getFile.

/**
 * @param  url
 * @param  service
 * @param  mapGetParameter
 * @return                     l'input stream de la piece
 * @throws SiScolRestException
 * @throws SiScolException
 */
public InputStream getFile(final String url, final String service, final MultiValueMap<String, String> mapGetParameter, final KeyValue header) throws SiScolRestException, SiScolException {
    try {
        final URI targetUrl = SiScolRestUtils.getURIForService(url, service, mapGetParameter);
        final RestTemplate restTemplate = new RestTemplate();
        restTemplate.setErrorHandler(new SiScolResponseErrorHandler());
        restTemplate.getMessageConverters().add(new ResourceHttpMessageConverter());
        final HttpHeaders headers = new HttpHeaders();
        headers.setAccept(Arrays.asList(MediaType.APPLICATION_OCTET_STREAM));
        if (header != null && header.isNotEmpty()) {
            headers.set(header.getKey(), header.getValue());
        }
        final HttpEntity<String> entity = new HttpEntity<>(headers);
        final ResponseEntity<Resource> response = restTemplate.exchange(targetUrl, HttpMethod.GET, entity, Resource.class);
        return response.getBody().getInputStream();
    } catch (final SiScolRestException e) {
        throw e;
    } catch (final Exception e) {
        throw new SiScolException("Erreur a l'appel du WS Rest des PJ", e);
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) Resource(org.springframework.core.io.Resource) URI(java.net.URI) SiScolRestException(fr.univlorraine.ecandidat.services.siscol.SiScolRestUtils.SiScolRestException) SiScolRestException(fr.univlorraine.ecandidat.services.siscol.SiScolRestUtils.SiScolRestException) RestTemplate(org.springframework.web.client.RestTemplate) ResourceHttpMessageConverter(org.springframework.http.converter.ResourceHttpMessageConverter) SiScolResponseErrorHandler(fr.univlorraine.ecandidat.services.siscol.SiScolRestUtils.SiScolResponseErrorHandler)

Example 2 with SiScolRestException

use of fr.univlorraine.ecandidat.services.siscol.SiScolRestUtils.SiScolRestException in project esup-ecandidat by EsupPortail.

the class SiScolRestServiceInterface method getList.

/**
 * @param  url
 * @param  service
 * @param  klass
 * @param  mapGetParameter
 * @return                 une liste d'objets pour un service donné
 * @throws SiScolException
 */
public <T> List<T> getList(final String url, final String service, final Class<T[]> klass, final MultiValueMap<String, String> mapGetParameter, final KeyValue header) throws SiScolRestException, SiScolException {
    try {
        final URI targetUrl = SiScolRestUtils.getURIForService(url, service, mapGetParameter);
        final RestTemplate restTemplate = new RestTemplate();
        restTemplate.setErrorHandler(new SiScolResponseErrorHandler());
        final HttpHeaders headers = new HttpHeaders();
        if (header != null && header.isNotEmpty()) {
            headers.set(header.getKey(), header.getValue());
        }
        final ResponseEntity<T[]> response = restTemplate.exchange(targetUrl, HttpMethod.GET, new HttpEntity<T[]>(headers), klass);
        final List<T> liste = Arrays.asList(response.getBody());
        return liste;
    } catch (final SiScolRestException e) {
        throw e;
    } catch (final Exception e) {
        throw new SiScolException("Erreur a l'appel du WS Rest des PJ", e);
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) RestTemplate(org.springframework.web.client.RestTemplate) SiScolResponseErrorHandler(fr.univlorraine.ecandidat.services.siscol.SiScolRestUtils.SiScolResponseErrorHandler) URI(java.net.URI) SiScolRestException(fr.univlorraine.ecandidat.services.siscol.SiScolRestUtils.SiScolRestException) SiScolRestException(fr.univlorraine.ecandidat.services.siscol.SiScolRestUtils.SiScolRestException)

Aggregations

SiScolResponseErrorHandler (fr.univlorraine.ecandidat.services.siscol.SiScolRestUtils.SiScolResponseErrorHandler)2 SiScolRestException (fr.univlorraine.ecandidat.services.siscol.SiScolRestUtils.SiScolRestException)2 URI (java.net.URI)2 HttpHeaders (org.springframework.http.HttpHeaders)2 RestTemplate (org.springframework.web.client.RestTemplate)2 Resource (org.springframework.core.io.Resource)1 HttpEntity (org.springframework.http.HttpEntity)1 ResourceHttpMessageConverter (org.springframework.http.converter.ResourceHttpMessageConverter)1