Java tutorial
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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 org.apache.nifi.processors.att.m2x; import java.io.ByteArrayInputStream; import java.io.IOException; import java.util.*; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicReference; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; import com.squareup.okhttp.MediaType; import com.squareup.okhttp.OkHttpClient; import com.squareup.okhttp.Request; import com.squareup.okhttp.RequestBody; import com.squareup.okhttp.Response; import com.squareup.okhttp.ResponseBody; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.datatype.joda.JodaModule; import org.joda.time.DateTime; import org.joda.time.format.DateTimeFormatter; import org.joda.time.format.ISODateTimeFormat; import org.apache.nifi.att.m2x.M2XStreamValue; import org.apache.nifi.att.m2x.M2XStreamValues; import org.apache.nifi.components.PropertyDescriptor; import org.apache.nifi.components.PropertyValue; import org.apache.nifi.components.state.Scope; import org.apache.nifi.components.state.StateManager; import org.apache.nifi.components.state.StateMap; import org.apache.nifi.flowfile.FlowFile; import org.apache.nifi.processor.*; import org.apache.nifi.annotation.behavior.InputRequirement; import org.apache.nifi.annotation.behavior.InputRequirement.Requirement; import org.apache.nifi.annotation.behavior.Stateful; import org.apache.nifi.annotation.behavior.ReadsAttribute; import org.apache.nifi.annotation.behavior.ReadsAttributes; import org.apache.nifi.annotation.behavior.WritesAttribute; import org.apache.nifi.annotation.behavior.WritesAttributes; import org.apache.nifi.annotation.lifecycle.OnScheduled; import org.apache.nifi.annotation.documentation.CapabilityDescription; import org.apache.nifi.annotation.documentation.SeeAlso; import org.apache.nifi.annotation.documentation.Tags; import org.apache.nifi.logging.ProcessorLog; import org.apache.nifi.processor.exception.ProcessException; import org.apache.nifi.processor.util.StandardValidators; public abstract class AbstractM2XProcessor extends AbstractProcessor { public static final String M2X_NUMERIC_STREAM = "numeric"; public static final String M2X_TEXT_STREAM = "text/plain"; public static final String M2X_JSON_STREAM = "application/json"; public static final PropertyDescriptor M2X_STREAM_TYPE = new PropertyDescriptor.Builder() .name("m2x-stream-type").displayName("Stream Type").description("The M2X stream type").required(true) .defaultValue(M2X_TEXT_STREAM).allowableValues(M2X_NUMERIC_STREAM, M2X_TEXT_STREAM, M2X_JSON_STREAM) .addValidator(StandardValidators.NON_EMPTY_VALIDATOR).build(); public static final PropertyDescriptor M2X_API_KEY = new PropertyDescriptor.Builder().name("m2x-api-key") .displayName("API Key").description("The M2X API key").required(true) .addValidator(StandardValidators.NON_EMPTY_VALIDATOR).sensitive(true).build(); public static final PropertyDescriptor M2X_API_URL = new PropertyDescriptor.Builder().name("m2x-api-url") .displayName("API URL").description("The M2X API URL").required(true) .defaultValue("http://api-m2x.att.com/v2/").addValidator(StandardValidators.URL_VALIDATOR).build(); public static final Relationship REL_SUCCESS = new Relationship.Builder().name("success").description("success") .build(); private List<PropertyDescriptor> descriptors; private Set<Relationship> relationships; private AtomicReference<OkHttpClient> httpClientRef = new AtomicReference<>(); protected OkHttpClient getHttpClient() { return httpClientRef.get(); } @Override protected void init(final ProcessorInitializationContext context) { final List<PropertyDescriptor> descriptors = new ArrayList<PropertyDescriptor>(); descriptors.add(M2X_STREAM_TYPE); descriptors.add(M2X_API_KEY); descriptors.add(M2X_API_URL); this.descriptors = Collections.unmodifiableList(descriptors); final Set<Relationship> relationships = new HashSet<Relationship>(); relationships.add(REL_SUCCESS); this.relationships = Collections.unmodifiableSet(relationships); } @Override public Set<Relationship> getRelationships() { return relationships; } @Override public List<PropertyDescriptor> getSupportedPropertyDescriptors() { return descriptors; } @OnScheduled public void onScheduled(final ProcessContext context) { httpClientRef.set(new OkHttpClient()); } }