Search in sources :

Example 1 with HttpServletContextMap

use of jodd.servlet.map.HttpServletContextMap 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)

Aggregations

IOException (java.io.IOException)1 ServletContext (javax.servlet.ServletContext)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 ScopeData (jodd.madvoc.ScopeData)1 HttpServletContextMap (jodd.servlet.map.HttpServletContextMap)1 HttpServletRequestMap (jodd.servlet.map.HttpServletRequestMap)1 HttpServletRequestParamMap (jodd.servlet.map.HttpServletRequestParamMap)1 HttpSessionMap (jodd.servlet.map.HttpSessionMap)1