-
Notifications
You must be signed in to change notification settings - Fork 1.3k
NUTCH-2522 Bidirectional URL exemption filter #290
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
okedoki
wants to merge
9
commits into
apache:master
Choose a base branch
from
okedoki:NUTCH-2522
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a898b5c
Merge branch 'master', remote branch 'origin'
okedoki 07cfff4
Merge remote branch 'origin/master'
8d5b917
Merge remote branch 'origin/master'
okedoki 3977018
bidirectionexemptionurlfilter added
okedoki bbe5409
added db-ignore-external-exemptions-bidirectional.txt in conf folder
okedoki 3b07d03
fixed name of jar for bidirectional exemption url filter
okedoki 20c46ed
refactoring of bidirectional exemption filter, inspired by urlnormali…
okedoki 375bc56
added super setup of config for BidirectionalExemptionUrlFilter
okedoki 010c0a2
FileReader was replaced with Reader in urlfilter-ignoreexempt-bidirec…
okedoki File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| # 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. | ||
| # | ||
| # | ||
| # Exemption rules to db.ignore.external.links | ||
|
|
||
|
|
||
| # Format : | ||
| #-------- | ||
| # The format is same same as `regex-urlfilter.txt`. | ||
| # Each non-comment, non-blank line contains a regular expression | ||
|
lewismc marked this conversation as resolved.
Outdated
|
||
| # prefixed by '+' or '-'. The first matching pattern in the file | ||
| # determines whether a URL is exempted or ignored. If no pattern | ||
| # matches, the URL is ignored. | ||
|
|
||
|
|
||
|
|
||
| # Example 1: | ||
| #---------- | ||
| # To exempt urls ending with image extensions, uncomment the below line | ||
|
lewismc marked this conversation as resolved.
Outdated
|
||
| -(www.) | ||
|
lewismc marked this conversation as resolved.
Outdated
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| <?xml version="1.0"?> | ||
| <!-- | ||
| 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. | ||
| --> | ||
| <project name="urlfilter-ignoreexempt-bidirectional" default="jar-core"> | ||
|
|
||
| <import file="../build-plugin.xml"/> | ||
|
|
||
| <!-- Build compilation dependencies --> | ||
| <target name="deps-jar"> | ||
| <ant target="jar" inheritall="false" dir="../lib-regex-filter"/> | ||
| </target> | ||
|
|
||
| <!-- Add compilation dependencies to classpath --> | ||
| <path id="plugin.deps"> | ||
| <fileset dir="${nutch.root}/build"> | ||
| <include name="**/lib-regex-filter/*.jar" /> | ||
| <include name="**/urlfilter-regex/*.jar" /> | ||
| </fileset> | ||
| <pathelement location="${nutch.root}/build/lib-regex-filter/test"/> | ||
| </path> | ||
|
|
||
|
|
||
| </project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| <?xml version="1.0" ?> | ||
|
|
||
| <!-- | ||
| 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. | ||
| --> | ||
|
|
||
| <ivy-module version="1.0"> | ||
| <info organisation="org.apache.nutch" module="${ant.project.name}"> | ||
| <license name="Apache 2.0"/> | ||
| <ivyauthor name="Apache Nutch Team" url="http://nutch.apache.org"/> | ||
| <description> | ||
| Apache Nutch | ||
| </description> | ||
| </info> | ||
|
|
||
| <configurations> | ||
| <include file="../../..//ivy/ivy-configurations.xml"/> | ||
| </configurations> | ||
|
|
||
| <publications> | ||
| <!--get the artifact from our module name--> | ||
| <artifact conf="master"/> | ||
| </publications> | ||
|
|
||
| <dependencies> | ||
| </dependencies> | ||
|
|
||
| </ivy-module> |
45 changes: 45 additions & 0 deletions
45
src/plugin/urlfilter-ignoreexempt-bidirectional/plugin.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <!-- | ||
| 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. | ||
| --> | ||
| <plugin | ||
| id="urlfilter-ignoreexempt-bidirectional" | ||
| name="External Domain Ignore Exemption Bidirectional" | ||
| version="1.0.0" | ||
| provider-name="nutch.org"> | ||
|
|
||
| <runtime> | ||
| <library name="urlfilter-ignoreexemptbidirectional.jar"> | ||
| <export name="*"/> | ||
| </library> | ||
| </runtime> | ||
|
|
||
| <requires> | ||
| <import plugin="nutch-extensionpoints"/> | ||
| <import plugin="lib-regex-filter"/> | ||
| <import plugin="urlfilter-regex"/> | ||
| </requires> | ||
|
|
||
| <extension id="org.apache.nutch.urlfilter.ignoreexempt.bidirectional" | ||
| name="Ignore Exemption Url Filter Bidirectional" | ||
| point="org.apache.nutch.net.URLExemptionFilter"> | ||
| <implementation id="BidirectionalExemptionUrlFilter" | ||
| class="org.apache.nutch.urlfilter.ignoreexempt.bidirectional.BidirectionalExemptionUrlFilter"> | ||
| <parameter name="file" value="db-ignore-external-exemptions-bidirectional.txt"/> | ||
| </implementation> | ||
| </extension> | ||
|
|
||
| </plugin> |
108 changes: 108 additions & 0 deletions
108
...rg/apache/nutch/urlfilter/ignoreexempt/bidirectional/BidirectionalExemptionUrlFilter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| /* | ||
| * 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.nutch.urlfilter.ignoreexempt.bidirectional; | ||
|
|
||
| import org.apache.commons.io.IOUtils; | ||
| import org.apache.hadoop.conf.Configuration; | ||
| import org.apache.nutch.net.URLExemptionFilter; | ||
| import org.apache.nutch.util.NutchConfiguration; | ||
| import org.apache.nutch.util.URLUtil; | ||
| import org.apache.nutch.urlfilter.api.RegexRule; | ||
| import org.apache.nutch.urlfilter.regex.RegexURLFilter; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import java.lang.invoke.MethodHandles; | ||
| import java.net.MalformedURLException; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.io.Reader; | ||
| import java.util.Arrays; | ||
| import java.util.regex.Pattern; | ||
| import java.util.List; | ||
| import java.util.ArrayList; | ||
|
|
||
|
|
||
| /** | ||
| * This implementation of {@link org.apache.nutch.net.URLExemptionFilter} uses regex configuration for both fromUrl and toUrl | ||
| * to check if URL is eligible for exemption from 'db.ignore.external'. | ||
| * When this filter is enabled, the external urls will be checked against configured sequence of regex rules. | ||
| *<p> | ||
| * The exemption rule file defaults to db-ignore-external-exemptions-bidirectional.txt in the classpath but can be | ||
| * overridden using the property <code>"db.ignore.external.exemptions.bidirectional.file" in ./conf/nutch-*.xml</code> | ||
| *</p> | ||
| * @since Mar 1, 2018 | ||
| * @version 1 | ||
| * @see org.apache.nutch.net.URLExemptionFilter | ||
| * @see org.apache.nutch.urlfilter.regex.RegexURLFilter | ||
| */ | ||
| public class BidirectionalExemptionUrlFilter extends RegexURLFilter | ||
| implements URLExemptionFilter { | ||
|
|
||
| public static final String DB_IGNORE_EXTERNAL_EXEMPTIONS_BIDIRECTIONAL_FILE | ||
| = "db.ignore.external.exemptions.bidirectional.file"; | ||
| private static final Logger LOG = LoggerFactory | ||
| .getLogger(MethodHandles.lookup().lookupClass()); | ||
|
|
||
| @Override | ||
| //This implementation checks rules exceptions for two arbitrary urls. True if reg_ex(toUrl) = fromUrl | ||
| public boolean filter(String fromUrl, String toUrl) { | ||
|
|
||
| String sourceHost = URLUtil.getHost(fromUrl).toLowerCase(); | ||
| String sourceDestination = URLUtil.getHost(toUrl).toLowerCase(); | ||
|
|
||
| if (LOG.isDebugEnabled()) { | ||
| LOG.debug("BidirectionalExemptionUrlFilter. Source url: " + fromUrl + " and destination url " + toUrl); | ||
| } | ||
|
|
||
| String modifiedSourceHost = sourceHost; | ||
| String modifiedDestinationHost = sourceDestination; | ||
| for (RegexRule rule : super.getRules()) { | ||
|
|
||
| if (LOG.isDebugEnabled()) { | ||
| LOG.debug("Applying rule [" + rule.regex() + "]"); | ||
| } | ||
|
|
||
| modifiedSourceHost = rule.replace(modifiedSourceHost, ""); | ||
| modifiedDestinationHost = rule.replace(modifiedDestinationHost, ""); | ||
| }; | ||
|
|
||
| return modifiedSourceHost.equals(modifiedDestinationHost); | ||
| } | ||
|
|
||
|
|
||
| protected Reader getRulesReader(Configuration conf) | ||
| throws IOException { | ||
| String fileRules = conf.get(DB_IGNORE_EXTERNAL_EXEMPTIONS_BIDIRECTIONAL_FILE); | ||
| this.getConf(); | ||
| return conf.getConfResourceAsReader(fileRules); | ||
| } | ||
| public static void main(String[] args) { | ||
|
|
||
| if (args.length != 2) { | ||
| System.out.println("Error: Invalid Args"); | ||
| System.out.println("Usage: " + | ||
| BidirectionalExemptionUrlFilter.class.getName() + " <url_source, url_destination>"); | ||
| return; | ||
| } | ||
| String sourceUrl = args[0]; | ||
| String destinationUrl = args[1]; | ||
| BidirectionalExemptionUrlFilter instance = new BidirectionalExemptionUrlFilter(); | ||
| instance.setConf(NutchConfiguration.create()); | ||
| System.out.println(instance.filter(sourceUrl, destinationUrl)); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.