DMAAP-MR - Merge MR repos
[dmaap/messagerouter/messageservice.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 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;
32
33 import javax.servlet.http.HttpServletResponse;
34 import java.io.IOException;
35 import java.io.InputStream;
36 import java.util.zip.GZIPInputStream;
37
38 /**
39  * An inbound event set.
40  * 
41  * @author peter
42  */
43 public class CambriaEventSet {
44         private final reader fReader;
45
46         /**
47          * constructor initialization
48          * 
49          * @param mediaType
50          * @param originalStream
51          * @param chunked
52          * @param defPartition
53          * @throws CambriaApiException
54          */
55         public CambriaEventSet(String mediaType, InputStream originalStream,
56                         boolean chunked, String defPartition) throws CambriaApiException {
57                 InputStream is = originalStream;
58                 if (chunked) {
59                         is = new ChunkedInputStream(originalStream);
60                 }
61
62                 if (("application/json").equals(mediaType)) {
63                         if (chunked) {
64                                 throw new CambriaApiException(
65                                                 HttpServletResponse.SC_BAD_REQUEST,
66                                                 "The JSON stream reader doesn't support chunking.");
67                         }
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)) {
72                         try {
73                                 is = new GZIPInputStream(is);
74                         } catch (IOException e) {
75                                 throw new CambriaApiException(HttpStatusCodes.k400_badRequest,
76                                                 "Couldn't read compressed format: " + e);
77                         }
78                         fReader = new CambriaStreamReader(is);
79                 } else if (("text/plain").equals(mediaType)) {
80                         fReader = new CambriaTextStreamReader(is, defPartition);
81                 } else {
82                         fReader = new CambriaRawStreamReader(is, defPartition);
83                 }
84         }
85
86         /**
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
89          * closed/broken).
90          * 
91          * @return a message, or null
92          * @throws IOException
93          * @throws CambriaApiException
94          */
95         public message next() throws IOException, CambriaApiException {
96                 return fReader.next();
97         }
98
99         /**
100          * 
101          * @author anowarul.islam
102          *
103          */
104         public interface reader {
105                 /**
106                  * 
107                  * @return
108                  * @throws IOException
109                  * @throws CambriaApiException
110                  */
111                 message next() throws IOException, CambriaApiException;
112         }
113 }