1111
1212import org .apache .http .conn .util .PublicSuffixMatcher ;
1313import org .apache .http .conn .util .PublicSuffixMatcherLoader ;
14+ import org .elasticsearch .core .Nullable ;
1415import org .elasticsearch .ingest .AbstractProcessor ;
1516import org .elasticsearch .ingest .ConfigurationUtils ;
1617import org .elasticsearch .ingest .IngestDocument ;
1920import java .util .Map ;
2021
2122public class RegisteredDomainProcessor extends AbstractProcessor {
22- private static final PublicSuffixMatcher SUFFIX_MATCHER = PublicSuffixMatcherLoader .getDefault ();
2323
2424 public static final String TYPE = "registered_domain" ;
25+ private static final PublicSuffixMatcher SUFFIX_MATCHER = PublicSuffixMatcherLoader .getDefault ();
2526
2627 private final String field ;
2728 private final String targetField ;
@@ -47,111 +48,86 @@ public boolean getIgnoreMissing() {
4748 }
4849
4950 @ Override
50- public IngestDocument execute (IngestDocument ingestDocument ) throws Exception {
51- DomainInfo info = getRegisteredDomain (ingestDocument );
51+ public IngestDocument execute (IngestDocument document ) throws Exception {
52+ final String fqdn = document .getFieldValue (field , String .class , ignoreMissing );
53+ final DomainInfo info = getRegisteredDomain (fqdn );
5254 if (info == null ) {
5355 if (ignoreMissing ) {
54- return ingestDocument ;
56+ return document ;
5557 } else {
5658 throw new IllegalArgumentException ("unable to set domain information for document" );
5759 }
5860 }
5961 String fieldPrefix = targetField ;
60- if (fieldPrefix .equals ( "" ) == false ) {
62+ if (fieldPrefix .isEmpty ( ) == false ) {
6163 fieldPrefix += "." ;
6264 }
6365 String domainTarget = fieldPrefix + "domain" ;
6466 String registeredDomainTarget = fieldPrefix + "registered_domain" ;
6567 String subdomainTarget = fieldPrefix + "subdomain" ;
6668 String topLevelDomainTarget = fieldPrefix + "top_level_domain" ;
6769
68- if (info .getDomain () != null ) {
69- ingestDocument .setFieldValue (domainTarget , info .getDomain ());
70+ if (info .domain () != null ) {
71+ document .setFieldValue (domainTarget , info .domain ());
7072 }
71- if (info .getRegisteredDomain () != null ) {
72- ingestDocument .setFieldValue (registeredDomainTarget , info .getRegisteredDomain ());
73+ if (info .registeredDomain () != null ) {
74+ document .setFieldValue (registeredDomainTarget , info .registeredDomain ());
7375 }
74- if (info .getETLD () != null ) {
75- ingestDocument .setFieldValue (topLevelDomainTarget , info .getETLD ());
76+ if (info .eTLD () != null ) {
77+ document .setFieldValue (topLevelDomainTarget , info .eTLD ());
7678 }
77- if (info .getSubdomain () != null ) {
78- ingestDocument .setFieldValue (subdomainTarget , info .getSubdomain ());
79+ if (info .subdomain () != null ) {
80+ document .setFieldValue (subdomainTarget , info .subdomain ());
7981 }
80- return ingestDocument ;
82+ return document ;
8183 }
8284
83- private DomainInfo getRegisteredDomain (IngestDocument d ) {
84- String fieldString = d .getFieldValue (field , String .class , ignoreMissing );
85- if (fieldString == null ) {
85+ @ Nullable
86+ // visible for testing
87+ static DomainInfo getRegisteredDomain (@ Nullable String fqdn ) {
88+ if (fqdn == null ) {
8689 return null ;
8790 }
88- String registeredDomain = SUFFIX_MATCHER .getDomainRoot (fieldString );
91+ String registeredDomain = SUFFIX_MATCHER .getDomainRoot (fqdn );
8992 if (registeredDomain == null ) {
90- if (SUFFIX_MATCHER .matches (fieldString )) {
91- return new DomainInfo ( fieldString );
93+ if (SUFFIX_MATCHER .matches (fqdn )) {
94+ return DomainInfo . of ( fqdn );
9295 }
9396 return null ;
9497 }
9598 if (registeredDomain .indexOf ('.' ) == -1 ) {
9699 // we have domain with no matching public suffix, but "." in it
97100 return null ;
98101 }
99- return new DomainInfo (registeredDomain , fieldString );
102+ return DomainInfo . of (registeredDomain , fqdn );
100103 }
101104
102105 @ Override
103106 public String getType () {
104107 return TYPE ;
105108 }
106109
107- private static class DomainInfo {
108- private final String domain ;
109- private final String registeredDomain ;
110- private final String eTLD ;
111- private final String subdomain ;
112-
113- private DomainInfo (String eTLD ) {
114- this .domain = eTLD ;
115- this .eTLD = eTLD ;
116- this .registeredDomain = null ;
117- this .subdomain = null ;
110+ // visible for testing
111+ record DomainInfo (
112+ String domain ,
113+ String registeredDomain ,
114+ String eTLD , // n.b. https://developer.mozilla.org/en-US/docs/Glossary/eTLD
115+ String subdomain
116+ ) {
117+ static DomainInfo of (final String eTLD ) {
118+ return new DomainInfo (eTLD , null , eTLD , null );
118119 }
119120
120- private DomainInfo ( String registeredDomain , String domain ) {
121+ static DomainInfo of ( final String registeredDomain , final String domain ) {
121122 int index = registeredDomain .indexOf ('.' ) + 1 ;
122123 if (index > 0 && index < registeredDomain .length ()) {
123- this .domain = domain ;
124- this .eTLD = registeredDomain .substring (index );
125- this .registeredDomain = registeredDomain ;
126124 int subdomainIndex = domain .lastIndexOf ("." + registeredDomain );
127- if (subdomainIndex > 0 ) {
128- this .subdomain = domain .substring (0 , subdomainIndex );
129- } else {
130- this .subdomain = null ;
131- }
125+ final String subdomain = subdomainIndex > 0 ? domain .substring (0 , subdomainIndex ) : null ;
126+ return new DomainInfo (domain , registeredDomain , registeredDomain .substring (index ), subdomain );
132127 } else {
133- this .domain = null ;
134- this .eTLD = null ;
135- this .registeredDomain = null ;
136- this .subdomain = null ;
128+ return new DomainInfo (null , null , null , null );
137129 }
138130 }
139-
140- public String getDomain () {
141- return domain ;
142- }
143-
144- public String getSubdomain () {
145- return subdomain ;
146- }
147-
148- public String getRegisteredDomain () {
149- return registeredDomain ;
150- }
151-
152- public String getETLD () {
153- return eTLD ;
154- }
155131 }
156132
157133 public static final class Factory implements Processor .Factory {
@@ -161,15 +137,15 @@ public static final class Factory implements Processor.Factory {
161137 @ Override
162138 public RegisteredDomainProcessor create (
163139 Map <String , Processor .Factory > registry ,
164- String processorTag ,
140+ String tag ,
165141 String description ,
166142 Map <String , Object > config
167143 ) throws Exception {
168- String field = ConfigurationUtils .readStringProperty (TYPE , processorTag , config , "field" );
169- String targetField = ConfigurationUtils .readStringProperty (TYPE , processorTag , config , "target_field" , DEFAULT_TARGET_FIELD );
170- boolean ignoreMissing = ConfigurationUtils .readBooleanProperty (TYPE , processorTag , config , "ignore_missing" , true );
144+ String field = ConfigurationUtils .readStringProperty (TYPE , tag , config , "field" );
145+ String targetField = ConfigurationUtils .readStringProperty (TYPE , tag , config , "target_field" , DEFAULT_TARGET_FIELD );
146+ boolean ignoreMissing = ConfigurationUtils .readBooleanProperty (TYPE , tag , config , "ignore_missing" , true );
171147
172- return new RegisteredDomainProcessor (processorTag , description , field , targetField , ignoreMissing );
148+ return new RegisteredDomainProcessor (tag , description , field , targetField , ignoreMissing );
173149 }
174150 }
175151}
0 commit comments