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;
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;
33 * This factory creates text block readers for breaking character streams into blocks of text.
35 * @author Liam Fallon (liam.fallon@ericsson.com)
37 public class TextBlockReaderFactory {
38 // The logger for this class
39 private static final XLogger LOGGER = XLoggerFactory.getXLogger(TextBlockReaderFactory.class);
42 * Get a text block reader for the given event protocol.
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
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;
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;
67 // Create the text block reader
68 final HeaderDelimitedTextBlockReader headerDelimitedTextBlockReader =
69 new HeaderDelimitedTextBlockReader(tokenDelimitedParameters);
70 headerDelimitedTextBlockReader.init(inputStream);
71 return headerDelimitedTextBlockReader;
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);