package eu.freme.broker.eservices;

import com.mashape.unirest.http.exceptions.UnirestException;
import eu.freme.conversion.rdf.RDFConstants;
import eu.freme.eservices.pipelines.core.PipelineService;
import eu.freme.eservices.pipelines.core.ServiceException;
import eu.freme.eservices.pipelines.requests.RequestBuilder;
import eu.freme.eservices.pipelines.requests.RequestFactory;
import eu.freme.eservices.pipelines.requests.SerializedRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import eu.freme.eservices.pipelines.core.PipelineResponse;

import java.io.IOException;
import java.util.List;

/**
 * @author Gerald Haesendonck
 */
@RestController
@SuppressWarnings("unused")
public class Pipelines {

	@Autowired
	PipelineService pipelineAPI;

	/**
	 * <p>Calls the pipelining service.</p>
	 * <p>Some predefined Requests can be formed using the class {@link RequestFactory}. It also converts request objects
	 * from and to JSON.</p>
	 * <p><To create custom requests, use the {@link RequestBuilder}.</p>
	 * <p>Examples can be found in the unit tests in the Pipelines repository.</p>
	 * @param requests	The requests to send to the service.
	 * @return          The response of the last request.
	 * @throws IOException
	 * @throws UnirestException
	 */
	@RequestMapping(value = "/pipelining/chain", method = RequestMethod.POST)
	public ResponseEntity<String> pipeline(@RequestBody String requests) throws IOException, UnirestException {
		List<SerializedRequest> serializedRequests = RequestFactory.fromJson(requests);
		try {
			PipelineResponse pipelineResult = pipelineAPI.chain(serializedRequests);
			MultiValueMap<String, String> headers = new HttpHeaders();
			headers.add(HttpHeaders.CONTENT_TYPE, pipelineResult.getContentType());
			return new ResponseEntity<>(pipelineResult.getBody(), headers, HttpStatus.OK);
		} catch (ServiceException serviceError) {
			MultiValueMap<String, String> headers = new HttpHeaders();
			headers.add(HttpHeaders.CONTENT_TYPE, serviceError.getResponse().getContentType());
			return new ResponseEntity<>(serviceError.getMessage(), headers, serviceError.getStatus());
		}
	}
}
@@ -40,105 +40,62 @@
 ort 
-com.mashape.unirest.http.exceptions.UnirestException;%0Aimport eu.freme.conversion.rdf.RDFConstants
+eu.freme.broker.exception.InternalServerErrorException
 ;%0Aim
@@ -954,36 +954,8 @@
 e;%0A%0A
-import java.io.IOException;%0A
 impo
@@ -1596,47 +1596,82 @@
 ws I
-OException%0A%09 * @throws UnirestException
+nternalServerErrorException%09%09Something goes wrong that shouldn't go wrong.
 %0A%09 *
@@ -1820,45 +1820,8 @@
 sts)
- throws IOException, UnirestException
  %7B%0A%09
@@ -2491,16 +2491,164 @@
 tus());%0A
+%09%09%7D catch (Throwable t) %7B%0A%09%09%09// throw a FREME exception if anything goes really wrong...%0A%09%09%09throw new InternalServerErrorException(t.getMessage());%0A
 %09%09%7D%0A%09%7D%0A%7D

Throw an InternalServerErrorException as a last resort.