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:com.crucialticketing.controllers.FileController.java

@RequestMapping(value = "/{filename}/", method = RequestMethod.GET)
@ResponseBody/*from   w w  w . ja v a  2s . co m*/
public void fileDispatcher(@PathVariable(value = "filename") String fileUploadId, HttpServletRequest request,
        HttpServletResponse response, ModelMap map) throws Exception {

    String fileName = request.getServletContext().getRealPath("/WEB-INF/upload/");

    Attachment attachment = attachmentService.getAttachmentById(Integer.valueOf(fileUploadId));

    if (attachment.getFileUploadId() == 0) {
        throw new Exception();
    }

    fileName += "\\" + attachment.getTicket().getTicketId() + "\\" + attachment.getFileName();

    try {
        InputStream in = new BufferedInputStream(new FileInputStream(new File(fileName)));

        String extension = "unknown";

        int i = fileName.lastIndexOf('.');

        if (i > 0) {
            extension = fileName.substring(i + 1);
        }

        response.setContentType("application/" + extension);
        response.setHeader("Content-Disposition", "attachment; filename=" + attachment.getFileName());

        ServletOutputStream out = response.getOutputStream();
        IOUtils.copy(in, out);
        response.flushBuffer();
    } catch (Exception e) {
    }
}

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

/**
 * showPage./* www. j  av  a2 s . c  o m*/
 *
 * @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("next");
    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)));
}

From source file:controladores.PeliculasController.java

public ModelAndView inicio(HttpServletRequest request, HttpServletResponse response) throws Exception {
    this.getConnection(request.getServletContext());
    ArrayList al = new ArrayList();
    try {/* w  w  w  . ja  va 2s .  c  o  m*/
        Statement stmt = this.conexion.createStatement();
        ResultSet rs = stmt
                .executeQuery("SELECT ids, nombre, observaciones, tipopeli, precio, foto FROM peliculas");
        while (rs.next()) {
            Pelicula p = new Pelicula(rs.getInt("ids"), rs.getString("nombre"), rs.getString("tipopeli"),
                    rs.getString("observaciones"), rs.getInt("precio"), rs.getString("foto").toUpperCase());
            al.add(p);
        }
        rs.close();
    } catch (SQLException ex) {
        ex.printStackTrace();
    }
    ModelAndView mv = new ModelAndView("index");
    mv.addObject("peliculas", al);
    mv.addObject("metodo", "inicio");
    return mv;
}

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

/**
 * getBlogInfo./*from   www  .  j av  a 2  s  .c o  m*/
 *
 * @throws Exception exception
 */
@Test(dependsOnMethods = "init")
public void getBlogInfo() throws Exception {
    final HttpServletRequest request = mock(HttpServletRequest.class);
    when(request.getServletContext()).thenReturn(mock(ServletContext.class));
    when(request.getRequestURI()).thenReturn("/blog/info");
    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, "{\"staticServePath\":\"http://localhost:8080\""));
}

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

/**
 * getInterestTags.//from   w  w w .j a  va2s .c o  m
 *
 * @throws Exception exception
 */
@Test(dependsOnMethods = "init")
public void getInterestTags() throws Exception {
    final HttpServletRequest request = mock(HttpServletRequest.class);
    when(request.getServletContext()).thenReturn(mock(ServletContext.class));
    when(request.getRequestURI()).thenReturn("/blog/interest-tags");
    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, "{\"data\":"));
}

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

/**
 * getArticlesTags.//from   w  ww  .  j  a v a  2 s .  co m
 *
 * @throws Exception exception
 */
@Test(dependsOnMethods = "init")
public void getArticlesTags() throws Exception {
    final HttpServletRequest request = mock(HttpServletRequest.class);
    when(request.getServletContext()).thenReturn(mock(ServletContext.class));
    when(request.getRequestURI()).thenReturn("/blog/articles-tags");
    when(request.getParameter("pwd")).thenReturn("pass");
    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, "{\"data\":"));
}

From source file:org.jetbrains.teamcity.widgets.WidgetContentController.java

@Override
protected void writeResourceContent(@NotNull HttpServletRequest request, @NotNull HttpServletResponse response,
        @NotNull File file) throws IOException {
    InputStream stream = request.getServletContext().getResourceAsStream(file.getAbsolutePath());
    if (stream == null) {
        throw new IllegalArgumentException(
                String.format("Requested resource %s doesn't exist.", file.getAbsolutePath()));
    }//w  w w.j av  a  2  s .  com
    FileCopyUtils.copy(new InputStreamReader(stream), response.getWriter());
}

From source file:org.apache.syncope.ext.saml2lsp.agent.Metadata.java

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

    SyncopeClient anonymous = (SyncopeClient) request.getServletContext()
            .getAttribute(Constants.SYNCOPE_ANONYMOUS_CLIENT);
    SAML2SPService service = anonymous.getService(SAML2SPService.class);
    WebClient.client(service).accept(MediaType.APPLICATION_XML_TYPE).type(MediaType.APPLICATION_XML_TYPE);
    try {/*from ww w  .  ja  v  a2 s  .co  m*/
        Response metadataResponse = service.getMetadata(
                StringUtils.substringBefore(request.getRequestURL().toString(), "/saml2sp"), "saml2sp");

        response.setContentType(metadataResponse.getMediaType().toString());
        IOUtils.copy((InputStream) metadataResponse.getEntity(), response.getOutputStream());
        ((InputStream) metadataResponse.getEntity()).close();
    } catch (Exception e) {
        throw new ServletException(e.getMessage());
    }
}

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

/**
 * showIndex.//from  www . j  a va 2s  .  co  m
 *
 * @throws Exception exception
 */
@Test(dependsOnMethods = "init")
public void showIndex() throws Exception {
    final HttpServletRequest request = mock(HttpServletRequest.class);
    when(request.getServletContext()).thenReturn(mock(ServletContext.class));
    when(request.getRequestURI()).thenReturn("");
    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:org.b3log.solo.processor.IndexProcessorTestCase.java

/**
 * showKillBrowser.// ww  w  .  jav  a  2 s.com
 *
 * @throws Exception exception
 */
@Test(dependsOnMethods = "init")
public void showKillBrowser() throws Exception {
    final HttpServletRequest request = mock(HttpServletRequest.class);
    when(request.getServletContext()).thenReturn(mock(ServletContext.class));
    when(request.getRequestURI()).thenReturn("/kill-browser");
    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>"));
}