package ch.hsr.sa.eai.sandbox.server.rest;

import java.util.ArrayList;
import java.util.List;

import org.apache.camel.Endpoint;
import org.apache.camel.Exchange;
import org.apache.camel.Route;
import org.springframework.stereotype.Component;

import ch.hsr.sa.eai.sandbox.server.rest.api.Job;
import ch.hsr.sa.eai.sandbox.server.rest.api.Jobs;

@Component(value = "restRouteManagement")
public class RestRouteManagement {

	private static final String ROUTE_PREFIX = "vm://trigger-";

	public Jobs process(Exchange exchange) {
		List<Route> routes = exchange.getContext().getRoutes();
		List<Job> result = new ArrayList<>();
		for (Route route : routes) {
			Endpoint endpoint = route.getEndpoint();
			String endpointUri = endpoint.getEndpointUri();
			String jobName = route.getId();
			String uri = (String) exchange.getIn().getHeader("CamelHttpUrl") + "/" + jobName;
			if (endpointUri.startsWith(ROUTE_PREFIX)) {
//				 jobName = endpointUri.replace(ROUTE_PREFIX, "");
				result.add(new Job(jobName, uri));
			} else if(endpointUri.startsWith("file://")) {
				result.add(new Job(jobName, uri));
			}
		}
		return new Jobs(result);
	}

}

@@ -873,16 +873,122 @@
 ttpUrl%22)
+;%0D%0A%09%09%09// add a slash if not provided by the incoming uri%0D%0A%09%09%09uri = uri.endsWith(%22/%22) ? uri + jobName : uri
  + %22/%22 +
@@ -1050,14 +1050,14 @@
  %7B%0D%0A
-//
 %09%09%09%09
+//
  job
@@ -1155,16 +1155,17 @@
  else if
+ 
 (endpoin

add slash to uri route sonly if necessary