Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/injectable request params #120

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -145,7 +146,7 @@ private ServiceRegistration createAdapterFactory(Collection<Class<?>> classes, S
SliceAdapterFactory factory = new SliceAdapterFactory(name);

Dictionary<String, Object> properties = new Hashtable<String, Object>();
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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -47,10 +48,16 @@ public SliceAdapterFactory(String injectorName) {

@Override
public <AdapterType> AdapterType getAdapter(Object adaptable, Class<AdapterType> 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) {
Expand All @@ -67,7 +74,7 @@ public <AdapterType> AdapterType getAdapter(Object adaptable, Class<AdapterType>
}
}

private InjectorWithContext getInjector(Resource resource) {
private InjectorWithContext getInjector(Resource resource) {
ResourceResolver resourceResolver = resource.getResourceResolver();
InjectorsRepository repository = resourceResolver.adaptTo(InjectorsRepository.class);
if (repository == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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:
*
* <pre>
* {@literal @}SliceResource
* public class ExampleModel {
*
* {@literal @}RequestAttribute
* private String myAttribute;
*
* {@literal @}RequestAttribute("second-attribute")
* private Boolean secondAttribute;
* }
* </pre>
*
* @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 "";
}
Original file line number Diff line number Diff line change
@@ -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:
*
* <pre>
* {@literal @}SliceResource
* public class ExampleModel {
*
* {@literal @}RequestParameter
* private String myParameter;
*
* {@literal @}RequestParameter("second-parameter")
* private String secondParameter;
* }
* </pre>
*
* @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 "";
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -65,6 +59,12 @@ public final class MapperBuilder {
@Inject
private ChildrenFieldProcessor childrenFieldProcessor;

@Inject
private RequestAttributeProcessor requestAttributeProcessor;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if this is a good place for this kind of logic. Conceptually, your class needs to be annotated with @SliceResource only if you want to map its fields from Sling resource. I can imagine a scenario when you don't need mapping to happen but still want to have request parameters/attributes injected. The mapper should not mix these things and fieldProcessors should rely on resource.

If not mapper, then what? I'd probably made RequestAttributeProcessor (and the other one) as Guice providers rather than FieldProcessor. This would make the design cleaner. The disadvantage would be that you'd need to use @Inject annotation whenever you want the values to be injected and probably specify the name of an attribute (I'm not sure if Guice will give you information about the field name - probably no):

@Inject
@RequestAttribute("attribute")
private Boolean attribute;

Could you try with this approach?


@Inject
private RequestParameterProcessor requestParameterProcessor;

@Inject
private CustomProcessorsCollector customProcessorsCollector;

Expand Down Expand Up @@ -120,6 +120,8 @@ public MapperBuilder addSliceProcessors() {
processors.add(sliceReferenceFieldProcessor); // @SliceReference
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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*-
* #%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.api.qualifier.Nullable;
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.resource.Resource;
import org.apache.sling.api.resource.ValueMap;

import javax.servlet.ServletRequest;
import java.lang.reflect.Field;

public class RequestAttributeProcessor implements FieldProcessor {

@Inject
@Nullable
private ServletRequest servletRequest;

@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) {
if (servletRequest != null) {
String attributeName = getAttributeName(field);
return servletRequest.getAttribute(attributeName);
}
return null;
}

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();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*-
* #%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.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.resource.Resource;
import org.apache.sling.api.resource.ValueMap;

import javax.servlet.ServletRequest;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Collection;

public class RequestParameterProcessor implements FieldProcessor {

@Inject
@Nullable
private ServletRequest servletRequest;

@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) {
if (servletRequest == null) {
return null;
}
String parameterName = getParameterName(field);
Class<?> fieldType = field.getType();
if (Collection.class.isAssignableFrom(fieldType)) {
String[] parameters = servletRequest.getParameterValues(parameterName);
if (parameters != null) {
return Arrays.asList(parameters);
}
} else {
return servletRequest.getParameter(parameterName);
}
return null;
}

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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,22 @@
import java.lang.reflect.Field;

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} are
* mapped.
*
* AnnotatedFieldMapperStrategy defines a strategy where only fields annotated by
* {@link JcrProperty}, {@link RequestAttribute} or {@link RequestParameter} 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)
|| field.isAnnotationPresent(RequestParameter.class));
}

}