Search in sources :

Example 1 with Request

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

the class Transaction method setWebRequest.

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

Example 2 with Request

use of com.newrelic.api.agent.Request 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 3 with Request

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

the class TransactionInboundHeadersTest method TestEmptyRequestHeaders.

@Test
public void TestEmptyRequestHeaders() {
    Transaction tx = Transaction.getTransaction();
    Dispatcher dispatcher = Mockito.mock(Dispatcher.class);
    Request request = Mockito.mock(Request.class);
    Mockito.when(dispatcher.getRequest()).thenReturn(request);
    tx.setDispatcher(dispatcher);
    InboundHeaders requestHeaders = Transaction.getRequestHeaders(tx);
    // Non-null request headers should be deobfuscated.
    Assert.assertEquals(requestHeaders.getClass(), DeobfuscatedInboundHeaders.class);
}
Also used : Request(com.newrelic.api.agent.Request) InboundHeaders(com.newrelic.api.agent.InboundHeaders) Dispatcher(com.newrelic.agent.dispatchers.Dispatcher) Test(org.junit.Test)

Example 4 with Request

use of com.newrelic.api.agent.Request 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 Request

use of com.newrelic.api.agent.Request 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

Request (com.newrelic.api.agent.Request)15 Response (com.newrelic.api.agent.Response)12 Test (org.junit.Test)11 MockHttpServletRequest (org.apache.struts.mock.MockHttpServletRequest)10 MockHttpServletResponse (org.apache.struts.mock.MockHttpServletResponse)10 HttpServletResponse (javax.servlet.http.HttpServletResponse)9 Transaction (com.newrelic.agent.Transaction)8 BrowserConfigTest (com.newrelic.agent.browser.BrowserConfigTest)8 ClassMethodSignature (com.newrelic.agent.tracers.ClassMethodSignature)8 OtherRootTracer (com.newrelic.agent.tracers.OtherRootTracer)8 SimpleMetricNameFormat (com.newrelic.agent.tracers.metricname.SimpleMetricNameFormat)8 MockHttpRequest (com.newrelic.agent.tracers.servlet.MockHttpRequest)8 MockHttpResponse (com.newrelic.agent.tracers.servlet.MockHttpResponse)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)2 TransactionStats (com.newrelic.agent.stats.TransactionStats)2 WebResponse (com.newrelic.agent.bridge.WebResponse)1