/** * Lets InspectIT optionally instrument the given response by wrapping it. * * @param httpRequestObj * the request (an instance of javax.servlet.ServletRequest) * @param httpResponseObj * the response object (an instance of javax.servlet.ServletResponse) * @return the new response object to use, or the original one if it was not instrumented. */ public Object instrumentResponse(Object httpRequestObj, Object httpResponseObj) { try { if (configurationValid && WHttpServletResponse.isInstance(httpResponseObj)) { if (!linker.isProxyInstance(httpResponseObj, TagInjectionResponseWrapper.class)) { ClassLoader cl = httpResponseObj.getClass().getClassLoader(); TagInjectionResponseWrapper wrap = new TagInjectionResponseWrapper(httpRequestObj, httpResponseObj, tracer, scriptTags); Object proxy = linker.createProxy(TagInjectionResponseWrapper.class, wrap, cl); if (proxy == null) { return httpResponseObj; } else { return proxy; } } } } catch (Throwable e) { // NOPMD LOG.error("Error instrumenting response object.", e); } return httpResponseObj; // No instrumentation }
@SuppressWarnings("unchecked") @BeforeMethod public void initLinker() { when(linker.isProxyInstance(any(TagInjectionResponseWrapper.class), any(Class.class))).thenAnswer(new Answer<Boolean>() { @Override public Boolean answer(InvocationOnMock invocation) throws Throwable { return invocation.getArguments()[0] instanceof FakeWrapper; } }); when(linker.createProxy(any(Class.class), any(TagInjectionResponseWrapper.class), any(ClassLoader.class))).then(new Answer<Object>() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { respWrapper = (TagInjectionResponseWrapper) invocation.getArguments()[1]; return new FakeWrapper(dummyResponse); } }); }
@SuppressWarnings("unchecked") @BeforeMethod public void init() { when(linker.isProxyInstance(any(Object.class), any(Class.class))).thenReturn(false); when(linker.createProxy(any(Class.class), any(TagInjectionOutputStream.class), any(ClassLoader.class))).then(new Answer<Object>() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { TagInjectionOutputStream injectedStream = (TagInjectionOutputStream) invocation.getArguments()[1]; injectedStream.proxyLinked(dummyStreamProxy, linker); return dummyStreamProxy; } }); streamResult.reset(); respWrapper = new TagInjectionResponseWrapper(dummyRequest, dummyResponse, tracer, tagPrinter); respWrapper.proxyLinked(dummyProxy, linker); }
/** * 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; }