@Override public boolean handle(HandlerContext handlerContext) { handlerContext.getContext().getAssetStorage().clear(); ; LOG.info("Cleared asset storage"); return true; } }
@Override public boolean handle(HandlerContext handlerContext) { handlerContext.getContext().getAssetStorage().clear(); ; LOG.info("Cleared asset storage"); return true; } }
@Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { getLogger().debug("Dandelion Asset servlet captured GET request {}", request.getRequestURI()); Context context = (Context) request.getAttribute(WebConstants.DANDELION_CONTEXT_ATTRIBUTE); // Get the asset content thanks to the cache key String cacheKey = AssetUtils.extractCacheKeyFromRequest(request); AssetType assetType = AssetType.extractFromRequest(request); LOG.debug("Retrieved asset type: {}, cache key: {}", assetType, cacheKey); response.setContentType(assetType.getContentType() == null ? "text/plain" : assetType.getContentType()); // Write the asset content PrintWriter writer = response.getWriter(); writer.write(context.getAssetStorage().get(cacheKey).getContents()); // The response is explicitely closed here instead of setting a // Content-Length header writer.close(); }
@Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { getLogger().debug("Dandelion Asset servlet captured GET request {}", request.getRequestURI()); Context context = (Context) request.getAttribute(WebConstants.DANDELION_CONTEXT_ATTRIBUTE); // Get the asset content thanks to the cache key String cacheKey = AssetUtils.extractCacheKeyFromRequest(request); AssetType assetType = AssetType.extractFromRequest(request); LOG.debug("Retrieved asset type: {}, cache key: {}", assetType, cacheKey); response.setContentType(assetType.getContentType() == null ? "text/plain" : assetType.getContentType()); // Write the asset content PrintWriter writer = response.getWriter(); writer.write(context.getAssetStorage().get(cacheKey).getContents()); // The response is explicitely closed here instead of setting a // Content-Length header writer.close(); }
@Override public InputStream getResourceAsStream(TemplateProcessingParameters templateProcessingParameters, String resourceName) { IContext context = templateProcessingParameters.getContext(); if (!(context instanceof IWebContext)) { throw new TemplateProcessingException("Resource resolution by ServletContext with " + this.getClass().getName() + " can only be performed " + "when context implements " + IWebContext.class.getName() + " [current context: " + context.getClass().getName() + "]"); } HttpServletRequest request = ((IWebContext) context).getHttpServletRequest(); Context dandelionContext = (Context) request.getAttribute(WebConstants.DANDELION_CONTEXT_ATTRIBUTE); // Get the asset content thanks to the cache key String cacheKey = AssetUtils.extractCacheKeyFromRequest(request); String contents = dandelionContext.getAssetStorage().get(cacheKey).getContents(); // Wrap the contents with prototype-only comment blocks StringBuilder adaptedContents = new StringBuilder(BLOCK_WRAP_START); adaptedContents.append(BLOCK_WRAP_CDATA_START); adaptedContents.append(contents); adaptedContents.append(BLOCK_WRAP_CDATA_END); adaptedContents.append(BLOCK_WRAP_END); String configuredEncoding = dandelionContext.getConfiguration().getEncoding(); InputStream is = null; try { is = new ByteArrayInputStream(adaptedContents.toString().getBytes(configuredEncoding)); } catch (UnsupportedEncodingException e) { throw new DandelionException("Unable to encode the Javascript asset using the '" + configuredEncoding + "', which doesn't seem to be supported", e); } return is; }
@Override public InputStream getResourceAsStream(TemplateProcessingParameters templateProcessingParameters, String resourceName) { IContext context = templateProcessingParameters.getContext(); if (!(context instanceof IWebContext)) { throw new TemplateProcessingException("Resource resolution by ServletContext with " + this.getClass().getName() + " can only be performed " + "when context implements " + IWebContext.class.getName() + " [current context: " + context.getClass().getName() + "]"); } HttpServletRequest request = ((IWebContext) context).getHttpServletRequest(); Context dandelionContext = (Context) request.getAttribute(WebConstants.DANDELION_CONTEXT_ATTRIBUTE); // Get the asset content thanks to the cache key String cacheKey = AssetUtils.extractCacheKeyFromRequest(request); String contents = dandelionContext.getAssetStorage().get(cacheKey).getContents(); // Wrap the contents with prototype-only comment blocks StringBuilder adaptedContents = new StringBuilder(BLOCK_WRAP_START); adaptedContents.append(BLOCK_WRAP_CDATA_START); adaptedContents.append(contents); adaptedContents.append(BLOCK_WRAP_CDATA_END); adaptedContents.append(BLOCK_WRAP_END); String configuredEncoding = dandelionContext.getConfiguration().getEncoding(); InputStream is = null; try { is = new ByteArrayInputStream(adaptedContents.toString().getBytes(configuredEncoding)); } catch (UnsupportedEncodingException e) { throw new DandelionException("Unable to encode the Javascript asset using the '" + configuredEncoding + "', which doesn't seem to be supported", e); } return is; }
@Override public String getAssetVersion(Asset asset) { LOG.debug("Calculating version hash for the asset: {}", asset.toLog()); StorageEntry storageEntry = getContext().getAssetStorage().get(asset.getStorageKey()); String contents = null; if(storageEntry != null) { contents = storageEntry.getContents(); } String version = null; if (StringUtils.isBlank(contents)) { LOG.warn("Asset {} has an empty contents. Using a random String for the hash", asset.toLog()); version = DigestUtils.md5Digest(UUID.randomUUID().toString()); } else { version = DigestUtils.md5Digest(contents); } return version; } }
@Override public String getAssetVersion(Asset asset) { LOG.debug("Calculating version hash for the asset: {}", asset.toLog()); StorageEntry storageEntry = getContext().getAssetStorage().get(asset.getStorageKey()); String contents = null; if(storageEntry != null) { contents = storageEntry.getContents(); } String version = null; if (StringUtils.isBlank(contents)) { LOG.warn("Asset {} has an empty contents. Using a random String for the hash", asset.toLog()); version = DigestUtils.md5Digest(UUID.randomUUID().toString()); } else { version = DigestUtils.md5Digest(contents); } return version; } }
public Asset process(Asset asset, HttpServletRequest request) { if (context.getActiveProcessors().isEmpty()) { return asset; } List<AssetProcessor> compatibleAssetProcessors = getCompatibleProcessorFor(asset); if (!compatibleAssetProcessors.isEmpty()) { // Context to be passed in asset processors ProcessingContext processingContext = new ProcessingContext(context, asset, request); AssetLocator locator = AssetUtils.getAssetLocator(asset, context); String contents = locator.getContent(asset, request); Reader assetReader = new StringReader(contents); Writer assetWriter = new StringWriter(); for (AssetProcessor assetProcessor : compatibleAssetProcessors) { LOG.trace("Applying processor {} on {}", assetProcessor.getProcessorKey(), asset.toLog()); assetWriter = new StringWriter(); assetProcessor.process(assetReader, assetWriter, processingContext); assetReader = new StringReader(assetWriter.toString()); } if (asset.isNotVendor()) { asset.setFinalLocation(AssetUtils.getAssetFinalLocation(request, asset, "min")); } // The cache system is updated with the new key/content pair context.getAssetStorage().put(asset.getStorageKey(), new StorageEntry(asset, assetWriter.toString())); } else { LOG.trace("No compatible processor was found for the asset {}", asset.toLog()); } return asset; }
public Asset process(Asset asset, HttpServletRequest request) { if (context.getActiveProcessors().isEmpty()) { return asset; } List<AssetProcessor> compatibleAssetProcessors = getCompatibleProcessorFor(asset); if (!compatibleAssetProcessors.isEmpty()) { // Context to be passed in asset processors ProcessingContext processingContext = new ProcessingContext(context, asset, request); AssetLocator locator = AssetUtils.getAssetLocator(asset, context); String contents = locator.getContent(asset, request); Reader assetReader = new StringReader(contents); Writer assetWriter = new StringWriter(); for (AssetProcessor assetProcessor : compatibleAssetProcessors) { LOG.trace("Applying processor {} on {}", assetProcessor.getProcessorKey(), asset.toLog()); assetWriter = new StringWriter(); assetProcessor.process(assetReader, assetWriter, processingContext); assetReader = new StringReader(assetWriter.toString()); } if (asset.isNotVendor()) { asset.setFinalLocation(AssetUtils.getAssetFinalLocation(request, asset, "min")); } // The cache system is updated with the new key/content pair context.getAssetStorage().put(asset.getStorageKey(), new StorageEntry(asset, assetWriter.toString())); } else { LOG.trace("No compatible processor was found for the asset {}", asset.toLog()); } return asset; }
@Override protected Map<String, Object> getPageContext() { Map<String, Object> pageContext = new HashMap<String, Object>(); AssetStorage storage = context.getContext().getAssetStorage(); Collection<StorageEntry> storageElements = storage.getAll(); List<Map<String, Object>> options = new ArrayList<Map<String, Object>>(); for (StorageEntry storageElement : storageElements) { Asset asset = storageElement.getAsset(); String storageKey = asset.getStorageKey(); options.add(new MapBuilder<String, Object>().entry("name", asset.getName()).entry("type", asset.getType()) .entry("version", asset.getVersion()).entry("bundle", asset.getBundle()) .entry("locationKey", asset.getConfigLocationKey()).entry("rawLocation", asset.getConfigLocation()) .entry("finalLocation", asset.getFinalLocation()).entry("storageKey", asset.getStorageKey()) .entry("contents", storage.contains(storageKey) ? storage.get(storageKey).getContents() : "Not fetched") .entry("presentInStorage", storage.contains(storageKey)).entry("version", asset.getVersion()).create()); } pageContext.put("impl", storage.getClass().getName()); pageContext.put("number", storage.size()); pageContext.put("assets", options); return pageContext; } }
Asset asset = handlerContext.getContext().getAssetStorage().get(cacheKey).getAsset();
Asset asset = handlerContext.getContext().getAssetStorage().get(cacheKey).getAsset();
@Override protected Map<String, Object> getPageContext() { Map<String, Object> pageContext = new HashMap<String, Object>(); AssetStorage storage = context.getContext().getAssetStorage(); Collection<StorageEntry> storageElements = storage.getAll(); List<Map<String, Object>> options = new ArrayList<Map<String, Object>>(); for (StorageEntry storageElement : storageElements) { Asset asset = storageElement.getAsset(); String storageKey = asset.getStorageKey(); options.add(new MapBuilder<String, Object>().entry("name", asset.getName()).entry("type", asset.getType()) .entry("version", asset.getVersion()).entry("bundle", asset.getBundle()) .entry("locationKey", asset.getConfigLocationKey()).entry("rawLocation", asset.getConfigLocation()) .entry("finalLocation", asset.getFinalLocation()).entry("storageKey", asset.getStorageKey()) .entry("contents", storage.contains(storageKey) ? storage.get(storageKey).getContents() : "Not fetched") .entry("presentInStorage", storage.contains(storageKey)).entry("version", asset.getVersion()).create()); } pageContext.put("impl", storage.getClass().getName()); pageContext.put("number", storage.size()); pageContext.put("assets", options); return pageContext; } }
this.context.getAssetStorage().put(asset.getStorageKey(), new StorageEntry(asset, contents));
this.context.getAssetStorage().put(asset.getStorageKey(), new StorageEntry(asset, contents));