protected JSONObject getJson(String pathInfo) throws IOException { URL jsonUrl = getJsonFileUrl(pathInfo); if (jsonUrl == null) { LOG.warn("Failed to load resource {}", pathInfo); return null; } byte[] jsonBytes = IOUtility.readFromUrl(jsonUrl); String json = new String(jsonBytes, StandardCharsets.UTF_8); return new JSONObject(json); } }
@SuppressWarnings("squid:S1149") protected void replaceIncludeTags() throws IOException { // <scout:include template="no-script.html" /> Matcher m = PATTERN_INCLUDE_TAG.matcher(m_workingContent); StringBuffer sb = new StringBuffer(); while (m.find()) { String includeName = m.group(1); URL includeUrl = BEANS.get(IWebContentService.class).getWebContentResource("/includes/" + includeName); if (includeUrl == null) { throw new IOException("Could not resolve include '" + includeName + "'"); } else { byte[] includeContent = IOUtility.readFromUrl(includeUrl); String replacement = new String(includeContent, StandardCharsets.UTF_8); // Ensure exactly 1 newline before and after the replacement (to improve readability in resulting document) replacement = "\n" + replacement.trim() + "\n"; m.appendReplacement(sb, replacement); LOG.trace("Resolved include '{}'", includeName); } } m.appendTail(sb); m_workingContent = sb.toString(); }
protected ScriptOutput processLibrary(String pathInfo, ScriptSource script, boolean compileAndMinify) throws IOException { if (!ObjectUtility.isOneOf(script.getNodeType(), NodeType.LIBRARY, NodeType.LIBRARY_MINIFIED)) { throw new IOException(script.getRequestPath() + " / " + script.getURL() + ": expected " + NodeType.LIBRARY + " or " + NodeType.LIBRARY_MINIFIED + ", but got " + script.getNodeType()); } long lastModified = script.getURL().openConnection().getLastModified(); String libraryContent = new String(IOUtility.readFromUrl(script.getURL()), StandardCharsets.UTF_8); // only minfy if not already minified if (compileAndMinify && script.getNodeType() != NodeType.LIBRARY_MINIFIED) { libraryContent = compileAndMinifyContent(script.getFileType(), libraryContent); } return new ScriptOutput(pathInfo, libraryContent.getBytes(StandardCharsets.UTF_8), lastModified); }
String content = new String(IOUtility.readFromUrl(script.getURL()), StandardCharsets.UTF_8); Matcher mat = matcherForScriptType(script, content); int pos = 0; switch (includeFragment.getNodeType()) { case SRC_FRAGMENT: { replacement = new String(IOUtility.readFromUrl(includeFragment.getURL()), StandardCharsets.UTF_8); lastModified = Math.max(lastModified, includeFragment.getURL().openConnection().getLastModified()); break;
String content = new String(IOUtility.readFromUrl(script.getURL()), StandardCharsets.UTF_8); int pos = 0; Matcher mat = matcherForScriptType(script, content);
@Override public BinaryResource loadResource(String pathInfo) throws IOException { URL url = BEANS.get(IWebContentService.class).getWebContentResource(pathInfo); if (url == null) { // not handled here return null; } byte[] bytes = IOUtility.readFromUrl(url); URLConnection connection = url.openConnection(); return BinaryResources.create() .withFilename(pathInfo) .withContent(bytes) .withLastModified(connection.getLastModified()) .withCachingAllowed(true) .withCacheMaxAge(HttpCacheControl.MAX_AGE_4_HOURS) .build(); }
@Override public BinaryResource loadResource(String pathInfo) throws IOException { URL url = BEANS.get(IWebContentService.class).getWebContentResource(pathInfo); if (url == null) { // not handled here return null; } // TODO [7.0] bsh: Maybe optimize memory consumption (unnecessary conversion of byte[] to String) String json = new String(IOUtility.readFromUrl(url), StandardCharsets.UTF_8); json = JsonUtility.stripCommentsFromJson(json); return BinaryResources.create() .withFilename(pathInfo) .withCharset(StandardCharsets.UTF_8) .withContent(json.getBytes(StandardCharsets.UTF_8)) .withLastModifiedNow() .withCachingAllowed(true) .withCacheMaxAge(HttpCacheControl.MAX_AGE_4_HOURS) .build(); }
@Override public BinaryResource loadResource(String pathInfo) throws IOException { HtmlDocumentParserParameters params = createHtmlDocumentParserParameters(pathInfo); URL url = BEANS.get(IWebContentService.class).getWebContentResource(pathInfo); if (url == null) { // not handled here return null; } byte[] document = IOUtility.readFromUrl(url); HtmlDocumentParser parser = createHtmlDocumentParser(params); byte[] parsedDocument = parser.parseDocument(document); return BinaryResources.create() .withFilename(pathInfo) .withCharset(StandardCharsets.UTF_8) .withContent(parsedDocument) .withLastModifiedNow() .withCachingAllowed(true) .build(); }