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