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 {
private final Map> availableOutputs;
@Inject
public MessageOutputFactory(Map> availableOutputs) {
this.availableOutputs = availableOutputs;
}
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);
final MessageOutput.Factory factory = this.availableOutputs.get(outputType);
Preconditions.checkArgument(factory != null, "Output type is not supported: %s!", outputType);
return factory.create(stream, configuration);
}
public Map getAvailableOutputs() {
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 = AvailableOutputSummary.create(
descriptor.getName(),
messageOutputEntry.getKey(),
descriptor.getHumanName(),
descriptor.getLinkToDocs(),
messageOutputFactoryClass.getConfig().getRequestedConfiguration()
);
result.put(messageOutputEntry.getKey(), availableOutputSummary);
}
return result;
}
public MessageOutput.Factory get(String type) {
return this.availableOutputs.get(type);
}
}