@Override
public Serializable execute(final PrincipalActionContext inActionContext) throws ExecutionException
{
String url = (String) inActionContext.getParams();
LinkInformation theLink = null;
try
{
// First see if the user supplied a url with a protocol. If they didn't
// prepend http:// onto it.
if (!Pattern.matches("^([a-z]+://.+)", url))
{
url = "http://" + url;
}
url = connection.getFinalUrl(url, inActionContext.getPrincipal().getAccountId());
UniqueStringRequest req = new UniqueStringRequest(url);
theLink = mapper.execute(req);
if (null == theLink)
{
theLink = new LinkInformation();
theLink.setUrl(url);
// set the source to the protocol + authority
// (Take everything up to the first slash beyond the protocol-authority separator ://)
int postAuthorityIndex = url.indexOf("/", url.indexOf("://") + "://".length());
theLink.setSource(postAuthorityIndex == -1 ? url : url.substring(0, postAuthorityIndex));
// Attempt to retrieve the contents of the resource.
log.debug("Downloading resource: " + url);
try
{
String htmlString = connection.downloadFile(url, inActionContext.getPrincipal().getAccountId());
htmlString = htmlString.replace("\\s+", " ");
String host = connection.getHost(url);
for (HtmlLinkParser strategy : parsingStrategies)
{
Matcher match = Pattern.compile(strategy.getRegex()).matcher(host);
if (match.find())
{
log.debug("Found: " + strategy.getRegex());
strategy.parseLinkInformation(htmlString, theLink, inActionContext.getPrincipal()
.getAccountId());
break;
}
else
{
log.debug("Didn't find: " + strategy.getRegex());
}
}
}
catch (Exception e)
{
log.info("Failed to download resource and extract link information from it.", e);
}
theLink.setCreated(new Date());
insertMapper.execute(new PersistenceRequest<LinkInformation>(theLink));
insertMapper.flush();
}
}
catch (Exception ex)