org.graylog2.outputs.MessageOutputFactory Maven / Gradle / Ivy
/**
* This file is part of Graylog.
*
* Graylog is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Graylog is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Graylog. If not, see .
*/
package org.graylog2.outputs;
import com.google.common.base.Preconditions;
import org.graylog2.plugin.configuration.Configuration;
import org.graylog2.plugin.outputs.MessageOutput;
import org.graylog2.plugin.outputs.MessageOutputConfigurationException;
import org.graylog2.plugin.streams.Output;
import org.graylog2.plugin.streams.Stream;
import org.graylog2.rest.resources.streams.outputs.AvailableOutputSummary;
import javax.inject.Inject;
import java.util.HashMap;
import java.util.Map;
public class MessageOutputFactory {
// We have two output lists for backwards compatibility.
// See comments in MessageOutput.Factory and MessageOutput.Factory2 for details
private final Map> availableOutputs;
private final Map> availableOutputs2;
@Inject
public MessageOutputFactory(Map> availableOutputs,
Map> availableOutputs2) {
this.availableOutputs = availableOutputs;
this.availableOutputs2 = availableOutputs2;
}
public MessageOutput fromStreamOutput(Output output, final Stream stream, Configuration configuration) throws MessageOutputConfigurationException {
Preconditions.checkNotNull(output);
Preconditions.checkNotNull(stream);
Preconditions.checkNotNull(configuration);
final String outputType = output.getType();
Preconditions.checkArgument(outputType != null);
// We have two output lists for backwards compatibility.
// See comments in MessageOutput.Factory and MessageOutput.Factory2 for details
final MessageOutput.Factory2 factory2 = this.availableOutputs2.get(outputType);
final MessageOutput.Factory factory = this.availableOutputs.get(outputType);
// If the same outputType value exists in both output factory maps for some reason, we want to prefer the
// one that is using the more recent interface.
if (factory2 != null) {
return factory2.create(output, stream, configuration);
} else if (factory != null) {
return factory.create(stream, configuration);
} else {
throw new IllegalArgumentException("Output type is not supported: " + outputType);
}
}
public Map getAvailableOutputs() {
// We have two output lists for backwards compatibility.
// See comments in MessageOutput.Factory and MessageOutput.Factory2 for details
final Map result = new HashMap<>(availableOutputs.size());
for (Map.Entry> messageOutputEntry : this.availableOutputs.entrySet()) {
final MessageOutput.Factory messageOutputFactoryClass = messageOutputEntry.getValue();
final MessageOutput.Descriptor descriptor = messageOutputFactoryClass.getDescriptor();
final AvailableOutputSummary availableOutputSummary = createOutputSummary(messageOutputEntry.getKey(),
descriptor, messageOutputFactoryClass.getConfig());
result.put(messageOutputEntry.getKey(), availableOutputSummary);
}
for (Map.Entry> messageOutputEntry : this.availableOutputs2.entrySet()) {
final MessageOutput.Factory2 messageOutputFactoryClass = messageOutputEntry.getValue();
final MessageOutput.Descriptor descriptor = messageOutputFactoryClass.getDescriptor();
final AvailableOutputSummary availableOutputSummary = createOutputSummary(messageOutputEntry.getKey(),
descriptor, messageOutputFactoryClass.getConfig());
result.put(messageOutputEntry.getKey(), availableOutputSummary);
}
return result;
}
private AvailableOutputSummary createOutputSummary(String outputType,
MessageOutput.Descriptor descriptor,
MessageOutput.Config config) {
return AvailableOutputSummary.create(
descriptor.getName(),
outputType,
descriptor.getHumanName(),
descriptor.getLinkToDocs(),
config.getRequestedConfiguration()
);
}
}