@Test public final void testFix() { try { List<String> textHtmlPatterns = new ArrayList<String>(); List<String> textCssPatterns = new ArrayList<String>(); textHtmlPatterns.add("(?i)\\burl\\((?![\"'].)([^\\)]*)\\)"); textHtmlPatterns.add("(?i)\\burl\\(\"([^\"]*)\"\\)"); textHtmlPatterns.add("(?i)\\burl\\('([^']*)'\\)"); textHtmlPatterns.add("(?i)@import\\s+\"([^\"]*)\""); textHtmlPatterns.add("(?i)<\\s*A\\s+[^>]*\\bHREF\\s*=\\s*\"((?!javascript:)[^\"]*)\""); textHtmlPatterns.add("(?i)<\\s*A\\s+[^>]*\\bHREF\\s*=\\s*'((?!javascript:)[^']*)'"); textHtmlPatterns.add("(?i)<\\s*a\\s+[^>]*\\bhref=((?!javascript:)[^\\t\\n\\x0B\\f\\r>\\\"']+)"); textHtmlPatterns.add("(?i)<\\s*META\\s+[^>]*\\bURL\\s*=\\s*\"([^\"]*)\""); textHtmlPatterns.add("(?i)<\\s*META\\s+[^>]*\\bURL\\s*=\\s*'([^']*)'"); textHtmlPatterns.add("(?i)<\\s*META\\s+[^>]*\\bURL=([^\\t\\n\\x0B\\f\\r>\\\"']+)"); textHtmlPatterns.add("(?i)<\\s*OBJECT\\s+[^>]*\\bCODEBASE\\s*=\\s*\"([^\"]*)\""); textHtmlPatterns.add("(?i)<\\s*OBJECT\\s+[^>]*\\bCODEBASE\\s*=\\s*'([^']*)'"); textHtmlPatterns.add("(?i)<\\s*OBJECT\\s+[^>]*\\bCODEBASE=([^\\t\\n\\x0B\\f\\r>\\\"']+)"); textHtmlPatterns.add("(?i)<\\s*OBJECT\\s+[^>]*\\bDATA\\s*=\\s*\"([^\"]*)\""); textHtmlPatterns.add("(?i)<\\s*OBJECT\\s+[^>]*\\bDATA\\s*=\\s*'([^']*)'"); textHtmlPatterns.add("(?i)<\\s*OBJECT\\s+[^>]*\\bDATA=([^\\t\\n\\x0B\\f\\r>\\\"']+)"); textHtmlPatterns.add("(?i)<\\s*APPLET\\s+[^>]*\\bCODEBASE\\s*=\\s*\"([^\"]*)\""); textHtmlPatterns.add("(?i)<\\s*APPLET\\s+[^>]*\\bCODEBASE\\s*=\\s*'([^']*)'"); textHtmlPatterns.add("(?i)<\\s*APPLET\\s+[^>]*\\bCODEBASE=([^\\t\\n\\x0B\\f\\r>\\\"']+)"); textHtmlPatterns.add("(?i)<\\s*APPLET\\s+[^>]*\\bARCHIVE\\s*=\\s*\"([^\"]*)\""); textHtmlPatterns.add("(?i)<\\s*APPLET\\s+[^>]*\\bARCHIVE\\s*=\\s*'([^']*)'"); textHtmlPatterns.add("(?i)<\\s*(?:BODY|TD)\\s+[^>]*\\bBACKGROUND\\s*=\\s*\"([^\"]*)\""); textHtmlPatterns.add("(?i)<\\s*(?:BODY|TD)\\s+[^>]*\\bBACKGROUND\\s*=\\s*'([^']*)'"); textHtmlPatterns.add("(?i)<\\s*(?:BODY|TD)\\s+[^>]*\\bBACKGROUND=(?!\\\\\")([^\\t\\n\\x0B\\f\\r>\"']+)"); textHtmlPatterns.add("(?i)<\\s*(?:LINK|AREA)\\s+[^>]*\\bHREF=\"([^\"]*)\""); textHtmlPatterns.add("(?i)<\\s*(?:LINK|AREA)\\s+[^>]*\\bHREF='([^']*)'"); textHtmlPatterns.add("(?i)<\\s*(?:LINK|AREA)\\s+[^>]*\\bHREF=([^\\\\t\\\\n\\\\x0B\\\\f\\\\r>\\\"']+)"); textHtmlPatterns.add("(?i)<\\s*(?:IMG|FRAME|SCRIPT|EMBED|INPUT)\\s+[^>]*\\bSRC\\s*=\\s*\"([^\"]*)\""); textHtmlPatterns.add("(?i)<\\s*(?:IMG|FRAME|SCRIPT|EMBED|INPUT)\\s+[^>]*\\bSRC\\s*=\\s*'([^']*)'"); textHtmlPatterns.add("(?i)<\\s*(?:IMG|FRAME|SCRIPT|EMBED|INPUT)\\s+[^>]*\\bSRC=([^\\t\\n\\x0B\\f\\r>\\\"']+)"); textHtmlPatterns.add("window.location=\"([^\"]*)\";"); contentTypePatterns.put("text/html", textHtmlPatterns); textCssPatterns.add("(?i)\\burl\\((?![\"'].)([^\\)]*)\\)"); textCssPatterns.add("(?i)\\burl\\(\"([^\"]*)\"\\)"); textCssPatterns.add("(?i)\\burl\\('([^']*)'\\)"); contentTypePatterns.put("text/css", textCssPatterns); BrowseHelper helper = new BrowseHelper(); helper.setPrefix("http: helper.setContentTypePatterns(contentTypePatterns); Map<String, String> fixes = new HashMap<String, String>(); fixes.put("top.location", " fixes.put("window.location", " fixes.put("http-equiv=&quot;refresh&quot; content=&quot;0; url=/", "http-equiv=&quot;refresh&quot; content=&quot;0; url=./"); BrowseController controller = new BrowseController(); controller.setFixTokens(fixes); DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); theFile = docBuilder.parse (new File(testFile)); NodeList pageTestNodes = theFile.getElementsByTagName("pagetest"); for (int i = 0; i < pageTestNodes.getLength(); i++) { String inFileName = ""; String outFileName = ""; String contentType = ""; String Oid = ""; String resourceUrl = ""; Node testNode = pageTestNodes.item(i); if(testNode.getNodeType() == Node.ELEMENT_NODE) { NodeList children = testNode.getChildNodes(); for(int j = 0; j < children.getLength(); j++) { Node child = children.item(j); if(child.getNodeType() == Node.ELEMENT_NODE) { if(child.getNodeName().equals("infilename")) { inFileName = getString(child); } else if(child.getNodeName().equals("outfilename")) { outFileName = getString(child); } else if(child.getNodeName().equals("contenttype")) { contentType = getString(child); } else if(child.getNodeName().equals("oid")) { Oid = getString(child); } else if(child.getNodeName().equals("resourceurl")) { resourceUrl = getString(child); } } } StringBuilder pageContent = readFile(inFileName); StringBuilder expectedPatchedContent = readFile(outFileName); helper.fix(pageContent, contentType, Long.valueOf(Oid), resourceUrl); log.debug("pageContent.length() is: " + pageContent.length()); log.debug("expectedPatchedContent.length() is: " + expectedPatchedContent.length()); assertTrue("BrowseHelper.fix(): patched content length is not equal to expected content length.", (pageContent.length() == expectedPatchedContent.length() + 2)); } } } catch (Exception e) { String message = e.getClass().toString() + " - " + e.getMessage(); log.debug(message); e.printStackTrace(); fail(message); } }
public void fix(StringBuilder content, String contentType, long hrOid, String currentResource) { String resourcePrefix = prefix + "/" + hrOid + "/"; String jsPrefix = prefix.replaceFirst("curator/tools/browse", "replay/client-rewrite.js"); if( "text/html".equals(contentType)) { String existingBaseHref = TagMagix.getBaseHref(content); if (existingBaseHref == null) { insertAtStartOfHead(content, "<base href=\"" + currentResource + "\" />"); } else { currentResource = existingBaseHref; } } ArchivalUrlResultURIConverter resURIConverter = new ArchivalUrlResultURIConverter(); resURIConverter.setReplayURIPrefix(resourcePrefix); if( "text/html".equals(contentType)) { for(TagMagixHelper helper : htmlTagPatterns) { TagMagix.markupTagREURIC(content, resURIConverter, "", currentResource, helper.tag, helper.attribute); } } if( contentTypePatterns.containsKey(contentType)) { for(Pattern pattern: contentTypePatterns.get(contentType)) { Matcher matcher = pattern.matcher(content); int idx = 0; while(matcher.find(idx)) { String currentVal = matcher.group(1); if(!"".equals(currentVal.trim())) { String newVal = convertUrl(hrOid, currentResource, currentVal); log.debug("Replacing " + matcher.group(1) + " with " + newVal); int origAttrLength = currentVal.length(); int attrStart = matcher.start(1); int attrEnd = matcher.end(1); int delta = newVal.length() - origAttrLength; content.replace(attrStart, attrEnd, newVal); idx = matcher.end(0) + delta; } else { idx = matcher.end(0); } } } } if( "text/html".equals(contentType)) { StringBuilder toInsert = new StringBuilder(300); toInsert.append("<script type=\"text/javascript\">\n"); toInsert.append(" var sWayBackCGI = \"" + resourcePrefix + "\";\n"); toInsert.append("</script>\n"); toInsert.append("<script type=\"text/javascript\" src=\"" + jsPrefix + "\" ></script>\n"); insertAtEndOfBody(content, toInsert.toString()); } if( "text/html".equals(contentType) || "application/javascript".equals(contentType)) { Map<String, String> tokens = BrowseController.getFixTokens(); Iterator<String> keys = tokens.keySet().iterator(); while (keys.hasNext()) { String token = keys.next(); int startIdx = content.toString().toLowerCase().indexOf(token); if (startIdx != -1) { int endIdx = startIdx + token.length(); content.replace(startIdx, endIdx, tokens.get(token)); }; } } }
BrowseHelper { public void fix(StringBuilder content, String contentType, long hrOid, String currentResource) { String resourcePrefix = prefix + "/" + hrOid + "/"; String jsPrefix = prefix.replaceFirst("curator/tools/browse", "replay/client-rewrite.js"); if( "text/html".equals(contentType)) { String existingBaseHref = TagMagix.getBaseHref(content); if (existingBaseHref == null) { insertAtStartOfHead(content, "<base href=\"" + currentResource + "\" />"); } else { currentResource = existingBaseHref; } } ArchivalUrlResultURIConverter resURIConverter = new ArchivalUrlResultURIConverter(); resURIConverter.setReplayURIPrefix(resourcePrefix); if( "text/html".equals(contentType)) { for(TagMagixHelper helper : htmlTagPatterns) { TagMagix.markupTagREURIC(content, resURIConverter, "", currentResource, helper.tag, helper.attribute); } } if( contentTypePatterns.containsKey(contentType)) { for(Pattern pattern: contentTypePatterns.get(contentType)) { Matcher matcher = pattern.matcher(content); int idx = 0; while(matcher.find(idx)) { String currentVal = matcher.group(1); if(!"".equals(currentVal.trim())) { String newVal = convertUrl(hrOid, currentResource, currentVal); log.debug("Replacing " + matcher.group(1) + " with " + newVal); int origAttrLength = currentVal.length(); int attrStart = matcher.start(1); int attrEnd = matcher.end(1); int delta = newVal.length() - origAttrLength; content.replace(attrStart, attrEnd, newVal); idx = matcher.end(0) + delta; } else { idx = matcher.end(0); } } } } if( "text/html".equals(contentType)) { StringBuilder toInsert = new StringBuilder(300); toInsert.append("<script type=\"text/javascript\">\n"); toInsert.append(" var sWayBackCGI = \"" + resourcePrefix + "\";\n"); toInsert.append("</script>\n"); toInsert.append("<script type=\"text/javascript\" src=\"" + jsPrefix + "\" ></script>\n"); insertAtEndOfBody(content, toInsert.toString()); } if( "text/html".equals(contentType) || "application/javascript".equals(contentType)) { Map<String, String> tokens = BrowseController.getFixTokens(); Iterator<String> keys = tokens.keySet().iterator(); while (keys.hasNext()) { String token = keys.next(); int startIdx = content.toString().toLowerCase().indexOf(token); if (startIdx != -1) { int endIdx = startIdx + token.length(); content.replace(startIdx, endIdx, tokens.get(token)); }; } } } }
BrowseHelper { public void fix(StringBuilder content, String contentType, long hrOid, String currentResource) { String resourcePrefix = prefix + "/" + hrOid + "/"; String jsPrefix = prefix.replaceFirst("curator/tools/browse", "replay/client-rewrite.js"); if( "text/html".equals(contentType)) { String existingBaseHref = TagMagix.getBaseHref(content); if (existingBaseHref == null) { insertAtStartOfHead(content, "<base href=\"" + currentResource + "\" />"); } else { currentResource = existingBaseHref; } } ArchivalUrlResultURIConverter resURIConverter = new ArchivalUrlResultURIConverter(); resURIConverter.setReplayURIPrefix(resourcePrefix); if( "text/html".equals(contentType)) { for(TagMagixHelper helper : htmlTagPatterns) { TagMagix.markupTagREURIC(content, resURIConverter, "", currentResource, helper.tag, helper.attribute); } } if( contentTypePatterns.containsKey(contentType)) { for(Pattern pattern: contentTypePatterns.get(contentType)) { Matcher matcher = pattern.matcher(content); int idx = 0; while(matcher.find(idx)) { String currentVal = matcher.group(1); if(!"".equals(currentVal.trim())) { String newVal = convertUrl(hrOid, currentResource, currentVal); log.debug("Replacing " + matcher.group(1) + " with " + newVal); int origAttrLength = currentVal.length(); int attrStart = matcher.start(1); int attrEnd = matcher.end(1); int delta = newVal.length() - origAttrLength; content.replace(attrStart, attrEnd, newVal); idx = matcher.end(0) + delta; } else { idx = matcher.end(0); } } } } if( "text/html".equals(contentType)) { StringBuilder toInsert = new StringBuilder(300); toInsert.append("<script type=\"text/javascript\">\n"); toInsert.append(" var sWayBackCGI = \"" + resourcePrefix + "\";\n"); toInsert.append("</script>\n"); toInsert.append("<script type=\"text/javascript\" src=\"" + jsPrefix + "\" ></script>\n"); insertAtEndOfBody(content, toInsert.toString()); } if( "text/html".equals(contentType) || "application/javascript".equals(contentType)) { Map<String, String> tokens = BrowseController.getFixTokens(); Iterator<String> keys = tokens.keySet().iterator(); while (keys.hasNext()) { String token = keys.next(); int startIdx = content.toString().toLowerCase().indexOf(token); if (startIdx != -1) { int endIdx = startIdx + token.length(); content.replace(startIdx, endIdx, tokens.get(token)); }; } } }  }
BrowseHelper { public void fix(StringBuilder content, String contentType, long hrOid, String currentResource) { String resourcePrefix = prefix + "/" + hrOid + "/"; String jsPrefix = prefix.replaceFirst("curator/tools/browse", "replay/client-rewrite.js"); if( "text/html".equals(contentType)) { String existingBaseHref = TagMagix.getBaseHref(content); if (existingBaseHref == null) { insertAtStartOfHead(content, "<base href=\"" + currentResource + "\" />"); } else { currentResource = existingBaseHref; } } ArchivalUrlResultURIConverter resURIConverter = new ArchivalUrlResultURIConverter(); resURIConverter.setReplayURIPrefix(resourcePrefix); if( "text/html".equals(contentType)) { for(TagMagixHelper helper : htmlTagPatterns) { TagMagix.markupTagREURIC(content, resURIConverter, "", currentResource, helper.tag, helper.attribute); } } if( contentTypePatterns.containsKey(contentType)) { for(Pattern pattern: contentTypePatterns.get(contentType)) { Matcher matcher = pattern.matcher(content); int idx = 0; while(matcher.find(idx)) { String currentVal = matcher.group(1); if(!"".equals(currentVal.trim())) { String newVal = convertUrl(hrOid, currentResource, currentVal); log.debug("Replacing " + matcher.group(1) + " with " + newVal); int origAttrLength = currentVal.length(); int attrStart = matcher.start(1); int attrEnd = matcher.end(1); int delta = newVal.length() - origAttrLength; content.replace(attrStart, attrEnd, newVal); idx = matcher.end(0) + delta; } else { idx = matcher.end(0); } } } } if( "text/html".equals(contentType)) { StringBuilder toInsert = new StringBuilder(300); toInsert.append("<script type=\"text/javascript\">\n"); toInsert.append(" var sWayBackCGI = \"" + resourcePrefix + "\";\n"); toInsert.append("</script>\n"); toInsert.append("<script type=\"text/javascript\" src=\"" + jsPrefix + "\" ></script>\n"); insertAtEndOfBody(content, toInsert.toString()); } if( "text/html".equals(contentType) || "application/javascript".equals(contentType)) { Map<String, String> tokens = BrowseController.getFixTokens(); Iterator<String> keys = tokens.keySet().iterator(); while (keys.hasNext()) { String token = keys.next(); int startIdx = content.toString().toLowerCase().indexOf(token); if (startIdx != -1) { int endIdx = startIdx + token.length(); content.replace(startIdx, endIdx, tokens.get(token)); }; } } }  void setPrefix(String prefix); void setContentTypePatterns(Map<String, List<String>> patterns); void setHtmlTagPatterns(List<String> patterns); static Pattern getTagMagixPattern(String tag, String attr); boolean isReplaceable(String contentType); void fix(StringBuilder content, String contentType, long hrOid, String currentResource); String convertUrl(Long hrOid, String currentResource, String urlToConvert); static String getAbsURL(String currentUrl, String subUrl); void setUrlConversionReplacements(List<StringReplacer> urlConversionReplacements); void insertAtStartOfHead(StringBuilder sb, String toInsert); void insertAtEndOfBody(StringBuilder sb, String toInsert); void insertAtStartOfBody(StringBuilder sb, String toInsert); boolean isUseUrlConversionReplacements(); void setUseUrlConversionReplacements(boolean useUrlConversionReplacements); }
BrowseHelper { public void fix(StringBuilder content, String contentType, long hrOid, String currentResource) { String resourcePrefix = prefix + "/" + hrOid + "/"; String jsPrefix = prefix.replaceFirst("curator/tools/browse", "replay/client-rewrite.js"); if( "text/html".equals(contentType)) { String existingBaseHref = TagMagix.getBaseHref(content); if (existingBaseHref == null) { insertAtStartOfHead(content, "<base href=\"" + currentResource + "\" />"); } else { currentResource = existingBaseHref; } } ArchivalUrlResultURIConverter resURIConverter = new ArchivalUrlResultURIConverter(); resURIConverter.setReplayURIPrefix(resourcePrefix); if( "text/html".equals(contentType)) { for(TagMagixHelper helper : htmlTagPatterns) { TagMagix.markupTagREURIC(content, resURIConverter, "", currentResource, helper.tag, helper.attribute); } } if( contentTypePatterns.containsKey(contentType)) { for(Pattern pattern: contentTypePatterns.get(contentType)) { Matcher matcher = pattern.matcher(content); int idx = 0; while(matcher.find(idx)) { String currentVal = matcher.group(1); if(!"".equals(currentVal.trim())) { String newVal = convertUrl(hrOid, currentResource, currentVal); log.debug("Replacing " + matcher.group(1) + " with " + newVal); int origAttrLength = currentVal.length(); int attrStart = matcher.start(1); int attrEnd = matcher.end(1); int delta = newVal.length() - origAttrLength; content.replace(attrStart, attrEnd, newVal); idx = matcher.end(0) + delta; } else { idx = matcher.end(0); } } } } if( "text/html".equals(contentType)) { StringBuilder toInsert = new StringBuilder(300); toInsert.append("<script type=\"text/javascript\">\n"); toInsert.append(" var sWayBackCGI = \"" + resourcePrefix + "\";\n"); toInsert.append("</script>\n"); toInsert.append("<script type=\"text/javascript\" src=\"" + jsPrefix + "\" ></script>\n"); insertAtEndOfBody(content, toInsert.toString()); } if( "text/html".equals(contentType) || "application/javascript".equals(contentType)) { Map<String, String> tokens = BrowseController.getFixTokens(); Iterator<String> keys = tokens.keySet().iterator(); while (keys.hasNext()) { String token = keys.next(); int startIdx = content.toString().toLowerCase().indexOf(token); if (startIdx != -1) { int endIdx = startIdx + token.length(); content.replace(startIdx, endIdx, tokens.get(token)); }; } } }  void setPrefix(String prefix); void setContentTypePatterns(Map<String, List<String>> patterns); void setHtmlTagPatterns(List<String> patterns); static Pattern getTagMagixPattern(String tag, String attr); boolean isReplaceable(String contentType); void fix(StringBuilder content, String contentType, long hrOid, String currentResource); String convertUrl(Long hrOid, String currentResource, String urlToConvert); static String getAbsURL(String currentUrl, String subUrl); void setUrlConversionReplacements(List<StringReplacer> urlConversionReplacements); void insertAtStartOfHead(StringBuilder sb, String toInsert); void insertAtEndOfBody(StringBuilder sb, String toInsert); void insertAtStartOfBody(StringBuilder sb, String toInsert); boolean isUseUrlConversionReplacements(); void setUseUrlConversionReplacements(boolean useUrlConversionReplacements);  }