Dmaap micro service jar
[appc.git] / services / appc-dmaap-service / appc-dmaap-event-service / src / main / java / org / onap / appc / services / dmaapService / PublishRequest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * 
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.appc.services.dmaapService;
23
24 import com.att.eelf.configuration.EELFLogger;
25 import com.att.eelf.configuration.EELFManager;
26
27 public class PublishRequest {
28     
29     private static final EELFLogger LOG = EELFManager.getInstance().getLogger(PublishRequest.class);
30     
31     private String props;
32     private String partition;
33     private String message;
34     private String topic;
35
36     private PublishRequest(String props, String partition, String topic, String message) {
37         this.props = props;
38         this.partition = partition;
39         this.message = message;
40         this.topic = topic;
41     }
42
43     public String getProps() {
44         return props;
45     }
46
47     public String getPartition() {
48         return partition;
49     }
50
51     public String getMessage() {
52         return message;
53     }
54
55     public String getTopic() {
56         return topic;
57     }
58
59     public static PublishRequest parsePublishRequest(String data) {
60         //body content: props, partition, topic, message
61         String[] bodyParameters = new String[4];
62         LOG.debug("Parsing message into " + bodyParameters.length + " parts: " + data);
63         int[] bodyParameterSizes = new int[bodyParameters.length];
64         for(int i = 0; i < bodyParameters.length; i ++) {
65             String[] split = data.split("\\.", 2);
66             try {
67                 bodyParameterSizes[i] = Integer.parseInt(split[0]);
68             } catch(NumberFormatException e) {
69                 LOG.error("Could not parse message: " + data);
70             }
71             data = split[1];
72         }
73         int cursor = 0;
74         for(int i = 0; i < bodyParameters.length; i ++) {
75             if(bodyParameterSizes[i] > 0) {
76                 bodyParameters[i] = data.substring(cursor, cursor + bodyParameterSizes[i]);
77                 cursor = cursor + bodyParameterSizes[i];
78             }
79         }
80         return new PublishRequest(bodyParameters[0], bodyParameters[1], bodyParameters[2], bodyParameters[3]);
81     }
82
83 }