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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.consumer;
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;
32 * This factory creates text block readers for breaking character streams into blocks of text.
34 * @author Liam Fallon (liam.fallon@ericsson.com)
36 public class TextBlockReaderFactory {
37 // The logger for this class
38 private static final XLogger LOGGER = XLoggerFactory.getXLogger(TextBlockReaderFactory.class);
41 * Get a text block reader for the given event protocol.
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
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;
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;
66 // Create the text block reader
67 final HeaderDelimitedTextBlockReader headerDelimitedTextBlockReader =
68 new HeaderDelimitedTextBlockReader(tokenDelimitedParameters);
69 headerDelimitedTextBlockReader.init(inputStream);
70 return headerDelimitedTextBlockReader;
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);