Search in sources :

Example 26 with ServletContext

use of javax.servlet.ServletContext in project sonarqube by SonarSource.

the class Resources method getActionMessage.

/**
     * Gets the <code>ActionMessage</code> based on the
     * <code>ValidatorAction</code> message and the <code>Field</code>'s arg
     * objects.
     *
     * @param validator the Validator
     * @param request   the servlet request
     * @param va        Validator action
     * @param field     the validator Field
     */
public static ActionMessage getActionMessage(Validator validator, HttpServletRequest request, ValidatorAction va, Field field) {
    Msg msg = field.getMessage(va.getName());
    if ((msg != null) && !msg.isResource()) {
        return new ActionMessage(msg.getKey(), false);
    }
    String msgKey = null;
    String msgBundle = null;
    if (msg == null) {
        msgKey = va.getMsg();
    } else {
        msgKey = msg.getKey();
        msgBundle = msg.getBundle();
    }
    if ((msgKey == null) || (msgKey.length() == 0)) {
        return new ActionMessage("??? " + va.getName() + "." + field.getProperty() + " ???", false);
    }
    ServletContext application = (ServletContext) validator.getParameterValue(SERVLET_CONTEXT_PARAM);
    MessageResources messages = getMessageResources(application, request, msgBundle);
    Locale locale = RequestUtils.getUserLocale(request, null);
    Arg[] args = field.getArgs(va.getName());
    String[] argValues = getArgValues(application, request, messages, locale, args);
    ActionMessage actionMessage = null;
    if (msgBundle == null) {
        actionMessage = new ActionMessage(msgKey, argValues);
    } else {
        String message = messages.getMessage(locale, msgKey, argValues);
        actionMessage = new ActionMessage(message, false);
    }
    return actionMessage;
}
Also used : Msg(org.apache.commons.validator.Msg) Locale(java.util.Locale) MessageResources(org.apache.struts.util.MessageResources) Arg(org.apache.commons.validator.Arg) ActionMessage(org.apache.struts.action.ActionMessage) ServletContext(javax.servlet.ServletContext)

Example 27 with ServletContext

use of javax.servlet.ServletContext in project sonarqube by SonarSource.

the class JavascriptValidatorTag method createDynamicJavascript.

/**
     * Generates the dynamic JavaScript for the form.
     *
     * @param config
     * @param resources
     * @param locale
     * @param form
     */
private String createDynamicJavascript(ModuleConfig config, ValidatorResources resources, Locale locale, Form form) throws JspException {
    StringBuffer results = new StringBuffer();
    MessageResources messages = TagUtils.getInstance().retrieveMessageResources(pageContext, bundle, true);
    HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
    ServletContext application = pageContext.getServletContext();
    List actions = this.createActionList(resources, form);
    final String methods = this.createMethods(actions, this.stopOnError(config));
    String formName = form.getName();
    jsFormName = formName;
    if (jsFormName.charAt(0) == '/') {
        String mappingName = TagUtils.getInstance().getActionMappingName(jsFormName);
        ActionMapping mapping = (ActionMapping) config.findActionConfig(mappingName);
        if (mapping == null) {
            JspException e = new JspException(messages.getMessage("formTag.mapping", mappingName));
            pageContext.setAttribute(Globals.EXCEPTION_KEY, e, PageContext.REQUEST_SCOPE);
            throw e;
        }
        jsFormName = mapping.getAttribute();
    }
    results.append(this.getJavascriptBegin(methods));
    for (Iterator i = actions.iterator(); i.hasNext(); ) {
        ValidatorAction va = (ValidatorAction) i.next();
        int jscriptVar = 0;
        String functionName = null;
        if ((va.getJsFunctionName() != null) && (va.getJsFunctionName().length() > 0)) {
            functionName = va.getJsFunctionName();
        } else {
            functionName = va.getName();
        }
        results.append("    function " + jsFormName + "_" + functionName + " () { \n");
        for (Iterator x = form.getFields().iterator(); x.hasNext(); ) {
            Field field = (Field) x.next();
            // retrieve from scope?))
            if (field.isIndexed() || (field.getPage() != page) || !field.isDependency(va.getName())) {
                continue;
            }
            String message = Resources.getMessage(application, request, messages, locale, va, field);
            message = (message != null) ? message : "";
            // prefix variable with 'a' to make it a legal identifier
            results.append("     this.a" + jscriptVar++ + " = new Array(\"" + field.getKey() + "\", \"" + escapeQuotes(message) + "\", ");
            results.append("new Function (\"varName\", \"");
            Map vars = field.getVars();
            // Loop through the field's variables.
            Iterator varsIterator = vars.keySet().iterator();
            while (varsIterator.hasNext()) {
                String varName = (String) varsIterator.next();
                Var var = (Var) vars.get(varName);
                String varValue = Resources.getVarValue(var, application, request, false);
                String jsType = var.getJsType();
                // fieldValue
                if (varName.startsWith("field")) {
                    continue;
                }
                String varValueEscaped = escapeJavascript(varValue);
                if (Var.JSTYPE_INT.equalsIgnoreCase(jsType)) {
                    results.append("this." + varName + "=" + varValueEscaped + "; ");
                } else if (Var.JSTYPE_REGEXP.equalsIgnoreCase(jsType)) {
                    results.append("this." + varName + "=/" + varValueEscaped + "/; ");
                } else if (Var.JSTYPE_STRING.equalsIgnoreCase(jsType)) {
                    results.append("this." + varName + "='" + varValueEscaped + "'; ");
                // So everyone using the latest format doesn't need to
                // change their xml files immediately.
                } else if ("mask".equalsIgnoreCase(varName)) {
                    results.append("this." + varName + "=/" + varValueEscaped + "/; ");
                } else {
                    results.append("this." + varName + "='" + varValueEscaped + "'; ");
                }
            }
            results.append(" return this[varName];\"));\n");
        }
        results.append("    } \n\n");
    }
    return results.toString();
}
Also used : ValidatorAction(org.apache.commons.validator.ValidatorAction) MessageResources(org.apache.struts.util.MessageResources) Var(org.apache.commons.validator.Var) HttpServletRequest(javax.servlet.http.HttpServletRequest) JspException(javax.servlet.jsp.JspException) Field(org.apache.commons.validator.Field) ActionMapping(org.apache.struts.action.ActionMapping) Iterator(java.util.Iterator) ServletContext(javax.servlet.ServletContext) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map)

Example 28 with ServletContext

use of javax.servlet.ServletContext in project che by eclipse.

the class CheBootstrapTest method setUp.

@BeforeMethod
public void setUp() throws Exception {
    systemPropertiesHelper = overrideSystemProperties();
    cheBootstrap = new CheBootstrap();
    servletContext = mock(ServletContext.class);
    URL classesDirUrl = Thread.currentThread().getContextClassLoader().getResource(".");
    File classesDir = new File(classesDirUrl.toURI());
    che = new File(classesDir, "che");
    che.mkdir();
    userCongDir = new File(System.getenv(CHE_LOCAL_CONF_DIR));
    userCongDir.mkdirs();
    mockServletContext();
}
Also used : ServletContext(javax.servlet.ServletContext) File(java.io.File) URL(java.net.URL) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 29 with ServletContext

use of javax.servlet.ServletContext in project jodd by oblac.

the class ApplicationScopeInjector method outject.

public void outject(ActionRequest actionRequest) {
    ScopeData[] outjectData = lookupScopeData(actionRequest);
    if (outjectData == null) {
        return;
    }
    Target[] targets = actionRequest.getTargets();
    ServletContext context = actionRequest.getHttpServletRequest().getSession().getServletContext();
    for (int i = 0; i < targets.length; i++) {
        Target target = targets[i];
        if (outjectData[i] == null) {
            continue;
        }
        ScopeData.Out[] scopes = outjectData[i].out;
        if (scopes == null) {
            continue;
        }
        for (ScopeData.Out out : scopes) {
            Object value = getTargetProperty(target, out);
            context.setAttribute(out.name, value);
        }
    }
}
Also used : ScopeData(jodd.madvoc.ScopeData) ServletContext(javax.servlet.ServletContext)

Example 30 with ServletContext

use of javax.servlet.ServletContext in project OpenAM by OpenRock.

the class XUIFilterTest method setUp.

@BeforeMethod
public void setUp() {
    XUIState xuiState = mock(XUIState.class);
    when(xuiState.isXUIEnabled()).thenReturn(true);
    filter = new XUIFilter(xuiState);
    FilterConfig filterConfig = mock(FilterConfig.class);
    ServletContext servletContext = mock(ServletContext.class);
    when(filterConfig.getServletContext()).thenReturn(servletContext);
    when(servletContext.getContextPath()).thenReturn(CONTEXT);
    filter.init(filterConfig);
}
Also used : ServletContext(javax.servlet.ServletContext) FilterConfig(javax.servlet.FilterConfig) BeforeMethod(org.testng.annotations.BeforeMethod)

Aggregations

ServletContext (javax.servlet.ServletContext)1111 Test (org.junit.Test)259 HttpServletRequest (javax.servlet.http.HttpServletRequest)186 HttpServletResponse (javax.servlet.http.HttpServletResponse)143 IOException (java.io.IOException)136 ServletException (javax.servlet.ServletException)101 ServletConfig (javax.servlet.ServletConfig)79 File (java.io.File)78 FilterConfig (javax.servlet.FilterConfig)66 HashMap (java.util.HashMap)64 Enumeration (java.util.Enumeration)52 InputStream (java.io.InputStream)51 ArrayList (java.util.ArrayList)50 URL (java.net.URL)48 HttpSession (javax.servlet.http.HttpSession)43 Map (java.util.Map)39 Locale (java.util.Locale)38 List (java.util.List)33 PrintWriter (java.io.PrintWriter)32 LinkedHashMap (java.util.LinkedHashMap)30