e48266634ff7f8c37c09f9a404de4e0747d55b53
[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
25 import org.onap.policy.apex.service.engine.event.ApexEventException;
26 import org.onap.policy.apex.service.parameters.eventprotocol.EventProtocolParameters;
27 import org.onap.policy.apex.service.parameters.eventprotocol.EventProtocolTextCharDelimitedParameters;
28 import org.onap.policy.apex.service.parameters.eventprotocol.EventProtocolTextTokenDelimitedParameters;
29 import org.slf4j.ext.XLogger;
30 import org.slf4j.ext.XLoggerFactory;
31
32 /**
33  * This factory creates text block readers for breaking character streams into blocks of text.
34  *
35  * @author Liam Fallon (liam.fallon@ericsson.com)
36  */
37 public class TextBlockReaderFactory {
38     // The logger for this class
39     private static final XLogger LOGGER = XLoggerFactory.getXLogger(TextBlockReaderFactory.class);
40
41     /**
42      * Get a text block reader for the given event protocol.
43      *
44      * @param inputStream the input stream that will be used for reading
45      * @param eventProtocolParameters the parameters that have been specified for event protocols
46      * @return the tagged reader
47      * @throws ApexEventException On an unsupported event protocol
48      */
49     public TextBlockReader getTaggedReader(final InputStream inputStream,
50             final EventProtocolParameters eventProtocolParameters) throws ApexEventException {
51         // Check the type of event protocol we have
52         if (eventProtocolParameters instanceof EventProtocolTextCharDelimitedParameters) {
53             // We have character delimited textual input
54             final EventProtocolTextCharDelimitedParameters charDelimitedParameters =
55                     (EventProtocolTextCharDelimitedParameters) eventProtocolParameters;
56
57             // Create the text block reader
58             final TextBlockReader characterDelimitedTextBlockReader =
59                     new CharacterDelimitedTextBlockReader(charDelimitedParameters);
60             characterDelimitedTextBlockReader.init(inputStream);
61             return characterDelimitedTextBlockReader;
62         } else if (eventProtocolParameters instanceof EventProtocolTextTokenDelimitedParameters) {
63             // We have token delimited textual input
64             final EventProtocolTextTokenDelimitedParameters tokenDelimitedParameters =
65                     (EventProtocolTextTokenDelimitedParameters) eventProtocolParameters;
66
67             // Create the text block reader
68             final HeaderDelimitedTextBlockReader headerDelimitedTextBlockReader =
69                     new HeaderDelimitedTextBlockReader(tokenDelimitedParameters);
70             headerDelimitedTextBlockReader.init(inputStream);
71             return headerDelimitedTextBlockReader;
72         } else {
73             final String errorMessage =
74                     "could not create text block reader for a textual event protocol, the required type "
75                             + eventProtocolParameters.getLabel() + " is not supported";
76             LOGGER.error(errorMessage);
77             throw new ApexEventException(errorMessage);
78         }
79     }
80 }