-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
1 parent
7e98ba4
commit 6ba0416
Showing
6 changed files
with
155 additions
and
6 deletions.
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/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
79 changes: 79 additions & 0 deletions
79
...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,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<String> getParameterValues(org.apache.sling.api.request.RequestParameter[] parameters) { | ||
List<String> result = new ArrayList<String>(); | ||
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(); | ||
} | ||
} |
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