Example usage for javax.servlet ServletRequest getAttribute

List of usage examples for javax.servlet ServletRequest getAttribute

Introduction

In this page you can find the example usage for javax.servlet ServletRequest 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:org.alfresco.web.app.portlet.AlfrescoFacesPortlet.java

/**
 * Creates a render URL from the given request and parameters
 * /*from www .j a  va 2  s.  co m*/
 * @param request
 *           the request
 * @param parameters
 *           the parameters
 * @return the render url
 */
public static String getRenderURL(ServletRequest request, Map<String, String[]> parameters) {
    RenderResponse renderResp = (RenderResponse) request.getAttribute("javax.portlet.response");
    if (renderResp == null) {
        throw new IllegalStateException("RenderResponse object is null");
    }

    PortletURL url = renderResp.createRenderURL();
    url.setParameters(parameters);
    return url.toString();
}

From source file:org.opencms.flex.CmsFlexController.java

/**
 * Removes the controller attribute from a request.<p>
 * /*from  w  ww .j  a  v a 2 s.c  o m*/
 * @param req the request to remove the controller from
 */
public static void removeController(ServletRequest req) {

    CmsFlexController controller = (CmsFlexController) req.getAttribute(ATTRIBUTE_NAME);
    if (controller != null) {
        controller.clear();
    }
}

From source file:org.opencms.flex.CmsFlexController.java

/**
 * Returns the wrapped CmsObject form the provided request, or <code>null</code> if the 
 * request is not running inside OpenCms.<p>
 * //  ww  w  .j  a v a 2 s . c  o m
 * @param req the current request
 * @return the wrapped CmsObject
 */
public static CmsObject getCmsObject(ServletRequest req) {

    CmsFlexController controller = (CmsFlexController) req.getAttribute(ATTRIBUTE_NAME);
    if (controller != null) {
        return controller.getCmsObject();
    } else {
        return null;
    }
}

From source file:org.opencms.flex.CmsFlexController.java

/**
 * Provides access to a root cause Exception that might have occurred in a complex include scenario.<p>
 * //  www.  j  a va 2 s  .  c o  m
 * @param req the current request
 * 
 * @return the root cause exception or null if no root cause exception is available
 * 
 * @see #getThrowable()
 */
public static Throwable getThrowable(ServletRequest req) {

    CmsFlexController controller = (CmsFlexController) req.getAttribute(ATTRIBUTE_NAME);
    if (controller != null) {
        return controller.getThrowable();
    } else {
        return null;
    }
}

From source file:org.opencms.flex.CmsFlexController.java

/**
 * Provides access to URI of a VFS resource that caused an exception that might have occurred in a complex include scenario.<p>
 * //w ww  .  ja  v  a  2 s. com
 * @param req the current request
 * 
 * @return to URI of a VFS resource that caused an exception, or <code>null</code>
 * 
 * @see #getThrowableResourceUri()
 */
public static String getThrowableResourceUri(ServletRequest req) {

    CmsFlexController controller = (CmsFlexController) req.getAttribute(ATTRIBUTE_NAME);
    if (controller != null) {
        return controller.getThrowableResourceUri();
    } else {
        return null;
    }
}

From source file:org.jruby.rack.mock.WebUtils.java

/**
 * Expose the specified request attribute if not already present.
 * @param request current servlet request
 * @param name the name of the attribute
 * @param value the suggested value of the attribute
 *//*from   ww w .  ja v  a 2  s .c  o m*/
private static void exposeRequestAttributeIfNotPresent(ServletRequest request, String name, Object value) {
    if (request.getAttribute(name) == null) {
        request.setAttribute(name, value);
    }
}

From source file:org.displaytag.filter.ExportDelegate.java

/**
 * Actually writes exported data. Extracts content from the Map stored in request with the
 * <code>TableTag.FILTER_CONTENT_OVERRIDE_BODY</code> key.
 * /*w  w w . j ava  2 s  . co m*/
 * @param wrapper BufferedResponseWrapper implementation
 * @param response HttpServletResponse
 * @param request ServletRequest
 * @throws IOException exception thrown by response writer/outputStream
 */
protected static void writeExport(HttpServletResponse response, ServletRequest request,
        BufferedResponseWrapper wrapper) throws IOException {

    if (wrapper.isOutRequested()) {
        // data already written
        log.debug("Filter operating in unbuffered mode. Everything done, exiting");
        return;
    }

    // if you reach this point the PARAMETER_EXPORTING has been found, but the special header has never been set in
    // response (this is the signal from table tag that it is going to write exported data)
    log.debug("Filter operating in buffered mode. ");

    Map bean = (Map) request.getAttribute(TableTag.FILTER_CONTENT_OVERRIDE_BODY);

    if (log.isDebugEnabled()) {
        log.debug(bean);
    }

    Object pageContent = bean.get(TableTagParameters.BEAN_BODY);

    if (pageContent == null) {
        if (log.isDebugEnabled()) {
            log.debug("Filter is enabled but exported content has not been found. Maybe an error occurred?");
        }

        PrintWriter out = response.getWriter();
        out.write(wrapper.getContentAsString());
        out.flush();
        return;
    }

    // clear headers
    if (!response.isCommitted()) {
        response.reset();
    }

    String filename = (String) bean.get(TableTagParameters.BEAN_FILENAME);
    String contentType = "application/download";

    String characterEncoding = wrapper.getCharacterEncoding();
    String wrappedContentType = wrapper.getContentType();

    if (wrappedContentType != null && wrappedContentType.indexOf("charset") > -1) {
        // charset is already specified (see #921811)
        characterEncoding = StringUtils.substringAfter(wrappedContentType, "charset=");
    }

    if (characterEncoding != null && contentType.indexOf("charset") == -1) //$NON-NLS-1$
    {
        contentType += "; charset=" + characterEncoding; //$NON-NLS-1$
    }

    ExibicaoDocumento exibicao = new ExibicaoDocumento();
    exibicao.setTipoConteudo(contentType);
    exibicao.setNomeArquivo(filename);
    exibicao.setAsAttachment(true);
    if (((String) bean.get(TableTagParameters.BEAN_CONTENTTYPE)).indexOf("excel") > -1) {
        exibicao.setExtensaoArquivo(".xls");
    } else if (((String) bean.get(TableTagParameters.BEAN_CONTENTTYPE)).indexOf("csv") > -1) {
        exibicao.setExtensaoArquivo(".csv");
    } else if (((String) bean.get(TableTagParameters.BEAN_CONTENTTYPE)).indexOf("xml") > -1) {
        exibicao.setExtensaoArquivo(".xml");
    } else if (((String) bean.get(TableTagParameters.BEAN_CONTENTTYPE)).indexOf("pdf") > -1) {
        exibicao.setExtensaoArquivo(".pdf");
    }

    byte[] content;
    if (pageContent instanceof String) {
        content = ((String) pageContent).getBytes(characterEncoding);
    } else {
        content = (byte[]) pageContent;
    }

    try {
        exibicao.gerarExibicaoDocumento(response, content);
    } catch (CDException cde) {
        // TODO Tratar erro....
    }
}

From source file:com.delmar.core.web.filter.ExportDelegate.java

/**
 * Actually writes exported data. Extracts content from the Map stored in request with the
 * <code>TableTag.FILTER_CONTENT_OVERRIDE_BODY</code> key.
 * @param wrapper BufferedResponseWrapper implementation
 * @param response HttpServletResponse//from  w  w w  . jav  a 2  s .  co  m
 * @param request ServletRequest
 * @throws java.io.IOException exception thrown by response writer/outputStream
 */
public static void writeExport(HttpServletResponse response, ServletRequest request,
        BufferedResponseWrapper wrapper) throws IOException {

    if (wrapper.isOutRequested()) {
        // data already written
        log.debug("Filter operating in unbuffered mode. Everything done, exiting");
        return;
    }

    // if you reach this point the PARAMETER_EXPORTING has been found, but the special header has never been set in
    // response (this is the signal from table tag that it is going to write exported data)
    log.debug("Filter operating in buffered mode. ");

    Map bean = (Map) request.getAttribute(TableTag.FILTER_CONTENT_OVERRIDE_BODY);

    if (log.isDebugEnabled()) {
        log.debug(bean);
    }

    Object pageContent = bean.get(TableTagParameters.BEAN_BODY);

    if (pageContent == null) {
        if (log.isDebugEnabled()) {
            log.debug("Filter is enabled but exported content has not been found. Maybe an error occurred?");
        }

        response.setContentType(wrapper.getContentType());
        PrintWriter out = response.getWriter();

        out.write(wrapper.getContentAsString());
        out.flush();
        return;
    }

    // clear headers
    if (!response.isCommitted()) {
        response.reset();
    }

    String filename = (String) bean.get(TableTagParameters.BEAN_FILENAME);
    String contentType = (String) bean.get(TableTagParameters.BEAN_CONTENTTYPE);

    if (StringUtils.isNotBlank(filename)) {
        response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
    }

    String characterEncoding = wrapper.getCharacterEncoding();
    String wrappedContentType = wrapper.getContentType();

    if (wrappedContentType != null && wrappedContentType.indexOf("charset") > -1) {
        // charset is already specified (see #921811)
        characterEncoding = StringUtils.substringAfter(wrappedContentType, "charset=");
    }

    if (characterEncoding != null && contentType.indexOf("charset") == -1) //$NON-NLS-1$
    {
        contentType += "; charset=" + characterEncoding; //$NON-NLS-1$
    }

    response.setContentType(contentType);

    if (pageContent instanceof String) {
        // text content
        if (characterEncoding != null) {
            response.setContentLength(((String) pageContent).getBytes(characterEncoding).length);
        } else {
            //FIXME  Reliance on default encoding
            // Found a call to a method which will perform a byte to String (or String to byte) conversion,
            // and will assume that the default platform encoding is suitable.
            // This will cause the application behaviour to vary between platforms.
            // Use an alternative API and specify a charset name or Charset object explicitly.
            response.setContentLength(((String) pageContent).getBytes().length);
        }

        PrintWriter out = response.getWriter();
        out.write((String) pageContent);
        out.flush();
    } else {
        // dealing with binary content
        byte[] content = (byte[]) pageContent;
        response.setContentLength(content.length);
        OutputStream out = response.getOutputStream();
        out.write(content);
        out.flush();
    }
}

From source file:org.yes.cart.web.support.util.HttpUtil.java

private static void dumpParamsAndAttrs(final ServletRequest req, final StringBuilder stringBuilder) {
    final Enumeration parameterNames = req.getParameterNames();
    while (parameterNames.hasMoreElements()) {
        final String key = (String) parameterNames.nextElement();
        stringBuilder.append(MessageFormat.format("\nParameter {0}={1}", key, req.getParameter(key)));
    }//from w  ww. j  a va2 s. c o m
    Enumeration attributeNames = req.getAttributeNames();
    while (attributeNames.hasMoreElements()) {
        final String key = (String) attributeNames.nextElement();
        stringBuilder.append(MessageFormat.format("\nAttribute {0}={1}", key, req.getAttribute(key)));
    }
}

From source file:com.concursive.connect.web.utils.PermissionUtils.java

public static boolean hasPermissionToAction(ServletRequest request, HttpSession session, String permission,
        String includeIf, String objectName) {
    // Use the object name specified, or use the default here
    String thisObjectName = objectName;
    if (thisObjectName == null) {
        thisObjectName = "project";
    }//from   w w w  .  ja  v  a  2 s . c  o  m

    try {
        // Find the project to check for permissions...
        Project thisProject = null;
        // Check the current portlet first
        PortletRequest renderRequest = (PortletRequest) request
                .getAttribute(org.apache.pluto.tags.Constants.PORTLET_REQUEST);
        if (renderRequest != null) {
            // Get the requested object
            Object object = null;
            int dotPos = thisObjectName.indexOf(".");
            if (dotPos > -1) {
                // Get the base object from the request
                String currentObject = thisObjectName.substring(0, dotPos);
                object = renderRequest.getAttribute(currentObject);
                // Get the parsed object
                currentObject = thisObjectName.substring(dotPos + 1);
                object = ObjectUtils.getObject(object, currentObject);
                thisProject = (Project) object;
            } else {
                thisProject = (Project) renderRequest.getAttribute(thisObjectName);
            }
            if (thisProject == null && objectName == null) {
                thisProject = PortalUtils.getProject(renderRequest);
            }
        }
        // Check the request object
        if (thisProject == null) {
            // Get the requested object
            Object object = null;
            int dotPos = thisObjectName.indexOf(".");
            if (dotPos > -1) {
                // Get the base object from the request
                String currentObject = thisObjectName.substring(0, dotPos);
                object = request.getAttribute(currentObject);
                // Get the parsed object
                currentObject = thisObjectName.substring(dotPos + 1);
                object = ObjectUtils.getObject(object, currentObject);
                thisProject = (Project) object;
            } else {
                thisProject = (Project) request.getAttribute(thisObjectName);
            }
        }
        // Deny if not found
        if (thisProject == null) {
            LOG.debug("Project is null");
            return false;
        }

        // Check this user's permissions
        User thisUser = null;
        // Check the portlet
        if (thisUser == null && renderRequest != null) {
            thisUser = PortalUtils.getUser(renderRequest);
        }
        // Check the session object
        if (thisUser == null) {
            thisUser = (User) session.getAttribute(Constants.SESSION_USER);
        }
        // Deny if not found
        if (thisUser == null) {
            return false;
        }

        // Multiple permissions to check
        boolean doCheck = true;
        String thisPermission = null;
        StringTokenizer st = new StringTokenizer(permission, ",");
        while (st.hasMoreTokens() || doCheck) {
            doCheck = false;
            if (st.hasMoreTokens()) {
                thisPermission = st.nextToken();
            } else {
                thisPermission = permission;
            }
            if (NONE.equals(includeIf)) {
                if (ProjectUtils.hasAccess(thisProject.getId(), thisUser, thisPermission)) {
                    return false;
                }
            } else if (ANY.equals(includeIf)) {
                if (ProjectUtils.hasAccess(thisProject.getId(), thisUser, thisPermission)) {
                    return true;
                }
            } else {
                if (!ProjectUtils.hasAccess(thisProject.getId(), thisUser, thisPermission)) {
                    return false;
                }
            }
        }
        // If the above didn't trigger, then go with the default
        if (NONE.equals(includeIf)) {
            return true;
        } else if (ANY.equals(includeIf)) {
            return false;
        } else {
            return true;
        }
    } catch (Exception e) {
        LOG.error("hasPermissionToAction", e);
        return false;
    }
}