All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.rosette.elasticsearch.RosetteAbstractProcessor Maven / Gradle / Ivy

There is a newer version: 7.17.0.0
Show newest version
/*
* Copyright 2020 Basis Technology Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.rosette.elasticsearch;

import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.SpecialPermission;
import org.elasticsearch.common.Strings;
import org.elasticsearch.ingest.AbstractProcessor;
import org.elasticsearch.ingest.IngestDocument;

/**
 * Class that holds code shared by all Rosette ingest processors
 */
public abstract class RosetteAbstractProcessor extends AbstractProcessor {

    protected String inputField;
    protected String targetField;
    protected RosetteApiWrapper rosAPI;
    protected final String processorType;

    RosetteAbstractProcessor(RosetteApiWrapper rosAPI, String tag, String description, String processorType,
                             String inputField, String targetField) {
        super(tag, description);
        this.inputField = inputField;
        this.targetField = targetField;
        this.rosAPI = rosAPI;
        this.processorType = processorType;
    }

    @Override
    public IngestDocument execute(IngestDocument ingestDocument) throws Exception {
        if (ingestDocument.hasField(targetField)) {
            throw new ElasticsearchException("Document already contains data in target field for this ingest "
                    + "processor: " + processorType);
        }
        if (!ingestDocument.hasField(inputField)) {
            //Do nothing
            return ingestDocument;
        }

        String inputText = ingestDocument.getFieldValue(inputField, String.class);

        if (Strings.isNullOrEmpty(inputText)) {
            //Do nothing
            return ingestDocument;
        }

        SecurityManager sm = System.getSecurityManager();
        if (sm != null) {
            sm.checkPermission(new SpecialPermission());
        }

        processDocument(inputText, ingestDocument);
        return ingestDocument;
    }

    @Override
    public String getType() {
        return this.processorType;
    }

    /**
     * Performs processor specific modifications to the document.
     * @param inputText value of the field in the document that was specified as the input field to process
     * @param ingestDocument document to be ingested
     * @throws Exception when something goes wrong
     */
    protected abstract void processDocument(String inputText, IngestDocument ingestDocument) throws Exception;
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy