-
Notifications
You must be signed in to change notification settings - Fork 26
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
royteeuwen
wants to merge
5
commits into
wttech:master
Choose a base branch
from
royteeuwen:feature/injectable-request-params
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7e98ba4
Added request attribute annotation. Can be used together with data-sl…
royteeuwen ce9748e
Fix the request processor by using the servlet request (which is alwa…
royteeuwen d9b165d
Make request nullable because in the unit/integration tests it might …
royteeuwen b3eb3e5
Made it possible to inject request parameters in a model and to adapt…
royteeuwen cab9f7a
Use servlet request for parameters as to make sure its always available
royteeuwen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
slice-mapper-api/src/main/java/com/cognifide/slice/mapper/annotation/RequestAttribute.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ""; | ||
} |
56 changes: 56 additions & 0 deletions
56
slice-mapper-api/src/main/java/com/cognifide/slice/mapper/annotation/RequestParameter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ""; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
...er/src/main/java/com/cognifide/slice/mapper/impl/processor/RequestAttributeProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
...er/src/main/java/com/cognifide/slice/mapper/impl/processor/RequestParameterProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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):Could you try with this approach?