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.messagerouter.msgrtr.nsa.cambria.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;
31 import org.onap.dmaap.messagerouter.msgrtr.nsa.cambria.CambriaApiException;
32 import org.onap.dmaap.messagerouter.msgrtr.nsa.cambria.backends.Publisher.message;
33 import org.onap.dmaap.messagerouter.msgrtr.nsa.cambria.beans.LogDetails;
34 import org.onap.dmaap.messagerouter.msgrtr.nsa.cambria.resources.CambriaEventSet.reader;
41 public class CambriaJsonStreamReader implements reader {
42 private final JSONTokener fTokens;
43 private final boolean fIsList;
45 private final String fDefPart;
46 public static final String kKeyField = "cambria.partition";
52 * @throws CambriaApiException
54 public CambriaJsonStreamReader(InputStream is, String defPart) throws CambriaApiException {
56 fTokens = new JSONTokener(is);
60 final int c = fTokens.next();
63 } else if (c == '{') {
67 throw new CambriaApiException(HttpServletResponse.SC_BAD_REQUEST, "Expecting an array or an object.");
69 } catch (JSONException e) {
70 throw new CambriaApiException(HttpServletResponse.SC_BAD_REQUEST, e.getMessage());
75 public message next() throws CambriaApiException {
77 if (!fTokens.more()) {
81 final int c = fTokens.next();
85 System.out.println("fCloseCount=" + fCloseCount +" fCount "+fCount);
88 if (c == ']' || (fCount > 0 && c == 10))
92 if (fCount > 0 && c != ',' && c!= 10) {
93 throw new CambriaApiException(HttpServletResponse.SC_BAD_REQUEST,
94 "Expected ',' or closing ']' after last object.");
97 if (fCount == 0 && c != '{' && c!= 10 && c!=32) {
98 throw new CambriaApiException(HttpServletResponse.SC_BAD_REQUEST, "Expected { to start an object.");
100 } else if (fCount != 0 || c != '{') {
101 throw new CambriaApiException(HttpServletResponse.SC_BAD_REQUEST, "Expected '{' to start an object.");
107 final JSONObject o = new JSONObject(fTokens);
110 } catch (JSONException e) {
111 throw new CambriaApiException(HttpServletResponse.SC_BAD_REQUEST, e.getMessage());
116 private class msg implements message {
117 private final String fKey;
119 private LogDetails logDetails;
120 private boolean transactionEnabled;
127 //public msg(JSONObject o){}
130 public msg(JSONObject o) {
131 String key = o.optString(kKeyField, fDefPart);
133 key = "" + System.currentTimeMillis();
137 fMsg = o.toString().trim();
142 public String getKey() {
147 public String getMessage() {
152 public boolean isTransactionEnabled() {
153 return transactionEnabled;
157 public void setTransactionEnabled(boolean transactionEnabled) {
158 this.transactionEnabled = transactionEnabled;
162 public void setLogDetails(LogDetails logDetails) {
163 this.logDetails = logDetails;
167 public LogDetails getLogDetails() {