Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
277 additions
and
9 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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
148 changes: 148 additions & 0 deletions
148
src/main/java/focusedCrawler/link/classifier/builder/MozAuthenticator.java
This file contains 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,148 @@ | ||
package focusedCrawler.link.classifier.builder; | ||
|
||
import java.net.URLEncoder; | ||
import java.security.InvalidKeyException; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.util.Date; | ||
import javax.xml.bind.DatatypeConverter; | ||
|
||
import javax.crypto.Mac; | ||
import javax.crypto.spec.SecretKeySpec; | ||
|
||
public class MozAuthenticator { | ||
|
||
private static final String HMAC_SHA1_ALGORITHM = "HmacSHA1"; | ||
|
||
/** | ||
* accessID The user's Access ID | ||
*/ | ||
private String accessID; | ||
|
||
/** | ||
* secretKey The user's Secret Key | ||
*/ | ||
private String secretKey; | ||
|
||
/** | ||
* expiresInterval The interval after which the authentication string | ||
* expires Default 300s | ||
*/ | ||
private long expiresInterval = 300; | ||
|
||
public MozAuthenticator() { | ||
|
||
} | ||
|
||
/** | ||
* Constructor to set all the variables | ||
* | ||
* @param accessID | ||
* @param secretKey | ||
* @param expiresInterval | ||
*/ | ||
public MozAuthenticator(String accessID, String secretKey, long expiresInterval) { | ||
this.accessID = accessID; | ||
this.secretKey = secretKey; | ||
this.expiresInterval = expiresInterval; | ||
} | ||
|
||
/** | ||
* | ||
* This method calculates the authentication String based on the user's | ||
* credentials. | ||
* | ||
* Set the user credentials before calling this method | ||
* | ||
* @return the authentication string | ||
* | ||
* @see #setAccessID(String) | ||
* @see #setSecretKey(String) | ||
*/ | ||
public String getAuthenticationStr() { | ||
long expires = ((new Date()).getTime()) / 1000 + expiresInterval; | ||
|
||
String stringToSign = accessID + "\n" + expires; | ||
|
||
SecretKeySpec signingKey = new SecretKeySpec(secretKey.getBytes(), HMAC_SHA1_ALGORITHM); | ||
|
||
// get an hmac_sha1 Mac instance and initialize with the signing key | ||
Mac mac = null; | ||
try { | ||
mac = Mac.getInstance(HMAC_SHA1_ALGORITHM); | ||
mac.init(signingKey); | ||
} catch (NoSuchAlgorithmException e) { | ||
e.printStackTrace(); | ||
return ""; | ||
} catch (InvalidKeyException e) { | ||
e.printStackTrace(); | ||
return ""; | ||
} | ||
|
||
// compute the hmac on input data bytes | ||
byte[] rawHmac = mac.doFinal(stringToSign.getBytes()); | ||
|
||
// base64-encode the hmac | ||
String urlSafeSignature = URLEncoder.encode(EncodeBase64(rawHmac)); | ||
|
||
String authenticationStr = "AccessID=" + accessID + "&Expires=" + expires + "&Signature=" | ||
+ urlSafeSignature; | ||
|
||
return authenticationStr; | ||
} | ||
|
||
/** | ||
* Encodes the rawdata in Base64 format | ||
* | ||
* @param rawData | ||
* @return | ||
*/ | ||
public String EncodeBase64(byte[] rawData) { | ||
return DatatypeConverter.printBase64Binary(rawData); | ||
} | ||
|
||
/** | ||
* @return the accessID | ||
*/ | ||
public String getAccessID() { | ||
return accessID; | ||
} | ||
|
||
/** | ||
* @param accessID | ||
* the accessID to set | ||
*/ | ||
public void setAccessID(String accessID) { | ||
this.accessID = accessID; | ||
} | ||
|
||
/** | ||
* @return the secretKey | ||
*/ | ||
public String getSecretKey() { | ||
return secretKey; | ||
} | ||
|
||
/** | ||
* @param secretKey | ||
* the secretKey to set | ||
*/ | ||
public void setSecretKey(String secretKey) { | ||
this.secretKey = secretKey; | ||
} | ||
|
||
/** | ||
* @return the expiresInterval | ||
*/ | ||
public long getExpiresInterval() { | ||
return expiresInterval; | ||
} | ||
|
||
/** | ||
* @param expiresInterval | ||
* the expiresInterval to set | ||
*/ | ||
public void setExpiresInterval(long expiresInterval) { | ||
this.expiresInterval = expiresInterval; | ||
} | ||
|
||
} |
This file contains 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
46 changes: 46 additions & 0 deletions
46
src/test/java/focusedCrawler/link/classifier/builder/BacklinkSurferTest.java
This file contains 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,46 @@ | ||
package focusedCrawler.link.classifier.builder; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import java.io.IOException; | ||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
|
||
import org.apache.commons.validator.routines.UrlValidator; | ||
import org.junit.Test; | ||
|
||
import focusedCrawler.util.parser.BackLinkNeighborhood; | ||
import focusedCrawler.util.parser.SimpleWrapper; | ||
|
||
public class BacklinkSurferTest { | ||
|
||
@Test | ||
public void backlinksShouldBeDownloaded() throws MalformedURLException, IOException { | ||
|
||
// of no use since we are using JSON for parsing. pattern taken from | ||
// config file. | ||
SimpleWrapper wrapper = new SimpleWrapper(",\"uu\":\"", "\"}"); | ||
|
||
BacklinkSurfer surfer = new BacklinkSurfer(wrapper); | ||
surfer.setAccessID("mozscape-4a1d0827fc"); | ||
surfer.setPassKey("d6ea0c3b253ab44425769e422624a0f"); | ||
|
||
String[] testURLs = { "http://www.bbc.co.uk/news/health-30577776" }; | ||
|
||
for (String url : testURLs) { | ||
BackLinkNeighborhood[] backlinks = surfer.getLNBacklinks(new URL(url)); | ||
assertEquals("Backlink extraction not working! ", true, isBackLinkSetValid(backlinks)); | ||
} | ||
|
||
} | ||
|
||
public boolean isBackLinkSetValid(BackLinkNeighborhood[] backlinks) { | ||
UrlValidator validator = new UrlValidator(); | ||
for (BackLinkNeighborhood backlink : backlinks) { | ||
if (validator.isValid(backlink.getLink())) | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
} |