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;
24 import com.att.nsa.apiServer.streams.ChunkedInputStream;
25 import com.att.nsa.drumlin.service.standards.HttpStatusCodes;
26 import org.onap.dmaap.dmf.mr.CambriaApiException;
27 import org.onap.dmaap.dmf.mr.backends.Publisher.message;
28 import org.onap.dmaap.dmf.mr.resources.streamReaders.CambriaJsonStreamReader;
29 import org.onap.dmaap.dmf.mr.resources.streamReaders.CambriaRawStreamReader;
30 import org.onap.dmaap.dmf.mr.resources.streamReaders.CambriaStreamReader;
31 import org.onap.dmaap.dmf.mr.resources.streamReaders.CambriaTextStreamReader;
33 import javax.servlet.http.HttpServletResponse;
34 import java.io.IOException;
35 import java.io.InputStream;
36 import java.util.zip.GZIPInputStream;
39 * An inbound event set.
43 public class CambriaEventSet {
44 private final reader fReader;
47 * constructor initialization
50 * @param originalStream
53 * @throws CambriaApiException
55 public CambriaEventSet(String mediaType, InputStream originalStream,
56 boolean chunked, String defPartition) throws CambriaApiException {
57 InputStream is = originalStream;
59 is = new ChunkedInputStream(originalStream);
62 if (("application/json").equals(mediaType)) {
64 throw new CambriaApiException(
65 HttpServletResponse.SC_BAD_REQUEST,
66 "The JSON stream reader doesn't support chunking.");
68 fReader = new CambriaJsonStreamReader(is, defPartition);
69 } else if (("application/cambria").equals(mediaType)) {
70 fReader = new CambriaStreamReader(is);
71 } else if (("application/cambria-zip").equals(mediaType)) {
73 is = new GZIPInputStream(is);
74 } catch (IOException e) {
75 throw new CambriaApiException(HttpStatusCodes.k400_badRequest,
76 "Couldn't read compressed format: " + e);
78 fReader = new CambriaStreamReader(is);
79 } else if (("text/plain").equals(mediaType)) {
80 fReader = new CambriaTextStreamReader(is, defPartition);
82 fReader = new CambriaRawStreamReader(is, defPartition);
87 * Get the next message from this event set. Returns null when the end of
88 * stream is reached. Will block until a message arrives (or the stream is
91 * @return a message, or null
93 * @throws CambriaApiException
95 public message next() throws IOException, CambriaApiException {
96 return fReader.next();
101 * @author anowarul.islam
104 public interface reader {
108 * @throws IOException
109 * @throws CambriaApiException
111 message next() throws IOException, CambriaApiException;