Search in sources :

Example 16 with RequestParameter

use of org.apache.sling.api.request.RequestParameter in project sling by apache.

the class ParameterMap method renameParameter.

void renameParameter(String oldName, String newName) {
    RequestParameter[] params = super.remove(oldName);
    for (RequestParameter param : params) {
        ((AbstractRequestParameter) param).setName(newName);
    }
    super.put(newName, params);
}
Also used : RequestParameter(org.apache.sling.api.request.RequestParameter)

Example 17 with RequestParameter

use of org.apache.sling.api.request.RequestParameter 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;
}
Also used : RequestParameterMap(org.apache.sling.api.request.RequestParameterMap) RequestParameter(org.apache.sling.api.request.RequestParameter)

Example 18 with RequestParameter

use of org.apache.sling.api.request.RequestParameter in project sling by apache.

the class SlingFileUploadHandler method setFile.

/**
     * Uses the file(s) in the request parameter for creation of new nodes.
     * if the parent node is a nt:folder a new nt:file is created. otherwise
     * just a nt:resource. if the <code>name</code> is '*', the filename of
     * the uploaded file is used.
     *
     * @param parent the parent node
     * @param prop the assembled property info
     * @throws PersistenceException if an error occurs
     */
public void setFile(final Resource parent, final RequestProperty prop, final List<Modification> changes) throws PersistenceException {
    for (final RequestParameter value : prop.getValues()) {
        // ignore if a plain form field or empty
        if (value.isFormField() || value.getSize() <= 0) {
            continue;
        }
        // get node name
        String name = prop.getName();
        if (name.equals("*")) {
            name = value.getFileName();
            // strip of possible path (some browsers include the entire path)
            name = name.substring(name.lastIndexOf('/') + 1);
            name = name.substring(name.lastIndexOf('\\') + 1);
        }
        name = Text.escapeIllegalJcrChars(name);
        // get content type
        String contentType = value.getContentType();
        if (contentType != null) {
            int idx = contentType.indexOf(';');
            if (idx > 0) {
                contentType = contentType.substring(0, idx);
            }
        }
        if (contentType == null || contentType.equals(MT_APP_OCTET)) {
            // try to find a better content type
            final ServletContext ctx = this.servletContext;
            if (ctx != null) {
                contentType = ctx.getMimeType(value.getFileName());
            }
            if (contentType == null) {
                contentType = MT_APP_OCTET;
            }
        }
        this.setFile(parent, prop, value, changes, name, contentType);
    }
}
Also used : RequestParameter(org.apache.sling.api.request.RequestParameter) ServletContext(javax.servlet.ServletContext)

Example 19 with RequestParameter

use of org.apache.sling.api.request.RequestParameter in project acs-aem-commons by Adobe-Consulting-Services.

the class RequestChecksumGeneratorOptions method getPaths.

public static Set<String> getPaths(SlingHttpServletRequest request) throws IOException {
    Set<String> paths = new HashSet<String>();
    if (request.getParameterValues(PATHS) != null) {
        String[] pathArr = request.getParameterValues(PATHS);
        for (String path : pathArr) {
            if (path.length() > 0) {
                paths.add(path);
            }
        }
    }
    paths.addAll(getPathsFromQuery(request.getResourceResolver(), request.getParameter(QUERY_TYPE), request.getParameter(QUERY)));
    RequestParameter data = request.getRequestParameter(DATA);
    if (data != null && data.getInputStream() != null) {
        paths.addAll(getPathsFromInputstream(data.getInputStream(), request.getCharacterEncoding()));
    }
    return paths;
}
Also used : RequestParameter(org.apache.sling.api.request.RequestParameter) HashSet(java.util.HashSet)

Example 20 with RequestParameter

use of org.apache.sling.api.request.RequestParameter in project acs-aem-commons by Adobe-Consulting-Services.

the class WCMViewsFilter method getRequestViews.

/**
 * Get the WCM Views from the Request passed by QueryParam.
 * *
 *
 * @param request the request
 * @return the WCM Views from the Request
 */
private List<String> getRequestViews(final SlingHttpServletRequest request) {
    final List<String> views = new ArrayList<String>();
    // Respect Query Parameters first
    final RequestParameter[] requestParameters = request.getRequestParameters(RP_WCM_VIEWS);
    if (requestParameters != null) {
        for (final RequestParameter requestParameter : requestParameters) {
            if (StringUtils.isNotBlank(requestParameter.getString())) {
                views.add(requestParameter.getString());
            }
        }
    }
    if (CollectionUtils.isNotEmpty(views)) {
        return views;
    }
    // If not Query Params can be found, check Cookie
    final Cookie cookie = CookieUtil.getCookie(request, COOKIE_WCM_VIEWS);
    if (cookie != null && StringUtils.isNotBlank(cookie.getValue())) {
        views.add(cookie.getValue());
    }
    return views;
}
Also used : Cookie(javax.servlet.http.Cookie) RequestParameter(org.apache.sling.api.request.RequestParameter) ArrayList(java.util.ArrayList)

Aggregations

RequestParameter (org.apache.sling.api.request.RequestParameter)25 RequestParameterMap (org.apache.sling.api.request.RequestParameterMap)8 Map (java.util.Map)5 InputStream (java.io.InputStream)4 HashMap (java.util.HashMap)4 Resource (org.apache.sling.api.resource.Resource)4 RequestProperty (org.apache.sling.servlets.post.impl.helper.RequestProperty)4 ArrayList (java.util.ArrayList)3 Node (javax.jcr.Node)3 Session (javax.jcr.Session)3 PersistenceException (org.apache.sling.api.resource.PersistenceException)3 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)3 PageManager (com.day.cq.wcm.api.PageManager)2 IOException (java.io.IOException)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2 PostConstruct (javax.annotation.PostConstruct)2 SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)2 ModifiableValueMap (org.apache.sling.api.resource.ModifiableValueMap)2 ValueMap (org.apache.sling.api.resource.ValueMap)2