@Override public void inject(final ActionRequest actionRequest, final Targets targets) { final HttpServletRequest servletRequest = actionRequest.getHttpServletRequest(); targets.forEachTargetAndIn(this, (target, in) -> { Object value = null; if (in.type() == Cookie.class) { // get single cookie final String cookieName = StringUtil.uncapitalize(in.name()); value = ServletUtil.getCookie(servletRequest, cookieName); } else if (in.type().isArray()) { if (in.type().getComponentType().equals(Cookie.class)) { if (StringUtil.isEmpty(in.name())) { // get all cookies value = servletRequest.getCookies(); } else { // get all cookies by name value = ServletUtil.getAllCookies(servletRequest, in.name()); } } } if (value != null) { target.writeValue(in, value, true); } }); }
/** * Builds injection point. */ protected InjectionPoint buildInjectionPoint( final String annotationValue, final String propertyName, final Class propertyType, final Class<? extends MadvocScope> scope) { final String value = annotationValue.trim(); final String name, targetName; if (StringUtil.isNotBlank(value)) { name = value; targetName = propertyName; } else { name = propertyName; targetName = null; } return new InjectionPoint(propertyType, name, targetName, scopeResolver.defaultOrScopeType(scope)); }
/** * Injects request attributes. */ protected void injectAttributes(final HttpServletRequest servletRequest, final Targets targets) { final Enumeration<String> attributeNames = servletRequest.getAttributeNames(); while (attributeNames.hasMoreElements()) { final String attrName = attributeNames.nextElement(); targets.forEachTargetAndIn(this, (target, in) -> { final String name = in.matchedName(attrName); if (name != null) { final Object attrValue = servletRequest.getAttribute(attrName); target.writeValue(name, attrValue, true); } }); } }
@Override public void inject(final ActionRequest actionRequest, final Targets targets) { final HttpServletRequest servletRequest = actionRequest.getHttpServletRequest(); targets.forEachTargetAndIn(this, (target, in) -> { final Enumeration<String> headerNames = servletRequest.getHeaders(in.name()); if (headerNames != null) { final List<String> allValues = new ArrayList<>(); while (headerNames.hasMoreElements()) { allValues.add(headerNames.nextElement()); } final Object value; if (allValues.size() == 1) { value = allValues.get(0); } else { value = allValues; } target.writeValue(in, value, true); } }); }
public void inject(final ServletContext servletContext, final Targets targets) { targets.forEachTargetAndIn(madvocScope, (target, in) -> { final Class inType = in.type(); Object value = null; if (inType == ServletContext.class) { value = servletContext; } if (value != null) { target.writeValue(in, value, true); } }); }
/** * Reads value from the target. If something goes wrong, exception * is thrown. We assume that outjection is controlled by developer * and that each reading of a value must be a successful operation. */ public Object readValue(final InjectionPoint injectionPoint) { return readValue(injectionPoint.targetName()); } public Object readValue(final String name) {
/** * Iterates all targets and for each target iterates all OUT injection points of given scope. */ public void forEachTargetAndOut(final MadvocScope scope, final BiConsumer<Target, InjectionPoint> biConsumer) { for (final Target target : targets) { final ScopeData scopeData = target.scopeData(); if (scopeData.out() == null) { continue; } for (final InjectionPoint out : scopeData.out()) { if (out.scope() != scope) { continue; } biConsumer.accept(target, out); } } }
@Override public void outject(final ActionRequest actionRequest, final Targets targets) { final ServletContext context = actionRequest.getHttpServletRequest().getServletContext(); targets.forEachTargetAndOut(this, (target, out) -> { final Object value = target.readValue(out); context.setAttribute(out.name(), value); }); }
@Override public void inject(final ActionRequest actionRequest, final Targets targets) { final String body = actionRequest.readRequestBody(); if (StringUtil.isEmpty(body)) { return; } targets.forEachTargetAndIn(this, (target, in) -> { if (in.type() == String.class) { target.writeValue(in, body, true); } else { final Object value = parseRequestBody(body, in.type()); target.writeValue(in, value, true); } }); }
/** * Writes value to this target. Depending on a flag, writing the value can be * completely silent, when no exception is thrown and with top performances. * Otherwise, an exception is thrown on a failure. */ public void writeValue(final InjectionPoint injectionPoint, final Object propertyValue, final boolean silent) { writeValue(injectionPoint.targetName(), propertyValue, silent); } public void writeValue(final String name, final Object propertyValue, final boolean silent) {
/** * Iterates all targets and for each target iterates all IN injection points of given scope. */ public void forEachTargetAndIn(final MadvocScope scope, final BiConsumer<Target, InjectionPoint> biConsumer) { for (final Target target : targets) { final ScopeData scopeData = target.scopeData(); if (scopeData.in() == null) { continue; } for (final InjectionPoint in : scopeData.in()) { if (in.scope() != scope) { continue; } biConsumer.accept(target, in); } } }
@Test void testGenericAction() { ScopeDataInspector scopeDataInspector = new ScopeDataInspector(); PetiteContainer madpc = new PetiteContainer(); scopeDataInspector.scopeResolver = new ScopeResolver(); scopeDataInspector.scopeResolver.madpc = madpc; madpc.addBean("madvocEncoding", new MadvocEncoding()); ScopeData scopeData = scopeDataInspector.inspectClassScopes(GenAction.class); InjectionPoint[] in1 = scopeData.in(); InjectionPoint[] out1 = scopeData.out(); InjectionPoint in = in1[0]; InjectionPoint out = out1[0]; assertEquals("input", in.name()); assertEquals(String.class, in.type()); assertEquals("output", out.name()); assertEquals(Integer.class, out.type()); }
@Override public void outject(final ActionRequest actionRequest, final Targets targets) { final HttpServletRequest servletRequest = actionRequest.getHttpServletRequest(); final HttpSession session = servletRequest.getSession(); targets.forEachTargetAndOut(this, (target, out) -> { final Object value = target.readValue(out); session.setAttribute(out.name(), value); }); } }
public void inject(final ActionRequest actionRequest, final Targets targets) { final HttpServletRequest servletRequest = actionRequest.getHttpServletRequest(); final HttpServletResponse servletResponse = actionRequest.getHttpServletResponse(); targets.forEachTargetAndIn(madvocScope, (target, in) -> { final Class inType = in.type(); Object value = null; if (inType == HttpServletRequest.class) { value = servletRequest; } else if (inType == ServletRequest.class) { value = servletRequest; } else if (inType == HttpServletResponse.class) { value = servletResponse; } else if (inType == ServletResponse.class) { value = servletResponse; } else if (inType == HttpSession.class) { value = servletRequest.getSession(); } else if (inType == ServletContext.class) { value = servletRequest.getServletContext(); } else if (inType == AsyncContext.class) { value = servletRequest.getAsyncContext(); } else if (inType == ActionRequest.class) { value = actionRequest; } if (value != null) { target.writeValue(in, value, true); } }); }
@Override public void inject(final ServletContext servletContext, final Targets targets) { instancesInjector.inject(servletContext, targets); final Enumeration<String> attributeNames = servletContext.getAttributeNames(); while (attributeNames.hasMoreElements()) { final String attrName = attributeNames.nextElement(); targets.forEachTargetAndIn(this, (target, in) -> { final String name = in.matchedName(attrName); if (name != null) { final Object attrValue = servletContext.getAttribute(attrName); target.writeValue(name, attrValue, true); } }); } }
@Test void testInAnnotations() { ScopeDataInspector scopeDataInspector = new ScopeDataInspector(); PetiteContainer madpc = new PetiteContainer(); scopeDataInspector.scopeResolver = new ScopeResolver(); scopeDataInspector.scopeResolver.madpc = madpc; madpc.addBean("madvocEncoding", new MadvocEncoding()); ScopeData scopeData = scopeDataInspector.inspectClassScopes(Action.class); InjectionPoint[] in1 = scopeData.in(); InjectionPoint in = in1[0]; assertEquals("input", in.name()); assertEquals(String.class, in.type()); }
@Override public void outject(final ActionRequest actionRequest, final Targets targets) { final HttpServletResponse servletResponse = actionRequest.getHttpServletResponse(); targets.forEachTargetAndOut(this, (target, out) -> { final String value = (String) target.readValue(out); if (value != null) { servletResponse.setHeader(out.name(), value); } }); } }
@Override public void inject(final ActionRequest actionRequest, final Targets targets) { final HttpServletRequest servletRequest = actionRequest.getHttpServletRequest(); final HttpSession session = servletRequest.getSession(); final Enumeration<String> attributeNames = session.getAttributeNames(); while (attributeNames.hasMoreElements()) { final String attrName = attributeNames.nextElement(); targets.forEachTargetAndIn(this, (target, in) -> { final String name = in.matchedName(attrName); if (name != null) { final Object attrValue = session.getAttribute(attrName); target.writeValue(name, attrValue, true); } }); } }
@Override public void outject(final ActionRequest actionRequest, final Targets targets) { final HttpServletRequest servletRequest = actionRequest.getHttpServletRequest(); targets.forEachTargetAndOut(this, (target, out) -> { final Object value = target.readValue(out); servletRequest.setAttribute(out.name(), value); }); }
final String name = in.matchedName(paramName); if (name != null) { String[] paramValues = servletRequest.getParameterValues(paramName);