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:br.eb.ime.pfc.domain.GeoServerCommunication.java

private static void redirectStream(String urlName, HttpServletRequest request, HttpServletResponse response) {
    URL url = null;//from  w  w  w .  j  a  v a2  s  .c o  m
    try {
        url = new URL(urlName);
    } catch (MalformedURLException e) {
        //Internal error, the user will receive no data.
        sendError(HTTP_STATUS.BAD_REQUEST, response);
        return;
    }
    HttpURLConnection conn = null;
    try {
        conn = (HttpURLConnection) url.openConnection();
        conn.addRequestProperty("Authorization", "Basic " + BASE64_AUTHORIZATION);
        //conn.setRequestMethod("GET");
        //conn.setDoOutput(true);
        conn.connect();
    } catch (IOException e) {
        sendError(HTTP_STATUS.INTERNAL_ERROR, response);
        return;
    }

    try (InputStream is = conn.getInputStream(); OutputStream os = response.getOutputStream()) {
        response.setContentType(conn.getContentType());
        IOUtils.copy(is, os);
    } catch (IOException e) {
        request.getServletContext().log("IO");
        sendError(HTTP_STATUS.INTERNAL_ERROR, response);
        return;
    } finally { //Close connection to save resources
        conn.disconnect();
    }
}

From source file:ge.taxistgela.servlet.AdminServlet.java

public void banUser(HttpServletRequest request, HttpServletResponse response) {
    UserManagerAPI userManager = (UserManagerAPI) request.getServletContext()
            .getAttribute(UserManagerAPI.class.getName());

    String userID = request.getParameter("userID");
    String password = RandomStringUtils.randomAlphanumeric(20);

    toogleBan(userManager, userID, password, request, response);
}

From source file:ge.taxistgela.servlet.AdminServlet.java

public void unbanUser(HttpServletRequest request, HttpServletResponse response) {
    UserManagerAPI userManager = (UserManagerAPI) request.getServletContext()
            .getAttribute(UserManagerAPI.class.getName());

    String userID = request.getParameter("userID");
    String password = request.getParameter("password");

    toogleBan(userManager, userID, password, request, response);
}

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

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

    SyncopeClient anonymous = (SyncopeClient) request.getServletContext()
            .getAttribute(Constants.SYNCOPE_ANONYMOUS_CLIENT);
    try {//from  w  ww .  ja  v a 2  s.c  o  m
        SAML2LoginResponseTO responseTO = anonymous.getService(SAML2SPService.class).validateLoginResponse(
                extract(StringUtils.substringBefore(request.getRequestURL().toString(), "/saml2sp"), "saml2sp",
                        request.getRemoteAddr(), request.getInputStream()));

        request.getSession(true).setAttribute(Constants.SAML2SPJWT, responseTO.getAccessToken());
        request.getSession(true).setAttribute(Constants.SAML2SPJWT_EXPIRE,
                responseTO.getAccessTokenExpiryTime());

        String successURL = getServletContext().getInitParameter(Constants.CONTEXT_PARAM_LOGIN_SUCCESS_URL);
        if (successURL == null) {
            request.setAttribute("responseTO", responseTO);
            request.getRequestDispatcher("loginSuccess.jsp").forward(request, response);
        } else {
            response.sendRedirect(successURL + "?sloSupported=" + responseTO.isSloSupported());
        }
    } catch (Exception e) {
        LOG.error("While processing authentication response from IdP", e);

        String errorURL = getServletContext().getInitParameter(Constants.CONTEXT_PARAM_LOGIN_ERROR_URL);
        if (errorURL == null) {
            request.setAttribute("exception", e);
            request.getRequestDispatcher("loginError.jsp").forward(request, response);

            e.printStackTrace(response.getWriter());
        } else {
            response.sendRedirect(errorURL + "?errorMessage="
                    + URLEncoder.encode(e.getMessage(), StandardCharsets.UTF_8.name()));
        }
    }
}

From source file:controller.UploadFileController.java

@RequestMapping(method = RequestMethod.POST)
public @ResponseBody String upload(@RequestParam(value = "file") MultipartFile mfile, HttpServletResponse res,
        HttpServletRequest req) {
    long time = Calendar.getInstance().getTimeInMillis();
    String fileName = "";
    try {//from   w w  w. j av  a  2s . c o m
        fileName = time + mfile.getOriginalFilename();
        File f = new File(req.getServletContext().getRealPath("/images") + "/" + fileName);
        if (!f.exists()) {
            f.createNewFile();
        }
        FileOutputStream out = new FileOutputStream(f);
        FileCopyUtils.copy(mfile.getBytes(), out);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return "<img src='" + req.getContextPath() + "/images/" + fileName + "'/>";

}

From source file:ge.taxistgela.servlet.AdminServlet.java

public void banDriver(HttpServletRequest request, HttpServletResponse response) {
    DriverManagerAPI driverManager = (DriverManagerAPI) request.getServletContext()
            .getAttribute(DriverManagerAPI.class.getName());

    String driverID = request.getParameter("driverID");
    String password = RandomStringUtils.randomAlphanumeric(20);

    toogleBan(driverManager, driverID, password, request, response);
}

From source file:ge.taxistgela.servlet.AdminServlet.java

public void unbanDriver(HttpServletRequest request, HttpServletResponse response) {
    DriverManagerAPI driverManager = (DriverManagerAPI) request.getServletContext()
            .getAttribute(DriverManagerAPI.class.getName());

    String driverID = request.getParameter("driverID");
    String password = RandomStringUtils.randomAlphanumeric(20);

    toogleBan(driverManager, driverID, password, request, response);
}

From source file:ge.taxistgela.servlet.AdminServlet.java

public void banCompany(HttpServletRequest request, HttpServletResponse response) {
    CompanyManagerAPI orderManager = (CompanyManagerAPI) request.getServletContext()
            .getAttribute(CompanyManagerAPI.class.getName());

    String companyID = request.getParameter("companyID");
    String password = RandomStringUtils.randomAlphanumeric(20);

    toogleBan(orderManager, companyID, password, request, response);
}

From source file:ge.taxistgela.servlet.AdminServlet.java

public void unbanCompany(HttpServletRequest request, HttpServletResponse response) {
    CompanyManagerAPI orderManager = (CompanyManagerAPI) request.getServletContext()
            .getAttribute(CompanyManagerAPI.class.getName());

    String companyID = request.getParameter("companyID");
    String password = request.getParameter("password");

    toogleBan(orderManager, companyID, password, request, response);
}

From source file:com.mingsoft.weixin.util.BaseUtils.java

/**
 * ???// ww  w  . j  ava  2 s.  c o  m
 * @param request 
 * @param filePath ?
 * @return
 */
public String getRealPath(HttpServletRequest request, String filePath) {
    return request.getServletContext().getRealPath(File.separator) + filePath;
}