| package org.eclipse.stem.cloud.controller; |
| |
| import com.fasterxml.jackson.databind.ObjectMapper; |
| import com.netflix.appinfo.InstanceInfo; |
| import com.netflix.discovery.EurekaClient; |
| import org.apache.http.HttpEntity; |
| import org.apache.http.client.methods.CloseableHttpResponse; |
| import org.apache.http.client.methods.HttpGet; |
| import org.apache.http.impl.client.CloseableHttpClient; |
| import org.apache.http.impl.client.HttpClients; |
| import org.eclipse.stem.cloud.model.StemProject; |
| import org.eclipse.stem.cloud.repository.StemProjectRepository; |
| import org.springframework.beans.factory.annotation.Autowired; |
| import org.springframework.beans.factory.annotation.Value; |
| import org.springframework.context.annotation.Lazy; |
| import org.springframework.http.HttpStatus; |
| import org.springframework.web.bind.annotation.GetMapping; |
| import org.springframework.web.bind.annotation.RequestParam; |
| import org.springframework.web.bind.annotation.ResponseStatus; |
| import org.springframework.web.bind.annotation.RestController; |
| import org.springframework.web.client.RestTemplate; |
| import org.slf4j.Logger; |
| import org.slf4j.LoggerFactory; |
| |
| import java.io.IOException; |
| import java.io.OutputStream; |
| import java.util.List; |
| |
| @RestController |
| public class ModelController { |
| private static final Logger log = LoggerFactory.getLogger(ModelController.class); |
| @Autowired |
| @Lazy |
| private EurekaClient eurekaClient; |
| |
| @Autowired |
| RestTemplate restTemplate; |
| @Autowired |
| StemProjectRepository stemProjectRepo; |
| |
| |
| @Value("${spring.application.name}") |
| private String appName; |
| |
| @GetMapping(value = "/core/cloud/scenarios", produces = "application/json") |
| @ResponseStatus(value = HttpStatus.OK) |
| public void scanScenarios(OutputStream out) throws IOException { |
| coreCall("engine/cloud/models?treetype=Scenario",out); |
| } |
| @GetMapping(value = "/core/cloud/graphs", produces = "application/json") |
| @ResponseStatus(value = HttpStatus.OK) |
| public void scanGraphs(OutputStream out) throws IOException { |
| coreCall("engine/cloud/models?treetype=graph",out); |
| } |
| @GetMapping(value = "/core/cloud/models", produces = "application/json") |
| @ResponseStatus(value = HttpStatus.OK) |
| public void scanModels(OutputStream out) throws IOException { |
| coreCall("engine/cloud/models?treetype=model",out); |
| } |
| |
| @GetMapping(value = "/core/cloud/predicate", produces = "application/json") |
| @ResponseStatus(value = HttpStatus.OK) |
| public void scanPredicate(OutputStream out) throws IOException { |
| coreCall("engine/cloud/models?treetype=predicate",out); |
| } |
| @GetMapping(value = "/core/cloud/sequencer", produces = "application/json") |
| @ResponseStatus(value = HttpStatus.OK) |
| public void scanSequencer(OutputStream out) throws IOException { |
| coreCall("engine/cloud/models?treetype=sequencer",out); |
| } |
| @GetMapping(value = "/core/cloud/trigger", produces = "application/json") |
| @ResponseStatus(value = HttpStatus.OK) |
| public void scanTrigger(OutputStream out) throws IOException { |
| coreCall("engine/cloud/models?treetype=trigger",out); |
| } |
| @GetMapping(value = "/core/cloud/experiments", produces = "application/json") |
| @ResponseStatus(value = HttpStatus.OK) |
| public void scanExperiments(OutputStream out) throws IOException { |
| coreCall("engine/cloud/models?treetype=experiments",out); |
| } |
| @GetMapping(value = "/core/cloud/stemprojects", produces = "application/json") |
| @ResponseStatus(value = HttpStatus.OK) |
| public void stemprojects(OutputStream out) throws IOException { |
| List<StemProject> stemProject= stemProjectRepo.findAll(); |
| String response = valueAsString(stemProject); |
| out.write(response.getBytes()); |
| out.flush(); |
| } |
| @GetMapping(value = "/core/cloud/newproject", produces = "application/json") |
| @ResponseStatus(value = HttpStatus.OK) |
| public void newproject(OutputStream out, @RequestParam("name") String name) throws IOException { |
| StemProject project = new StemProject(); |
| project.setUserName("Ahmad"); |
| String dirs[] = {"Models","Scenarios","Sequences"}; |
| project.setSubDirectories(dirs); |
| stemProjectRepo.save(project); |
| coreCall("engine/cloud/models?treetype=newproject&projectName="+name,out); |
| } |
| |
| public String valueAsString(Object object) { |
| // Creating Object of ObjectMapper define in Jakson Api |
| ObjectMapper Obj = new ObjectMapper(); |
| String jsonStr = ""; |
| |
| try { |
| // get Oraganisation object as a json string |
| jsonStr = Obj.writeValueAsString(object); |
| } |
| |
| catch (IOException e) { |
| e.printStackTrace(); |
| } |
| return jsonStr; |
| } |
| private String serverIPAddr(){ |
| eurekaClient.getApplication(appName).shuffleAndStoreInstances(true); |
| InstanceInfo instance = eurekaClient.getApplication(appName).getInstances().get(0); |
| return instance.getHomePageUrl(); |
| } |
| private String prepareFullPath(String subPath){ |
| return serverIPAddr()+subPath; |
| } |
| |
| private void coreCall(String subPath, OutputStream out) throws IOException { |
| String url =prepareFullPath(subPath); |
| CloseableHttpClient httpclient = HttpClients.createDefault(); |
| HttpGet httpGet = new HttpGet(url); |
| CloseableHttpResponse response = httpclient.execute(httpGet); |
| try { |
| HttpEntity entity = response.getEntity(); |
| // Return its response |
| entity.writeTo(out); |
| } finally { |
| response.close(); |
| } |
| } |
| |
| |
| } |