Example usage for javax.servlet.http HttpServletRequest getServletContext

List of usage examples for javax.servlet.http HttpServletRequest getServletContext

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletRequest getServletContext.

Prototype

public ServletContext getServletContext();

Source Link

Document

Gets the servlet context to which this ServletRequest was last dispatched.

Usage

From source file:org.b3log.solo.processor.IndexProcessorTestCase.java

/**
 * register.//from   w w  w. ja  v  a  2 s  .  c  o  m
 *
 * @throws Exception exception
 */
@Test(dependsOnMethods = "init")
public void register() throws Exception {
    final HttpServletRequest request = mock(HttpServletRequest.class);
    when(request.getServletContext()).thenReturn(mock(ServletContext.class));
    when(request.getRequestURI()).thenReturn("/register");
    when(request.getMethod()).thenReturn("GET");
    when(request.getAttribute(Keys.TEMAPLTE_DIR_NAME)).thenReturn("next");
    when(request.getAttribute(Keys.HttpRequest.START_TIME_MILLIS)).thenReturn(System.currentTimeMillis());

    final MockDispatcherServlet dispatcherServlet = new MockDispatcherServlet();
    dispatcherServlet.init();

    final StringWriter stringWriter = new StringWriter();
    final PrintWriter printWriter = new PrintWriter(stringWriter);

    final HttpServletResponse response = mock(HttpServletResponse.class);
    when(response.getWriter()).thenReturn(printWriter);

    dispatcherServlet.service(request, response);

    final String content = stringWriter.toString();
    Assert.assertTrue(StringUtils.contains(content, "<title>Solo </title>"));
}

From source file:com.vaadin.tests.applicationservlet.VaadinRefreshServlet.java

@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    if (request.getRequestURI().contains("/UIDL")) {
        InputStream loginHtml = request.getServletContext().getResourceAsStream("/statictestfiles/login.html");
        IOUtils.copy(loginHtml, response.getOutputStream());
        return;/*from w w  w .j  av  a2 s  .c  o m*/
    }
    super.service(request, response);
}

From source file:org.wso2.appserver.hibernate.jndi.sample.listener.EmployeeManager.java

protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    SessionFactory sessionFactory = (SessionFactory) request.getServletContext().getAttribute("SessionFactory");
    Session session = sessionFactory.getCurrentSession();

    Transaction tx = session.beginTransaction();
    String empName = request.getParameter("empName");
    Employee employee = new Employee();
    employee.setName(empName);/*  www  . j a  va  2 s.c  om*/
    session.persist(employee);
    tx.commit();

    PrintWriter out = response.getWriter();
    out.print("Successfully persist the Employee");
}

From source file:org.hippoecm.frontend.util.RequestUtilsTest.java

private Request createRequest(final ServletContext servletContext, final String httpForwardedForHeaderValue,
        final String customHttpForwardedForHeaderValue) {
    HttpServletRequest servletRequest = createNiceMock(HttpServletRequest.class);
    expect(servletRequest.getServletContext()).andReturn(servletContext).anyTimes();

    expect(servletRequest.getRequestURI()).andReturn("/").anyTimes();
    expect(servletRequest.getRemoteAddr()).andReturn(DEFAULT_REMOTE_ADDR).anyTimes();
    expect(servletRequest.getHeader(RequestUtils.DEFAULT_HTTP_FORWARDED_FOR_HEADER))
            .andReturn(httpForwardedForHeaderValue).anyTimes();
    expect(servletRequest.getHeader(CUSTOM_X_FORWARDED_FOR_HEADER_NAME))
            .andReturn(customHttpForwardedForHeaderValue).anyTimes();

    replay(servletRequest);/*from   ww  w. j  av a  2s.c  o  m*/

    ServletWebRequest request = new ServletWebRequest(servletRequest, "",
            Url.parse("http://localhost:8080/cms/"));

    return request;
}

From source file:com.ibm.util.merge.web.InitializeServlet.java

/**
 * Reinitializes IDMU upon POST to this servlet
 *///  w w w  .  j  a v  a 2s . co  m
@Override
public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException {
    initializeApp(req.getServletContext());
    new TextResponseWriter(res, "Successfully reinitialized IDMU.\n").write();
}

From source file:com.alliander.osgp.shared.security.MellonTokenProcessingFilter.java

private boolean isLogoutRequest(final HttpServletRequest httpRequest) {
    return httpRequest.getRequestURI().equals(httpRequest.getServletContext().getContextPath() + "/logout");
}

From source file:org.b3log.solo.processor.FeedProcessorTestCase.java

/**
 * blogArticlesRSS./*from   ww w  . jav  a2  s  . c o m*/
 *
 * @throws Exception exception
 */
@Test(dependsOnMethods = "init")
public void blogArticlesRSS() throws Exception {
    final HttpServletRequest request = mock(HttpServletRequest.class);
    when(request.getServletContext()).thenReturn(mock(ServletContext.class));
    when(request.getRequestURI()).thenReturn("/blog-articles-rss.do");
    when(request.getMethod()).thenReturn("GET");
    final MockDispatcherServlet dispatcherServlet = new MockDispatcherServlet();
    dispatcherServlet.init();

    final StringWriter stringWriter = new StringWriter();
    final PrintWriter printWriter = new PrintWriter(stringWriter);

    final HttpServletResponse response = mock(HttpServletResponse.class);
    when(response.getWriter()).thenReturn(printWriter);

    dispatcherServlet.service(request, response);

    final String content = stringWriter.toString();
    Assert.assertTrue(StringUtils.startsWith(content, "<?xml version=\"1.0\""));
}

From source file:org.b3log.solo.processor.FeedProcessorTestCase.java

/**
 * blogArticlesAtom.//from   ww w.j  ava2  s . c o m
 *
 * @throws Exception exception
 */
@Test(dependsOnMethods = "init")
public void blogArticlesAtom() throws Exception {
    final HttpServletRequest request = mock(HttpServletRequest.class);
    when(request.getServletContext()).thenReturn(mock(ServletContext.class));
    when(request.getRequestURI()).thenReturn("/blog-articles-feed.do");
    when(request.getMethod()).thenReturn("GET");

    final MockDispatcherServlet dispatcherServlet = new MockDispatcherServlet();
    dispatcherServlet.init();

    final StringWriter stringWriter = new StringWriter();
    final PrintWriter printWriter = new PrintWriter(stringWriter);

    final HttpServletResponse response = mock(HttpServletResponse.class);
    when(response.getWriter()).thenReturn(printWriter);

    dispatcherServlet.service(request, response);

    final String content = stringWriter.toString();
    Assert.assertTrue(StringUtils.startsWith(content, "<?xml version=\"1.0\"?>"));
}

From source file:org.b3log.solo.processor.FeedProcessorTestCase.java

/**
 * tagArticlesRSS./*from   w  w w .  j  a va 2  s .c  om*/
 *
 * @throws Exception exception
 */
@Test(dependsOnMethods = "init")
public void tagArticlesRSS() throws Exception {
    final HttpServletRequest request = mock(HttpServletRequest.class);
    when(request.getServletContext()).thenReturn(mock(ServletContext.class));
    when(request.getRequestURI()).thenReturn("/tag-articles-rss.do");
    when(request.getMethod()).thenReturn("GET");
    final JSONObject tag = getTagRepository().get(new Query()).optJSONArray(Keys.RESULTS).optJSONObject(0);
    when(request.getQueryString()).thenReturn("tag=" + tag.optString(Keys.OBJECT_ID));

    final MockDispatcherServlet dispatcherServlet = new MockDispatcherServlet();
    dispatcherServlet.init();

    final StringWriter stringWriter = new StringWriter();
    final PrintWriter printWriter = new PrintWriter(stringWriter);

    final HttpServletResponse response = mock(HttpServletResponse.class);
    when(response.getWriter()).thenReturn(printWriter);

    dispatcherServlet.service(request, response);

    final String content = stringWriter.toString();
    Assert.assertTrue(StringUtils.startsWith(content, "<?xml version=\"1.0\""));
}

From source file:org.b3log.solo.processor.FeedProcessorTestCase.java

/**
 * tagArticlesAtom.//from  w w w. j  a  v a2 s.com
 *
 * @throws Exception exception
 */
@Test(dependsOnMethods = "init")
public void tagArticlesAtom() throws Exception {
    final HttpServletRequest request = mock(HttpServletRequest.class);
    when(request.getServletContext()).thenReturn(mock(ServletContext.class));
    when(request.getRequestURI()).thenReturn("/tag-articles-feed.do");
    when(request.getMethod()).thenReturn("GET");

    final JSONObject tag = getTagRepository().get(new Query()).optJSONArray(Keys.RESULTS).optJSONObject(0);
    when(request.getQueryString()).thenReturn("tag=" + tag.optString(Keys.OBJECT_ID));

    final MockDispatcherServlet dispatcherServlet = new MockDispatcherServlet();
    dispatcherServlet.init();

    final StringWriter stringWriter = new StringWriter();
    final PrintWriter printWriter = new PrintWriter(stringWriter);

    final HttpServletResponse response = mock(HttpServletResponse.class);
    when(response.getWriter()).thenReturn(printWriter);

    dispatcherServlet.service(request, response);

    final String content = stringWriter.toString();
    Assert.assertTrue(StringUtils.startsWith(content, "<?xml version=\"1.0\""));
}