use of org.b3log.latke.servlet.HTTPRequestContext in project solo by b3log.
the class ArticleProcessorTestCase method showArticle.
/**
* showArticle.
*
* @throws Exception exception
*/
@Test(dependsOnMethods = "init")
public void showArticle() throws Exception {
final JSONObject article = getArticleRepository().get(new Query()).optJSONArray(Keys.RESULTS).optJSONObject(0);
final HttpServletRequest request = mock(HttpServletRequest.class);
when(request.getServletContext()).thenReturn(mock(ServletContext.class));
when(request.getRequestURI()).thenReturn("/pagepermalink");
when(request.getMethod()).thenReturn("GET");
when(request.getAttribute(Keys.TEMAPLTE_DIR_NAME)).thenReturn(Option.DefaultPreference.DEFAULT_SKIN_DIR_NAME);
when(request.getAttribute(Keys.HttpRequest.START_TIME_MILLIS)).thenReturn(System.currentTimeMillis());
when(request.getAttribute(Article.ARTICLE)).thenReturn(article);
final StringWriter stringWriter = new StringWriter();
final PrintWriter printWriter = new PrintWriter(stringWriter);
final HttpServletResponse response = mock(HttpServletResponse.class);
when(response.getWriter()).thenReturn(printWriter);
final HTTPRequestContext httpRequestContext = new HTTPRequestContext();
httpRequestContext.setRequest(request);
httpRequestContext.setResponse(response);
final ArticleProcessor articleProcessor = Lifecycle.getBeanManager().getReference(ArticleProcessor.class);
articleProcessor.showArticle(httpRequestContext, request, response);
final Map<String, Object> dataModel = httpRequestContext.getRenderer().getRenderDataModel();
final JSONObject handledArticle = (JSONObject) dataModel.get(Article.ARTICLE);
Assert.assertTrue(StringUtils.contains(handledArticle.optString(Keys.OBJECT_ID), article.optString(Keys.OBJECT_ID)));
}
use of org.b3log.latke.servlet.HTTPRequestContext in project solo by b3log.
the class InitCheckFilter method doFilter.
/**
* If Solo has not been initialized, so redirects to /init.
*
* @param request the specified request
* @param response the specified response
* @param chain filter chain
* @throws IOException io exception
* @throws ServletException servlet exception
*/
@Override
public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException {
final HttpServletRequest httpServletRequest = (HttpServletRequest) request;
final String requestURI = httpServletRequest.getRequestURI();
LOGGER.log(Level.TRACE, "Request[URI={0}]", requestURI);
// If requests Latke Remote APIs, skips this filter
if (requestURI.startsWith(Latkes.getContextPath() + "/latke/remote")) {
chain.doFilter(request, response);
return;
}
final LatkeBeanManager beanManager = Lifecycle.getBeanManager();
final InitService initService = beanManager.getReference(InitService.class);
if (initService.isInited()) {
chain.doFilter(request, response);
return;
}
if ("POST".equalsIgnoreCase(httpServletRequest.getMethod()) && (Latkes.getContextPath() + "/init").equals(requestURI)) {
// Do initailization
chain.doFilter(request, response);
return;
}
if (!initReported) {
LOGGER.log(Level.DEBUG, "Solo has not been initialized, so redirects to /init");
initReported = true;
}
final HTTPRequestContext context = new HTTPRequestContext();
context.setRequest((HttpServletRequest) request);
context.setResponse((HttpServletResponse) response);
request.setAttribute(Keys.HttpRequest.REQUEST_URI, Latkes.getContextPath() + "/init");
request.setAttribute(Keys.HttpRequest.REQUEST_METHOD, HTTPRequestMethod.GET.name());
final HttpControl httpControl = new HttpControl(DispatcherServlet.SYS_HANDLER.iterator(), context);
try {
httpControl.nextHandler();
} catch (final Exception e) {
context.setRenderer(new HTTP500Renderer(e));
}
DispatcherServlet.result(context);
}
use of org.b3log.latke.servlet.HTTPRequestContext in project solo by b3log.
the class PermalinkFilter method dispatchToArticleOrPageProcessor.
/**
* Dispatches the specified request to the specified article or page
* processor with the specified response.
*
* @param request the specified request
* @param response the specified response
* @param article the specified article
* @param page the specified page
* @throws ServletException servlet exception
* @throws IOException io exception
* @see HTTPRequestDispatcher#dispatch(org.b3log.latke.servlet.HTTPRequestContext)
*/
private void dispatchToArticleOrPageProcessor(final ServletRequest request, final ServletResponse response, final JSONObject article, final JSONObject page) throws ServletException, IOException {
final HTTPRequestContext context = new HTTPRequestContext();
context.setRequest((HttpServletRequest) request);
context.setResponse((HttpServletResponse) response);
if (null != article) {
request.setAttribute(Article.ARTICLE, article);
request.setAttribute(Keys.HttpRequest.REQUEST_URI, Latkes.getContextPath() + "/article");
} else {
request.setAttribute(Page.PAGE, page);
request.setAttribute(Keys.HttpRequest.REQUEST_URI, Latkes.getContextPath() + "/page");
}
request.setAttribute(Keys.HttpRequest.REQUEST_METHOD, HTTPRequestMethod.GET.name());
final HttpControl httpControl = new HttpControl(DispatcherServlet.SYS_HANDLER.iterator(), context);
try {
httpControl.nextHandler();
} catch (final Exception e) {
context.setRenderer(new HTTP500Renderer(e));
}
DispatcherServlet.result(context);
}
use of org.b3log.latke.servlet.HTTPRequestContext in project solo by b3log.
the class MockDispatcherServlet method service.
protected void service(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
final HTTPRequestContext httpRequestContext = new HTTPRequestContext();
httpRequestContext.setRequest(req);
httpRequestContext.setResponse(resp);
final HttpControl httpControl = new HttpControl(SYS_HANDLER.iterator(), httpRequestContext);
try {
httpControl.nextHandler();
} catch (final Exception e) {
httpRequestContext.setRenderer(new HTTP500Renderer(e));
}
result(httpRequestContext);
}
use of org.b3log.latke.servlet.HTTPRequestContext in project solo by b3log.
the class PageProcessorTestCase method showPage.
/**
* showPage.
*
* @throws Exception exception
*/
@Test(dependsOnMethods = "init")
public void showPage() throws Exception {
final JSONObject page = addPage();
final HttpServletRequest request = mock(HttpServletRequest.class);
when(request.getServletContext()).thenReturn(mock(ServletContext.class));
when(request.getRequestURI()).thenReturn("/pagepermalink");
when(request.getMethod()).thenReturn("GET");
when(request.getAttribute(Keys.TEMAPLTE_DIR_NAME)).thenReturn(Option.DefaultPreference.DEFAULT_SKIN_DIR_NAME);
when(request.getAttribute(Keys.HttpRequest.START_TIME_MILLIS)).thenReturn(System.currentTimeMillis());
when(request.getAttribute(Page.PAGE)).thenReturn(page);
final StringWriter stringWriter = new StringWriter();
final PrintWriter printWriter = new PrintWriter(stringWriter);
final HttpServletResponse response = mock(HttpServletResponse.class);
when(response.getWriter()).thenReturn(printWriter);
final HTTPRequestContext httpRequestContext = new HTTPRequestContext();
httpRequestContext.setRequest(request);
httpRequestContext.setResponse(response);
final PageProcessor pageProcessor = Lifecycle.getBeanManager().getReference(PageProcessor.class);
pageProcessor.showPage(httpRequestContext);
final Map<String, Object> dataModel = httpRequestContext.getRenderer().getRenderDataModel();
final JSONObject handledPage = (JSONObject) dataModel.get(Page.PAGE);
Assert.assertTrue(StringUtils.contains(handledPage.optString(Keys.OBJECT_ID), page.optString(Keys.OBJECT_ID)));
}
Aggregations