9eb16913aa2eb9bcfbff2ebe8b7dcb38e471b983
[dmaap/messagerouter/msgrtr.git] / src / main / java / org / onap / dmaap / messagerouter / msgrtr / nsa / cambria / 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.messagerouter.msgrtr.nsa.cambria.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.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;
36
37 import com.att.nsa.apiServer.streams.ChunkedInputStream;
38 import com.att.nsa.drumlin.service.standards.HttpStatusCodes;
39
40 /**
41  * An inbound event set.
42  * 
43  * @author author
44  */
45 public class CambriaEventSet {
46         private final reader fReader;
47
48         /**
49          * constructor initialization
50          * 
51          * @param mediaType
52          * @param originalStream
53          * @param chunked
54          * @param defPartition
55          * @throws CambriaApiException
56          */
57         public CambriaEventSet(String mediaType, InputStream originalStream,
58                         boolean chunked, String defPartition) throws CambriaApiException {
59                 InputStream is = originalStream;
60                 if (chunked) {
61                         is = new ChunkedInputStream(originalStream);
62                 }
63
64                 if (("application/json").equals(mediaType)) {
65                         if (chunked) {
66                                 throw new CambriaApiException(
67                                                 HttpServletResponse.SC_BAD_REQUEST,
68                                                 "The JSON stream reader doesn't support chunking.");
69                         }
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)) {
74                         try {
75                                 is = new GZIPInputStream(is);
76                         } catch (IOException e) {
77                                 throw new CambriaApiException(HttpStatusCodes.k400_badRequest,
78                                                 "Couldn't read compressed format: " + e);
79                         }
80                         fReader = new CambriaStreamReader(is);
81                 } else if (("text/plain").equals(mediaType)) {
82                         fReader = new CambriaTextStreamReader(is, defPartition);
83                 } else {
84                         fReader = new CambriaRawStreamReader(is, defPartition);
85                 }
86         }
87
88         /**
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
91          * closed/broken).
92          * 
93          * @return a message, or null
94          * @throws IOException
95          * @throws CambriaApiException
96          */
97         public message next() throws IOException, CambriaApiException {
98                 return fReader.next();
99         }
100
101         /**
102          * 
103          * @author author
104          *
105          */
106         public interface reader {
107                 /**
108                  * 
109                  * @return
110                  * @throws IOException
111                  * @throws CambriaApiException
112                  */
113                 message next() throws IOException, CambriaApiException;
114         }
115 }