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);
}
}
}
}
}
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);
}
}
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);
}
}
}
}
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);
}
}
}
}
}
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);
}
}
}
Aggregations