@Test public final void performActionDefaultStrategyTest() throws Exception { final String theHtml = "<html><title>Some Title</title><body>text text text</html>"; final String theUrl = "http: context.checking(new Expectations() { { oneOf(fileDownloader).downloadFile(theUrl, TEST_ACCOUNT); will(returnValue(theHtml)); oneOf(fileDownloader).getFinalUrl(theUrl, TEST_ACCOUNT); will(returnValue(theUrl)); oneOf(titleParser).parseInformation(with(equal(theHtml)), with(any(LinkInformation.class)), with(any(String.class))); oneOf(descParser).parseInformation(with(equal(theHtml)), with(any(LinkInformation.class)), with(any(String.class))); oneOf(imgParser).parseInformation(with(equal(theHtml)), with(any(LinkInformation.class)), with(any(String.class))); oneOf(mapperMock).execute(with(any(UniqueStringRequest.class))); will(returnValue(null)); oneOf(insertMapper).execute(with(any(PersistenceRequest.class))); oneOf(insertMapper).flush(); oneOf(fileDownloader).getHost(theUrl); will(returnValue("www.someurl.com/")); } }); ServiceActionContext currentContext = new ServiceActionContext(theUrl, principalMock); sut.execute(currentContext); context.assertIsSatisfied(); }
@Override public Serializable execute(final PrincipalActionContext inActionContext) throws ExecutionException { String url = (String) inActionContext.getParams(); LinkInformation theLink = null; try { if (!Pattern.matches("^([a-z]+: { url = "http: } 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); int postAuthorityIndex = url.indexOf("/", url.indexOf(": theLink.setSource(postAuthorityIndex == -1 ? url : url.substring(0, postAuthorityIndex)); 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) { int dontCare = 0; } return theLink; }
GetParsedLinkInformationExecution implements ExecutionStrategy<PrincipalActionContext> { @Override public Serializable execute(final PrincipalActionContext inActionContext) throws ExecutionException { String url = (String) inActionContext.getParams(); LinkInformation theLink = null; try { if (!Pattern.matches("^([a-z]+: { url = "http: } 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); int postAuthorityIndex = url.indexOf("/", url.indexOf(": theLink.setSource(postAuthorityIndex == -1 ? url : url.substring(0, postAuthorityIndex)); 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) { int dontCare = 0; } return theLink; } }
GetParsedLinkInformationExecution implements ExecutionStrategy<PrincipalActionContext> { @Override public Serializable execute(final PrincipalActionContext inActionContext) throws ExecutionException { String url = (String) inActionContext.getParams(); LinkInformation theLink = null; try { if (!Pattern.matches("^([a-z]+: { url = "http: } 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); int postAuthorityIndex = url.indexOf("/", url.indexOf(": theLink.setSource(postAuthorityIndex == -1 ? url : url.substring(0, postAuthorityIndex)); 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) { int dontCare = 0; } return theLink; } GetParsedLinkInformationExecution(final ConnectionFacade inConnection,
            final FindLinkInformationByUrl inMapper, final InsertMapper<LinkInformation> inInsertMapper,
            final List<HtmlLinkParser> inParsingStrategies); }
GetParsedLinkInformationExecution implements ExecutionStrategy<PrincipalActionContext> { @Override public Serializable execute(final PrincipalActionContext inActionContext) throws ExecutionException { String url = (String) inActionContext.getParams(); LinkInformation theLink = null; try { if (!Pattern.matches("^([a-z]+: { url = "http: } 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); int postAuthorityIndex = url.indexOf("/", url.indexOf(": theLink.setSource(postAuthorityIndex == -1 ? url : url.substring(0, postAuthorityIndex)); 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) { int dontCare = 0; } return theLink; } GetParsedLinkInformationExecution(final ConnectionFacade inConnection,
            final FindLinkInformationByUrl inMapper, final InsertMapper<LinkInformation> inInsertMapper,
            final List<HtmlLinkParser> inParsingStrategies); @Override Serializable execute(final PrincipalActionContext inActionContext); }
GetParsedLinkInformationExecution implements ExecutionStrategy<PrincipalActionContext> { @Override public Serializable execute(final PrincipalActionContext inActionContext) throws ExecutionException { String url = (String) inActionContext.getParams(); LinkInformation theLink = null; try { if (!Pattern.matches("^([a-z]+: { url = "http: } 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); int postAuthorityIndex = url.indexOf("/", url.indexOf(": theLink.setSource(postAuthorityIndex == -1 ? url : url.substring(0, postAuthorityIndex)); 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) { int dontCare = 0; } return theLink; } GetParsedLinkInformationExecution(final ConnectionFacade inConnection,
            final FindLinkInformationByUrl inMapper, final InsertMapper<LinkInformation> inInsertMapper,
            final List<HtmlLinkParser> inParsingStrategies); @Override Serializable execute(final PrincipalActionContext inActionContext);  }