DMAAP-MR - Merge MR repos
[dmaap/messagerouter/messageservice.git] / src / main / java / org / onap / dmaap / dmf / mr / resources / streamReaders / CambriaTextStreamReader.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.streamReaders;
23
24 import org.onap.dmaap.dmf.mr.CambriaApiException;
25 import org.onap.dmaap.dmf.mr.backends.Publisher.message;
26 import org.onap.dmaap.dmf.mr.beans.LogDetails;
27 import org.onap.dmaap.dmf.mr.resources.CambriaEventSet.reader;
28
29 import javax.servlet.http.HttpServletResponse;
30 import java.io.BufferedReader;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.io.InputStreamReader;
34
35 /**
36  * This stream reader just pulls single lines. It uses the default partition if provided. If
37  * not, the key is the current time, which does not guarantee ordering.
38  * 
39  * @author peter
40  *
41  */
42 public class CambriaTextStreamReader implements reader
43 {
44         /**
45          * This is the constructor for Cambria Text Reader format
46          * @param is
47          * @param defPart
48          * @throws CambriaApiException
49          */
50         public CambriaTextStreamReader ( InputStream is, String defPart ) throws CambriaApiException
51         {
52                 fReader = new BufferedReader ( new InputStreamReader ( is ) );
53                 fDefPart = defPart;
54         }
55
56         @Override
57         /**
58          * next() method iterates through msg length
59          * throws IOException
60          * throws CambriaApiException
61          * 
62          */ 
63         public message next () throws CambriaApiException
64         {
65                 try
66                 {
67                         final String line = fReader.readLine ();
68                         if ( line == null ) {
69                                 return null;
70                         }
71                         
72                         return new message ()
73                         {
74                                 private LogDetails logDetails;
75                                 private boolean transactionEnabled;
76
77                                 /**
78                                  * returns boolean value which 
79                                  * indicates whether transaction is enabled
80                                  * @return
81                                  */
82                                 public boolean isTransactionEnabled() {
83                                         return transactionEnabled;
84                                 }
85
86                                 /**
87                                  * sets boolean value which 
88                                  * indicates whether transaction is enabled
89                                  */
90                                 public void setTransactionEnabled(boolean transactionEnabled) {
91                                         this.transactionEnabled = transactionEnabled;
92                                 }
93                                 
94                                 @Override
95                                 /**
96                                  * @returns key
97                                  * It ch4ecks whether fDefPart value is Null.
98                                  * If yes, it will return ystem.currentTimeMillis () else
99                                  * it will return fDefPart variable value
100                                  */
101                                 public String getKey ()
102                                 {
103                                         return fDefPart == null ? "" + System.currentTimeMillis () : fDefPart;
104                                 }
105
106                                 @Override
107                                 /**
108                                  * returns the message in String type object
109                                  * @return
110                                  */
111                                 public String getMessage ()
112                                 {
113                                         return line;
114                                 }
115
116                                 @Override
117                                 /**
118                                  * set log details in logDetails variable
119                                  */
120                                 public void setLogDetails(LogDetails logDetails) {
121                                         this.logDetails = logDetails;
122                                 }
123
124                                 @Override
125                                 /**
126                                  * get the log details
127                                  */
128                                 public LogDetails getLogDetails() {
129                                         return this.logDetails;
130                                 }
131                         };
132                 }
133                 catch ( IOException e )
134                 {
135                         throw new CambriaApiException ( HttpServletResponse.SC_BAD_REQUEST, e.getMessage () );
136                 }
137         }
138         
139         private final BufferedReader fReader;
140         private final String fDefPart;
141 }