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.dmf.mr.resources;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.util.zip.GZIPInputStream;
28 import javax.servlet.http.HttpServletResponse;
30 import com.att.dmf.mr.CambriaApiException;
31 import com.att.dmf.mr.backends.Publisher.message;
32 import com.att.dmf.mr.resources.streamReaders.CambriaJsonStreamReader;
33 import com.att.dmf.mr.resources.streamReaders.CambriaRawStreamReader;
34 import com.att.dmf.mr.resources.streamReaders.CambriaStreamReader;
35 import com.att.dmf.mr.resources.streamReaders.CambriaTextStreamReader;
36 import com.att.nsa.apiServer.streams.ChunkedInputStream;
37 import com.att.nsa.drumlin.service.standards.HttpStatusCodes;
40 * An inbound event set.
44 public class CambriaEventSet {
45 private final reader fReader;
48 * constructor initialization
51 * @param originalStream
54 * @throws CambriaApiException
56 public CambriaEventSet(String mediaType, InputStream originalStream,
57 boolean chunked, String defPartition) throws CambriaApiException {
58 InputStream is = originalStream;
60 is = new ChunkedInputStream(originalStream);
63 if (("application/json").equals(mediaType)) {
65 throw new CambriaApiException(
66 HttpServletResponse.SC_BAD_REQUEST,
67 "The JSON stream reader doesn't support chunking.");
69 fReader = new CambriaJsonStreamReader(is, defPartition);
70 } else if (("application/cambria").equals(mediaType)) {
71 fReader = new CambriaStreamReader(is);
72 } else if (("application/cambria-zip").equals(mediaType)) {
74 is = new GZIPInputStream(is);
75 } catch (IOException e) {
76 throw new CambriaApiException(HttpStatusCodes.k400_badRequest,
77 "Couldn't read compressed format: " + e);
79 fReader = new CambriaStreamReader(is);
80 } else if (("text/plain").equals(mediaType)) {
81 fReader = new CambriaTextStreamReader(is, defPartition);
83 fReader = new CambriaRawStreamReader(is, defPartition);
88 * Get the next message from this event set. Returns null when the end of
89 * stream is reached. Will block until a message arrives (or the stream is
92 * @return a message, or null
94 * @throws CambriaApiException
96 public message next() throws IOException, CambriaApiException {
97 return fReader.next();
102 * @author anowarul.islam
105 public interface reader {
109 * @throws IOException
110 * @throws CambriaApiException
112 message next() throws IOException, CambriaApiException;