@Test public void testCompileJs() throws Exception { final String templateName = "templates/template1.soy"; final String jsData = "jsData"; final HttpServletRequest request = mock(HttpServletRequest.class); when(localeProvider.resolveLocale(request)).thenReturn(Optional.<Locale>absent()); when(soyMsgBundleResolver.resolve(any(Optional.class))).thenReturn(Optional.<SoyMsgBundle>absent()); final URL url1 = getClass().getClassLoader().getResource(templateName); when(templateFilesResolver.resolve(templateName)).thenReturn(Optional.of(url1)); when(tofuCompiler.compileToJsSrc(url1, null)).thenReturn(Optional.of(jsData)); final ResponseEntity<String> responseEntity = soyAjaxController.compile("", new String[]{templateName}, null, "true", request); Assert.assertEquals("data should be equal", jsData, responseEntity.getBody()); Assert.assertTrue("http status should be equal", (responseEntity.getStatusCode() == HttpStatus.OK)); }
private ResponseEntity<String> compileJs(final String[] templateFileNames, final String hash, final boolean disableProcessors, final HttpServletRequest request, final String locale ) throws IOException { Preconditions.checkNotNull(templateFilesResolver, "templateFilesResolver cannot be null"); if (isHotReloadModeOff()) { final Optional<String> template = extractAndCombineAll(hash, templateFileNames); if (template.isPresent()) { return prepareResponseFor(template.get(), disableProcessors); } } try { final Map<URL,String> compiledTemplates = compileTemplates(templateFileNames, request, locale); final Optional<String> allCompiledTemplates = concatCompiledTemplates(compiledTemplates); if (!allCompiledTemplates.isPresent()) { throw notFound("Template file(s) could not be resolved."); } if (isHotReloadModeOff()) { synchronized (cachedJsTemplates) { Map<String, String> map = cachedJsTemplates.getIfPresent(hash); if (map == null) { map = new ConcurrentHashMap<String, String>(); } else { map.put(PathUtils.arrayToPath(templateFileNames), allCompiledTemplates.get()); } this.cachedJsTemplates.put(hash, map); } } return prepareResponseFor(allCompiledTemplates.get(), disableProcessors); } catch (final SecurityException ex) { return new ResponseEntity<String>(ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); } catch (final HttpClientErrorException ex) { return new ResponseEntity<String>(ex.getMessage(), ex.getStatusCode()); } }
SoyAjaxController { private ResponseEntity<String> compileJs(final String[] templateFileNames, final String hash, final boolean disableProcessors, final HttpServletRequest request, final String locale ) throws IOException { Preconditions.checkNotNull(templateFilesResolver, "templateFilesResolver cannot be null"); if (isHotReloadModeOff()) { final Optional<String> template = extractAndCombineAll(hash, templateFileNames); if (template.isPresent()) { return prepareResponseFor(template.get(), disableProcessors); } } try { final Map<URL,String> compiledTemplates = compileTemplates(templateFileNames, request, locale); final Optional<String> allCompiledTemplates = concatCompiledTemplates(compiledTemplates); if (!allCompiledTemplates.isPresent()) { throw notFound("Template file(s) could not be resolved."); } if (isHotReloadModeOff()) { synchronized (cachedJsTemplates) { Map<String, String> map = cachedJsTemplates.getIfPresent(hash); if (map == null) { map = new ConcurrentHashMap<String, String>(); } else { map.put(PathUtils.arrayToPath(templateFileNames), allCompiledTemplates.get()); } this.cachedJsTemplates.put(hash, map); } } return prepareResponseFor(allCompiledTemplates.get(), disableProcessors); } catch (final SecurityException ex) { return new ResponseEntity<String>(ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); } catch (final HttpClientErrorException ex) { return new ResponseEntity<String>(ex.getMessage(), ex.getStatusCode()); } } }
SoyAjaxController { private ResponseEntity<String> compileJs(final String[] templateFileNames, final String hash, final boolean disableProcessors, final HttpServletRequest request, final String locale ) throws IOException { Preconditions.checkNotNull(templateFilesResolver, "templateFilesResolver cannot be null"); if (isHotReloadModeOff()) { final Optional<String> template = extractAndCombineAll(hash, templateFileNames); if (template.isPresent()) { return prepareResponseFor(template.get(), disableProcessors); } } try { final Map<URL,String> compiledTemplates = compileTemplates(templateFileNames, request, locale); final Optional<String> allCompiledTemplates = concatCompiledTemplates(compiledTemplates); if (!allCompiledTemplates.isPresent()) { throw notFound("Template file(s) could not be resolved."); } if (isHotReloadModeOff()) { synchronized (cachedJsTemplates) { Map<String, String> map = cachedJsTemplates.getIfPresent(hash); if (map == null) { map = new ConcurrentHashMap<String, String>(); } else { map.put(PathUtils.arrayToPath(templateFileNames), allCompiledTemplates.get()); } this.cachedJsTemplates.put(hash, map); } } return prepareResponseFor(allCompiledTemplates.get(), disableProcessors); } catch (final SecurityException ex) { return new ResponseEntity<String>(ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); } catch (final HttpClientErrorException ex) { return new ResponseEntity<String>(ex.getMessage(), ex.getStatusCode()); } } SoyAjaxController(); }
SoyAjaxController { private ResponseEntity<String> compileJs(final String[] templateFileNames, final String hash, final boolean disableProcessors, final HttpServletRequest request, final String locale ) throws IOException { Preconditions.checkNotNull(templateFilesResolver, "templateFilesResolver cannot be null"); if (isHotReloadModeOff()) { final Optional<String> template = extractAndCombineAll(hash, templateFileNames); if (template.isPresent()) { return prepareResponseFor(template.get(), disableProcessors); } } try { final Map<URL,String> compiledTemplates = compileTemplates(templateFileNames, request, locale); final Optional<String> allCompiledTemplates = concatCompiledTemplates(compiledTemplates); if (!allCompiledTemplates.isPresent()) { throw notFound("Template file(s) could not be resolved."); } if (isHotReloadModeOff()) { synchronized (cachedJsTemplates) { Map<String, String> map = cachedJsTemplates.getIfPresent(hash); if (map == null) { map = new ConcurrentHashMap<String, String>(); } else { map.put(PathUtils.arrayToPath(templateFileNames), allCompiledTemplates.get()); } this.cachedJsTemplates.put(hash, map); } } return prepareResponseFor(allCompiledTemplates.get(), disableProcessors); } catch (final SecurityException ex) { return new ResponseEntity<String>(ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); } catch (final HttpClientErrorException ex) { return new ResponseEntity<String>(ex.getMessage(), ex.getStatusCode()); } } SoyAjaxController(); @PostConstruct void init(); @RequestMapping(value="/soy/compileJs", method=GET) ResponseEntity<String> compile(@RequestParam(required = false, value="hash", defaultValue = "") final String hash,
                                                            @RequestParam(required = true, value = "file") final String[] templateFileNames,
                                                            @RequestParam(required = false, value = "locale") String locale,
                                                            @RequestParam(required = false, value = "disableProcessors", defaultValue = "false") String disableProcessors,
                                                            final HttpServletRequest request); void setCacheControl(final String cacheControl); void setTemplateFilesResolver(final TemplateFilesResolver templateFilesResolver); void setTofuCompiler(final TofuCompiler tofuCompiler); void setSoyMsgBundleResolver(final SoyMsgBundleResolver soyMsgBundleResolver); void setLocaleProvider(final LocaleProvider localeProvider); void setHotReloadMode(final boolean hotReloadMode); void setEncoding(final String encoding); void setExpiresHeaders(final String expiresHeaders); void setOutputProcessors(List<OutputProcessor> outputProcessors); void setAuthManager(AuthManager authManager); void setCacheMaxSize(int cacheMaxSize); void setExpireAfterWrite(int expireAfterWrite); void setExpireAfterWriteUnit(String expireAfterWriteUnit); }
SoyAjaxController { private ResponseEntity<String> compileJs(final String[] templateFileNames, final String hash, final boolean disableProcessors, final HttpServletRequest request, final String locale ) throws IOException { Preconditions.checkNotNull(templateFilesResolver, "templateFilesResolver cannot be null"); if (isHotReloadModeOff()) { final Optional<String> template = extractAndCombineAll(hash, templateFileNames); if (template.isPresent()) { return prepareResponseFor(template.get(), disableProcessors); } } try { final Map<URL,String> compiledTemplates = compileTemplates(templateFileNames, request, locale); final Optional<String> allCompiledTemplates = concatCompiledTemplates(compiledTemplates); if (!allCompiledTemplates.isPresent()) { throw notFound("Template file(s) could not be resolved."); } if (isHotReloadModeOff()) { synchronized (cachedJsTemplates) { Map<String, String> map = cachedJsTemplates.getIfPresent(hash); if (map == null) { map = new ConcurrentHashMap<String, String>(); } else { map.put(PathUtils.arrayToPath(templateFileNames), allCompiledTemplates.get()); } this.cachedJsTemplates.put(hash, map); } } return prepareResponseFor(allCompiledTemplates.get(), disableProcessors); } catch (final SecurityException ex) { return new ResponseEntity<String>(ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); } catch (final HttpClientErrorException ex) { return new ResponseEntity<String>(ex.getMessage(), ex.getStatusCode()); } } SoyAjaxController(); @PostConstruct void init(); @RequestMapping(value="/soy/compileJs", method=GET) ResponseEntity<String> compile(@RequestParam(required = false, value="hash", defaultValue = "") final String hash,
                                                            @RequestParam(required = true, value = "file") final String[] templateFileNames,
                                                            @RequestParam(required = false, value = "locale") String locale,
                                                            @RequestParam(required = false, value = "disableProcessors", defaultValue = "false") String disableProcessors,
                                                            final HttpServletRequest request); void setCacheControl(final String cacheControl); void setTemplateFilesResolver(final TemplateFilesResolver templateFilesResolver); void setTofuCompiler(final TofuCompiler tofuCompiler); void setSoyMsgBundleResolver(final SoyMsgBundleResolver soyMsgBundleResolver); void setLocaleProvider(final LocaleProvider localeProvider); void setHotReloadMode(final boolean hotReloadMode); void setEncoding(final String encoding); void setExpiresHeaders(final String expiresHeaders); void setOutputProcessors(List<OutputProcessor> outputProcessors); void setAuthManager(AuthManager authManager); void setCacheMaxSize(int cacheMaxSize); void setExpireAfterWrite(int expireAfterWrite); void setExpireAfterWriteUnit(String expireAfterWriteUnit);  }