use of org.apache.sling.api.request.RequestParameterMap in project sling by apache.
the class AbstractCreateOperation method generateName.
protected String generateName(SlingHttpServletRequest request, String basePath) throws PersistenceException {
// SLING-1091: If a :name parameter is supplied, the (first) value of this parameter is used unmodified as the name
// for the new node. If the name is illegally formed with respect to JCR name requirements, an exception will be
// thrown when trying to create the node. The assumption with the :name parameter is, that the caller knows what
// he (or she) is supplying and should get the exact result if possible.
RequestParameterMap parameters = request.getRequestParameterMap();
RequestParameter specialParam = parameters.getValue(SlingPostConstants.RP_NODE_NAME);
if (specialParam != null) {
if (specialParam.getString() != null && specialParam.getString().length() > 0) {
// If the path ends with a *, create a node under its parent, with
// a generated node name
basePath = basePath += "/" + specialParam.getString();
// if the resulting path already exists then report an error
if (request.getResourceResolver().getResource(basePath) != null) {
throw new PersistenceException("Collision in node names for path=" + basePath);
}
return basePath;
}
}
// no :name value was supplied, so generate a name
boolean requirePrefix = requireItemPathPrefix(request);
String generatedName = null;
if (extraNodeNameGenerators != null) {
for (NodeNameGenerator generator : extraNodeNameGenerators) {
generatedName = generator.getNodeName(request, basePath, requirePrefix, defaultNodeNameGenerator);
if (generatedName != null) {
break;
}
}
}
if (generatedName == null) {
generatedName = defaultNodeNameGenerator.getNodeName(request, basePath, requirePrefix, defaultNodeNameGenerator);
}
// If the path ends with a *, create a node under its parent, with
// a generated node name
basePath += "/" + generatedName;
basePath = ensureUniquePath(request, basePath);
return basePath;
}
use of org.apache.sling.api.request.RequestParameterMap in project sling by apache.
the class RequestPropertyTest method collectContent.
@SuppressWarnings("unchecked")
private Map<String, RequestProperty> collectContent(Param... kvs) throws Throwable {
final List<Map.Entry<String, RequestParameter>> params = new ArrayList<Map.Entry<String, RequestParameter>>();
for (int i = 0; i < kvs.length; i++) {
final Param kv = kvs[i];
final RequestParameter[] param = new RequestParameter[kv.value.length];
for (int j = 0; j < kv.value.length; j++) {
final String strValue = kv.value[j];
final RequestParameter aparam = context.mock(RequestParameter.class, "requestParameter" + i + "#" + j);
context.checking(new Expectations() {
{
allowing(aparam).getString();
will(returnValue(strValue));
}
});
param[j] = aparam;
}
final Map.Entry<String, RequestParameter> entry = context.mock(Map.Entry.class, "entry" + i);
context.checking(new Expectations() {
{
allowing(entry).getKey();
will(returnValue(kv.key));
allowing(entry).getValue();
will(returnValue(param));
}
});
params.add(entry);
}
final Set set = context.mock(Set.class);
context.checking(new Expectations() {
{
one(set).iterator();
will(returnValue(params.iterator()));
}
});
final RequestParameterMap map = context.mock(RequestParameterMap.class);
context.checking(new Expectations() {
{
one(map).entrySet();
will(returnValue(set));
}
});
final SlingHttpServletRequest request = context.mock(SlingHttpServletRequest.class);
context.checking(new Expectations() {
{
Vector names = new Vector();
names.add("./param");
one(request).getParameterNames();
will(returnValue(names.elements()));
one(request).getRequestParameterMap();
will(returnValue(map));
}
});
final HtmlResponse response = new HtmlResponse();
response.setPath("/test/path");
Map<String, RequestProperty> props = (Map<String, RequestProperty>) PrivateAccessor.invoke(new ModifyOperation(), "collectContent", COLLECT_CLASSES, new Object[] { request, response });
return props;
}
use of org.apache.sling.api.request.RequestParameterMap in project sling by apache.
the class DefaultNodeNameGenerator method getNodeName.
/**
* Get a "nice" node name, if possible, based on given request
*
* @param request the request
* @param basePath the base path
* @param requirePrefix <code>true</code> if the parameter names for
* properties requires a prefix
* @param defaultNodeNameGenerator a default generator
* @return a nice node name
*/
public String getNodeName(SlingHttpServletRequest request, String basePath, boolean requirePrefix, NodeNameGenerator defaultNodeNameGenerator) {
RequestParameterMap parameters = request.getRequestParameterMap();
String valueToUse = null;
boolean doFilter = true;
// our parameterNames, in order, and has a value
if (parameters != null) {
// we first check for the special sling parameters
RequestParameter specialParam = parameters.getValue(SlingPostConstants.RP_NODE_NAME);
if (specialParam != null) {
if (specialParam.getString() != null && specialParam.getString().length() > 0) {
valueToUse = specialParam.getString();
doFilter = false;
}
}
if (valueToUse == null) {
specialParam = parameters.getValue(SlingPostConstants.RP_NODE_NAME_HINT);
if (specialParam != null) {
if (specialParam.getString() != null && specialParam.getString().length() > 0) {
valueToUse = specialParam.getString();
}
}
}
if (valueToUse == null) {
for (String param : parameterNames) {
if (valueToUse != null) {
break;
}
if (requirePrefix) {
param = SlingPostConstants.ITEM_PREFIX_RELATIVE_CURRENT.concat(param);
}
final RequestParameter[] pp = parameters.get(param);
if (pp != null) {
for (RequestParameter p : pp) {
valueToUse = p.getString();
if (valueToUse != null && valueToUse.length() > 0) {
break;
}
valueToUse = null;
}
}
}
}
}
String result;
// should we filter?
if (valueToUse != null) {
if (doFilter) {
// filter value so that it works as a node name
result = filter.filter(valueToUse);
} else {
result = valueToUse;
}
} else {
// default value if none provided
result = nextCounter() + "_" + System.currentTimeMillis();
}
if (doFilter) {
// max length
if (result.length() > maxLength) {
result = result.substring(0, maxLength);
}
}
return result;
}
Aggregations