Changes for checkstyle 8.32
[policy/apex-pdp.git] / services / services-engine / src / main / java / org / onap / policy / apex / service / engine / event / impl / filecarrierplugin / consumer / TextBlockReaderFactory.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * 
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.consumer;
22
23 import java.io.InputStream;
24 import org.onap.policy.apex.service.engine.event.ApexEventException;
25 import org.onap.policy.apex.service.parameters.eventprotocol.EventProtocolParameters;
26 import org.onap.policy.apex.service.parameters.eventprotocol.EventProtocolTextCharDelimitedParameters;
27 import org.onap.policy.apex.service.parameters.eventprotocol.EventProtocolTextTokenDelimitedParameters;
28 import org.slf4j.ext.XLogger;
29 import org.slf4j.ext.XLoggerFactory;
30
31 /**
32  * This factory creates text block readers for breaking character streams into blocks of text.
33  *
34  * @author Liam Fallon (liam.fallon@ericsson.com)
35  */
36 public class TextBlockReaderFactory {
37     // The logger for this class
38     private static final XLogger LOGGER = XLoggerFactory.getXLogger(TextBlockReaderFactory.class);
39
40     /**
41      * Get a text block reader for the given event protocol.
42      *
43      * @param inputStream the input stream that will be used for reading
44      * @param eventProtocolParameters the parameters that have been specified for event protocols
45      * @return the tagged reader
46      * @throws ApexEventException On an unsupported event protocol
47      */
48     public TextBlockReader getTaggedReader(final InputStream inputStream,
49             final EventProtocolParameters eventProtocolParameters) throws ApexEventException {
50         // Check the type of event protocol we have
51         if (eventProtocolParameters instanceof EventProtocolTextCharDelimitedParameters) {
52             // We have character delimited textual input
53             final EventProtocolTextCharDelimitedParameters charDelimitedParameters =
54                     (EventProtocolTextCharDelimitedParameters) eventProtocolParameters;
55
56             // Create the text block reader
57             final TextBlockReader characterDelimitedTextBlockReader =
58                     new CharacterDelimitedTextBlockReader(charDelimitedParameters);
59             characterDelimitedTextBlockReader.init(inputStream);
60             return characterDelimitedTextBlockReader;
61         } else if (eventProtocolParameters instanceof EventProtocolTextTokenDelimitedParameters) {
62             // We have token delimited textual input
63             final EventProtocolTextTokenDelimitedParameters tokenDelimitedParameters =
64                     (EventProtocolTextTokenDelimitedParameters) eventProtocolParameters;
65
66             // Create the text block reader
67             final HeaderDelimitedTextBlockReader headerDelimitedTextBlockReader =
68                     new HeaderDelimitedTextBlockReader(tokenDelimitedParameters);
69             headerDelimitedTextBlockReader.init(inputStream);
70             return headerDelimitedTextBlockReader;
71         } else {
72             final String errorMessage =
73                     "could not create text block reader for a textual event protocol, the required type "
74                             + eventProtocolParameters.getLabel() + " is not supported";
75             LOGGER.error(errorMessage);
76             throw new ApexEventException(errorMessage);
77         }
78     }
79 }