├── src ├── test │ └── java │ │ └── com │ │ └── zhongan │ │ └── tech │ │ └── submarineevents │ │ └── Tests.java └── main │ ├── java │ └── com │ │ └── zhongan │ │ └── tech │ │ └── submarineevents │ │ ├── Application.java │ │ ├── enums │ │ └── ResultCode.java │ │ ├── consumer │ │ ├── RestAPIEventConsumer.java │ │ └── RestAPIEventConsumerImpl.java │ │ ├── exception │ │ └── EventException.java │ │ ├── vo │ │ ├── BaseBean.java │ │ └── BaseResult.java │ │ ├── dto │ │ ├── RestAPIEvent.java │ │ └── EventAdapterRule.java │ │ ├── config │ │ ├── RestTemplateConfig.java │ │ ├── EventAdapterRuleMapConfig.java │ │ └── EventAdapterRules.java │ │ ├── adapter │ │ ├── EventAdapter.java │ │ └── EventAdapterImpl.java │ │ ├── demo │ │ └── TargetUriDemo.java │ │ ├── utils │ │ ├── RestTemplateManager.java │ │ └── JSONUtil.java │ │ ├── stream │ │ └── handler │ │ │ └── EventHandler.java │ │ └── web │ │ └── EventController.java │ └── resources │ ├── bootstrap.yml │ └── logback.xml ├── pom.xml ├── README.md └── LICENSE /src/test/java/com/zhongan/tech/submarineevents/Tests.java: -------------------------------------------------------------------------------- 1 | package com.zhongan.tech.submarineevents; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class Tests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/zhongan/tech/submarineevents/Application.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Zhongan.com All right reserved. This software is the 3 | * confidential and proprietary information of Zhongan.com ("Confidential 4 | * Information"). You shall not disclose such Confidential Information and shall 5 | * use it only in accordance with the terms of the license agreement you entered 6 | * into with Zhongan.com. 7 | */ 8 | package com.zhongan.tech.submarineevents; 9 | import org.springframework.boot.SpringApplication; 10 | import org.springframework.boot.autoconfigure.SpringBootApplication; 11 | 12 | @SpringBootApplication 13 | public class Application { 14 | 15 | public static void main(String[] args) { 16 | SpringApplication.run(Application.class, args); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/zhongan/tech/submarineevents/enums/ResultCode.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Zhongan.com All right reserved. This software is the 3 | * confidential and proprietary information of Zhongan.com ("Confidential 4 | * Information"). You shall not disclose such Confidential Information and shall 5 | * use it only in accordance with the terms of the license agreement you entered 6 | * into with Zhongan.com. 7 | */ 8 | package com.zhongan.tech.submarineevents.enums; 9 | 10 | import lombok.AllArgsConstructor; 11 | import lombok.Getter; 12 | 13 | import java.io.Serializable; 14 | 15 | @Getter 16 | @AllArgsConstructor 17 | public enum ResultCode implements Serializable { 18 | 19 | SUCCESS("200", "Success"), 20 | 21 | EVENT_ADAPTER_RULE_NOT_EXIST("1001","Event adapter rule doesn't exist"); 22 | 23 | private String code; 24 | 25 | private String message; 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/zhongan/tech/submarineevents/consumer/RestAPIEventConsumer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Zhongan.com All right reserved. This software is the 3 | * confidential and proprietary information of Zhongan.com ("Confidential 4 | * Information"). You shall not disclose such Confidential Information and shall 5 | * use it only in accordance with the terms of the license agreement you entered 6 | * into with Zhongan.com. 7 | */ 8 | package com.zhongan.tech.submarineevents.consumer; 9 | 10 | import com.alibaba.fastjson.JSONObject; 11 | import com.zhongan.tech.submarineevents.dto.RestAPIEvent; 12 | 13 | public interface RestAPIEventConsumer { 14 | 15 | /** 16 | * @Description Execute http call synchronously for RestAPIEvent object 17 | * @param restAPIEvent RestAPIEvent object 18 | * @return http response body 19 | */ 20 | JSONObject syncConsume(RestAPIEvent restAPIEvent); 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/zhongan/tech/submarineevents/exception/EventException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Zhongan.com All right reserved. This software is the 3 | * confidential and proprietary information of Zhongan.com ("Confidential 4 | * Information"). You shall not disclose such Confidential Information and shall 5 | * use it only in accordance with the terms of the license agreement you entered 6 | * into with Zhongan.com. 7 | */ 8 | package com.zhongan.tech.submarineevents.exception; 9 | 10 | import com.zhongan.tech.submarineevents.enums.ResultCode; 11 | 12 | import lombok.Data; 13 | 14 | @Data 15 | public class EventException extends Exception { 16 | 17 | private ResultCode resultCode; 18 | 19 | private static final long serialVersionUID = 580665819760410455L; 20 | 21 | public EventException(ResultCode resultCode) { 22 | super(resultCode.getMessage()); 23 | this.resultCode = resultCode; 24 | } 25 | 26 | @Override 27 | public String toString() { 28 | return "EventException [ errCode is " + this.resultCode.getCode() + ", errMsg is " + this.resultCode.getMessage() + "]"; 29 | } 30 | 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/zhongan/tech/submarineevents/vo/BaseBean.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Zhongan.com All right reserved. This software is the 3 | * confidential and proprietary information of Zhongan.com ("Confidential 4 | * Information"). You shall not disclose such Confidential Information and shall 5 | * use it only in accordance with the terms of the license agreement you entered 6 | * into with Zhongan.com. 7 | */ 8 | package com.zhongan.tech.submarineevents.vo; 9 | 10 | import java.io.Serializable; 11 | import java.util.UUID; 12 | 13 | import org.apache.commons.lang3.SerializationUtils; 14 | 15 | public class BaseBean implements Serializable, Cloneable { 16 | 17 | private static final long serialVersionUID = -265741933689599525L; 18 | 19 | private String requestId = UUID.randomUUID().toString().replace("-", ""); 20 | 21 | public String getRequestId() { 22 | return requestId; 23 | } 24 | 25 | public void setRequestId(String requestId) { 26 | this.requestId = requestId; 27 | } 28 | 29 | @Override 30 | public Object clone() { 31 | return SerializationUtils.clone(this); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/com/zhongan/tech/submarineevents/dto/RestAPIEvent.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Zhongan.com All right reserved. This software is the 3 | * confidential and proprietary information of Zhongan.com ("Confidential 4 | * Information"). You shall not disclose such Confidential Information and shall 5 | * use it only in accordance with the terms of the license agreement you entered 6 | * into with Zhongan.com. 7 | */ 8 | package com.zhongan.tech.submarineevents.dto; 9 | 10 | import java.net.URI; 11 | 12 | import com.alibaba.fastjson.JSONObject; 13 | 14 | import lombok.Data; 15 | 16 | import org.springframework.http.HttpMethod; 17 | 18 | @Data 19 | public class RestAPIEvent { 20 | 21 | /* 22 | * cloudEvent type 23 | */ 24 | private String type; 25 | 26 | /* 27 | * cloudEvent id 28 | */ 29 | private String id; 30 | 31 | /* 32 | * cloudEvent source 33 | */ 34 | private URI source; 35 | 36 | /* 37 | * restful http api 38 | */ 39 | private URI target; 40 | 41 | /* 42 | * http method 43 | */ 44 | private HttpMethod httpMethod; 45 | 46 | /* 47 | * request of restful http api 48 | */ 49 | private JSONObject request; 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/com/zhongan/tech/submarineevents/consumer/RestAPIEventConsumerImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Zhongan.com All right reserved. This software is the 3 | * confidential and proprietary information of Zhongan.com ("Confidential 4 | * Information"). You shall not disclose such Confidential Information and shall 5 | * use it only in accordance with the terms of the license agreement you entered 6 | * into with Zhongan.com. 7 | */ 8 | package com.zhongan.tech.submarineevents.consumer; 9 | 10 | import org.springframework.beans.factory.annotation.Autowired; 11 | import org.springframework.stereotype.Service; 12 | 13 | import com.alibaba.fastjson.JSONObject; 14 | import com.zhongan.tech.submarineevents.dto.RestAPIEvent; 15 | import com.zhongan.tech.submarineevents.utils.RestTemplateManager; 16 | 17 | @Service 18 | public class RestAPIEventConsumerImpl implements RestAPIEventConsumer{ 19 | 20 | @Autowired 21 | RestTemplateManager restTemplateManager; 22 | 23 | @Override 24 | public JSONObject syncConsume(RestAPIEvent restAPIEvent) { 25 | return restTemplateManager.call(restAPIEvent.getTarget(), restAPIEvent.getHttpMethod(), restAPIEvent.getRequest()); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/zhongan/tech/submarineevents/dto/EventAdapterRule.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Zhongan.com All right reserved. This software is the 3 | * confidential and proprietary information of Zhongan.com ("Confidential 4 | * Information"). You shall not disclose such Confidential Information and shall 5 | * use it only in accordance with the terms of the license agreement you entered 6 | * into with Zhongan.com. 7 | */ 8 | package com.zhongan.tech.submarineevents.dto; 9 | 10 | import java.net.URI; 11 | import java.util.Map; 12 | 13 | import org.springframework.http.HttpMethod; 14 | 15 | import lombok.Data; 16 | 17 | @Data 18 | public class EventAdapterRule { 19 | 20 | /* 21 | * cloudEvent type 22 | */ 23 | private String type; 24 | 25 | /* 26 | * restful http api 27 | */ 28 | private URI targetUri; 29 | 30 | /* 31 | * http method 32 | */ 33 | private HttpMethod httpMethod; 34 | 35 | /* 36 | * rule map which convert CloudEvent.data to request of restful http api 37 | */ 38 | private Map requestKeyMap; 39 | 40 | /* 41 | * rule map which convert response of restful http api 42 | */ 43 | private Map responseKeyMap; 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/com/zhongan/tech/submarineevents/config/RestTemplateConfig.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Zhongan.com All right reserved. This software is the 3 | * confidential and proprietary information of Zhongan.com ("Confidential 4 | * Information"). You shall not disclose such Confidential Information and shall 5 | * use it only in accordance with the terms of the license agreement you entered 6 | * into with Zhongan.com. 7 | */ 8 | package com.zhongan.tech.submarineevents.config; 9 | 10 | import org.springframework.context.annotation.Bean; 11 | import org.springframework.context.annotation.Configuration; 12 | import org.springframework.http.client.ClientHttpRequestFactory; 13 | import org.springframework.http.client.SimpleClientHttpRequestFactory; 14 | import org.springframework.web.client.RestTemplate; 15 | 16 | @Configuration 17 | public class RestTemplateConfig { 18 | 19 | @Bean 20 | public RestTemplate restTemplate(){ 21 | return new RestTemplate(clientHttpRequestFactory()); 22 | } 23 | 24 | @Bean 25 | public ClientHttpRequestFactory clientHttpRequestFactory(){ 26 | SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory(); 27 | factory.setReadTimeout(20000); 28 | factory.setConnectTimeout(20000); 29 | return factory; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/zhongan/tech/submarineevents/adapter/EventAdapter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Zhongan.com All right reserved. This software is the 3 | * confidential and proprietary information of Zhongan.com ("Confidential 4 | * Information"). You shall not disclose such Confidential Information and shall 5 | * use it only in accordance with the terms of the license agreement you entered 6 | * into with Zhongan.com. 7 | */ 8 | package com.zhongan.tech.submarineevents.adapter; 9 | 10 | import com.alibaba.fastjson.JSONObject; 11 | import com.zhongan.tech.submarineevents.dto.RestAPIEvent; 12 | import com.zhongan.tech.submarineevents.exception.EventException; 13 | 14 | import io.cloudevents.CloudEvent; 15 | 16 | public interface EventAdapter { 17 | 18 | /** 19 | * @Description Convert CloudEvent object to RestAPIEvent object 20 | * @param cloudEvent CloudEvent object 21 | * @return RestAPIEvent object 22 | * @throws convert error 23 | */ 24 | RestAPIEvent convertCloudEvent(CloudEvent cloudEvent) throws EventException; 25 | 26 | /** 27 | * @Description Convert response 28 | * @param type type of event adapter rule 29 | * @param response original response 30 | * @return converted response 31 | * @throws convert error 32 | */ 33 | JSONObject convertResponse(String type, JSONObject response) throws EventException; 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/com/zhongan/tech/submarineevents/demo/TargetUriDemo.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Zhongan.com All right reserved. This software is the 3 | * confidential and proprietary information of Zhongan.com ("Confidential 4 | * Information"). You shall not disclose such Confidential Information and shall 5 | * use it only in accordance with the terms of the license agreement you entered 6 | * into with Zhongan.com. 7 | */ 8 | package com.zhongan.tech.submarineevents.demo; 9 | 10 | import java.util.HashMap; 11 | import java.util.Map; 12 | 13 | import org.springframework.web.bind.annotation.RequestMapping; 14 | import org.springframework.web.bind.annotation.RequestMethod; 15 | import org.springframework.web.bind.annotation.RequestParam; 16 | import org.springframework.web.bind.annotation.RestController; 17 | 18 | @RestController 19 | @RequestMapping("/v1/demo") 20 | public class TargetUriDemo { 21 | 22 | @RequestMapping(value="/toUpperCase", method = RequestMethod.GET) 23 | public Map toUpperCase(@RequestParam("convertedReqName1") String newReqName1, @RequestParam("convertedReqName2") String newReqName2) { 24 | Map demoResponse = new HashMap(); 25 | demoResponse.put("oriRespName1", newReqName1.toUpperCase()); 26 | demoResponse.put("oriRespName2", newReqName2.toUpperCase()); 27 | return demoResponse; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/zhongan/tech/submarineevents/vo/BaseResult.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Zhongan.com All right reserved. This software is the 3 | * confidential and proprietary information of Zhongan.com ("Confidential 4 | * Information"). You shall not disclose such Confidential Information and shall 5 | * use it only in accordance with the terms of the license agreement you entered 6 | * into with Zhongan.com. 7 | */ 8 | package com.zhongan.tech.submarineevents.vo; 9 | 10 | import lombok.Data; 11 | 12 | @Data 13 | public class BaseResult extends BaseBean { 14 | 15 | private static final long serialVersionUID = -6440697160058238736L; 16 | 17 | private boolean success; 18 | 19 | private String code; 20 | 21 | private String msg; 22 | 23 | private T data; 24 | 25 | public BaseResult(boolean success, String code, String msg, T data){ 26 | super(); 27 | this.success = success; 28 | this.code = code; 29 | this.msg = msg; 30 | this.data = data; 31 | } 32 | 33 | public static BaseResult success(String code, String msg, T data) { 34 | BaseResult result = new BaseResult(true, code, msg, data); 35 | return result; 36 | } 37 | 38 | public static BaseResult fail(String code, String msg, T data) { 39 | BaseResult result = new BaseResult(false, code, msg, data); 40 | return result; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/main/resources/bootstrap.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8080 3 | 4 | spring: 5 | application: 6 | name: submarine-events 7 | cloud: 8 | #nacos: 9 | #config: 10 | #server-addr: localhost:80 #your nacos server address 11 | #file-extension: yaml 12 | stream: 13 | default-binder: kafka 14 | kafka: 15 | binder: 16 | brokers: localhost:9092 #your kafka server address 17 | bindings: 18 | input: 19 | destination: my_topic #your topic 20 | group: my_topic_group1 #consumer group of your topic 21 | consumer: 22 | concurrency: 4 #concurrent consumer count, default is 1, less than or equal to partition count 23 | max-attempts: 5 #max retry count, default is 3 24 | function: 25 | definition: restAPIConsume #function to consume message, already implemented in stream/handler/EventHandler.java 26 | 27 | event-adapter-rules: 28 | rule[0]: 29 | type: Demo #event type 30 | targetUri: http://localhost:8080/v1/demo/toUpperCase #RESTful HTTP API where event is routed to, here is a demo API implemented in demo/TargetUriDemo.java 31 | httpMethod: GET #method of RESTful HTTP API 32 | requestKeyMap: '{"oriReqName1": "convertedReqName1", "oriReqName2": "convertedReqName2"}' #request key map, not required 33 | responseKeyMap: '{"oriRespName1": "convertedRespName1", "oriRespName2":"convertedRespName2"}' #response key map, not required 34 | #rule[1]: more rules can be added here 35 | -------------------------------------------------------------------------------- /src/main/java/com/zhongan/tech/submarineevents/config/EventAdapterRuleMapConfig.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Zhongan.com All right reserved. This software is the 3 | * confidential and proprietary information of Zhongan.com ("Confidential 4 | * Information"). You shall not disclose such Confidential Information and shall 5 | * use it only in accordance with the terms of the license agreement you entered 6 | * into with Zhongan.com. 7 | */ 8 | package com.zhongan.tech.submarineevents.config; 9 | 10 | import java.util.Map; 11 | import java.util.Map.Entry; 12 | 13 | import org.springframework.beans.factory.annotation.Autowired; 14 | import org.springframework.cloud.context.config.annotation.RefreshScope; 15 | import org.springframework.context.annotation.Bean; 16 | import org.springframework.context.annotation.Configuration; 17 | 18 | import com.alibaba.fastjson.JSON; 19 | import com.zhongan.tech.submarineevents.dto.EventAdapterRule; 20 | 21 | import lombok.extern.slf4j.Slf4j; 22 | 23 | @Slf4j 24 | @Configuration 25 | public class EventAdapterRuleMapConfig { 26 | 27 | @Autowired 28 | EventAdapterRules eventAdapterRules; 29 | 30 | @RefreshScope 31 | @Bean 32 | public Map initEventAdapterRuleMap() { 33 | log.info("Start to init event adapter rule map"); 34 | Map eventAdapterRuleMap = eventAdapterRules.convertToEventAdapterRuleMap(); 35 | printEventAdapterRules(eventAdapterRuleMap); 36 | return eventAdapterRuleMap; 37 | } 38 | 39 | private void printEventAdapterRules(Map eventAdapterRuleMap) { 40 | if(null == eventAdapterRuleMap || eventAdapterRuleMap.isEmpty()) { 41 | log.info("Event adapter rule map is null or empty"); 42 | return; 43 | } 44 | log.info("Event adapter rule map:"); 45 | for(Entry e : eventAdapterRuleMap.entrySet()) { 46 | log.info(e.getKey()+" : "+JSON.toJSONString(e.getValue())); 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/com/zhongan/tech/submarineevents/utils/RestTemplateManager.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Zhongan.com All right reserved. This software is the 3 | * confidential and proprietary information of Zhongan.com ("Confidential 4 | * Information"). You shall not disclose such Confidential Information and shall 5 | * use it only in accordance with the terms of the license agreement you entered 6 | * into with Zhongan.com. 7 | */ 8 | package com.zhongan.tech.submarineevents.utils; 9 | 10 | import com.alibaba.fastjson.JSONObject; 11 | 12 | import java.net.URI; 13 | import java.util.Map; 14 | import java.util.Map.Entry; 15 | 16 | import org.springframework.beans.factory.annotation.Autowired; 17 | import org.springframework.http.HttpEntity; 18 | import org.springframework.http.HttpMethod; 19 | import org.springframework.http.ResponseEntity; 20 | import org.springframework.stereotype.Component; 21 | import org.springframework.web.client.RestTemplate; 22 | import org.springframework.web.util.UriComponentsBuilder; 23 | 24 | @Component 25 | public class RestTemplateManager { 26 | 27 | @Autowired 28 | private RestTemplate restTemplate; 29 | 30 | /** 31 | * @Description Execute http call 32 | * @param uri http request location 33 | * @param httpMethod http method 34 | * @param req http request 35 | * @return http response 36 | */ 37 | public JSONObject call(URI uri, HttpMethod httpMethod, JSONObject req) { 38 | 39 | UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(uri.toString()); 40 | 41 | if(!httpMethod.equals(HttpMethod.POST)) { 42 | Map params = JSONObject.toJavaObject(req, Map.class); 43 | if(null != params && !params.isEmpty()) { 44 | for(Entry e: params.entrySet()) { 45 | builder.queryParam(e.getKey(), e.getValue()); 46 | } 47 | } 48 | } 49 | 50 | HttpEntity request = new HttpEntity(req); 51 | 52 | ResponseEntity response = restTemplate.exchange(builder.toUriString(), httpMethod, request, JSONObject.class); 53 | 54 | return response.getBody(); 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/com/zhongan/tech/submarineevents/stream/handler/EventHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Zhongan.com All right reserved. This software is the 3 | * confidential and proprietary information of Zhongan.com ("Confidential 4 | * Information"). You shall not disclose such Confidential Information and shall 5 | * use it only in accordance with the terms of the license agreement you entered 6 | * into with Zhongan.com. 7 | */ 8 | package com.zhongan.tech.submarineevents.stream.handler; 9 | 10 | import java.util.function.Consumer; 11 | 12 | import org.springframework.beans.factory.annotation.Autowired; 13 | import org.springframework.context.annotation.Bean; 14 | import org.springframework.context.annotation.Configuration; 15 | 16 | import com.alibaba.fastjson.JSON; 17 | import com.alibaba.fastjson.JSONObject; 18 | import com.zhongan.tech.submarineevents.adapter.EventAdapter; 19 | import com.zhongan.tech.submarineevents.consumer.RestAPIEventConsumer; 20 | import com.zhongan.tech.submarineevents.dto.RestAPIEvent; 21 | import com.zhongan.tech.submarineevents.exception.EventException; 22 | 23 | import io.cloudevents.CloudEvent; 24 | import lombok.extern.slf4j.Slf4j; 25 | 26 | @Slf4j 27 | @Configuration 28 | public class EventHandler { 29 | 30 | @Autowired EventAdapter eventAdapter; 31 | 32 | @Autowired RestAPIEventConsumer restAPIEventConsumer; 33 | 34 | @Bean 35 | public Consumer restAPIConsume() { 36 | 37 | return (cloudEvent)->{ 38 | 39 | log.info("Receive CloudEvent: {}", JSON.toJSONString(cloudEvent)); 40 | 41 | try { 42 | //convert CloudEvent object to RestAPIEvent object 43 | RestAPIEvent restAPIEvent = eventAdapter.convertCloudEvent(cloudEvent); 44 | //Execute http call synchronously for RestAPIEvent object 45 | JSONObject response = restAPIEventConsumer.syncConsume(restAPIEvent); 46 | //convert response 47 | response = eventAdapter.convertResponse(cloudEvent.getType(), response); 48 | log.info("response: {}", response); 49 | } catch(EventException e) { 50 | e.printStackTrace(); 51 | } 52 | 53 | }; 54 | 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/main/java/com/zhongan/tech/submarineevents/utils/JSONUtil.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Zhongan.com All right reserved. This software is the 3 | * confidential and proprietary information of Zhongan.com ("Confidential 4 | * Information"). You shall not disclose such Confidential Information and shall 5 | * use it only in accordance with the terms of the license agreement you entered 6 | * into with Zhongan.com. 7 | */ 8 | package com.zhongan.tech.submarineevents.utils; 9 | 10 | import java.util.Map; 11 | import java.util.Map.Entry; 12 | 13 | import com.alibaba.fastjson.JSONObject; 14 | 15 | public class JSONUtil { 16 | 17 | /** 18 | * @Description Convert json object according to rule map 19 | * @param source the json object to be converted 20 | * @param keyMap rule map 21 | * @return the converted json object 22 | */ 23 | public static JSONObject convertJSONObject(JSONObject source, Map keyMap) { 24 | 25 | JSONObject target = new JSONObject(); 26 | 27 | for(Entry e: keyMap.entrySet()) { 28 | String sourceKey = e.getKey(); 29 | String[] sourceKeys = sourceKey.split("\\."); 30 | Object value = null; 31 | boolean sourceKeyExist = false; 32 | JSONObject subSource = source; 33 | for(int i = 0; i < sourceKeys.length; i++) { 34 | if(null == subSource.get(sourceKeys[i])) { 35 | break; 36 | }else { 37 | if(i != sourceKeys.length - 1) { 38 | subSource = subSource.getJSONObject(sourceKeys[i]); 39 | }else { 40 | sourceKeyExist = true; 41 | value = subSource.get(sourceKeys[i]); 42 | } 43 | } 44 | } 45 | if(!sourceKeyExist) continue; 46 | 47 | String targetKey = e.getValue(); 48 | String[] targetKeys = targetKey.split("\\."); 49 | JSONObject subTarget = target; 50 | for(int i = 0; i < targetKeys.length; i++) { 51 | if(null == subTarget.getJSONObject(targetKeys[i])) { 52 | if(i != targetKeys.length - 1) { 53 | subTarget.put(targetKeys[i], new JSONObject()); 54 | subTarget = subTarget.getJSONObject(targetKeys[i]); 55 | }else { 56 | subTarget.put(targetKeys[i], value); 57 | } 58 | }else { 59 | subTarget = subTarget.getJSONObject(targetKeys[i]); 60 | } 61 | } 62 | } 63 | return target; 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/main/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | System.out 8 | 9 | ${MONITOR_PATTERN} 10 | 11 | 12 | 14 | ${log.dir}/logs/${HOSTNAME}_app_${projectname}_lt_info.log 15 | 16 | INFO 17 | 18 | 19 | ${HOSTNAME}_app_${projectname}_lt_info_%d{yyyy-MM-dd}.log 20 | 21 | 30 22 | 23 | 24 | ${MONITOR_PATTERN} 25 | 26 | 27 | 29 | ${log.dir}/logs/${HOSTNAME}_app_${projectname}_lt_error.log 30 | 31 | WARN 32 | 33 | 34 | ${log.dir}/logs/${HOSTNAME}_app_${projectname}_lt_error_%d{yyyy-MM-dd}.log 35 | 36 | 30 37 | 38 | 39 | ${MONITOR_PATTERN} 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.7.RELEASE 9 | 10 | 11 | com.zhongan.tech 12 | submarine-events 13 | 1.0.0 14 | submarine-events 15 | 16 | 17 | 1.8 18 | 19 | 20 | 21 | 22 | org.springframework.boot 23 | spring-boot-starter-web 24 | 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-starter-test 29 | test 30 | 31 | 32 | 33 | org.springframework.cloud 34 | spring-cloud-stream 35 | 2.2.1.RELEASE 36 | 37 | 38 | 39 | org.springframework.cloud 40 | spring-cloud-stream-binder-kafka 41 | 2.2.1.RELEASE 42 | 43 | 44 | 45 | io.cloudevents 46 | cloudevents-api 47 | 0.2.1 48 | 49 | 50 | 51 | com.alibaba 52 | fastjson 53 | 1.2.62 54 | 55 | 56 | 57 | com.alibaba.cloud 58 | spring-cloud-starter-alibaba-nacos-config 59 | 2.1.0.RELEASE 60 | 61 | 62 | 63 | org.projectlombok 64 | lombok 65 | 1.16.16 66 | 67 | 68 | 69 | org.apache.commons 70 | commons-lang3 71 | 3.4 72 | 73 | 74 | 75 | 76 | ${project.artifactId} 77 | 78 | 79 | org.springframework.boot 80 | spring-boot-maven-plugin 81 | 82 | 83 | 84 | 85 | 86 | -------------------------------------------------------------------------------- /src/main/java/com/zhongan/tech/submarineevents/web/EventController.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Zhongan.com All right reserved. This software is the 3 | * confidential and proprietary information of Zhongan.com ("Confidential 4 | * Information"). You shall not disclose such Confidential Information and shall 5 | * use it only in accordance with the terms of the license agreement you entered 6 | * into with Zhongan.com. 7 | */ 8 | package com.zhongan.tech.submarineevents.web; 9 | 10 | import org.springframework.beans.factory.annotation.Autowired; 11 | import org.springframework.web.bind.annotation.RequestBody; 12 | import org.springframework.web.bind.annotation.RequestMapping; 13 | import org.springframework.web.bind.annotation.RequestMethod; 14 | import org.springframework.web.bind.annotation.RestController; 15 | 16 | import com.alibaba.fastjson.JSON; 17 | import com.alibaba.fastjson.JSONObject; 18 | import com.zhongan.tech.submarineevents.adapter.EventAdapter; 19 | import com.zhongan.tech.submarineevents.consumer.RestAPIEventConsumer; 20 | import com.zhongan.tech.submarineevents.dto.RestAPIEvent; 21 | import com.zhongan.tech.submarineevents.enums.ResultCode; 22 | import com.zhongan.tech.submarineevents.exception.EventException; 23 | import com.zhongan.tech.submarineevents.vo.BaseResult; 24 | 25 | import io.cloudevents.CloudEvent; 26 | import lombok.extern.slf4j.Slf4j; 27 | 28 | @Slf4j 29 | @RestController 30 | @RequestMapping("/v1/event") 31 | public class EventController { 32 | 33 | @Autowired 34 | EventAdapter eventAdapter; 35 | 36 | @Autowired 37 | RestAPIEventConsumer restAPIEventConsumer; 38 | 39 | /** 40 | * @Description Get CloudEvent object, then call restful http api for received event 41 | * @param cloudEvent CloudEvent object 42 | * @return http response 43 | */ 44 | @RequestMapping(value="/send", method = RequestMethod.POST) 45 | public BaseResult sendEvent(@RequestBody CloudEvent cloudEvent) { 46 | 47 | log.info("EventController receive CloudEvent: {}", JSON.toJSONString(cloudEvent)); 48 | 49 | try { 50 | //convert CloudEvent object to RestAPIEvent object 51 | RestAPIEvent restAPIEvent = eventAdapter.convertCloudEvent(cloudEvent); 52 | //Execute http call synchronously for RestAPIEvent object 53 | JSONObject response = restAPIEventConsumer.syncConsume(restAPIEvent); 54 | //convert response 55 | response = eventAdapter.convertResponse(cloudEvent.getType(), response); 56 | BaseResult baseResult = BaseResult.success(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), response); 57 | return baseResult; 58 | } catch (EventException e) { 59 | BaseResult baseResult = BaseResult.fail(e.getResultCode().getCode(), e.getResultCode().getMessage(), null); 60 | return baseResult; 61 | } 62 | 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/main/java/com/zhongan/tech/submarineevents/config/EventAdapterRules.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Zhongan.com All right reserved. This software is the 3 | * confidential and proprietary information of Zhongan.com ("Confidential 4 | * Information"). You shall not disclose such Confidential Information and shall 5 | * use it only in accordance with the terms of the license agreement you entered 6 | * into with Zhongan.com. 7 | */ 8 | package com.zhongan.tech.submarineevents.config; 9 | 10 | import java.net.URI; 11 | import java.net.URISyntaxException; 12 | import java.util.HashMap; 13 | import java.util.List; 14 | import java.util.Map; 15 | 16 | import org.springframework.boot.context.properties.ConfigurationProperties; 17 | import org.springframework.http.HttpMethod; 18 | import org.springframework.stereotype.Component; 19 | 20 | import com.alibaba.fastjson.JSONObject; 21 | import com.zhongan.tech.submarineevents.dto.EventAdapterRule; 22 | 23 | import lombok.Data; 24 | import lombok.extern.slf4j.Slf4j; 25 | 26 | /* 27 | * event-adapter-rules in config file 28 | */ 29 | @Slf4j 30 | @Component 31 | @ConfigurationProperties(prefix = "event-adapter-rules") 32 | @Data 33 | public class EventAdapterRules { 34 | 35 | List> rule; 36 | 37 | /** 38 | * @Description Convert event-adapter-rules in config file to EventAdapterRule map 39 | * @return EventAdapterRule map 40 | */ 41 | public Map convertToEventAdapterRuleMap() { 42 | Map eventAdapterRuleMap = new HashMap(rule.size()); 43 | for(Map properties : rule) { 44 | try { 45 | eventAdapterRuleMap.put(properties.get("type"), convertToEventAdapterRule(properties)); 46 | log.info("Success to load adapter rule of {}", properties.get("type")); 47 | } catch (Exception e) { 48 | log.error("Fail to load adapter rule of {} : {}", properties.get("type"), e.getMessage()); 49 | } 50 | } 51 | return eventAdapterRuleMap; 52 | } 53 | 54 | /** 55 | * @Description Convert rule properties in config file to EventAdapterRule object 56 | * @param properties rule properties in config file 57 | * @return EventAdapterRule object 58 | * @throws convert error 59 | */ 60 | private EventAdapterRule convertToEventAdapterRule(Map properties) throws URISyntaxException { 61 | EventAdapterRule eventAdapterRule = new EventAdapterRule(); 62 | 63 | eventAdapterRule.setType(properties.get("type")); 64 | eventAdapterRule.setTargetUri(new URI(properties.get("targetUri"))); 65 | eventAdapterRule.setHttpMethod(HttpMethod.valueOf(properties.get("httpMethod"))); 66 | 67 | String requestKeyMap = properties.get("requestKeyMap"); 68 | if(null != requestKeyMap && requestKeyMap.trim().length() > 0) { 69 | JSONObject jsonObject = JSONObject.parseObject(requestKeyMap); 70 | eventAdapterRule.setRequestKeyMap(JSONObject.toJavaObject(jsonObject, Map.class)); 71 | } 72 | 73 | String responseKeyMap = properties.get("responseKeyMap"); 74 | if(null != responseKeyMap && responseKeyMap.trim().length() > 0) { 75 | JSONObject jsonObject = JSONObject.parseObject(responseKeyMap); 76 | eventAdapterRule.setResponseKeyMap(JSONObject.toJavaObject(jsonObject, Map.class)); 77 | } 78 | 79 | return eventAdapterRule; 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /src/main/java/com/zhongan/tech/submarineevents/adapter/EventAdapterImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Zhongan.com All right reserved. This software is the 3 | * confidential and proprietary information of Zhongan.com ("Confidential 4 | * Information"). You shall not disclose such Confidential Information and shall 5 | * use it only in accordance with the terms of the license agreement you entered 6 | * into with Zhongan.com. 7 | */ 8 | package com.zhongan.tech.submarineevents.adapter; 9 | 10 | import java.util.Map; 11 | import java.util.Optional; 12 | 13 | import org.springframework.beans.factory.annotation.Autowired; 14 | import org.springframework.stereotype.Service; 15 | 16 | import com.alibaba.fastjson.JSON; 17 | import com.alibaba.fastjson.JSONObject; 18 | import com.zhongan.tech.submarineevents.dto.EventAdapterRule; 19 | import com.zhongan.tech.submarineevents.dto.RestAPIEvent; 20 | import com.zhongan.tech.submarineevents.enums.ResultCode; 21 | import com.zhongan.tech.submarineevents.exception.EventException; 22 | import com.zhongan.tech.submarineevents.utils.JSONUtil; 23 | 24 | import io.cloudevents.CloudEvent; 25 | import lombok.extern.slf4j.Slf4j; 26 | 27 | @Slf4j 28 | @Service 29 | public class EventAdapterImpl implements EventAdapter{ 30 | 31 | @Autowired 32 | Map eventAdapterRuleMap; 33 | 34 | @Override 35 | public RestAPIEvent convertCloudEvent(CloudEvent cloudEvent) throws EventException { 36 | EventAdapterRule eventAdapterRule = eventAdapterRuleMap.get(cloudEvent.getType()); 37 | if(null != eventAdapterRule) { 38 | RestAPIEvent restAPIEvent = new RestAPIEvent(); 39 | restAPIEvent.setType(cloudEvent.getType()); 40 | restAPIEvent.setId(cloudEvent.getId()); 41 | restAPIEvent.setSource(cloudEvent.getSource()); 42 | restAPIEvent.setTarget(eventAdapterRule.getTargetUri()); 43 | restAPIEvent.setHttpMethod(eventAdapterRule.getHttpMethod()); 44 | if(Optional.empty() != cloudEvent.getData()) { 45 | JSONObject sourceRequest = JSONObject.parseObject(JSON.toJSONString(cloudEvent.getData().get())); 46 | if(null != eventAdapterRule.getRequestKeyMap()) { 47 | JSONObject targetRequest = JSONUtil.convertJSONObject(sourceRequest, eventAdapterRule.getRequestKeyMap()); 48 | restAPIEvent.setRequest(targetRequest); 49 | }else { 50 | restAPIEvent.setRequest(sourceRequest); 51 | } 52 | } 53 | log.info("Success to convert CloudEvent to RestAPIEvent: {}", JSON.toJSONString(restAPIEvent)); 54 | return restAPIEvent; 55 | }else { 56 | log.error("Adapter rule of {} doesn't exist", cloudEvent.getType()); 57 | throw new EventException(ResultCode.EVENT_ADAPTER_RULE_NOT_EXIST); 58 | } 59 | } 60 | 61 | @Override 62 | public JSONObject convertResponse(String type, JSONObject response) throws EventException { 63 | EventAdapterRule eventAdapterRule = eventAdapterRuleMap.get(type); 64 | if(null != eventAdapterRule) { 65 | if(null != eventAdapterRule.getResponseKeyMap() && null != response) { 66 | return JSONUtil.convertJSONObject(response, eventAdapterRule.getResponseKeyMap()); 67 | }else { 68 | return response; 69 | } 70 | }else { 71 | log.error("Adapter rule of {} doesn't exist", type); 72 | throw new EventException(ResultCode.EVENT_ADAPTER_RULE_NOT_EXIST); 73 | } 74 | } 75 | 76 | } 77 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # submarine-events 2 | 3 | ## Getting Start 4 | submarine-events is a lightweight, event-driven micro-service framework. It provides a common solution to receive events transported from producer and consume them via RESTful HTTP API. Event producers can send events to submarine-events via Kafka or submarine-events API. The event data must be described in a common format defined by CloudEvents protocol, which allows data exchange between different systems. submarine-events listens to events, then routes them to specific destinations via RESTful HTTP API configured by user. 5 | 6 | ## Features 7 | - Event-driven architecture, connecting event producers and consumers in a decoupled fashion 8 | 9 | - CloudEvents protocol, achieving interoperability across different systems 10 | 11 | - Dynamic configuration, eliminating the need of redeployment 12 | 13 | - Sync or async mode, providing flexibility to fit different tasks 14 | 15 | ## How To Use 16 | 17 | #### 1.Configure Kafka 18 | 19 | submarine-events can receive event via both Kafka and submarine-events API. If you choose Kafka, configure Kafka as following. Otherwise, skip this step. 20 | ``` 21 | # /src/main/resources/bootstrap.yml 22 | spring: 23 | cloud: 24 | stream: 25 | default-binder: kafka 26 | kafka: 27 | binder: 28 | brokers: localhost:9092 #your kafka server address 29 | bindings: 30 | input: 31 | destination: my_topic #your topic 32 | group: my_topic_group1 #consumer group of your topic 33 | consumer: 34 | concurrency: 4 #concurrent consumer count, default is 1, less than or equal to partition count 35 | max-attempts: 5 #max retry count, default is 3 36 | function: 37 | definition: restAPIConsume #function to consume message, already implemented in stream/handler/EventHandler.java 38 | ``` 39 | 40 | #### 2.Configure Event Adapter Rules 41 | 42 | ##### I) Local Event Adapter Rules 43 | ``` 44 | # /src/main/resources/bootstrap.yml 45 | event-adapter-rules: 46 | rule[0]: 47 | type: Demo #event type 48 | targetUri: http://localhost:8080/v1/demo/toUpperCase #RESTful HTTP API where event is routed to, here is a demo API implemented in demo/TargetUriDemo.java 49 | httpMethod: GET #method of RESTful HTTP API 50 | requestKeyMap: '{"oriReqName1": "convertedReqName1", "oriReqName2": "convertedReqName2"}' #request key map, not required 51 | responseKeyMap: '{"oriRespName1": "convertedRespName1", "oriRespName2":"convertedRespName2"}' #response key map, not required 52 | #rule[1]: more rules can be added here 53 | ``` 54 | 55 | ##### II) Dynamic Event Adapter Rules via Nacos 56 | 57 | Event adapter rules can also be configured dynamically in Nacos if available. 58 | 59 | ###### a) Configure Nacos 60 | ``` 61 | # /src/main/resources/bootstrap.yml 62 | spring: 63 | application: 64 | name: submarine-events 65 | cloud: 66 | nacos: 67 | config: 68 | server-addr: localhost:80 #your nacos server address 69 | file-extension: yaml 70 | ``` 71 | 72 | ###### b) Configure Event Adapter Rules in Nacos 73 | ``` 74 | # Nacos/submarine-events.yaml 75 | event-adapter-rules: 76 | rule[0]: ... 77 | rule[1]: ... 78 | ... 79 | ``` 80 | 81 | #### 3.Send Event 82 | 83 | Send an event defined by CloudEvents protocol to submarine-events via 1) the specific Kafka topic you configured or 2) submarine-events API: http://localhost:8080/v1/event/send. 84 | 85 | Here is a demo event: 86 | ``` 87 | { 88 | "type":"Demo", 89 | "data": { 90 | "oriReqName1":"cat", 91 | "oriReqName2":"dog" 92 | }, 93 | "id":"000000001", 94 | "source":"/event/source" 95 | } 96 | ``` 97 | Learn more about [CloudEvents](https://github.com/cloudevents/spec). 98 | 99 | #### 4.Route event to RESTful HTTP API 100 | 101 | submarine-events finds corresponding event adapter rule according to `"type"`: 102 | ``` 103 | event-adapter-rules: 104 | rule[0]: 105 | type: Demo #event type 106 | targetUri: http://localhost:8080/v1/demo/toUpperCase #RESTful HTTP API where event is routed to, here is a demo API implemented in demo/TargetUriDemo.java 107 | httpMethod: GET #method of RESTful HTTP API 108 | requestKeyMap: '{"oriReqName1": "convertedReqName1", "oriReqName2": "convertedReqName2"}' #request key map, not required 109 | responseKeyMap: '{"oriRespName1": "convertedRespName1", "oriRespName2":"convertedRespName2"}' #response key map, not required 110 | ``` 111 | submarine-events calls http://localhost:8080/v1/demo/toUpperCase?convertedReqName1=cat&convertedReqName2=dog, uri is from `targetUri` and request parameter is converted from `"data"` by `requestKeyMap`, you can get response converted by `"responseKeyMap"`: 112 | ``` 113 | { 114 | "convertedRespName1": "CAT", 115 | "convertedRespName2": "DOG" 116 |  } 117 | ``` 118 | 119 | ## Contributing 120 | First and foremost, thank you! We appreciate that you want to contribute to submarine-events, your time is valuable, and your contributions mean a lot to us. 121 | There are many ways to contribute, including the following: 122 | - Creating an issue 123 | - Updating or correcting documentation 124 | - Feature requests 125 | - Bug reports 126 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [2019] [submarine-events author] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | --------------------------------------------------------------------------------