List of usage examples for javax.servlet.http HttpServletRequest getServletContext
public ServletContext getServletContext();
From source file:org.ameba.http.AbstractTenantAwareFilter.java
/** * {@inheritDoc}/*from ww w . ja v a 2s. c o m*/ */ @Override protected final void doFilterInternal(HttpServletRequest r, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { String fromSC = (String) r.getServletContext().getAttribute(Constants.PARAM_MULTI_TENANCY_ENABLED); boolean multiTenancyEnabled = Boolean .valueOf(fromSC == null ? getFilterConfig().getInitParameter(Constants.PARAM_MULTI_TENANCY_ENABLED) : fromSC); String tenant = null; if (multiTenancyEnabled && !"OPTIONS".equalsIgnoreCase(r.getMethod())) { tenant = r.getHeader(Constants.HEADER_VALUE_TENANT) == null || r.getHeader(Constants.HEADER_VALUE_TENANT).isEmpty() ? r.getHeader(Constants.HEADER_VALUE_X_TENANT) : r.getHeader(Constants.HEADER_VALUE_TENANT); if (null == tenant || tenant.isEmpty()) { String throwParam = (String) r.getServletContext() .getAttribute(Constants.PARAM_MULTI_TENANCY_THROW_IF_NOT_PRESENT); boolean throwIfNotPresent = Boolean.valueOf(throwParam == null ? getFilterConfig().getInitParameter(Constants.PARAM_MULTI_TENANCY_THROW_IF_NOT_PRESENT) : throwParam); if (throwIfNotPresent) { response.setStatus(HttpStatus.BAD_REQUEST.value()); throw new IllegalArgumentException(String.format( "No tenant information available in http header. Expected header [%s/%s] attribute not present.", Constants.HEADER_VALUE_TENANT, Constants.HEADER_VALUE_X_TENANT)); } } else { doBefore(r, response, filterChain, tenant); } } try { filterChain.doFilter(r, response); } finally { doAfter(r, response, filterChain, tenant); } }
From source file:org.b3log.solo.processor.CommentProcessorTestCase.java
/** * addPageComment.//from w w w. ja v a 2 s. co m * * @throws Exception exception */ @Test(dependsOnMethods = "init") public void addPageComment() throws Exception { final HttpServletRequest request = mock(HttpServletRequest.class); when(request.getServletContext()).thenReturn(mock(ServletContext.class)); when(request.getRequestURI()).thenReturn("/add-page-comment.do"); when(request.getMethod()).thenReturn("POST"); final HttpSession session = mock(HttpSession.class); when(session.getAttribute(CaptchaProcessor.CAPTCHA)).thenReturn("captcha123456"); when(request.getSession(false)).thenReturn(session); final JSONObject requestJSON = new JSONObject(); requestJSON.put("captcha", "captcha123456"); requestJSON.put("oId", addPage()); requestJSON.put("commentName", "88250"); requestJSON.put("commentEmail", "d@hacpai.com"); requestJSON.put("commentURL", "https://hacpai.com"); requestJSON.put("commentContent", ""); final BufferedReader reader = new BufferedReader(new StringReader(requestJSON.toString())); when(request.getReader()).thenReturn(reader); 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, "\"sc\":true")); }
From source file:org.b3log.solo.processor.CommentProcessorTestCase.java
/** * addArticleComment.//w ww .j ava2 s . c o m * * @throws Exception exception */ @Test(dependsOnMethods = "init") public void addArticleComment() throws Exception { final HttpServletRequest request = mock(HttpServletRequest.class); when(request.getServletContext()).thenReturn(mock(ServletContext.class)); when(request.getRequestURI()).thenReturn("/add-article-comment.do"); when(request.getMethod()).thenReturn("POST"); final HttpSession session = mock(HttpSession.class); when(session.getAttribute(CaptchaProcessor.CAPTCHA)).thenReturn("captcha123456"); when(request.getSession(false)).thenReturn(session); final JSONObject requestJSON = new JSONObject(); requestJSON.put("captcha", "captcha123456"); requestJSON.put("oId", addArticle()); requestJSON.put("commentName", "88250"); requestJSON.put("commentEmail", "d@hacpai.com"); requestJSON.put("commentURL", "https://hacpai.com"); requestJSON.put("commentContent", ""); final BufferedReader reader = new BufferedReader(new StringReader(requestJSON.toString())); when(request.getReader()).thenReturn(reader); 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, "\"sc\":true")); }
From source file:controladores.PeliculasController.java
public ModelAndView verMas(HttpServletRequest request, HttpServletResponse response) throws Exception { int pid = Integer.valueOf(request.getParameter("pid")); this.getConnection(request.getServletContext()); Pelicula p = null;//from w w w .ja va 2 s .c o m try { PreparedStatement ps = this.conexion.prepareStatement( "SELECT ids, nombre, observaciones, tipopeli, precio, foto FROM peliculas WHERE ids = ?"); ps.setInt(1, pid); ResultSet rs = ps.executeQuery(); if (rs.next()) { p = new Pelicula(rs.getInt("ids"), rs.getString("nombre"), rs.getString("tipopeli"), rs.getString("observaciones"), rs.getInt("precio"), rs.getString("foto").toUpperCase()); } rs.close(); } catch (SQLException ex) { ex.printStackTrace(); } ModelAndView mv; if (p != null) { mv = new ModelAndView("detalles"); mv.addObject("pelicula", p); } else { mv = this.inicio(request, response); } mv.addObject("metodo", "verMas"); return mv; }
From source file:br.com.ifpb.bdnc.projeto.geo.servlets.CadastraImagem.java
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Image image = mountImage(request); Persister.persist(image);/*from w w w . j ava 2 s. com*/ request.getServletContext().getRequestDispatcher("/carregaimagens").forward(request, response); }
From source file:com.dlshouwen.wzgl.reload.controller.ReloadCacheController.java
/** * ?/*www .j a va 2s . c o m*/ * * @param request */ @RequestMapping(value = "", method = RequestMethod.POST) @ResponseBody public AjaxResponse reload(HttpServletRequest request) { JSONObject jobj = null; AjaxResponse ajaxResp = new AjaxResponse(); String urls = AttributeUtils.getAttributeContent(request.getServletContext(), "wzgl_reload_interface"); boolean reloadFlag = true; String errorUrl = ""; if (StringUtils.isNotEmpty(urls)) { try { String url[] = urls.split(","); for (int i = 0; i < url.length; i++) { HttpRespons resps = requester.sendPost(url[i]); resps.getContent(); if (StringUtils.isNotEmpty(resps.getContent())) { try { jobj = JSONObject.fromObject(resps.getContent()); if (StringUtils.isEmpty(jobj.getString("reloadOK")) || !jobj.getString("reloadOK").equals("true")) { errorUrl += "??" + (i + 1) + "" + url[i] + "<br/>"; errorUrl += "?" + jobj.getString("reloadErrorMsg") + "<br/>"; reloadFlag = false; } } catch (Exception e) { e.printStackTrace(); errorUrl += "??" + (i + 1) + "" + url[i] + "<br/>"; reloadFlag = false; } } else { errorUrl += "??" + (i + 1) + "" + url[i] + "<br/>"; reloadFlag = false; } } if (reloadFlag) { ajaxResp.setSuccess(true); ajaxResp.setSuccessMessage("??"); } else { ajaxResp.setError(true); ajaxResp.setErrorMessage("???<br/>" + errorUrl); } } catch (Exception ex) { ex.printStackTrace(); ajaxResp.setError(true); ajaxResp.setErrorMessage("???"); } } else { ajaxResp.setError(true); ajaxResp.setErrorMessage("???"); } return ajaxResp; }
From source file:controladores.EnfermoController.java
@Override public ModelAndView handleRequest(HttpServletRequest hsr, HttpServletResponse hsr1) throws Exception { String inscripcion = hsr.getParameter("insEnf"); ModelAndView mv = new ModelAndView("index"); this.getConnection(hsr.getServletContext()); mv.addObject("enfermos", this.getEnfermos()); if (inscripcion != null) { int res = this.borrarInscripcion(Integer.valueOf(inscripcion)); if (res != 0) { mv.addObject("inscripcion", inscripcion); }//ww w. j av a2 s. co m } return mv; }
From source file:org.b3log.solo.processor.LoginProcessorTestCase.java
/** * logout.// w w w . ja va 2s . com * * @throws Exception exception */ @Test(dependsOnMethods = "init") public void logout() throws Exception { final HttpServletRequest request = mock(HttpServletRequest.class); when(request.getServletContext()).thenReturn(mock(ServletContext.class)); when(request.getRequestURI()).thenReturn("/logout"); when(request.getAttribute(Keys.TEMAPLTE_DIR_NAME)).thenReturn("next"); when(request.getMethod()).thenReturn("GET"); 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); verify(response).sendRedirect("/"); }
From source file:org.b3log.solo.processor.LoginProcessorTestCase.java
/** * showLogin.//from ww w . j a v a 2 s . c o m * * @throws Exception exception */ @Test(dependsOnMethods = "init") public void showLogin() throws Exception { final HttpServletRequest request = mock(HttpServletRequest.class); when(request.getServletContext()).thenReturn(mock(ServletContext.class)); when(request.getRequestURI()).thenReturn("/login"); when(request.getAttribute(Keys.TEMAPLTE_DIR_NAME)).thenReturn("next"); when(request.getMethod()).thenReturn("GET"); 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:org.b3log.solo.processor.LoginProcessorTestCase.java
/** * showForgot.//from w w w . j a v a2s . c om * * @throws Exception exception */ @Test(dependsOnMethods = "init") public void showForgot() throws Exception { final HttpServletRequest request = mock(HttpServletRequest.class); when(request.getServletContext()).thenReturn(mock(ServletContext.class)); when(request.getRequestURI()).thenReturn("/forgot"); when(request.getAttribute(Keys.TEMAPLTE_DIR_NAME)).thenReturn("next"); when(request.getMethod()).thenReturn("GET"); 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>")); }