bump the version
[dmaap/messagerouter/msgrtr.git] / src / main / java / org / onap / dmaap / dmf / mr / resources / CambriaEventSet.java
1 /*******************************************************************************
2  *  ============LICENSE_START=======================================================
3  *  org.onap.dmaap
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
11 *  
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=========================================================
18  *  
19  *  ECOMP is a trademark and service mark of AT&T Intellectual Property.
20  *  
21  *******************************************************************************/
22 package org.onap.dmaap.dmf.mr.resources;
23
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.util.zip.GZIPInputStream;
27
28 import javax.servlet.http.HttpServletResponse;
29
30 import org.onap.dmaap.dmf.mr.CambriaApiException;
31 import org.onap.dmaap.dmf.mr.backends.Publisher.message;
32 import org.onap.dmaap.dmf.mr.resources.streamReaders.CambriaJsonStreamReader;
33 import org.onap.dmaap.dmf.mr.resources.streamReaders.CambriaRawStreamReader;
34 import org.onap.dmaap.dmf.mr.resources.streamReaders.CambriaStreamReader;
35 import org.onap.dmaap.dmf.mr.resources.streamReaders.CambriaTextStreamReader;
36 import com.att.nsa.apiServer.streams.ChunkedInputStream;
37 import com.att.nsa.drumlin.service.standards.HttpStatusCodes;
38
39 /**
40  * An inbound event set.
41  * 
42  * @author peter
43  */
44 public class CambriaEventSet {
45         private final reader fReader;
46
47         /**
48          * constructor initialization
49          * 
50          * @param mediaType
51          * @param originalStream
52          * @param chunked
53          * @param defPartition
54          * @throws CambriaApiException
55          */
56         public CambriaEventSet(String mediaType, InputStream originalStream,
57                         boolean chunked, String defPartition) throws CambriaApiException {
58                 InputStream is = originalStream;
59                 if (chunked) {
60                         is = new ChunkedInputStream(originalStream);
61                 }
62
63                 if (("application/json").equals(mediaType)) {
64                         if (chunked) {
65                                 throw new CambriaApiException(
66                                                 HttpServletResponse.SC_BAD_REQUEST,
67                                                 "The JSON stream reader doesn't support chunking.");
68                         }
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)) {
73                         try {
74                                 is = new GZIPInputStream(is);
75                         } catch (IOException e) {
76                                 throw new CambriaApiException(HttpStatusCodes.k400_badRequest,
77                                                 "Couldn't read compressed format: " + e);
78                         }
79                         fReader = new CambriaStreamReader(is);
80                 } else if (("text/plain").equals(mediaType)) {
81                         fReader = new CambriaTextStreamReader(is, defPartition);
82                 } else {
83                         fReader = new CambriaRawStreamReader(is, defPartition);
84                 }
85         }
86
87         /**
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
90          * closed/broken).
91          * 
92          * @return a message, or null
93          * @throws IOException
94          * @throws CambriaApiException
95          */
96         public message next() throws IOException, CambriaApiException {
97                 return fReader.next();
98         }
99
100         /**
101          * 
102          * @author anowarul.islam
103          *
104          */
105         public interface reader {
106                 /**
107                  * 
108                  * @return
109                  * @throws IOException
110                  * @throws CambriaApiException
111                  */
112                 message next() throws IOException, CambriaApiException;
113         }
114 }