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;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.util.zip.GZIPInputStream;
28 import javax.servlet.http.HttpServletResponse;
30 import org.onap.dmaap.messagerouter.msgrtr.nsa.cambria.CambriaApiException;
31 import org.onap.dmaap.messagerouter.msgrtr.nsa.cambria.backends.Publisher.message;
32 import org.onap.dmaap.messagerouter.msgrtr.nsa.cambria.resources.streamReaders.CambriaJsonStreamReader;
33 import org.onap.dmaap.messagerouter.msgrtr.nsa.cambria.resources.streamReaders.CambriaRawStreamReader;
34 import org.onap.dmaap.messagerouter.msgrtr.nsa.cambria.resources.streamReaders.CambriaStreamReader;
35 import org.onap.dmaap.messagerouter.msgrtr.nsa.cambria.resources.streamReaders.CambriaTextStreamReader;
37 import com.att.nsa.apiServer.streams.ChunkedInputStream;
38 import com.att.nsa.drumlin.service.standards.HttpStatusCodes;
41 * An inbound event set.
45 public class CambriaEventSet {
46 private final reader fReader;
49 * constructor initialization
52 * @param originalStream
55 * @throws CambriaApiException
57 public CambriaEventSet(String mediaType, InputStream originalStream,
58 boolean chunked, String defPartition) throws CambriaApiException {
59 InputStream is = originalStream;
61 is = new ChunkedInputStream(originalStream);
64 if (("application/json").equals(mediaType)) {
66 throw new CambriaApiException(
67 HttpServletResponse.SC_BAD_REQUEST,
68 "The JSON stream reader doesn't support chunking.");
70 fReader = new CambriaJsonStreamReader(is, defPartition);
71 } else if (("application/cambria").equals(mediaType)) {
72 fReader = new CambriaStreamReader(is);
73 } else if (("application/cambria-zip").equals(mediaType)) {
75 is = new GZIPInputStream(is);
76 } catch (IOException e) {
77 throw new CambriaApiException(HttpStatusCodes.k400_badRequest,
78 "Couldn't read compressed format: " + e);
80 fReader = new CambriaStreamReader(is);
81 } else if (("text/plain").equals(mediaType)) {
82 fReader = new CambriaTextStreamReader(is, defPartition);
84 fReader = new CambriaRawStreamReader(is, defPartition);
89 * Get the next message from this event set. Returns null when the end of
90 * stream is reached. Will block until a message arrives (or the stream is
93 * @return a message, or null
95 * @throws CambriaApiException
97 public message next() throws IOException, CambriaApiException {
98 return fReader.next();
106 public interface reader {
110 * @throws IOException
111 * @throws CambriaApiException
113 message next() throws IOException, CambriaApiException;