From 7e98ba4aa3af339ce7ba286417568021cfb153e6 Mon Sep 17 00:00:00 2001 From: Roy Teeuwen Date: Fri, 13 Oct 2017 20:52:57 +0200 Subject: [PATCH 1/5] Added request attribute annotation. Can be used together with data-sly-use=${'com.cognifide.slice.TestModel' @ myAttribute='aValue'} to read out the attribute --- .../mapper/annotation/RequestAttribute.java | 56 +++++++++++++++++++ .../slice/mapper/annotation/package-info.java | 2 +- .../cognifide/slice/mapper/MapperBuilder.java | 12 ++-- .../processor/RequestAttributeProcessor.java | 55 ++++++++++++++++++ .../impl/AnnotatedFieldMapperStrategy.java | 10 ++-- 5 files changed, 123 insertions(+), 12 deletions(-) create mode 100644 slice-mapper-api/src/main/java/com/cognifide/slice/mapper/annotation/RequestAttribute.java create mode 100644 slice-mapper/src/main/java/com/cognifide/slice/mapper/impl/processor/RequestAttributeProcessor.java diff --git a/slice-mapper-api/src/main/java/com/cognifide/slice/mapper/annotation/RequestAttribute.java b/slice-mapper-api/src/main/java/com/cognifide/slice/mapper/annotation/RequestAttribute.java new file mode 100644 index 00000000..bd0110c0 --- /dev/null +++ b/slice-mapper-api/src/main/java/com/cognifide/slice/mapper/annotation/RequestAttribute.java @@ -0,0 +1,56 @@ +/*- + * #%L + * Slice - Mapper API + * %% + * Copyright (C) 2012 Cognifide Limited + * %% + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * #L% + */ +package com.cognifide.slice.mapper.annotation; + +import java.lang.annotation.*; + +/** + * Indicates that a given field should be mapped from a request attribute . The + * name of the request attribute is indicated by the name of the field or value of {@link RequestAttribute} (if + * specified). + * Example: + * + *
+ * {@literal @}SliceResource
+ * public class ExampleModel {
+ *
+ *   {@literal @}RequestAttribute
+ *   private String myAttribute;
+ *
+ *   {@literal @}RequestAttribute("second-attribute")
+ *   private Boolean secondAttribute;
+ * }
+ * 
+ * + * @author roy.teeuwen + * + */ +@Documented +@Target(ElementType.FIELD) +@Retention(RetentionPolicy.RUNTIME) +public @interface RequestAttribute { + + /** + * Custom request attribute name. If empty, property name is read from field's name. + * + * @return value + */ + String value() default ""; +} diff --git a/slice-mapper-api/src/main/java/com/cognifide/slice/mapper/annotation/package-info.java b/slice-mapper-api/src/main/java/com/cognifide/slice/mapper/annotation/package-info.java index 786bb96b..40e27dec 100644 --- a/slice-mapper-api/src/main/java/com/cognifide/slice/mapper/annotation/package-info.java +++ b/slice-mapper-api/src/main/java/com/cognifide/slice/mapper/annotation/package-info.java @@ -17,7 +17,7 @@ * limitations under the License. * #L% */ -@Version("4.3.0") +@Version("4.4.0") package com.cognifide.slice.mapper.annotation; import aQute.bnd.annotation.Version; diff --git a/slice-mapper/src/main/java/com/cognifide/slice/mapper/MapperBuilder.java b/slice-mapper/src/main/java/com/cognifide/slice/mapper/MapperBuilder.java index 503da0b2..ad77dc15 100644 --- a/slice-mapper/src/main/java/com/cognifide/slice/mapper/MapperBuilder.java +++ b/slice-mapper/src/main/java/com/cognifide/slice/mapper/MapperBuilder.java @@ -30,13 +30,7 @@ import com.cognifide.slice.mapper.api.processor.PriorityFieldProcessor; import com.cognifide.slice.mapper.impl.CustomProcessorsCollector; import com.cognifide.slice.mapper.impl.postprocessor.EscapeValuePostProcessor; -import com.cognifide.slice.mapper.impl.processor.BooleanFieldProcessor; -import com.cognifide.slice.mapper.impl.processor.ChildrenFieldProcessor; -import com.cognifide.slice.mapper.impl.processor.DefaultFieldProcessor; -import com.cognifide.slice.mapper.impl.processor.EnumFieldProcessor; -import com.cognifide.slice.mapper.impl.processor.ListFieldProcessor; -import com.cognifide.slice.mapper.impl.processor.SliceReferenceFieldProcessor; -import com.cognifide.slice.mapper.impl.processor.SliceResourceFieldProcessor; +import com.cognifide.slice.mapper.impl.processor.*; import com.google.inject.Inject; import com.google.inject.Module; @@ -65,6 +59,9 @@ public final class MapperBuilder { @Inject private ChildrenFieldProcessor childrenFieldProcessor; + @Inject + private RequestAttributeProcessor requestAttributeProcessor; + @Inject private CustomProcessorsCollector customProcessorsCollector; @@ -120,6 +117,7 @@ public MapperBuilder addSliceProcessors() { processors.add(sliceReferenceFieldProcessor); // @SliceReference processors.add(sliceResourceFieldProcessor); // @SliceResource processors.add(childrenFieldProcessor); // child models @Children + processors.add(requestAttributeProcessor); // @RequestAttribute processors.add(new ListFieldProcessor()); // Subclasses of Collection and arrays processors.add(new BooleanFieldProcessor()); // booleans processors.add(new EnumFieldProcessor()); // enums diff --git a/slice-mapper/src/main/java/com/cognifide/slice/mapper/impl/processor/RequestAttributeProcessor.java b/slice-mapper/src/main/java/com/cognifide/slice/mapper/impl/processor/RequestAttributeProcessor.java new file mode 100644 index 00000000..9300d06c --- /dev/null +++ b/slice-mapper/src/main/java/com/cognifide/slice/mapper/impl/processor/RequestAttributeProcessor.java @@ -0,0 +1,55 @@ +/*- + * #%L + * Slice - Mapper + * %% + * Copyright (C) 2012 Cognifide Limited + * %% + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * #L% + */ +package com.cognifide.slice.mapper.impl.processor; + +import com.cognifide.slice.mapper.annotation.RequestAttribute; +import com.cognifide.slice.mapper.api.processor.FieldProcessor; +import com.google.inject.Inject; +import org.apache.commons.lang.StringUtils; +import org.apache.sling.api.SlingHttpServletRequest; +import org.apache.sling.api.resource.Resource; +import org.apache.sling.api.resource.ValueMap; + +import java.lang.reflect.Field; + +public class RequestAttributeProcessor implements FieldProcessor { + + @Inject + private SlingHttpServletRequest slingRequest; + + @Override + public boolean accepts(final Resource resource, final Field field) { + return field.isAnnotationPresent(RequestAttribute.class); + } + + @Override + public Object mapResourceToField(Resource resource, ValueMap valueMap, Field field, String propertyName) { + String attributeName = getAttributeName(field); + return slingRequest.getAttribute(attributeName); + } + + private String getAttributeName(Field field) { + final RequestAttribute annotation = field.getAnnotation(RequestAttribute.class); + if ((annotation != null) && StringUtils.isNotBlank(annotation.value())) { + return annotation.value(); + } + return field.getName(); + } +} diff --git a/slice-mapper/src/main/java/com/cognifide/slice/mapper/strategy/impl/AnnotatedFieldMapperStrategy.java b/slice-mapper/src/main/java/com/cognifide/slice/mapper/strategy/impl/AnnotatedFieldMapperStrategy.java index 54777e34..3fab787d 100644 --- a/slice-mapper/src/main/java/com/cognifide/slice/mapper/strategy/impl/AnnotatedFieldMapperStrategy.java +++ b/slice-mapper/src/main/java/com/cognifide/slice/mapper/strategy/impl/AnnotatedFieldMapperStrategy.java @@ -23,18 +23,20 @@ import java.lang.reflect.Field; import com.cognifide.slice.mapper.annotation.JcrProperty; +import com.cognifide.slice.mapper.annotation.RequestAttribute; import com.cognifide.slice.mapper.strategy.MapperStrategy; /** - * AnnotatedFieldMapperStrategy defines a strategy where only fields annotated by {@link JcrProperty} are - * mapped. - * + * AnnotatedFieldMapperStrategy defines a strategy where only fields annotated by + * {@link JcrProperty}, {@link RequestAttribute} are mapped. + * */ public class AnnotatedFieldMapperStrategy implements MapperStrategy { @Override public boolean shouldFieldBeMapped(Field field) { - return field.isAnnotationPresent(JcrProperty.class); + return (field.isAnnotationPresent(JcrProperty.class) + || field.isAnnotationPresent(RequestAttribute.class)); } } From ce9748e7ff934d5c4857fb80c5b9f4c81192cec2 Mon Sep 17 00:00:00 2001 From: Roy Teeuwen Date: Fri, 13 Oct 2017 21:31:38 +0200 Subject: [PATCH 2/5] Fix the request processor by using the servlet request (which is always available, in contrast of sling servlet request) --- .../mapper/impl/processor/RequestAttributeProcessor.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/slice-mapper/src/main/java/com/cognifide/slice/mapper/impl/processor/RequestAttributeProcessor.java b/slice-mapper/src/main/java/com/cognifide/slice/mapper/impl/processor/RequestAttributeProcessor.java index 9300d06c..571f07f0 100644 --- a/slice-mapper/src/main/java/com/cognifide/slice/mapper/impl/processor/RequestAttributeProcessor.java +++ b/slice-mapper/src/main/java/com/cognifide/slice/mapper/impl/processor/RequestAttributeProcessor.java @@ -23,16 +23,16 @@ import com.cognifide.slice.mapper.api.processor.FieldProcessor; import com.google.inject.Inject; import org.apache.commons.lang.StringUtils; -import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.api.resource.Resource; import org.apache.sling.api.resource.ValueMap; +import javax.servlet.ServletRequest; import java.lang.reflect.Field; public class RequestAttributeProcessor implements FieldProcessor { @Inject - private SlingHttpServletRequest slingRequest; + private ServletRequest servletRequest; @Override public boolean accepts(final Resource resource, final Field field) { @@ -42,7 +42,7 @@ public boolean accepts(final Resource resource, final Field field) { @Override public Object mapResourceToField(Resource resource, ValueMap valueMap, Field field, String propertyName) { String attributeName = getAttributeName(field); - return slingRequest.getAttribute(attributeName); + return servletRequest.getAttribute(attributeName); } private String getAttributeName(Field field) { From d9b165d70ccb213100f752a7dbf4b76c61ed27a4 Mon Sep 17 00:00:00 2001 From: Roy Teeuwen Date: Fri, 13 Oct 2017 21:33:09 +0200 Subject: [PATCH 3/5] Make request nullable because in the unit/integration tests it might not be available --- .../mapper/impl/processor/RequestAttributeProcessor.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/slice-mapper/src/main/java/com/cognifide/slice/mapper/impl/processor/RequestAttributeProcessor.java b/slice-mapper/src/main/java/com/cognifide/slice/mapper/impl/processor/RequestAttributeProcessor.java index 571f07f0..00e5c2ec 100644 --- a/slice-mapper/src/main/java/com/cognifide/slice/mapper/impl/processor/RequestAttributeProcessor.java +++ b/slice-mapper/src/main/java/com/cognifide/slice/mapper/impl/processor/RequestAttributeProcessor.java @@ -19,6 +19,7 @@ */ package com.cognifide.slice.mapper.impl.processor; +import com.cognifide.slice.api.qualifier.Nullable; import com.cognifide.slice.mapper.annotation.RequestAttribute; import com.cognifide.slice.mapper.api.processor.FieldProcessor; import com.google.inject.Inject; @@ -32,6 +33,7 @@ public class RequestAttributeProcessor implements FieldProcessor { @Inject + @Nullable private ServletRequest servletRequest; @Override @@ -41,8 +43,11 @@ public boolean accepts(final Resource resource, final Field field) { @Override public Object mapResourceToField(Resource resource, ValueMap valueMap, Field field, String propertyName) { - String attributeName = getAttributeName(field); - return servletRequest.getAttribute(attributeName); + if (servletRequest != null) { + String attributeName = getAttributeName(field); + return servletRequest.getAttribute(attributeName); + } + return null; } private String getAttributeName(Field field) { From b3eb3e5f7361ce06943d46237bc46875344d6ea6 Mon Sep 17 00:00:00 2001 From: Roy Teeuwen Date: Fri, 13 Oct 2017 20:55:55 +0200 Subject: [PATCH 4/5] Made it possible to inject request parameters in a model and to adapt from a request immediatly instead of using request.getResource(). This way you could adapt your request in a servlet to a model with request parameters as fields. --- .../adapter/AdapterFactoryManager.java | 3 +- .../internal/adapter/SliceAdapterFactory.java | 13 ++- .../mapper/annotation/RequestParameter.java | 56 +++++++++++++ .../cognifide/slice/mapper/MapperBuilder.java | 4 + .../processor/RequestParameterProcessor.java | 79 +++++++++++++++++++ .../impl/AnnotatedFieldMapperStrategy.java | 6 +- 6 files changed, 155 insertions(+), 6 deletions(-) create mode 100644 slice-mapper-api/src/main/java/com/cognifide/slice/mapper/annotation/RequestParameter.java create mode 100644 slice-mapper/src/main/java/com/cognifide/slice/mapper/impl/processor/RequestParameterProcessor.java diff --git a/slice-core/src/main/java/com/cognifide/slice/core/internal/adapter/AdapterFactoryManager.java b/slice-core/src/main/java/com/cognifide/slice/core/internal/adapter/AdapterFactoryManager.java index 817d5660..5bb3df7d 100644 --- a/slice-core/src/main/java/com/cognifide/slice/core/internal/adapter/AdapterFactoryManager.java +++ b/slice-core/src/main/java/com/cognifide/slice/core/internal/adapter/AdapterFactoryManager.java @@ -31,6 +31,7 @@ import org.apache.felix.scr.annotations.Component; import org.apache.felix.scr.annotations.Deactivate; import org.apache.felix.scr.annotations.Service; +import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.api.adapter.AdapterFactory; import org.apache.sling.api.resource.Resource; import org.osgi.framework.Bundle; @@ -145,7 +146,7 @@ private ServiceRegistration createAdapterFactory(Collection> classes, S SliceAdapterFactory factory = new SliceAdapterFactory(name); Dictionary properties = new Hashtable(); - properties.put(AdapterFactory.ADAPTABLE_CLASSES, new String[] { Resource.class.getName() }); + properties.put(AdapterFactory.ADAPTABLE_CLASSES, new String[] { Resource.class.getName(), SlingHttpServletRequest.class.getName() }); properties.put(AdapterFactory.ADAPTER_CLASSES, adapterClassNames); return bundleContext.registerService(AdapterFactory.class.getName(), factory, properties); } diff --git a/slice-core/src/main/java/com/cognifide/slice/core/internal/adapter/SliceAdapterFactory.java b/slice-core/src/main/java/com/cognifide/slice/core/internal/adapter/SliceAdapterFactory.java index b4783fee..bc212471 100644 --- a/slice-core/src/main/java/com/cognifide/slice/core/internal/adapter/SliceAdapterFactory.java +++ b/slice-core/src/main/java/com/cognifide/slice/core/internal/adapter/SliceAdapterFactory.java @@ -20,6 +20,7 @@ package com.cognifide.slice.core.internal.adapter; +import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.api.adapter.AdapterFactory; import org.apache.sling.api.resource.Resource; import org.apache.sling.api.resource.ResourceResolver; @@ -47,10 +48,16 @@ public SliceAdapterFactory(String injectorName) { @Override public AdapterType getAdapter(Object adaptable, Class type) { - if (!(adaptable instanceof Resource)) { + if (!((adaptable instanceof Resource) || (adaptable instanceof SlingHttpServletRequest))) { return null; } - Resource resource = (Resource) adaptable; + + Resource resource; + if(adaptable instanceof Resource) { + resource = (Resource) adaptable; + } else { + resource = ((SlingHttpServletRequest) adaptable).getResource(); + } InjectorWithContext injector = getInjector(resource); if (injector != null) { @@ -67,7 +74,7 @@ public AdapterType getAdapter(Object adaptable, Class } } - private InjectorWithContext getInjector(Resource resource) { + private InjectorWithContext getInjector(Resource resource) { ResourceResolver resourceResolver = resource.getResourceResolver(); InjectorsRepository repository = resourceResolver.adaptTo(InjectorsRepository.class); if (repository == null) { diff --git a/slice-mapper-api/src/main/java/com/cognifide/slice/mapper/annotation/RequestParameter.java b/slice-mapper-api/src/main/java/com/cognifide/slice/mapper/annotation/RequestParameter.java new file mode 100644 index 00000000..202d5512 --- /dev/null +++ b/slice-mapper-api/src/main/java/com/cognifide/slice/mapper/annotation/RequestParameter.java @@ -0,0 +1,56 @@ +/*- + * #%L + * Slice - Mapper API + * %% + * Copyright (C) 2012 Cognifide Limited + * %% + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * #L% + */ +package com.cognifide.slice.mapper.annotation; + +import java.lang.annotation.*; + +/** + * Indicates that a given field should be mapped from a request parameter . The + * name of the request parameter is indicated by the name of the field or value of {@link RequestParameter} (if + * specified). Only a type of string is allowed + * Example: + * + *
+ * {@literal @}SliceResource
+ * public class ExampleModel {
+ *
+ *   {@literal @}RequestParameter
+ *   private String myParameter;
+ *
+ *   {@literal @}RequestParameter("second-parameter")
+ *   private String secondParameter;
+ * }
+ * 
+ * + * @author roy.teeuwen + * + */ +@Documented +@Target(ElementType.FIELD) +@Retention(RetentionPolicy.RUNTIME) +public @interface RequestParameter { + + /** + * Custom request parameter name. If empty, property name is read from field's name. + * + * @return value + */ + String value() default ""; +} diff --git a/slice-mapper/src/main/java/com/cognifide/slice/mapper/MapperBuilder.java b/slice-mapper/src/main/java/com/cognifide/slice/mapper/MapperBuilder.java index ad77dc15..be69272a 100644 --- a/slice-mapper/src/main/java/com/cognifide/slice/mapper/MapperBuilder.java +++ b/slice-mapper/src/main/java/com/cognifide/slice/mapper/MapperBuilder.java @@ -62,6 +62,9 @@ public final class MapperBuilder { @Inject private RequestAttributeProcessor requestAttributeProcessor; + @Inject + private RequestParameterProcessor requestParameterProcessor; + @Inject private CustomProcessorsCollector customProcessorsCollector; @@ -118,6 +121,7 @@ public MapperBuilder addSliceProcessors() { processors.add(sliceResourceFieldProcessor); // @SliceResource processors.add(childrenFieldProcessor); // child models @Children processors.add(requestAttributeProcessor); // @RequestAttribute + processors.add(requestParameterProcessor); // @RequestParameter processors.add(new ListFieldProcessor()); // Subclasses of Collection and arrays processors.add(new BooleanFieldProcessor()); // booleans processors.add(new EnumFieldProcessor()); // enums diff --git a/slice-mapper/src/main/java/com/cognifide/slice/mapper/impl/processor/RequestParameterProcessor.java b/slice-mapper/src/main/java/com/cognifide/slice/mapper/impl/processor/RequestParameterProcessor.java new file mode 100644 index 00000000..3afb951c --- /dev/null +++ b/slice-mapper/src/main/java/com/cognifide/slice/mapper/impl/processor/RequestParameterProcessor.java @@ -0,0 +1,79 @@ +/*- + * #%L + * Slice - Mapper + * %% + * Copyright (C) 2012 Cognifide Limited + * %% + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * #L% + */ +package com.cognifide.slice.mapper.impl.processor; + +import com.cognifide.slice.mapper.annotation.RequestParameter; +import com.cognifide.slice.mapper.api.processor.FieldProcessor; +import com.google.inject.Inject; +import org.apache.commons.lang.StringUtils; +import org.apache.sling.api.SlingHttpServletRequest; +import org.apache.sling.api.resource.Resource; +import org.apache.sling.api.resource.ValueMap; + +import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +public class RequestParameterProcessor implements FieldProcessor { + + @Inject + private SlingHttpServletRequest slingRequest; + + @Override + public boolean accepts(final Resource resource, final Field field) { + Class fieldType = field.getType(); + return (Collection.class.isAssignableFrom(fieldType) || fieldType.equals(String.class)) && field.isAnnotationPresent(RequestParameter.class); + } + + @Override + public Object mapResourceToField(Resource resource, ValueMap valueMap, Field field, String propertyName) { + String parameterName = getParameterName(field); + Class fieldType = field.getType(); + if (Collection.class.isAssignableFrom(fieldType)) { + org.apache.sling.api.request.RequestParameter[] parameters = slingRequest.getRequestParameters(parameterName); + if (parameters != null) { + return getParameterValues(parameters); + } + } else { + org.apache.sling.api.request.RequestParameter parameter = slingRequest.getRequestParameter(parameterName); + if (parameter != null) { + return parameter.getString(); + } + } + return null; + } + + private List getParameterValues(org.apache.sling.api.request.RequestParameter[] parameters) { + List result = new ArrayList(); + for (org.apache.sling.api.request.RequestParameter parameter : parameters) { + result.add(parameter.getString()); + } + return result; + } + + private String getParameterName(Field field) { + final RequestParameter annotation = field.getAnnotation(RequestParameter.class); + if ((annotation != null) && StringUtils.isNotBlank(annotation.value())) { + return annotation.value(); + } + return field.getName(); + } +} diff --git a/slice-mapper/src/main/java/com/cognifide/slice/mapper/strategy/impl/AnnotatedFieldMapperStrategy.java b/slice-mapper/src/main/java/com/cognifide/slice/mapper/strategy/impl/AnnotatedFieldMapperStrategy.java index 3fab787d..a4c26352 100644 --- a/slice-mapper/src/main/java/com/cognifide/slice/mapper/strategy/impl/AnnotatedFieldMapperStrategy.java +++ b/slice-mapper/src/main/java/com/cognifide/slice/mapper/strategy/impl/AnnotatedFieldMapperStrategy.java @@ -24,11 +24,12 @@ import com.cognifide.slice.mapper.annotation.JcrProperty; import com.cognifide.slice.mapper.annotation.RequestAttribute; +import com.cognifide.slice.mapper.annotation.RequestParameter; import com.cognifide.slice.mapper.strategy.MapperStrategy; /** * AnnotatedFieldMapperStrategy defines a strategy where only fields annotated by - * {@link JcrProperty}, {@link RequestAttribute} are mapped. + * {@link JcrProperty}, {@link RequestAttribute} or {@link RequestParameter} are mapped. * */ public class AnnotatedFieldMapperStrategy implements MapperStrategy { @@ -36,7 +37,8 @@ public class AnnotatedFieldMapperStrategy implements MapperStrategy { @Override public boolean shouldFieldBeMapped(Field field) { return (field.isAnnotationPresent(JcrProperty.class) - || field.isAnnotationPresent(RequestAttribute.class)); + || field.isAnnotationPresent(RequestAttribute.class) + || field.isAnnotationPresent(RequestParameter.class)); } } From cab9f7aa60bf95a46bd419e73997275d35908437 Mon Sep 17 00:00:00 2001 From: Roy Teeuwen Date: Fri, 13 Oct 2017 21:39:29 +0200 Subject: [PATCH 5/5] Use servlet request for parameters as to make sure its always available --- .../processor/RequestParameterProcessor.java | 29 +++++++------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/slice-mapper/src/main/java/com/cognifide/slice/mapper/impl/processor/RequestParameterProcessor.java b/slice-mapper/src/main/java/com/cognifide/slice/mapper/impl/processor/RequestParameterProcessor.java index 3afb951c..7b2e57ba 100644 --- a/slice-mapper/src/main/java/com/cognifide/slice/mapper/impl/processor/RequestParameterProcessor.java +++ b/slice-mapper/src/main/java/com/cognifide/slice/mapper/impl/processor/RequestParameterProcessor.java @@ -19,23 +19,24 @@ */ package com.cognifide.slice.mapper.impl.processor; +import com.cognifide.slice.api.qualifier.Nullable; import com.cognifide.slice.mapper.annotation.RequestParameter; import com.cognifide.slice.mapper.api.processor.FieldProcessor; import com.google.inject.Inject; import org.apache.commons.lang.StringUtils; -import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.api.resource.Resource; import org.apache.sling.api.resource.ValueMap; +import javax.servlet.ServletRequest; import java.lang.reflect.Field; -import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; -import java.util.List; public class RequestParameterProcessor implements FieldProcessor { @Inject - private SlingHttpServletRequest slingRequest; + @Nullable + private ServletRequest servletRequest; @Override public boolean accepts(final Resource resource, final Field field) { @@ -45,30 +46,22 @@ public boolean accepts(final Resource resource, final Field field) { @Override public Object mapResourceToField(Resource resource, ValueMap valueMap, Field field, String propertyName) { + if (servletRequest == null) { + return null; + } String parameterName = getParameterName(field); Class fieldType = field.getType(); if (Collection.class.isAssignableFrom(fieldType)) { - org.apache.sling.api.request.RequestParameter[] parameters = slingRequest.getRequestParameters(parameterName); + String[] parameters = servletRequest.getParameterValues(parameterName); if (parameters != null) { - return getParameterValues(parameters); + return Arrays.asList(parameters); } } else { - org.apache.sling.api.request.RequestParameter parameter = slingRequest.getRequestParameter(parameterName); - if (parameter != null) { - return parameter.getString(); - } + return servletRequest.getParameter(parameterName); } return null; } - private List getParameterValues(org.apache.sling.api.request.RequestParameter[] parameters) { - List result = new ArrayList(); - for (org.apache.sling.api.request.RequestParameter parameter : parameters) { - result.add(parameter.getString()); - } - return result; - } - private String getParameterName(Field field) { final RequestParameter annotation = field.getAnnotation(RequestParameter.class); if ((annotation != null) && StringUtils.isNotBlank(annotation.value())) {