/** * Builds the JS agent from single char arguments. * * @param arguments * all arguments together as a string. * @return the generated stream which builds the agent. */ public String buildJsFile(String arguments) { StringBuilder script = new StringBuilder(); script.append(JS_AGENT_PREAMBLE); script.append("window.inspectIT_settings.activeAgentModules = \"").append(arguments).append("\";"); script.append(getAgentCoreSource()); // prevent duplicates of the modules added Set<JSAgentModule> alreadyAddedModules = new HashSet<JSAgentModule>(); // add the modules source code for (char moduleIdentifier : arguments.toCharArray()) { if (JSAgentModule.IDENTIFIER_MAP.containsKey(moduleIdentifier)) { JSAgentModule module = JSAgentModule.IDENTIFIER_MAP.get(moduleIdentifier); if (!alreadyAddedModules.contains(module)) { script.append(getAgentModuleSource(module)); alreadyAddedModules.add(module); } } } script.append(JS_AGENT_EPILOGUE); return script.toString(); }
/** * @return the core agent source code, either laoded from the resources or directly fetched form * the cache. */ @SuppressWarnings({ "PMD" }) private String getAgentCoreSource() { if (agentCoreSource == null) { synchronized (JSAgentBuilder.class) { if (agentCoreSource == null) { try { agentCoreSource = readResourceFile(JSBASE_RESOURCE); } catch (Exception e) { LOG.error("unable to read JS Agent core"); return ""; } } } } return agentCoreSource; }
/** * Sends the script using the given response object. * * @param res * the response to write * @param activeModules * a String listing the identifiers of the active modules. */ private void sendScript(WHttpServletResponse res, String activeModules) { // we respond with the script code res.setStatus(200); res.setContentType("application/javascript"); res.addHeader("Cache-Control", "public, max-age=" + JSAgentBuilder.JS_AGENT_CACHE_MAX_AGE_SECONDS); PrintWriter writer = res.getWriter(); writer.write(agentBuilder.buildJsFile(activeModules)); writer.flush(); }
@BeforeMethod public void initMocks() throws IOException, StorageException { eumConfig.setActiveModules("a12"); eumConfig.setEnabled(true); eumConfig.setScriptBaseUrl("/baseUrl/"); when(dummyRequest.getRequestURI()).thenReturn("/mycool/url/here"); when(config.getEndUserMonitoringConfig()).thenReturn(eumConfig); when(dummyResponse.getWriter()).thenReturn(Mockito.mock(PrintWriter.class)); when(dummyResponse.getOutputStream()).thenReturn(Mockito.mock(ServletOutputStream.class)); when(agentBuilder.buildJsFile(any(String.class))).thenReturn(AGENT_CODE); }
/** * @param module * the module of which the source code shall be returned * @return @return the modules source code, either loaded from the resources or directly fetched * form the cache. */ private String getAgentModuleSource(JSAgentModule module) { if (!moduleSourceCache.containsKey(module)) { try { String src = readResourceFile(module.getModuleSourceFile()); moduleSourceCache.putIfAbsent(module, src); return src; } catch (Exception e) { LOG.error("unable to read JS Agent core"); return ""; } } else { return moduleSourceCache.get(module); } }