Search in sources :

Example 1 with Response

use of com.newrelic.api.agent.Response in project newrelic-java-agent by newrelic.

the class Transaction method setRequestAndResponse.

public void setRequestAndResponse(Request request, Response response) {
    Request req = request == null ? DUMMY_REQUEST : request;
    Response res = response == null ? DUMMY_RESPONSE : response;
    setDispatcher(new WebRequestDispatcher(req, res, this));
}
Also used : Response(com.newrelic.api.agent.Response) WebResponse(com.newrelic.agent.bridge.WebResponse) Request(com.newrelic.api.agent.Request) WebRequestDispatcher(com.newrelic.agent.dispatchers.WebRequestDispatcher)

Example 2 with Response

use of com.newrelic.api.agent.Response in project newrelic-java-agent by newrelic.

the class Transaction method setWebResponse.

/**
 * Sets the response for the current transaction.
 * Setting the response will convert the current transaction into a web transaction.
 * Successive calls will have no effect (first wins).
 *
 * @param resp The current transaction's response.
 */
public void setWebResponse(Response resp) {
    final Response response = resp == null ? DUMMY_RESPONSE : resp;
    synchronized (lock) {
        // Set web response at most once.
        if (dispatcher instanceof WebRequestDispatcher) {
            if (DUMMY_RESPONSE.equals(dispatcher.getResponse())) {
                dispatcher.setResponse(response);
                Agent.LOG.log(Level.FINEST, "Set web response for transaction {0} to {1}", this, response);
            } else {
                Agent.LOG.log(Level.FINEST, "Not setting web response for transaction {0}. Web response is already set.", this);
            }
        } else {
            Agent.LOG.log(Level.FINEST, "Set web response for transaction {0}. Transaction does not have a corresponding request", this);
            setDispatcher(new WebRequestDispatcher(DUMMY_REQUEST, response, this));
        }
    }
}
Also used : Response(com.newrelic.api.agent.Response) WebResponse(com.newrelic.agent.bridge.WebResponse) WebRequestDispatcher(com.newrelic.agent.dispatchers.WebRequestDispatcher)

Example 3 with Response

use of com.newrelic.api.agent.Response in project newrelic-java-agent by newrelic.

the class CrossProcessTransactionStateImpl method writeResponseHeaders.

@Override
public void writeResponseHeaders() {
    if (tx.isIgnore()) {
        return;
    }
    Dispatcher dispatcher = tx.getDispatcher();
    if (dispatcher == null) {
        return;
    }
    try {
        Response response = dispatcher.getResponse();
        long contentLength = -1;
        if (response instanceof ExtendedResponse) {
            contentLength = ((ExtendedResponse) response).getContentLength();
        } else {
            contentLength = tx.getInboundHeaderState().getRequestContentLength();
        }
        processOutboundResponseHeaders(response, contentLength);
    } catch (Throwable e) {
        Agent.LOG.log(Level.FINEST, e, "Error attempting to write response headers in transaction: {0}", tx);
    }
}
Also used : ExtendedResponse(com.newrelic.api.agent.ExtendedResponse) Response(com.newrelic.api.agent.Response) ExtendedResponse(com.newrelic.api.agent.ExtendedResponse) Dispatcher(com.newrelic.agent.dispatchers.Dispatcher)

Example 4 with Response

use of com.newrelic.api.agent.Response in project newrelic-java-agent by newrelic.

the class ApiCallingAsyncTest method testNaming1.

@Test(timeout = 10000)
public void testNaming1() throws Exception {
    final Request req = new Request() {

        @Override
        public HeaderType getHeaderType() {
            return null;
        }

        @Override
        public String getHeader(String name) {
            return null;
        }

        @Override
        public String getRequestURI() {
            return null;
        }

        @Override
        public String getRemoteUser() {
            return null;
        }

        @Override
        public Enumeration<?> getParameterNames() {
            return null;
        }

        @Override
        public String[] getParameterValues(String name) {
            return null;
        }

        @Override
        public Object getAttribute(String name) {
            return null;
        }

        @Override
        public String getCookieValue(String name) {
            return null;
        }
    };
    final Response resp = new Response() {

        @Override
        public HeaderType getHeaderType() {
            return null;
        }

        @Override
        public void setHeader(String name, String value) {
        }

        @Override
        public int getStatus() throws Exception {
            return 0;
        }

        @Override
        public String getStatusMessage() throws Exception {
            return null;
        }

        @Override
        public String getContentType() {
            return null;
        }
    };
    // The two threads call APIs in different orders. The purpose of the test is to ensure
    // that the same metrics get reported either way.
    Thread t1 = new Thread() {

        @Override
        @Trace(dispatcher = true)
        public void run() {
            NewRelic.setTransactionName(null, "MyOtherTransaction");
            NewRelic.setRequestAndResponse(req, resp);
        }
    };
    t1.start();
    t1.join();
    TransactionStats s1 = getStats();
    Thread t2 = new Thread() {

        @Override
        @Trace(dispatcher = true)
        public void run() {
            NewRelic.setRequestAndResponse(req, resp);
            NewRelic.setTransactionName(null, "MyOtherTransaction");
        }
    };
    t2.start();
    t2.join();
    TransactionStats s2 = getStats();
    assertSameKeys(s1, s2, new String[] { "Java/com.newrelic.agent.async.ApiCallingAsyncTest\\$\\d/run" });
}
Also used : Response(com.newrelic.api.agent.Response) TransactionStats(com.newrelic.agent.stats.TransactionStats) Request(com.newrelic.api.agent.Request) Test(org.junit.Test)

Example 5 with Response

use of com.newrelic.api.agent.Response in project newrelic-java-agent by newrelic.

the class TransactionTimeTest method doWebWork.

@Trace(dispatcher = true)
public void doWebWork() throws InterruptedException {
    Request request = new RequestWrapper(new MockHttpServletRequest("/", "mytest", "", "&test=dude"));
    Response response = new ResponseWrapper(new MockHttpServletResponse());
    NewRelic.setRequestAndResponse(request, response);
    hello1();
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) MockHttpServletResponse(org.apache.struts.mock.MockHttpServletResponse) Response(com.newrelic.api.agent.Response) MockHttpServletRequest(org.apache.struts.mock.MockHttpServletRequest) Request(com.newrelic.api.agent.Request) MockHttpServletRequest(org.apache.struts.mock.MockHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletResponse(org.apache.struts.mock.MockHttpServletResponse) Trace(com.newrelic.api.agent.Trace)

Aggregations

Response (com.newrelic.api.agent.Response)18 Request (com.newrelic.api.agent.Request)12 Transaction (com.newrelic.agent.Transaction)10 ClassMethodSignature (com.newrelic.agent.tracers.ClassMethodSignature)10 OtherRootTracer (com.newrelic.agent.tracers.OtherRootTracer)10 SimpleMetricNameFormat (com.newrelic.agent.tracers.metricname.SimpleMetricNameFormat)10 MockHttpServletRequest (org.apache.struts.mock.MockHttpServletRequest)10 MockHttpServletResponse (org.apache.struts.mock.MockHttpServletResponse)10 Test (org.junit.Test)10 MockHttpRequest (com.newrelic.agent.tracers.servlet.MockHttpRequest)9 MockHttpResponse (com.newrelic.agent.tracers.servlet.MockHttpResponse)9 HttpServletResponse (javax.servlet.http.HttpServletResponse)9 BrowserConfigTest (com.newrelic.agent.browser.BrowserConfigTest)8 ExtendedRequest (com.newrelic.api.agent.ExtendedRequest)8 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)8 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)8 TransactionDataList (com.newrelic.agent.TransactionDataList)5 WebRequestDispatcher (com.newrelic.agent.dispatchers.WebRequestDispatcher)3 WebResponse (com.newrelic.agent.bridge.WebResponse)2 TransactionStats (com.newrelic.agent.stats.TransactionStats)2