Last active
August 29, 2015 14:13
-
-
Save klcodanr/98d54be5088bb8dd082b to your computer and use it in GitHub Desktop.
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
package com.sixd.sly; | |
import java.util.ArrayList; | |
import java.util.Collection; | |
import java.util.Enumeration; | |
import java.util.HashSet; | |
import java.util.Map; | |
import java.util.Set; | |
import org.apache.sling.api.SlingHttpServletRequest; | |
public class RequestAttr implements Map<String, Object> { | |
private SlingHttpServletRequest request; | |
public RequestAttr(SlingHttpServletRequest request) { | |
this.request = request; | |
} | |
/* | |
* (non-Javadoc) | |
* | |
* @see java.util.Map#size() | |
*/ | |
@Override | |
public int size() { | |
int i = 0; | |
Enumeration<?> attrNames = request.getAttributeNames(); | |
while (attrNames.hasMoreElements()) { | |
i++; | |
attrNames.nextElement(); | |
} | |
return i; | |
} | |
/* | |
* (non-Javadoc) | |
* | |
* @see java.util.Map#isEmpty() | |
*/ | |
@Override | |
public boolean isEmpty() { | |
return size() > 0; | |
} | |
/* | |
* (non-Javadoc) | |
* | |
* @see java.util.Map#containsKey(java.lang.Object) | |
*/ | |
@Override | |
public boolean containsKey(Object key) { | |
Enumeration<?> attrNames = request.getAttributeNames(); | |
while (attrNames.hasMoreElements()) { | |
Object attr = attrNames.nextElement(); | |
if (attr.equals(key)) { | |
return true; | |
} | |
} | |
return false; | |
} | |
/* | |
* (non-Javadoc) | |
* | |
* @see java.util.Map#containsValue(java.lang.Object) | |
*/ | |
@Override | |
public boolean containsValue(Object value) { | |
Enumeration<?> attrNames = request.getAttributeNames(); | |
while (attrNames.hasMoreElements()) { | |
Object attr = attrNames.nextElement(); | |
if (request.getAttribute((String) attr).equals(value)) { | |
return true; | |
} | |
} | |
return false; | |
} | |
/* | |
* (non-Javadoc) | |
* | |
* @see java.util.Map#get(java.lang.Object) | |
*/ | |
@Override | |
public Object get(Object key) { | |
Object value = request.getAttribute((String) key); | |
return value; | |
} | |
/* | |
* (non-Javadoc) | |
* | |
* @see java.util.Map#put(java.lang.Object, java.lang.Object) | |
*/ | |
@Override | |
public Object put(String key, Object value) { | |
request.setAttribute((String) key, value); | |
return value; | |
} | |
/* | |
* (non-Javadoc) | |
* | |
* @see java.util.Map#remove(java.lang.Object) | |
*/ | |
@Override | |
public Object remove(Object key) { | |
Object oldValue = get(key); | |
request.removeAttribute((String) key); | |
return oldValue; | |
} | |
/* | |
* (non-Javadoc) | |
* | |
* @see java.util.Map#putAll(java.util.Map) | |
*/ | |
@Override | |
public void putAll(Map<? extends String, ? extends Object> m) { | |
for (Map.Entry<? extends String, ? extends Object> entry : m.entrySet()) { | |
put(entry.getKey(), entry.getValue()); | |
} | |
} | |
/* | |
* (non-Javadoc) | |
* | |
* @see java.util.Map#clear() | |
*/ | |
@Override | |
public void clear() { | |
throw new UnsupportedOperationException(); | |
} | |
/* | |
* (non-Javadoc) | |
* | |
* @see java.util.Map#keySet() | |
*/ | |
@Override | |
public Set<String> keySet() { | |
Set<String> keys = new HashSet<String>(); | |
Enumeration<?> attrNames = request.getAttributeNames(); | |
while (attrNames.hasMoreElements()) { | |
String attr = (String) attrNames.nextElement(); | |
keys.add(attr); | |
} | |
return keys; | |
} | |
/* | |
* (non-Javadoc) | |
* | |
* @see java.util.Map#values() | |
*/ | |
@Override | |
public Collection<Object> values() { | |
Collection<Object> values = new ArrayList<Object>(); | |
Enumeration<?> attrNames = request.getAttributeNames(); | |
while (attrNames.hasMoreElements()) { | |
String attr = (String) attrNames.nextElement(); | |
values.add(request.getAttribute(attr)); | |
} | |
return values; | |
} | |
/* | |
* (non-Javadoc) | |
* | |
* @see java.util.Map#entrySet() | |
*/ | |
@Override | |
public Set<java.util.Map.Entry<String, Object>> entrySet() { | |
Set<Map.Entry<String, Object>> entries = new HashSet<Map.Entry<String, Object>>(); | |
Enumeration<?> attrNames = request.getAttributeNames(); | |
while (attrNames.hasMoreElements()) { | |
final String attr = (String) attrNames.nextElement(); | |
Map.Entry<String, Object> entry = new Map.Entry<String, Object>() { | |
@Override | |
public String getKey() { | |
return attr; | |
} | |
@Override | |
public Object getValue() { | |
return request.getAttribute(attr); | |
} | |
@Override | |
public Object setValue(Object value) { | |
Object oldValue = getValue(); | |
request.setAttribute(attr, value); | |
return oldValue; | |
} | |
}; | |
entries.add(entry); | |
} | |
return entries; | |
} | |
} |
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
package com.sixd.sly.impl; | |
import org.apache.felix.scr.annotations.Component; | |
import org.apache.felix.scr.annotations.Properties; | |
import org.apache.felix.scr.annotations.Property; | |
import org.apache.felix.scr.annotations.Service; | |
import org.apache.sling.api.SlingHttpServletRequest; | |
import org.apache.sling.api.adapter.AdapterFactory; | |
import com.sixd.sly.RequestAttr; | |
/** | |
* Allows for adapting sling requests into a map. | |
* | |
* @author dklco | |
*/ | |
@Component(metatype = false) | |
@Service | |
@Properties({ | |
@Property(name = "adaptables", value = "org.apache.sling.api.SlingHttpServletRequest"), | |
@Property(name = "adapters", value = "com.sixd.sly.RequestAttr") }) | |
public class RequestAttrAdapterFactory implements AdapterFactory { | |
/* | |
* (non-Javadoc) | |
* | |
* @see | |
* org.apache.sling.api.adapter.AdapterFactory#getAdapter(java.lang.Object, | |
* java.lang.Class) | |
*/ | |
@Override | |
public <AdapterType> AdapterType getAdapter(Object adaptable, | |
Class<AdapterType> type) { | |
if (adaptable instanceof SlingHttpServletRequest) { | |
return type.cast(new RequestAttr( | |
(SlingHttpServletRequest) adaptable)); | |
} | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment