Skip to content

Commit 6ba0416

Browse files
committed
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.
1 parent 7e98ba4 commit 6ba0416

File tree

6 files changed

+155
-6
lines changed

6 files changed

+155
-6
lines changed

slice-core/src/main/java/com/cognifide/slice/core/internal/adapter/AdapterFactoryManager.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.apache.felix.scr.annotations.Component;
3232
import org.apache.felix.scr.annotations.Deactivate;
3333
import org.apache.felix.scr.annotations.Service;
34+
import org.apache.sling.api.SlingHttpServletRequest;
3435
import org.apache.sling.api.adapter.AdapterFactory;
3536
import org.apache.sling.api.resource.Resource;
3637
import org.osgi.framework.Bundle;
@@ -145,7 +146,7 @@ private ServiceRegistration createAdapterFactory(Collection<Class<?>> classes, S
145146
SliceAdapterFactory factory = new SliceAdapterFactory(name);
146147

147148
Dictionary<String, Object> properties = new Hashtable<String, Object>();
148-
properties.put(AdapterFactory.ADAPTABLE_CLASSES, new String[] { Resource.class.getName() });
149+
properties.put(AdapterFactory.ADAPTABLE_CLASSES, new String[] { Resource.class.getName(), SlingHttpServletRequest.class.getName() });
149150
properties.put(AdapterFactory.ADAPTER_CLASSES, adapterClassNames);
150151
return bundleContext.registerService(AdapterFactory.class.getName(), factory, properties);
151152
}

slice-core/src/main/java/com/cognifide/slice/core/internal/adapter/SliceAdapterFactory.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
package com.cognifide.slice.core.internal.adapter;
2222

23+
import org.apache.sling.api.SlingHttpServletRequest;
2324
import org.apache.sling.api.adapter.AdapterFactory;
2425
import org.apache.sling.api.resource.Resource;
2526
import org.apache.sling.api.resource.ResourceResolver;
@@ -47,10 +48,16 @@ public SliceAdapterFactory(String injectorName) {
4748

4849
@Override
4950
public <AdapterType> AdapterType getAdapter(Object adaptable, Class<AdapterType> type) {
50-
if (!(adaptable instanceof Resource)) {
51+
if (!((adaptable instanceof Resource) || (adaptable instanceof SlingHttpServletRequest))) {
5152
return null;
5253
}
53-
Resource resource = (Resource) adaptable;
54+
55+
Resource resource;
56+
if(adaptable instanceof Resource) {
57+
resource = (Resource) adaptable;
58+
} else {
59+
resource = ((SlingHttpServletRequest) adaptable).getResource();
60+
}
5461

5562
InjectorWithContext injector = getInjector(resource);
5663
if (injector != null) {
@@ -67,7 +74,7 @@ public <AdapterType> AdapterType getAdapter(Object adaptable, Class<AdapterType>
6774
}
6875
}
6976

70-
private InjectorWithContext getInjector(Resource resource) {
77+
private InjectorWithContext getInjector(Resource resource) {
7178
ResourceResolver resourceResolver = resource.getResourceResolver();
7279
InjectorsRepository repository = resourceResolver.adaptTo(InjectorsRepository.class);
7380
if (repository == null) {
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*-
2+
* #%L
3+
* Slice - Mapper API
4+
* %%
5+
* Copyright (C) 2012 Cognifide Limited
6+
* %%
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* #L%
19+
*/
20+
package com.cognifide.slice.mapper.annotation;
21+
22+
import java.lang.annotation.*;
23+
24+
/**
25+
* Indicates that a given field should be mapped from a request parameter . The
26+
* name of the request parameter is indicated by the name of the field or value of {@link RequestParameter} (if
27+
* specified). Only a type of string is allowed
28+
* Example:
29+
*
30+
* <pre>
31+
* {@literal @}SliceResource
32+
* public class ExampleModel {
33+
*
34+
* {@literal @}RequestParameter
35+
* private String myParameter;
36+
*
37+
* {@literal @}RequestParameter("second-parameter")
38+
* private String secondParameter;
39+
* }
40+
* </pre>
41+
*
42+
* @author roy.teeuwen
43+
*
44+
*/
45+
@Documented
46+
@Target(ElementType.FIELD)
47+
@Retention(RetentionPolicy.RUNTIME)
48+
public @interface RequestParameter {
49+
50+
/**
51+
* Custom request parameter name. If empty, property name is read from field's name.
52+
*
53+
* @return value
54+
*/
55+
String value() default "";
56+
}

slice-mapper/src/main/java/com/cognifide/slice/mapper/MapperBuilder.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ public final class MapperBuilder {
6262
@Inject
6363
private RequestAttributeProcessor requestAttributeProcessor;
6464

65+
@Inject
66+
private RequestParameterProcessor requestParameterProcessor;
67+
6568
@Inject
6669
private CustomProcessorsCollector customProcessorsCollector;
6770

@@ -118,6 +121,7 @@ public MapperBuilder addSliceProcessors() {
118121
processors.add(sliceResourceFieldProcessor); // @SliceResource
119122
processors.add(childrenFieldProcessor); // child models @Children
120123
processors.add(requestAttributeProcessor); // @RequestAttribute
124+
processors.add(requestParameterProcessor); // @RequestParameter
121125
processors.add(new ListFieldProcessor()); // Subclasses of Collection<?> and arrays
122126
processors.add(new BooleanFieldProcessor()); // booleans
123127
processors.add(new EnumFieldProcessor()); // enums
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*-
2+
* #%L
3+
* Slice - Mapper
4+
* %%
5+
* Copyright (C) 2012 Cognifide Limited
6+
* %%
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* #L%
19+
*/
20+
package com.cognifide.slice.mapper.impl.processor;
21+
22+
import com.cognifide.slice.mapper.annotation.RequestParameter;
23+
import com.cognifide.slice.mapper.api.processor.FieldProcessor;
24+
import com.google.inject.Inject;
25+
import org.apache.commons.lang.StringUtils;
26+
import org.apache.sling.api.SlingHttpServletRequest;
27+
import org.apache.sling.api.resource.Resource;
28+
import org.apache.sling.api.resource.ValueMap;
29+
30+
import java.lang.reflect.Field;
31+
import java.util.ArrayList;
32+
import java.util.Collection;
33+
import java.util.List;
34+
35+
public class RequestParameterProcessor implements FieldProcessor {
36+
37+
@Inject
38+
private SlingHttpServletRequest slingRequest;
39+
40+
@Override
41+
public boolean accepts(final Resource resource, final Field field) {
42+
Class<?> fieldType = field.getType();
43+
return (Collection.class.isAssignableFrom(fieldType) || fieldType.equals(String.class)) && field.isAnnotationPresent(RequestParameter.class);
44+
}
45+
46+
@Override
47+
public Object mapResourceToField(Resource resource, ValueMap valueMap, Field field, String propertyName) {
48+
String parameterName = getParameterName(field);
49+
Class<?> fieldType = field.getType();
50+
if (Collection.class.isAssignableFrom(fieldType)) {
51+
org.apache.sling.api.request.RequestParameter[] parameters = slingRequest.getRequestParameters(parameterName);
52+
if (parameters != null) {
53+
return getParameterValues(parameters);
54+
}
55+
} else {
56+
org.apache.sling.api.request.RequestParameter parameter = slingRequest.getRequestParameter(parameterName);
57+
if (parameter != null) {
58+
return parameter.getString();
59+
}
60+
}
61+
return null;
62+
}
63+
64+
private List<String> getParameterValues(org.apache.sling.api.request.RequestParameter[] parameters) {
65+
List<String> result = new ArrayList<String>();
66+
for (org.apache.sling.api.request.RequestParameter parameter : parameters) {
67+
result.add(parameter.getString());
68+
}
69+
return result;
70+
}
71+
72+
private String getParameterName(Field field) {
73+
final RequestParameter annotation = field.getAnnotation(RequestParameter.class);
74+
if ((annotation != null) && StringUtils.isNotBlank(annotation.value())) {
75+
return annotation.value();
76+
}
77+
return field.getName();
78+
}
79+
}

slice-mapper/src/main/java/com/cognifide/slice/mapper/strategy/impl/AnnotatedFieldMapperStrategy.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,21 @@
2424

2525
import com.cognifide.slice.mapper.annotation.JcrProperty;
2626
import com.cognifide.slice.mapper.annotation.RequestAttribute;
27+
import com.cognifide.slice.mapper.annotation.RequestParameter;
2728
import com.cognifide.slice.mapper.strategy.MapperStrategy;
2829

2930
/**
3031
* AnnotatedFieldMapperStrategy defines a strategy where only fields annotated by
31-
* {@link JcrProperty}, {@link RequestAttribute} are mapped.
32+
* {@link JcrProperty}, {@link RequestAttribute} or {@link RequestParameter} are mapped.
3233
*
3334
*/
3435
public class AnnotatedFieldMapperStrategy implements MapperStrategy {
3536

3637
@Override
3738
public boolean shouldFieldBeMapped(Field field) {
3839
return (field.isAnnotationPresent(JcrProperty.class)
39-
|| field.isAnnotationPresent(RequestAttribute.class));
40+
|| field.isAnnotationPresent(RequestAttribute.class)
41+
|| field.isAnnotationPresent(RequestParameter.class));
4042
}
4143

4244
}

0 commit comments

Comments
 (0)