|
| 1 | +/* |
| 2 | + * Copyright (c) 2015 General Electric Company. All rights reserved. |
| 3 | + * |
| 4 | + * The copyright to the computer software herein is the property of |
| 5 | + * General Electric Company. The software may be used and/or copied only |
| 6 | + * with the written permission of General Electric Company or in accordance |
| 7 | + * with the terms and conditions stipulated in the agreement/contract |
| 8 | + * under which the software has been supplied. |
| 9 | + */ |
| 10 | +package com.ge.predix.demo; |
| 11 | + |
| 12 | +import org.springframework.http.HttpStatus; |
| 13 | +import org.springframework.http.ResponseEntity; |
| 14 | +import org.springframework.web.bind.annotation.RequestBody; |
| 15 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 16 | +import org.springframework.web.bind.annotation.RequestMethod; |
| 17 | +import org.springframework.web.bind.annotation.RestController; |
| 18 | + |
| 19 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | + |
| 21 | +/** |
| 22 | + * REST API with the URL of api/v1/demoadder/execution where |
| 23 | + * "api" is the URL prefix expected by Predix Analytics |
| 24 | + * "v1" is the version of the analytic defined in the analytic catalog |
| 25 | + * "demoadder" is the name of the analytic defined in the analytic catalog |
| 26 | + * "execution" is the REST resource name expected by Predix Analytics |
| 27 | + * |
| 28 | + * Note: The version of the analytic defined in the analytic catalog is transformed into URL friendly name by |
| 29 | + * - Substituting dots '.' with underscores '_' |
| 30 | + * - Converting to all lower cases |
| 31 | + * |
| 32 | + * Note: The name of the analytic defined in the analytic catalog is transformed into URL friendly name by |
| 33 | + * - Substituting spaces ' ' with underscores '_' |
| 34 | + * - Converting to all lower cases |
| 35 | + * |
| 36 | + * For example, suppose you defined the following analytic in the analytic catalog |
| 37 | + * Name: Anomaly Detection |
| 38 | + * Version: v1.0 |
| 39 | + * Then define the REST URL as api/v1_0/anomaly_detection/execution |
| 40 | + */ |
| 41 | +@RestController |
| 42 | +@RequestMapping(value = DemoAnalyticService.ANALYTIC_REST_URL) |
| 43 | +public class DemoAnalyticService { |
| 44 | + |
| 45 | + public static final String ANALYTIC_NAME = "demo_adder_java"; |
| 46 | + public static final String ANALYTIC_VERSION = "v1"; |
| 47 | + public static final String ANALYTIC_REST_URL_PREFIX = "api"; |
| 48 | + public static final String ANALYTIC_REST_RESOURCE = "execution"; |
| 49 | + public static final String ANALYTIC_REST_URL = ANALYTIC_REST_URL_PREFIX + "/" + ANALYTIC_VERSION + "/" + ANALYTIC_NAME; |
| 50 | + |
| 51 | + // api/v1/demo_analytic_java/execution |
| 52 | + public static final String ANALYTIC_REST_RESOURCE_URL = ANALYTIC_REST_URL + "/" + ANALYTIC_REST_RESOURCE; |
| 53 | + |
| 54 | + @RequestMapping(value = ANALYTIC_REST_RESOURCE, method = RequestMethod.POST) |
| 55 | + public ResponseEntity<String> execute(@RequestBody String input) { |
| 56 | + try { |
| 57 | + return new ResponseEntity<>(run(input), HttpStatus.OK); |
| 58 | + } catch (Exception ex) { |
| 59 | + throw new RuntimeException(ex); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + public String run(String input) throws Exception { |
| 64 | + ObjectMapper mapper = new ObjectMapper(); |
| 65 | + DemoAnalyticRequest request = mapper.readValue(input, DemoAnalyticRequest.class); |
| 66 | + DemoAnalyticResponse response = new DemoAnalyticResponse(); |
| 67 | + response.setResult(request.getNumber1() + request.getNumber2()); |
| 68 | + return mapper.writeValueAsString(response); |
| 69 | + } |
| 70 | + |
| 71 | +} |
0 commit comments