Example usage for javax.servlet.http HttpServletRequest getAttribute

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

Introduction

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

Prototype

public Object getAttribute(String name);

Source Link

Document

Returns the value of the named attribute as an Object, or null if no attribute of the given name exists.

Usage

From source file:com.glaf.core.util.RequestUtils.java

public static String getAttribute(HttpServletRequest request, String name) {
    Object value = request.getAttribute(name);
    if (value != null) {
        return value.toString();
    }/*from w ww .  j ava 2  s .  co m*/
    return "";
}

From source file:com.groupon.odo.controllers.PluginController.java

/**
 * Fetches a resource from a plugin jar with the given pluginName
 * Returns 404 if the resource cannot be found
 *//*from w  ww .  j  a va  2  s.  c om*/
@RequestMapping(value = "/api/resource/{pluginName}/**", method = RequestMethod.GET)
public @ResponseBody byte[] getResourceFile(Model model, @PathVariable String pluginName,
        HttpServletRequest request) throws Exception {
    try {
        String url = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
        String fileName = url.replaceFirst("/api/resource/" + pluginName + "/", "");

        return PluginManager.getInstance().getResource(pluginName, fileName);
    } catch (Exception e) {
        throw new ResourceNotFoundException();
    }
}

From source file:com.twosigma.beaker.core.module.elfinder.impl.commands.UploadCommand.java

@Override
@SuppressWarnings("unchecked")
public void execute(FsService fsService, HttpServletRequest request, ServletContext servletContext,
        JSONObject json) throws Exception {
    List<FileItemStream> listFiles = (List<FileItemStream>) request
            .getAttribute(FileItemStream.class.getName());
    List<FsItemEx> added = new ArrayList<FsItemEx>();

    String target = request.getParameter("target");
    FsItemEx dir = super.findItem(fsService, target);

    FsItemFilter filter = FsItemFilterUtils.createFilterFromRequest(request);
    for (FileItemStream fis : listFiles) {
        java.nio.file.Path p = java.nio.file.Paths.get(fis.getName());
        FsItemEx newFile = new FsItemEx(dir, p.getFileName().toString());
        newFile.createFile();//w w  w . j  av a 2 s  .c om
        InputStream is = fis.openStream();
        newFile.writeStream(is);
        if (filter.accepts(newFile))
            added.add(newFile);
    }

    json.put("added", files2JsonArray(request, added));
}

From source file:net.sourceforge.fenixedu.presentationTier.Action.resourceAllocationManager.AddClassesDA.java

public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request,
        HttpServletResponse response) throws Exception {

    InfoShift infoShift = (InfoShift) request.getAttribute(PresentationConstants.SHIFT);

    DynaActionForm addClassesForm = (DynaActionForm) form;
    String[] selectedClasses = (String[]) addClassesForm.get("selectedItems");

    List classOIDs = new ArrayList();
    for (String selectedClasse : selectedClasses) {
        classOIDs.add(selectedClasse);//from   w w w . j  a  v  a 2 s . c om
    }

    try {
        AddSchoolClassesToShift.run(infoShift, classOIDs);
    } catch (FenixServiceException ex) {
        // No probem, the user refreshed the page after adding classes
        request.setAttribute("selectMultipleItemsForm", null);
        return mapping.getInputForward();
    }

    request.setAttribute("selectMultipleItemsForm", null);

    return mapping.findForward("BackToEditShift");
}

From source file:RequestDisplay.java

public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, java.io.IOException {

    ContextObject obj = (ContextObject) request.getAttribute("com.java2s.RequestObject");
    //display the attribute's Map keys
    response.setContentType("text/html");
    java.io.PrintWriter out = response.getWriter();
    out.println("<html><head><title>Request Attribute</title></head><body>");
    out.println("<h2>Request attribute values</h2>");
    if (obj != null)
        out.println(obj.getValues());//from w  w w.  jav  a2 s.  c o  m
    out.println("</body></html>");

}

From source file:ar.com.zauber.commons.spring.web.controllers.ExceptionController.java

/** @see AbstractController#handleRequestInternal(
 * HttpServletRequest, HttpServletResponse) */
@Override//from  ww w  .  j a v  a2 s  .  c  o  m
protected final ModelAndView handleRequestInternal(final HttpServletRequest request,
        final HttpServletResponse response) throws Exception {

    final String view;
    final Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");

    if (map.containsKey(statusCode)) {
        view = map.get(statusCode);
    } else {
        view = this.defaultView;
    }

    return new ModelAndView(view);
}

From source file:com.allanditzel.springframework.security.web.csrf.CsrfTokenResponseHeaderBindingFilter.java

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
        javax.servlet.FilterChain filterChain) throws ServletException, IOException {
    CsrfToken token = (CsrfToken) request.getAttribute(REQUEST_ATTRIBUTE_NAME);

    if (token != null) {
        response.setHeader(RESPONSE_HEADER_NAME, token.getHeaderName());
        response.setHeader(RESPONSE_PARAM_NAME, token.getParameterName());
        response.setHeader(RESPONSE_TOKEN_NAME, token.getToken());
    }/*ww w . j a va  2 s . c o  m*/

    filterChain.doFilter(request, response);
}

From source file:com.test.servlet.AppErrorHandler.java

private void processError(HttpServletRequest request, HttpServletResponse response) throws IOException {
    // Analyze the servlet exception
    Throwable throwable = (Throwable) request.getAttribute("javax.servlet.error.exception");
    Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
    String servletName = (String) request.getAttribute("javax.servlet.error.servlet_name");
    if (servletName == null) {
        servletName = "Unknown";
    }//from  ww  w  .j  a v a2  s. c o m
    String requestUri = (String) request.getAttribute("javax.servlet.error.request_uri");
    if (requestUri == null) {
        requestUri = "Unknown";
    }

    // Set response content type
    response.setContentType("application/json");

    PrintWriter out = response.getWriter();
    JSONObject object = new JSONObject();
    object.put("success", -1);
    JSONObject error = new JSONObject();
    error.put("status_code", statusCode);
    error.put("requested_uri", requestUri);
    error.put("servlet_name", servletName);
    error.put("exception_name", throwable.getClass().getName());
    error.put("exception_message", throwable.getMessage());
    object.put("error", error);
    out.write(object.toString());
}

From source file:com.persistent.cloudninja.controller.TenantDashBoardController.java

/**
 * Access Denied Page/*  w  ww  .j a  v a2 s .com*/
 * @param cookie
 * @return ModelAndView mapped to access denied
 */
@RequestMapping("{tenantId}/showAccessDenied.htm")
public ModelAndView showAccessDenied(HttpServletRequest request, @CookieValue("CLOUDNINJAAUTH") String cookie,
        @PathVariable("tenantId") String tenantId) {
    if (cookie == null) {
        cookie = request.getAttribute("cookieNameAttr").toString();
    }

    String userName = AuthFilterUtils.getFieldValueFromCookieString(CloudNinjaConstants.COOKIE_USERNAME_PREFIX,
            cookie);
    ModelAndView model = new ModelAndView("accessDenied", "userName", userName);
    return model;
}

From source file:net.groupbuy.interceptor.ExecuteTimeInterceptor.java

@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
        ModelAndView modelAndView) throws Exception {
    Long executeTime = (Long) request.getAttribute(EXECUTE_TIME_ATTRIBUTE_NAME);
    if (executeTime == null) {
        Long startTime = (Long) request.getAttribute(START_TIME_ATTRIBUTE_NAME);
        Long endTime = System.currentTimeMillis();
        executeTime = endTime - startTime;
        request.setAttribute(START_TIME_ATTRIBUTE_NAME, startTime);
    }//from  w  ww  .ja v a  2 s .c  om

    if (modelAndView != null) {
        String viewName = modelAndView.getViewName();
        if (!StringUtils.startsWith(viewName, REDIRECT_VIEW_NAME_PREFIX)) {
            modelAndView.addObject(EXECUTE_TIME_ATTRIBUTE_NAME, executeTime);
        }
    }
    if (logger.isDebugEnabled()) {
        logger.debug("[" + handler + "] executeTime: " + executeTime + "ms");
    }
}