Example usage for org.springframework.web.servlet.tags RequestContextAwareTag REQUEST_CONTEXT_PAGE_ATTRIBUTE

List of usage examples for org.springframework.web.servlet.tags RequestContextAwareTag REQUEST_CONTEXT_PAGE_ATTRIBUTE

Introduction

In this page you can find the example usage for org.springframework.web.servlet.tags RequestContextAwareTag REQUEST_CONTEXT_PAGE_ATTRIBUTE.

Prototype

String REQUEST_CONTEXT_PAGE_ATTRIBUTE

To view the source code for org.springframework.web.servlet.tags RequestContextAwareTag REQUEST_CONTEXT_PAGE_ATTRIBUTE.

Click Source Link

Document

javax.servlet.jsp.PageContext attribute for the page-level RequestContext instance.

Usage

From source file:org.parancoe.web.test.junit4.AbstractJspTest.java

/**
 * Reset all contexts.//from www. j a va  2s  . com
 */
protected void resetContexts() {
    GenericWebApplicationContext context = (GenericWebApplicationContext) applicationContext;
    ServletContext servletContext = context.getServletContext();
    pageContext = new MockPageContext(servletContext, request, response);
    requestContext = new JspAwareRequestContext(pageContext);
    pageContext.setAttribute(RequestContextAwareTag.REQUEST_CONTEXT_PAGE_ATTRIBUTE, requestContext);
}

From source file:org.openmrs.web.taglib.FormatDateTag.java

public int doStartTag() {
    RequestContext requestContext = (RequestContext) this.pageContext
            .getAttribute(RequestContextAwareTag.REQUEST_CONTEXT_PAGE_ATTRIBUTE);

    if (date == null && getPath() != null) {
        try {/*from   w w  w . j  a  v a2  s .c om*/
            // get the "path" object from the pageContext
            String resolvedPath = getPath();
            String nestedPath = (String) pageContext.getAttribute(NestedPathTag.NESTED_PATH_VARIABLE_NAME,
                    PageContext.REQUEST_SCOPE);
            if (nestedPath != null) {
                resolvedPath = nestedPath + resolvedPath;
            }

            BindStatus status = new BindStatus(requestContext, resolvedPath, false);
            log.debug("status: " + status);

            if (status.getValue() != null) {
                log.debug("status.value: " + status.getValue());
                if (status.getValue().getClass() == Date.class) {
                    // if no editor was registered all will go well here
                    date = (Date) status.getValue();
                } else {
                    // if a "Date" property editor was registerd for the form, the status.getValue()
                    // object will be a java.lang.String.  This is useless.  Try getting the original
                    // value from the troublesome editor
                    log.debug("status.valueType: " + status.getValueType());
                    Timestamp timestamp = (Timestamp) status.getEditor().getValue();
                    date = new Date(timestamp.getTime());
                }
            }
        } catch (Exception e) {
            log.warn("Unable to get a date object from path: " + getPath(), e);
            return SKIP_BODY;
        }
    }

    if (!dateWasSet && date == null) {
        log.warn("Both 'date' and 'path' cannot be null.  Page: " + pageContext.getPage() + " localname:"
                + pageContext.getRequest().getLocalName() + " rd:"
                + pageContext.getRequest().getRequestDispatcher(""));
        return SKIP_BODY;
    }

    if (type == null) {
        type = "";
    }

    DateFormat dateFormat = null;

    if (format != null && format.length() > 0) {
        dateFormat = new SimpleDateFormat(format, Context.getLocale());
    } else if (type.equals("xml")) {
        dateFormat = new SimpleDateFormat("dd-MMM-yyyy", Context.getLocale());
    } else {
        log.debug("context locale: " + Context.getLocale());

        if (type.equals("long")) {
            dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, Context.getLocale());
        } else if (type.equals("medium")) {
            dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, Context.getLocale());
        } else {
            dateFormat = Context.getDateFormat();
        }
    }

    if (dateFormat == null) {
        dateFormat = new SimpleDateFormat("MM-dd-yyyy");
    }

    String datestr = "";

    try {
        if (date != null) {
            if (type.equals("milliseconds")) {
                datestr = "" + date.getTime();
            } else {
                if (showTodayOrYesterday && (DateUtils.isSameDay(Calendar.getInstance().getTime(), date)
                        || OpenmrsUtil.isYesterday(date))) {
                    //print only time of day but maintaining the format(24 Vs 12) if any was specified
                    String timeFormatString = (format != null && !format.contains("a")) ? "HH:mm" : "h:mm a";
                    dateFormat = new SimpleDateFormat(timeFormatString);
                    if (DateUtils.isSameDay(Calendar.getInstance().getTime(), date)) {
                        datestr = Context.getMessageSourceService().getMessage("general.today") + " "
                                + dateFormat.format(date);
                    } else {
                        datestr = Context.getMessageSourceService().getMessage("general.yesterday") + " "
                                + dateFormat.format(date);
                    }
                } else {
                    datestr = dateFormat.format(date);
                }
            }
        }
    } catch (IllegalArgumentException e) {
        //format or date is invalid
        log.error("date: " + date);
        log.error("format: " + format);
        log.error(e);
        datestr = date.toString();
    }

    try {
        pageContext.getOut().write(datestr);
    } catch (IOException e) {
        log.error(e);
    }

    // reset the objects to null because taglibs are reused
    release();

    return SKIP_BODY;
}

From source file:test.pl.chilldev.facelets.taglib.spring.web.form.ErrorsTagTest.java

@Test
public void apply() throws IOException, FacesException {
    String path = "foo.bar";
    String var = "error";

    Map<String, Object> config = new HashMap<>();
    config.put(ErrorsTag.ATTRIBUTE_PATH, path);
    config.put(ErrorsTag.ATTRIBUTE_VAR, var);

    ErrorsTag tag = new ErrorsTag(MockTagConfig.factory(config, this.nextHandler));

    // set up context
    FaceletContext context = new MockFaceletContext();
    context.setAttribute(RequestContextAwareTag.REQUEST_CONTEXT_PAGE_ATTRIBUTE, this.requestContext);

    List<FieldError> objectErrors = new ArrayList<>();
    objectErrors.add(new FieldError("foo", "bar", "Test 1"));
    objectErrors.add(new FieldError("foo", "bar", "Test 2"));

    when(this.requestContext.getErrors("foo", false)).thenReturn(this.errors);
    when(this.errors.getFieldErrors("bar")).thenReturn(objectErrors);

    // run the tag
    tag.apply(context, this.parent);

    verify(this.nextHandler, times(2)).apply(context, this.parent);
}

From source file:org.gvnix.datatables.tags.SpringContextHelper.java

/**
 * Returns the current request Spring {@link RequestContext} object. If a
 * {@link RequestContext} is not already available, a new one is created and
 * included in the {@link PageContext}/*from   ww  w  .ja v  a2 s .c om*/
 * 
 * @param pageContext the current page context
 * @return the {@link RequestContext} related to this request.
 */
public RequestContext getRequestContext(PageContext pageContext) {
    RequestContext requestContext = (RequestContext) pageContext
            .getAttribute(RequestContextAwareTag.REQUEST_CONTEXT_PAGE_ATTRIBUTE);
    if (requestContext == null) {
        requestContext = new JspAwareRequestContext(pageContext);
        pageContext.setAttribute(RequestContextAwareTag.REQUEST_CONTEXT_PAGE_ATTRIBUTE, requestContext);
    }
    return requestContext;
}

From source file:org.openmrs.web.taglib.OpenmrsRequestContextAwareBodyTag.java

/**
 * After tag body has been evaluated and buffered this creates and exposes the current RequestContext. Delegates to
 * {@link #doEndTagInternal()} for actual work.
 *//*  ww  w . j  a  va  2  s  .co  m*/
@Override
public final int doEndTag() throws JspException {
    try {
        // get request content available from pageContext
        this.requestContext = (RequestContext) this.pageContext
                .getAttribute(RequestContextAwareTag.REQUEST_CONTEXT_PAGE_ATTRIBUTE);
        // if request context is not specified, create empty request context and set it into pageContext
        if (this.requestContext == null) {
            this.requestContext = new JspAwareRequestContext(this.pageContext);
            this.pageContext.setAttribute(RequestContextAwareTag.REQUEST_CONTEXT_PAGE_ATTRIBUTE,
                    this.requestContext);
        }
        // do actual work of this tag
        return doEndTagInternal();
    } catch (JspException ex) {
        logger.error(ex.getMessage(), ex);
        throw ex;
    } catch (RuntimeException ex) {
        logger.error(ex.getMessage(), ex);
        throw ex;
    } catch (Exception ex) {
        logger.error(ex.getMessage(), ex);
        throw new JspTagException(ex.getMessage());
    }
}

From source file:org.hdiv.web.servlet.tags.form.AbstractHtmlElementTagTests.java

protected MockPageContext createAndPopulatePageContext() throws JspException {
    MockPageContext pageContext = createPageContext();
    MockHttpServletRequest request = (MockHttpServletRequest) pageContext.getRequest();
    RequestContext requestContext = new JspAwareRequestContext(pageContext);
    pageContext.setAttribute(RequestContextAwareTag.REQUEST_CONTEXT_PAGE_ATTRIBUTE, requestContext);
    extendRequest(request);/*from  ww w  .  java2  s  .  c  om*/
    extendPageContext(pageContext);
    return pageContext;
}

From source file:org.parancoe.web.test.TagTest.java

/**
 * Reset the request and the response, maintaining the same session. Useful, for example, to
 * call a post after calling the get of the form.
 *//*from  w w w .j ava 2 s . c  o m*/
protected void resetRequestAndResponse() {
    HttpSession httpSession = null;
    // preparing the multipart request
    if (mpReq != null) {
        httpSession = mpReq.getSession();
    }
    mpReq = new MockMultipartHttpServletRequest();
    mpReq.setSession(httpSession);
    mpReq.setMethod("GET");
    // preparing the normal request
    if (req != null) {
        httpSession = req.getSession();
    }
    req = new MockHttpServletRequest();
    req.setSession(httpSession);
    req.setMethod("GET");
    req.setContextPath("/testctx");
    req.setRequestURI("/testctx/test/request/uri");
    req.setAttribute(WebUtils.FORWARD_REQUEST_URI_ATTRIBUTE, "/testctx/test/forward/request/uri");
    req.setQueryString("p1=v1&p2=v2&p3=v3");
    res = new MockHttpServletResponse();
    pc = new MockPageContext(((WebApplicationContext) this.getApplicationContext()).getServletContext(), req,
            res);
    rc = new JspAwareRequestContext(pc);
    pc.setAttribute(RequestContextAwareTag.REQUEST_CONTEXT_PAGE_ATTRIBUTE, rc);
}

From source file:test.pl.chilldev.facelets.taglib.spring.web.form.ErrorsTagTest.java

@Test
public void applyAllErrors() throws IOException, FacesException {
    String path = "foo.*";
    String var = "error";

    Map<String, Object> config = new HashMap<>();
    config.put(ErrorsTag.ATTRIBUTE_PATH, path);
    config.put(ErrorsTag.ATTRIBUTE_VAR, var);

    ErrorsTag tag = new ErrorsTag(MockTagConfig.factory(config, this.nextHandler));

    // set up context
    FaceletContext context = new MockFaceletContext();
    context.setAttribute(RequestContextAwareTag.REQUEST_CONTEXT_PAGE_ATTRIBUTE, this.requestContext);

    List<ObjectError> objectErrors = new ArrayList<>();
    objectErrors.add(new ObjectError("foo", "Test 1"));
    objectErrors.add(new ObjectError("foo", "Test 2"));

    when(this.requestContext.getErrors("foo", false)).thenReturn(this.errors);
    when(this.errors.getAllErrors()).thenReturn(objectErrors);

    // run the tag
    tag.apply(context, this.parent);

    verify(this.nextHandler, times(2)).apply(context, this.parent);
}

From source file:pl.chilldev.facelets.taglib.spring.web.form.ErrorsTag.java

/**
 * Attempts to retrive current request context.
 *
 * @param faceletContext Current view context.
 * @return Request context.//from w ww .j a v a  2 s. c om
 * @since 0.0.1
 */
protected RequestContext getRequestContext(FaceletContext faceletContext) {
    // first check if there is a context stored in view
    Object context = faceletContext.getAttribute(RequestContextAwareTag.REQUEST_CONTEXT_PAGE_ATTRIBUTE);
    if (context instanceof RequestContext) {
        return (RequestContext) context;
    }

    // if not then create completely new one
    RequestContext requestContext = null;
    ExternalContext externalContext = faceletContext.getFacesContext().getExternalContext();

    Object request = externalContext.getRequest();
    Object response = externalContext.getResponse();
    context = externalContext.getContext();

    // check all types
    if (request instanceof HttpServletRequest && response instanceof HttpServletResponse
            && context instanceof ServletContext) {
        requestContext = new RequestContext((HttpServletRequest) request, (HttpServletResponse) response,
                (ServletContext) context, null);
        faceletContext.setAttribute(RequestContextAwareTag.REQUEST_CONTEXT_PAGE_ATTRIBUTE, requestContext);
    }
    return requestContext;
}

From source file:test.pl.chilldev.facelets.taglib.spring.web.form.ErrorsTagTest.java

@Test
public void applyGlobalErrors() throws IOException, FacesException {
    String path = "foo";
    String var = "error";

    Map<String, Object> config = new HashMap<>();
    config.put(ErrorsTag.ATTRIBUTE_PATH, path);
    config.put(ErrorsTag.ATTRIBUTE_VAR, var);

    ErrorsTag tag = new ErrorsTag(MockTagConfig.factory(config, this.nextHandler));

    // set up context
    FaceletContext context = new MockFaceletContext();
    context.setAttribute(RequestContextAwareTag.REQUEST_CONTEXT_PAGE_ATTRIBUTE, this.requestContext);

    List<ObjectError> objectErrors = new ArrayList<>();
    objectErrors.add(new ObjectError("foo", "Test 1"));
    objectErrors.add(new ObjectError("foo", "Test 2"));

    when(this.requestContext.getErrors(path, false)).thenReturn(this.errors);
    when(this.errors.getGlobalErrors()).thenReturn(objectErrors);

    // run the tag
    tag.apply(context, this.parent);

    verify(this.nextHandler, times(2)).apply(context, this.parent);
}