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