@Override public PrintWriter printf(String format, Object... args) { return this.format(format, args); }
/** * {@inheritDoc} */ @Override protected EumScriptTagPrinter clone() { // NOPMD EumScriptTagPrinter result = new EumScriptTagPrinter(); result.scriptSourceURL = this.getScriptSourceURL(); result.settings = new HashMap<String, String>(this.getSettings()); return result; }
/** * Called when the headers are commited. At this point of time we have to decide whether we * force chunked encoding. */ private void commitHeaderData() { if (headerCommitted) { return; } headerCommitted = true; if (isNonHtmlContentTypeSet()) { // replicate the commands setting the content-length for (Runnable cmd : contentLengthHeaderModifications) { cmd.run(); } } else { setSessionIDCookie(); setTraceCorrelationInformation(); } }
@Test public void testDataValid() { printer.setSetting("opta", "valuea"); EumScriptTagPrinter copy = printer.clone(); assertThat(copy.getSettings(), equalTo(printer.getSettings())); assertThat(copy.getScriptSourceURL(), equalTo(printer.getScriptSourceURL())); }
/** * Proxy for {@link javax.servlet.ServletResponse#getOutputStream()}. * * @return the instrumented stream * @throws IOException * if an exception getting the original stream occurs. */ @ProxyMethod(returnType = "javax.servlet.ServletOutputStream") public OutputStream getOutputStream() throws IOException { commitHeaderData(); if (wrappedStream == null) { OutputStream originalStream = wrappedResponse.getOutputStream(); // avoid rewrapping or unnecessary wrapping if (isNonHtmlContentTypeSet() || linker.isProxyInstance(originalStream, TagInjectionOutputStream.class)) { wrappedStream = originalStream; } else { TagInjectionOutputStream resultStr = new TagInjectionOutputStream(originalStream, tagToInject.printTags()); resultStr.setEncoding(wrappedResponse.getCharacterEncoding()); ClassLoader cl = wrappedResponse.getWrappedElement().getClass().getClassLoader(); wrappedStream = (OutputStream) linker.createProxy(TagInjectionOutputStream.class, resultStr, cl); if (wrappedStream == null) { // fallback to the normal stream if it can not be linked wrappedStream = originalStream; } } } return wrappedStream; }
@Test public void testModification() { printer.setSetting("opta", "valuea"); EumScriptTagPrinter copy = printer.clone(); copy.setSetting("opta", "notvaluea"); assertThat(printer.getSettings().get("opta"), equalTo("valuea")); } }
@Test public void test() { printer.setScriptSourceURL("my_script_url with space"); printer.setSetting("opt_a", "value_a"); printer.setSetting("opt_b", "\"value b\""); String printedTags = printer.printTags(); assertThat("Url not correctly set in script tag", Pattern.compile("<script[^>]*src(\\s*)=(\\s*)\"my_script_url with space\"").matcher(printedTags).find()); assertThat("opt_a not correctly set as option", Pattern.compile("window\\.inspectIT_settings\\s*=\\s*\\{\\s*(.+\\s*,\\s*)?opt_a\\s*:\\s*value_a\\s*(,|\\})").matcher(printedTags).find()); assertThat("opt_b not correctly set as option", Pattern.compile("window\\.inspectIT_settings\\s*=\\s*\\{\\s*(.+\\s*,\\s*)?opt_b\\s*:\\s*\"value b\"\\s*(,|\\})").matcher(printedTags).find()); } }
/** * Proxy for {@link javax.servlet.ServletResponse#getWriter()}. * * @return the instrumented writer * @throws IOException * if an exception getting the original writer occurs. */ @ProxyMethod public PrintWriter getWriter() throws IOException { commitHeaderData(); if (wrappedWriter == null) { PrintWriter originalWriter = wrappedResponse.getWriter(); // avoid rewrapping or unnecessary wrapping if (isNonHtmlContentTypeSet() || (originalWriter instanceof TagInjectionPrintWriter)) { wrappedWriter = originalWriter; } else { wrappedWriter = new TagInjectionPrintWriter(originalWriter, tagToInject.printTags()); } } return wrappedWriter; }
@BeforeMethod public void init() { printerResult.getBuffer().setLength(0); respWrapper = new TagInjectionResponseWrapper(dummyRequest, dummyResponse, tracer, tagPrinter); respWrapper.proxyLinked(dummyProxy, linker); }
/** * 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(); }
/** * Constructor. After the Construction, a proxy has to be generated using a * {@link IRuntimeLinker}. * * @param requestObject * the javax.servlet.http.HTTPServletResponse which triggered this response. * @param responseObject * the javax.servlet.http.HTTPServletResponse to wrap. * @param tracer * the tracer used for request correlation * @param tagToInject * the tag to inject */ public TagInjectionResponseWrapper(Object requestObject, Object responseObject, TracerImpl tracer, EumScriptTagPrinter tagToInject) { // copy the tag printer we further modify it, for example with request correlation // information this.tagToInject = tagToInject.clone(); this.tracer = tracer; wrappedResponse = WHttpServletResponse.wrap(responseObject); wrappedRequest = WHttpServletRequest.wrap(requestObject); contentLengthHeaderModifications = new ArrayList<Runnable>(); }
@SuppressWarnings("unchecked") @Test public void testPlainTextNoInjection() throws IOException { ArgumentCaptor<TagInjectionOutputStream> streamCaptor = ArgumentCaptor.forClass(TagInjectionOutputStream.class); respWrapper.getOutputStream(); verify(linker, times(1)).createProxy(any(Class.class), streamCaptor.capture(), any(ClassLoader.class)); TagInjectionOutputStream stream = streamCaptor.getValue(); byte[] bytes = NON_HTML_TEST_CASE_A.getBytes(CHARACTER_ENCODING); int pos = 0; while (pos < bytes.length) { stream.write(bytes, pos, Math.min(3, bytes.length - pos)); pos += 3; // write 3 bytes at once } String result = new String(streamResult.toByteArray(), CHARACTER_ENCODING); assertThat(result, equalTo(NON_HTML_TEST_CASE_A)); }
@Test public void testPlainTextNoInjection() throws IOException { respWrapper.getWriter().write(NON_HTML_TEST_CASE_A); assertThat(printerResult.toString(), equalTo(NON_HTML_TEST_CASE_A)); }
/** * @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; }
@Override public Object answer(InvocationOnMock invocation) throws Throwable { TagInjectionOutputStream injectedStream = (TagInjectionOutputStream) invocation.getArguments()[1]; injectedStream.proxyLinked(dummyStreamProxy, linker); return dummyStreamProxy; } });
@Test public void testHeadInjection() throws IOException { respWrapper.getWriter().write(HTML_TEST_CASE_A); assertThat(printerResult.toString(), equalTo(HTML_TEST_CASE_A_REFERENCE)); }
@Override public PrintWriter printf(Locale l, String format, Object... args) { return this.format(l, format, args); }
/** * @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); } }
@Test public void testInvalidMarkupNoInjection() throws IOException { respWrapper.getWriter().write(NON_HTML_TEST_CASE_B); assertThat(printerResult.toString(), equalTo(NON_HTML_TEST_CASE_B)); }
@Test public void testBodyInjection() throws IOException { respWrapper.getWriter().write(HTML_TEST_CASE_B); assertThat(printerResult.toString(), equalTo(HTML_TEST_CASE_B_REFERENCE)); }