1 /*******************************************************************************
2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 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 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 * ============LICENSE_END=========================================================
19 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21 *******************************************************************************/
22 package org.onap.dmaap.dmf.mr.resources.streamReaders;
24 import java.io.InputStream;
26 import javax.servlet.http.HttpServletResponse;
28 import org.json.JSONException;
29 import org.json.JSONObject;
30 import org.json.JSONTokener;
32 import org.onap.dmaap.dmf.mr.CambriaApiException;
33 import org.onap.dmaap.dmf.mr.backends.Publisher.message;
34 import org.onap.dmaap.dmf.mr.beans.LogDetails;
35 import org.onap.dmaap.dmf.mr.resources.CambriaEventSet.reader;
39 * @author anowarul.islam
42 public class CambriaJsonStreamReader implements reader {
43 private final JSONTokener fTokens;
44 private final boolean fIsList;
46 private final String fDefPart;
47 public static final String kKeyField = "cambria.partition";
53 * @throws CambriaApiException
55 public CambriaJsonStreamReader(InputStream is, String defPart) throws CambriaApiException {
57 fTokens = new JSONTokener(is);
61 final int c = fTokens.next();
64 } else if (c == '{') {
68 throw new CambriaApiException(HttpServletResponse.SC_BAD_REQUEST, "Expecting an array or an object.");
70 } catch (JSONException e) {
71 throw new CambriaApiException(HttpServletResponse.SC_BAD_REQUEST, e.getMessage());
76 public message next() throws CambriaApiException {
78 if (!fTokens.more()) {
82 final int c = fTokens.next();
86 if (c == ']' || (fCount > 0 && c == 10))
90 if (fCount > 0 && c != ',' && c!= 10) {
91 throw new CambriaApiException(HttpServletResponse.SC_BAD_REQUEST,
92 "Expected ',' or closing ']' after last object.");
95 if (fCount == 0 && c != '{' && c!= 10 && c!=32) {
96 throw new CambriaApiException(HttpServletResponse.SC_BAD_REQUEST, "Expected { to start an object.");
98 } else if (fCount != 0 || c != '{') {
99 throw new CambriaApiException(HttpServletResponse.SC_BAD_REQUEST, "Expected '{' to start an object.");
105 final JSONObject o = new JSONObject(fTokens);
108 } catch (JSONException e) {
109 throw new CambriaApiException(HttpServletResponse.SC_BAD_REQUEST, e.getMessage());
114 private class msg implements message {
115 private final String fKey;
117 private LogDetails logDetails;
118 private boolean transactionEnabled;
128 public msg(JSONObject o) {
129 String key = o.optString(kKeyField, fDefPart);
131 key = "" + System.currentTimeMillis();
135 fMsg = o.toString().trim();
140 public String getKey() {
145 public String getMessage() {
150 public boolean isTransactionEnabled() {
151 return transactionEnabled;
155 public void setTransactionEnabled(boolean transactionEnabled) {
156 this.transactionEnabled = transactionEnabled;
160 public void setLogDetails(LogDetails logDetails) {
161 this.logDetails = logDetails;
165 public LogDetails getLogDetails() {