Search in sources :

Example 16 with ScopeData

use of jodd.madvoc.ScopeData in project jodd by oblac.

the class RequestScopeInjector method injectParameters.

/**
	 * Inject request parameters.
	 */
protected void injectParameters(Target[] targets, ScopeData[] injectData, HttpServletRequest servletRequest) {
    boolean encode = encodeGetParams && servletRequest.getMethod().equals("GET");
    Enumeration paramNames = servletRequest.getParameterNames();
    while (paramNames.hasMoreElements()) {
        String paramName = (String) paramNames.nextElement();
        if (servletRequest.getAttribute(paramName) != null) {
            continue;
        }
        for (int i = 0; i < targets.length; i++) {
            Target target = targets[i];
            if (injectData[i] == null) {
                continue;
            }
            ScopeData.In[] scopes = injectData[i].in;
            if (scopes == null) {
                continue;
            }
            for (ScopeData.In in : scopes) {
                String name = getMatchedPropertyName(in, paramName);
                if (name != null) {
                    String[] paramValues = servletRequest.getParameterValues(paramName);
                    paramValues = ServletUtil.prepareParameters(paramValues, trimParams, treatEmptyParamsAsNull, ignoreEmptyRequestParams);
                    if (paramValues == null) {
                        continue;
                    }
                    if (encode) {
                        for (int j = 0; j < paramValues.length; j++) {
                            String p = paramValues[j];
                            if (p != null) {
                                paramValues[j] = StringUtil.convertCharset(p, StringPool.ISO_8859_1, encoding);
                            }
                        }
                    }
                    Object value = (paramValues.length != 1 ? paramValues : paramValues[0]);
                    setTargetProperty(target, name, value);
                }
            }
        }
    }
}
Also used : Enumeration(java.util.Enumeration) ScopeData(jodd.madvoc.ScopeData)

Example 17 with ScopeData

use of jodd.madvoc.ScopeData in project jodd by oblac.

the class RequestScopeInjector method inject.

public void inject(ActionRequest actionRequest) {
    Target[] targets = actionRequest.getTargets();
    ScopeData[] injectData = lookupScopeData(actionRequest);
    if (injectData == null) {
        return;
    }
    HttpServletRequest servletRequest = actionRequest.getHttpServletRequest();
    if (injectAttributes) {
        injectAttributes(targets, injectData, servletRequest);
    }
    if (injectParameters) {
        injectParameters(targets, injectData, servletRequest);
        injectUploadedFiles(targets, injectData, servletRequest);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ScopeData(jodd.madvoc.ScopeData)

Example 18 with ScopeData

use of jodd.madvoc.ScopeData in project jodd by oblac.

the class ServletContextScopeInjector method inject.

/**
	 * Injects servlet context scope data.
	 */
@SuppressWarnings({ "ConstantConditions" })
public void inject(ActionRequest actionRequest) {
    ScopeData[] injectData = lookupScopeData(actionRequest);
    if (injectData == null) {
        return;
    }
    Target[] targets = actionRequest.getTargets();
    HttpServletRequest servletRequest = actionRequest.getHttpServletRequest();
    HttpServletResponse servletResponse = actionRequest.getHttpServletResponse();
    for (int i = 0; i < targets.length; i++) {
        Target target = targets[i];
        if (injectData[i] == null) {
            continue;
        }
        ScopeData.In[] scopes = injectData[i].in;
        if (scopes == null) {
            continue;
        }
        for (ScopeData.In in : scopes) {
            Class fieldType = in.type;
            Object value = null;
            // raw servlet types
            if (fieldType.equals(HttpServletRequest.class)) {
                // correct would be: ReflectUtil.isSubclass()
                value = servletRequest;
            } else if (fieldType.equals(HttpServletResponse.class)) {
                value = servletResponse;
            } else if (fieldType.equals(HttpSession.class)) {
                value = servletRequest.getSession();
            } else if (fieldType.equals(ServletContext.class)) {
                value = servletRequest.getSession().getServletContext();
            } else // names
            if (in.name.equals(REQUEST_MAP)) {
                value = new HttpServletRequestMap(servletRequest);
            } else if (in.name.equals(REQUEST_PARAM_MAP)) {
                value = new HttpServletRequestParamMap(servletRequest);
            } else if (in.name.equals(REQUEST_BODY)) {
                try {
                    value = ServletUtil.readRequestBody(servletRequest);
                } catch (IOException e) {
                    value = e.toString();
                }
            } else if (in.name.equals(REQUEST_BODY)) {
                value = new HttpServletRequestParamMap(servletRequest);
            } else if (in.name.equals(SESSION_MAP)) {
                value = new HttpSessionMap(servletRequest);
            } else if (in.name.equals(CONTEXT_MAP)) {
                value = new HttpServletContextMap(servletRequest);
            } else // names partial
            if (in.name.startsWith(REQUEST_NAME)) {
                value = BeanUtil.declared.getProperty(servletRequest, StringUtil.uncapitalize(in.name.substring(REQUEST_NAME.length())));
            } else if (in.name.startsWith(SESSION_NAME)) {
                value = BeanUtil.declared.getProperty(servletRequest.getSession(), StringUtil.uncapitalize(in.name.substring(SESSION_NAME.length())));
            } else if (in.name.startsWith(CONTEXT_NAME)) {
                value = BeanUtil.declared.getProperty(servletRequest.getSession().getServletContext(), StringUtil.uncapitalize(in.name.substring(CONTEXT_NAME.length())));
            } else // csrf
            if (in.name.equals(CSRF_NAME)) {
                value = Boolean.valueOf(CsrfShield.checkCsrfToken(servletRequest));
            }
            // cookies
            if (in.name.startsWith(COOKIE_NAME)) {
                String cookieName = StringUtil.uncapitalize(in.name.substring(COOKIE_NAME.length()));
                if (fieldType.isArray()) {
                    if (fieldType.getComponentType().equals(Cookie.class)) {
                        if (StringUtil.isEmpty(cookieName)) {
                            // get all cookies
                            value = servletRequest.getCookies();
                        } else {
                            // get all cookies by name
                            value = ServletUtil.getAllCookies(servletRequest, cookieName);
                        }
                    }
                } else {
                    // get single cookie
                    value = ServletUtil.getCookie(servletRequest, cookieName);
                }
            }
            if (value != null) {
                String property = in.target != null ? in.target : in.name;
                setTargetProperty(target, property, value);
            }
        }
    }
}
Also used : HttpSessionMap(jodd.servlet.map.HttpSessionMap) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletRequestParamMap(jodd.servlet.map.HttpServletRequestParamMap) ScopeData(jodd.madvoc.ScopeData) HttpServletContextMap(jodd.servlet.map.HttpServletContextMap) ServletContext(javax.servlet.ServletContext) HttpServletRequestMap(jodd.servlet.map.HttpServletRequestMap)

Example 19 with ScopeData

use of jodd.madvoc.ScopeData in project jodd by oblac.

the class SessionScopeInjector method inject.

public void inject(ActionRequest actionRequest) {
    ScopeData[] injectData = lookupScopeData(actionRequest);
    if (injectData == null) {
        return;
    }
    Target[] targets = actionRequest.getTargets();
    HttpServletRequest servletRequest = actionRequest.getHttpServletRequest();
    HttpSession session = servletRequest.getSession();
    Enumeration attributeNames = session.getAttributeNames();
    while (attributeNames.hasMoreElements()) {
        String attrName = (String) attributeNames.nextElement();
        for (int i = 0; i < targets.length; i++) {
            Target target = targets[i];
            if (injectData[i] == null) {
                continue;
            }
            ScopeData.In[] scopes = injectData[i].in;
            if (scopes == null) {
                continue;
            }
            for (ScopeData.In in : scopes) {
                String name = getMatchedPropertyName(in, attrName);
                if (name != null) {
                    Object attrValue = session.getAttribute(attrName);
                    setTargetProperty(target, name, attrValue);
                }
            }
        }
    }
}
Also used : Enumeration(java.util.Enumeration) HttpSession(javax.servlet.http.HttpSession) HttpServletRequest(javax.servlet.http.HttpServletRequest) ScopeData(jodd.madvoc.ScopeData)

Example 20 with ScopeData

use of jodd.madvoc.ScopeData in project jodd by oblac.

the class SessionScopeInjector method outject.

public void outject(ActionRequest actionRequest) {
    ScopeData[] outjectData = lookupScopeData(actionRequest);
    if (outjectData == null) {
        return;
    }
    Target[] targets = actionRequest.getTargets();
    HttpServletRequest servletRequest = actionRequest.getHttpServletRequest();
    HttpSession session = servletRequest.getSession();
    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);
            session.setAttribute(out.name, value);
        }
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpSession(javax.servlet.http.HttpSession) ScopeData(jodd.madvoc.ScopeData)

Aggregations

ScopeData (jodd.madvoc.ScopeData)21 Enumeration (java.util.Enumeration)6 HttpServletRequest (javax.servlet.http.HttpServletRequest)6 ServletContext (javax.servlet.ServletContext)4 In (jodd.madvoc.meta.In)4 Out (jodd.madvoc.meta.Out)3 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 HttpSession (javax.servlet.http.HttpSession)2 ScopeType (jodd.madvoc.ScopeType)2 ScopeDataResolver (jodd.madvoc.component.ScopeDataResolver)2 InOut (jodd.madvoc.meta.InOut)2 Test (org.junit.Test)2 IOException (java.io.IOException)1 Annotation (java.lang.annotation.Annotation)1 ArrayList (java.util.ArrayList)1 Cookie (javax.servlet.http.Cookie)1 ClassDescriptor (jodd.introspector.ClassDescriptor)1 PropertyDescriptor (jodd.introspector.PropertyDescriptor)1 ActionConfig (jodd.madvoc.ActionConfig)1 ActionConfigSet (jodd.madvoc.ActionConfigSet)1