/** * @see de.matrixweb.smaller.javascript.JavaScriptExecutor#addScriptFile(java.net.URL) */ @Override public void addScriptFile(final URL url) { try { addScriptSource(IOUtils.toString(url), url.getFile()); } catch (final IOException e) { throw new SmallerException("Failed to include script file", e); } }
@SuppressWarnings({ "unchecked", "rawtypes" }) private void patchCssEmbedd() { try { final Field imageTypes = CSSURLEmbedder.class .getDeclaredField("imageTypes"); imageTypes.setAccessible(true); ((Set) imageTypes.get(null)).add("svg"); final Field binaryTypes = DataURIGenerator.class .getDeclaredField("binaryTypes"); binaryTypes.setAccessible(true); ((Map) binaryTypes.get(null)).put("svg", "image/svg+xml"); } catch (final NoSuchFieldException e) { throw new SmallerException("Failed to patch cssembed", e); } catch (final IllegalAccessException e) { throw new SmallerException("Failed to patch cssembed", e); } }
private final void setupBinary() { try { this.workingDir = File.createTempFile("nodejs-v" + this.version, ".dir"); this.workingDir.delete(); this.workingDir.mkdirs(); extractBinary(this.workingDir); } catch (final IOException e) { throw new SmallerException("Unable to setup the node folder", e); } }
/** * @see de.matrixweb.smaller.javascript.JavaScriptExecutor#addGlobalFunction(java.lang.String, * java.lang.Object, java.lang.String) */ @Override public void addGlobalFunction(final String name, final Object object, final String method) { try { final Method javaMethod = object.getClass().getMethod(method, String.class); this.engine.addCallbackFunction(new StringFunctor(name) { @Override public String call(final String input) { try { final Object result = javaMethod.invoke(object, input); return result == null ? null : result.toString(); } catch (final IllegalAccessException e) { throw new SmallerException("Illegal access to callback method", e); } catch (final InvocationTargetException e) { throw new SmallerException("Failed to execute callback method", e .getTargetException()); } } }); } catch (final NoSuchMethodException e) { throw new SmallerException("Failed to reflect global method", e); } }
/** * @see de.matrixweb.smaller.javascript.JavaScriptExecutor#run(java.io.Reader, * java.io.Writer) */ @Override public void run(final Reader input, final Writer output) throws IOException { LOGGER.info("Executeing V8 engine"); try { final String data = new ObjectMapper().writeValueAsString(IOUtils .toString(input)); output.write(this.engine.execute(String.format(this.source, data))); } catch (final NativeEngineException e) { throw new SmallerException("Failed to execute javascript", e); } }
/** * @see de.matrixweb.smaller.javascript.JavaScriptExecutor#addScriptFile(java.lang.String) */ @Override public void addScriptFile(final String file) { final InputStream script = getClass().getResourceAsStream(file); try { addScriptSource(IOUtils.toString(script), file); } catch (final IOException e) { throw new SmallerException("Failed to include script file", e); } finally { if (script != null) { IOUtils.closeQuietly(script); } } }
private void copyModuleToWorkingDirectory(final URL url) throws IOException { try { if ("file".equals(url.getProtocol())) { copyModuleFromFolder(url); } else if ("jar".equals(url.getProtocol())) { copyModuleFromJar(url); } else { throw new SmallerException("Unsupported url schema: " + url); } } catch (final URISyntaxException e) { throw new IOException("Invalid uri syntax", e); } }
/** * @see de.matrixweb.smaller.javascript.JavaScriptExecutor#addScriptFile(java.net.URL) */ @Override public void addScriptFile(final URL url) { try { Context.getCurrentContext().evaluateString(this.moduleScope, IOUtils.toString(url), url.getFile(), 1, null); } catch (final IOException e) { throw new SmallerException("Failed to include script file", e); } }
/** * @see de.matrixweb.smaller.resource.ResourceResolver#resolve(java.lang.String) */ @Override public Resource resolve(final String path) { try { return new VFSResource(this, this.vfs.find(path.startsWith("/") ? path : '/' + path)); } catch (final IOException e) { throw new SmallerException("Failed to resolve '" + path + "'", e); } }
/** * @see de.matrixweb.smaller.javascript.JavaScriptExecutor#addScriptFile(java.lang.String) */ @Override public void addScriptFile(final String file) { try { final InputStream script = getClass().getResourceAsStream(file); try { Context.getCurrentContext().evaluateString(this.moduleScope, IOUtils.toString(script), file, 1, null); } finally { IOUtils.closeQuietly(script); } } catch (final IOException e) { throw new SmallerException("Failed to include script file", e); } }
private byte[] handleResponse(final HttpResponse response) throws IOException, ExecutionException { final InputStream in = response.getEntity().getContent(); try { if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { throw new ExecutionException(IOUtils.toString(in)); } if (getHeader(response, "X-Smaller-Status").equals("ERROR")) { throw new SmallerException(getHeader(response, "X-Smaller-Message") .replace("#@@#", "\n")); } return IOUtils.toByteArray(in); } finally { IOUtils.closeQuietly(in); } }
/** * @param version * The spec version to execute * @param vfs * The file system to operate in * @param resolver * {@link ResourceResolver} used to locate resources * @param task * The task definition * @return Returns the processed results as {@link Resource}s */ @Deprecated public Result execute(final Version version, final VFS vfs, final ResourceResolver resolver, final Task task) { try { return execute(version, vfs, resolver, ResourceUtil.createResourceGroup(version, resolver, task), task); } catch (final IOException e) { throw new SmallerException("Failed to run processor chain", e); } }
@Deprecated private boolean validate(final Task task) { if (CollectionUtils.exists( CollectionUtils.getCardinalityMap( CollectionUtils.collect(Arrays.asList(task.getOut()), new Transformer() { @Override public Object transform(final Object input) { return FilenameUtils.getExtension(input.toString()); } })).values(), new Predicate() { @Override public boolean evaluate(final Object object) { return ((Integer) object).intValue() > 1; } })) { throw new SmallerException("Each output type must exist only once"); } final String[] processors = task.getProcessor().toLowerCase().split(","); boolean cssembedFound = false; for (final String processor : processors) { if (processor.equals("cssembed")) { cssembedFound = true; } else if (processor.equals("yuicompressor") && cssembedFound) { throw new SmallerException("yuiCompressor must run before cssembed"); } } return true; }
/** * @see de.matrixweb.smaller.javascript.JavaScriptExecutor#run(java.io.Reader, * java.io.Writer) */ @Override public void run(final Reader input, final Writer output) throws IOException { LOGGER.info("Executeing Rhino engine"); if (this.initializing && Context.getCurrentContext() != null) { this.initializing = false; Context.exit(); } final String data = new ObjectMapper().writeValueAsString(IOUtils .toString(input)); final Context context = Context.enter(); try { final ScriptableObject scope = (ScriptableObject) context .initStandardObjects(this.moduleScope); final Object result = context.evaluateString(scope, String.format(this.source, data), this.name, 1, null); output.append(String.valueOf(result)); } catch (final JavaScriptException e) { throw new SmallerException("Failed to run javascript", e); } finally { Context.exit(); } }
@Deprecated private Result execute(final Version version, final VFS vfs, final ResourceResolver resolver, final Resources resources, final Task task) { try { validate(task); final List<ProcessorOptions> entries = setupProcessors(version, task); if (version.isAtLeast(Version._1_0_0)) { execute1_0(vfs, resolver, resources, entries, task); } else { execute0_0(vfs, resources, entries); } LOGGER.info("Finished executing pipeline"); return prepareResult(vfs, resolver, task); } catch (final IOException e) { throw new SmallerException("Failed to run processor chain", e); } }
/** * @see de.matrixweb.smaller.resource.Processor#execute(de.matrixweb.vfs.VFS, * de.matrixweb.smaller.resource.Resource, java.util.Map) */ @Override public Resource execute(final VFS vfs, final Resource resource, final Map<String, Object> options) throws IOException { if (this.node == null) { try { this.node = new NodeJsExecutor(); this.node.setModule(getClass(), "sweetjs-" + this.version, null); } catch (final IOException e) { this.node = null; throw new SmallerException("Failed to setup node for sweetjs", e); } } String outfile = this.node.run(vfs, resource != null ? resource.getPath() : null, options); if (outfile != null) { final VFile file = vfs.find('/' + outfile); if (!file.exists()) { throw new SmallerException("SweetjsProcessor result does not exists"); } } return resource == null || outfile == null ? resource : resource.getResolver().resolve('/' + outfile); }
/** * @see de.matrixweb.smaller.resource.Processor#execute(de.matrixweb.vfs.VFS, * de.matrixweb.smaller.resource.Resource, java.util.Map) */ @Override public Resource execute(final VFS vfs, final Resource resource, final Map<String, Object> options) throws IOException { if (this.node == null) { try { this.node = new NodeJsExecutor(); this.node.setModule(getClass(), "svgo-" + this.version, null); } catch (final IOException e) { this.node = null; throw new SmallerException("Failed to setup node for svgo", e); } } String outfile = this.node.run(vfs, resource != null ? resource.getPath() : null, options); if (outfile != null) { final VFile file = vfs.find('/' + outfile); if (!file.exists()) { throw new SmallerException("SvgoProcessor result does not exists"); } } return resource == null || outfile == null ? resource : resource.getResolver().resolve('/' + outfile); }
/** * @see de.matrixweb.smaller.resource.Processor#execute(de.matrixweb.vfs.VFS, * de.matrixweb.smaller.resource.Resource, java.util.Map) */ @Override public Resource execute(final VFS vfs, final Resource resource, final Map<String, Object> options) throws IOException { if (this.node == null) { try { this.node = new NodeJsExecutor(); this.node.setModule(getClass(), "browserify-" + this.version, "browserify.js"); } catch (final IOException e) { this.node = null; throw new SmallerException("Failed to setup node for browserify", e); } } final String outfile = this.node.run(vfs, resource != null ? resource.getPath() : null, options); if (outfile != null) { final VFile file = vfs.find('/' + outfile); if (!file.exists()) { throw new SmallerException("BrowserifyProcessor result does not exists"); } } return resource == null || outfile == null ? resource : resource .getResolver().resolve('/' + outfile); }
private Resource executeWithNode(final VFS vfs, final Resource resource, final Map<String, Object> options) throws IOException { if (this.node == null) { try { this.node = new NodeJsExecutor(); this.node.setModule(getClass(), "uglifyjs-" + this.version); } catch (final IOException e) { this.node = null; throw new SmallerException("Failed to setup node for uglify", e); } } final VFile infile = vfs.find(resource.getPath()); if (!infile.exists()) { throw new SmallerException("Uglify input '" + infile + "' does not exists"); } final String resultPath = this.node.run(vfs, resource.getPath(), options); final VFile outfile = vfs.find('/' + resultPath); if (!outfile.exists()) { throw new SmallerException("Uglify result '" + outfile + "' does not exists"); } return resource.getResolver().resolve(outfile.getPath()); }
private void init(final Class<?> clazz) { final Context context = Context.enter(); context.setOptimizationLevel(this.optimizationLevel); context.setLanguageVersion(Context.VERSION_1_7); final ScriptableObject scope = context.initStandardObjects(); final Require require = new Require(Context.getCurrentContext(), scope, getModuleScriptProvider(clazz), null, null, false); require.install(scope); try { this.moduleScope = new ModuleScope(scope, new URI("./" + this.name), null); } catch (final URISyntaxException e) { throw new SmallerException("Failed to create moduleScope", e); } addGlobalFunction("print", LOGGER, "info"); }