Example usage for org.springframework.web.servlet.support RequestContext RequestContext

List of usage examples for org.springframework.web.servlet.support RequestContext RequestContext

Introduction

In this page you can find the example usage for org.springframework.web.servlet.support RequestContext RequestContext.

Prototype

public RequestContext(HttpServletRequest request) 

Source Link

Document

Create a new RequestContext for the given request, using the request attributes for Errors retrieval.

Usage

From source file:com.surfs.storage.web.utils.WebUtils.java

public static String getPropertiesMessage(HttpServletRequest request, String messageId, Object[] params) {
    RequestContext requestContext = new RequestContext(request);

    WebApplicationContext webContext = requestContext.getWebApplicationContext();
    String message = webContext.getMessage(messageId, params, "", requestContext.getLocale());
    return message;
}

From source file:org.parancoe.web.LanguageInterceptor.java

@Override
public boolean preHandle(HttpServletRequest req, HttpServletResponse res, Object object) throws Exception {
    RequestContext rc = new RequestContext(req);
    req.setAttribute("requestContext", rc);
    req.setAttribute("lang", rc.getLocale().getLanguage());
    logger.debug("LanguageInterceptor.preHandle()");
    return true;//from   ww  w .  jav a 2s.c o m
}

From source file:org.openmrs.module.web.taglib.ExtensionPopupMenuTag.java

public int doStartTag() throws JspException {
    if (showLabelIfNoExtensions == null) {
        showLabelIfNoExtensions = true;//from w  w  w .  j av a2 s  . co m
    }
    // using this as we'd use MessageSourceAccessor
    RequestContext context = new RequestContext((HttpServletRequest) this.pageContext.getRequest());

    boolean below = !"above".equals(position);
    Map<String, String> parameters = new HashMap<String, String>();
    if (this.parameters != null) {
        parameters.putAll(OpenmrsUtil.parseParameterList(this.parameters));
    }

    StringBuilder sb = new StringBuilder();
    sb.append("<span style=\"position: relative\">");
    if (below) {
        sb.append("<div id=\"" + popupDivId
                + "\" style=\"width: 35em; border: 1px solid black; background-color: #f0f0a0; position: absolute; top: 0px; padding-right: 1.2em; z-index: 1; display: none\">");
    } else {
        sb.append("<div id=\"" + popupDivId
                + "\" style=\"width: 35em; border: 1px solid black; background-color: #f0f0a0; position: absolute; bottom: 0px; padding-right: 1.2em; z-index: 1; display: none\">");
    }

    sb.append("<div style=\"float: right\"><a href=\"javascript:hideLayer('" + popupDivId + "');\" >["
            + context.getMessage("general.close") + "]</a></div>");
    sb.append("<ul>");
    boolean anyExtensionsFound = false;
    List<Extension> extensions = ModuleFactory.getExtensions(pointId, Extension.MEDIA_TYPE.html);
    for (Extension e : extensions) {
        if (e instanceof LinkProviderExtension) {
            anyExtensionsFound = true;
            LinkProviderExtension extension = (LinkProviderExtension) e;
            List<Link> links = extension.getLinks();
            log.debug("extension of class " + e.getClass() + " provides " + links.size() + " links");
            for (Link link : links) {
                String url = link.getUrl();
                log.debug("url = " + url);
                StringBuilder hiddenVars = new StringBuilder();
                Map<String, String> javascriptSubstitutions = new HashMap<String, String>();
                for (Map.Entry<String, String> entry : link.getQueryParameters().entrySet()) {
                    hiddenVars.append("<input type=\"hidden\" name=\"" + entry.getKey() + "\" value=\""
                            + entry.getValue() + "\"/>\n");
                }
                for (Map.Entry<String, String> entry : parameters.entrySet()) {
                    hiddenVars.append("<input type=\"hidden\" name=\"" + entry.getKey() + "\" ");
                    if (entry.getValue().startsWith("javascript:")) {
                        String function = entry.getValue();
                        function = function.substring(function.indexOf(":") + 1);
                        String random = randomString();
                        javascriptSubstitutions.put(random, function);
                        hiddenVars.append("id=\"" + random + "\" value=\"\"");
                    } else {
                        hiddenVars.append("value=\"" + entry.getValue() + "\"");
                    }
                    hiddenVars.append("/>\n");
                }
                String formId = randomString();

                StringBuilder onClick = new StringBuilder();
                if (javascriptSubstitutions.size() > 0) {
                    onClick.append(" var _popup_tmp = ''; ");
                }
                for (Map.Entry<String, String> entry : javascriptSubstitutions.entrySet()) {
                    String id = entry.getKey();
                    String function = entry.getValue();
                    onClick.append(" _popup_tmp = " + function
                            + "; if (_popup_tmp == null) return; document.getElementById('" + id
                            + "').value = _popup_tmp; ");
                }
                onClick.append("document.getElementById('" + formId + "').submit();");

                sb.append("<li>");
                sb.append("<form id=\"" + formId + "\" method=\"post\" action=\"" + url + "\">\n");
                sb.append(hiddenVars);
                sb.append("\n<a href=\"#\" onClick=\"javascript:" + onClick + "\">"
                        + context.getMessage(link.getLabel(), link.getLabel()) + "</a>");
                if (link.getDescription() != null) {
                    sb.append("<br/><small>" + context.getMessage(link.getDescription(), link.getDescription())
                            + "</small>");
                }
                sb.append("</form>");
                sb.append("</li>");
            }
        }
    }
    if (!anyExtensionsFound) {
        sb.append("<li>" + context.getMessage("general.none") + "</li>");
    }

    sb.append("</ul>");
    sb.append("</div>");
    sb.append("</span>");
    sb.append("<a href=\"#\" onClick=\"toggleLayer('" + popupDivId + "')\" style=\"border: 1px black solid\">"
            + context.getMessage(label, label) + "</a>");

    try {
        if (anyExtensionsFound || showLabelIfNoExtensions) {
            pageContext.getOut().print(sb);
        }
    } catch (IOException ex) {
        throw new JspException(ex);
    }

    resetValues();
    return SKIP_BODY;
}

From source file:org.openmrs.contrib.metadatarepository.webapp.taglib.LabelTag.java

public int doStartTag() throws JspException {

    try {//from w w  w .j a va 2  s. c  o m
        this.requestContext = new RequestContext((HttpServletRequest) this.pageContext.getRequest());
    } catch (RuntimeException ex) {
        throw ex;
    } catch (Exception ex) {
        pageContext.getServletContext().log("Exception in custom tag", ex);
    }

    // Look up this key to see if its a field of the current form
    boolean requiredField = false;
    boolean validationError = false;

    ValidatorResources resources = getValidatorResources();

    Locale locale = pageContext.getRequest().getLocale();

    if (locale == null) {
        locale = Locale.getDefault();
    }

    // get the name of the bean from the key
    String formName = key.substring(0, key.indexOf('.'));
    String fieldName = key.substring(formName.length() + 1);

    if (resources != null) {
        Form form = resources.getForm(locale, formName);

        if (form != null) {
            Field field = form.getField(fieldName);

            if (field != null) {
                if (field.isDependency("required") || field.isDependency("validwhen")) {
                    requiredField = true;
                }
            }
        }
    }

    Errors errors = requestContext.getErrors(formName, false);
    List fes = null;
    if (errors != null) {
        fes = errors.getFieldErrors(fieldName);
        //String errorMsg = getErrorMessages(fes);
    }

    if (fes != null && fes.size() > 0) {
        validationError = true;
    }

    // Retrieve the message string we are looking for
    String message = null;
    try {
        message = getMessageSource().getMessage(key, null, locale);
    } catch (NoSuchMessageException nsm) {
        message = "???" + key + "???";
    }

    String cssClass = null;
    if (styleClass != null) {
        cssClass = styleClass;
    } else if (requiredField) {
        cssClass = "required";
    }

    String cssErrorClass = (errorClass != null) ? errorClass : "error";
    StringBuffer label = new StringBuffer();

    if ((message == null) || "".equals(message.trim())) {
        label.append("");
    } else {
        label.append("<label for=\"").append(fieldName).append("\"");

        if (validationError) {
            label.append(" class=\"").append(cssErrorClass).append("\"");
        } else if (cssClass != null) {
            label.append(" class=\"").append(cssClass).append("\"");
        }

        label.append(">").append(message);
        label.append((requiredField) ? " <span class=\"req\">*</span>" : "");
        label.append((colon) ? ":" : "");
        label.append("</label>");

        if (validationError) {
            label.append("<img class=\"validationWarning\" alt=\"");
            label.append(getMessageSource().getMessage("icon.warning", null, locale));
            label.append("\"");

            String context = ((HttpServletRequest) pageContext.getRequest()).getContextPath();

            label.append(" src=\"").append(context);
            label.append(getMessageSource().getMessage("icon.warning.img", null, locale));
            label.append("\" />");
        }
    }

    // Print the retrieved message to our output writer
    try {
        writeMessage(label.toString());
    } catch (IOException io) {
        io.printStackTrace();
        throw new JspException("Error writing label: " + io.getMessage());
    }

    // Continue processing this page
    return (SKIP_BODY);
}

From source file:alpha.portal.webapp.taglib.LabelTag.java

@Override
public int doStartTag() throws JspException {

    try {/*from w  w  w  .j  a v a 2 s  .  co  m*/
        this.requestContext = new RequestContext((HttpServletRequest) this.pageContext.getRequest());
    } catch (final RuntimeException ex) {
        throw ex;
    } catch (final Exception ex) {
        this.pageContext.getServletContext().log("Exception in custom tag", ex);
    }

    // Look up this key to see if its a field of the current form
    boolean requiredField = false;
    boolean validationError = false;

    final ValidatorResources resources = this.getValidatorResources();

    Locale locale = this.pageContext.getRequest().getLocale();

    if (locale == null) {
        locale = Locale.getDefault();
    }

    // get the name of the bean from the key
    final String formName = this.key.substring(0, this.key.indexOf('.'));
    final String fieldName = this.key.substring(formName.length() + 1);

    if (resources != null) {
        final Form form = resources.getForm(locale, formName);

        if (form != null) {
            final Field field = form.getField(fieldName);

            if (field != null) {
                if (field.isDependency("required") || field.isDependency("validwhen")) {
                    requiredField = true;
                }
            }
        }
    }

    final Errors errors = this.requestContext.getErrors(formName, false);
    List fes = null;
    if (errors != null) {
        fes = errors.getFieldErrors(fieldName);
        // String errorMsg = getErrorMessages(fes);
    }

    if ((fes != null) && (fes.size() > 0)) {
        validationError = true;
    }

    // Retrieve the message string we are looking for
    String message = null;
    try {
        message = this.getMessageSource().getMessage(this.key, null, locale);
    } catch (final NoSuchMessageException nsm) {
        message = "???" + this.key + "???";
    }

    String cssClass = null;
    if (this.styleClass != null) {
        cssClass = this.styleClass;
    } else if (requiredField) {
        cssClass = "required";
    }

    final String cssErrorClass = (this.errorClass != null) ? this.errorClass : "error";
    final StringBuffer label = new StringBuffer();

    if ((message == null) || "".equals(message.trim())) {
        label.append("");
    } else {
        label.append("<label for=\"").append(fieldName).append("\"");

        if (validationError) {
            label.append(" class=\"").append(cssErrorClass).append("\"");
        } else if (cssClass != null) {
            label.append(" class=\"").append(cssClass).append("\"");
        }

        label.append(">").append(message);
        label.append((requiredField) ? " <span class=\"req\">*</span>" : "");
        label.append((this.colon) ? ":" : "");
        label.append("</label>");

        if (validationError) {
            label.append("<img class=\"validationWarning\" alt=\"");
            label.append(this.getMessageSource().getMessage("icon.warning", null, locale));
            label.append("\"");

            final String context = ((HttpServletRequest) this.pageContext.getRequest()).getContextPath();

            label.append(" src=\"").append(context);
            label.append(this.getMessageSource().getMessage("icon.warning.img", null, locale));
            label.append("\" />");
        }
    }

    // Print the retrieved message to our output writer
    try {
        this.writeMessage(label.toString());
    } catch (final IOException io) {
        io.printStackTrace();
        throw new JspException("Error writing label: " + io.getMessage());
    }

    // Continue processing this page
    return (Tag.SKIP_BODY);
}

From source file:org.hdiv.web.servlet.view.DummyMacroRequestContext.java

/**
 * @see org.springframework.web.servlet.support.RequestContext#getBindStatus(String)
 *//*from  ww w.  j a v  a 2s . com*/
public BindStatus getBindStatus(String path) throws IllegalStateException {
    return new BindStatus(new RequestContext(this.request), path, false);
}

From source file:org.hdiv.web.servlet.view.DummyMacroRequestContext.java

/**
 * @see org.springframework.web.servlet.support.RequestContext#getBindStatus(String, boolean)
 *//*from   w ww. j ava 2s  .c  o m*/
public BindStatus getBindStatus(String path, boolean htmlEscape) throws IllegalStateException {
    return new BindStatus(new RequestContext(this.request), path, true);
}

From source file:org.springmodules.validation.commons.taglib.JavascriptValidatorTag.java

/**
 * Use the application context itself for default message resolution.
 *///from w w w  .  j a  va 2 s .c  o  m
private MessageSource getMessageSource() {
    try {
        this.requestContext = new RequestContext((HttpServletRequest) this.pageContext.getRequest());
    } catch (RuntimeException ex) {
        throw ex;
    } catch (Exception ex) {
        pageContext.getServletContext().log("Exception in custom tag", ex);
    }
    return requestContext.getWebApplicationContext();
}

From source file:org.bibsonomy.webapp.util.RequestLogic.java

/**
 * @return The locale associated with the current request.
 */
public Locale getLocale() {
    return new RequestContext(request).getLocale();
}