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 com.att.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;
32 import com.att.nsa.cambria.CambriaApiException;
33 import com.att.nsa.cambria.backends.Publisher.message;
34 import com.att.nsa.cambria.beans.LogDetails;
35 import com.att.nsa.cambria.resources.CambriaEventSet.reader;
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 System.out.println("fCloseCount=" + fCloseCount +" fCount "+fCount);
89 if (c == ']' || (fCount > 0 && c == 10))
93 if (fCount > 0 && c != ',' && c!= 10) {
94 throw new CambriaApiException(HttpServletResponse.SC_BAD_REQUEST,
95 "Expected ',' or closing ']' after last object.");
98 if (fCount == 0 && c != '{' && c!= 10 && c!=32) {
99 throw new CambriaApiException(HttpServletResponse.SC_BAD_REQUEST, "Expected { to start an object.");
101 } else if (fCount != 0 || c != '{') {
102 throw new CambriaApiException(HttpServletResponse.SC_BAD_REQUEST, "Expected '{' to start an object.");
108 final JSONObject o = new JSONObject(fTokens);
111 } catch (JSONException e) {
112 throw new CambriaApiException(HttpServletResponse.SC_BAD_REQUEST, e.getMessage());
117 private class msg implements message {
118 private final String fKey;
120 private LogDetails logDetails;
121 private boolean transactionEnabled;
128 //public msg(JSONObject o){}
131 public msg(JSONObject o) {
132 String key = o.optString(kKeyField, fDefPart);
134 key = "" + System.currentTimeMillis();
138 fMsg = o.toString().trim();
143 public String getKey() {
148 public String getMessage() {
153 public boolean isTransactionEnabled() {
154 return transactionEnabled;
158 public void setTransactionEnabled(boolean transactionEnabled) {
159 this.transactionEnabled = transactionEnabled;
163 public void setLogDetails(LogDetails logDetails) {
164 this.logDetails = logDetails;
168 public LogDetails getLogDetails() {