/** * Returns a request parameter as long. * @param request Request. * @param param Parameter name. * @return Parameter value or 0 if it does not exist or is not a number. */ public static long getLong(@NotNull ServletRequest request, @NotNull String param) { return getLong(request, param, 0L); }
/** * Returns a request parameter as integer. * @param request Request. * @param param Parameter name. * @return Parameter value or 0 if it does not exist or is not a number. */ public static int getInt(@NotNull ServletRequest request, @NotNull String param) { return getInt(request, param, 0); }
/** * Returns a request parameter as double. * @param request Request. * @param param Parameter name. * @return Parameter value or 0 if it does not exist or is not a number. */ public static double getDouble(@NotNull ServletRequest request, @NotNull String param) { return getDouble(request, param, 0d); }
/** * Returns a request parameter.<br> * In addition the method fixes problems with incorrect UTF-8 characters returned by the servlet engine. * All character data is converted from ISO-8859-1 to UTF-8 if not '_charset_' parameter is provided. * @param request Request. * @param param Parameter name. * @return Parameter value or null if it is not set. */ public static @Nullable String get(@NotNull ServletRequest request, @NotNull String param) { return get(request, param, null); }
private static List<AssetRequest> getAssetRequestsFromUrlParams(String assetPath, SlingHttpServletRequest request) { String[] mediaFormats = ObjectUtils.defaultIfNull(RequestParam.getMultiple(request, RP_MEDIAFORMAT), new String[0]); String[] widthStrings = ObjectUtils.defaultIfNull(RequestParam.getMultiple(request, RP_WIDTH), new String[0]); String[] heightStrings = ObjectUtils.defaultIfNull(RequestParam.getMultiple(request, RP_HEIGHT), new String[0]); int maxParamIndex = NumberUtils.max(mediaFormats.length, widthStrings.length, heightStrings.length); List<AssetRequest> requests = new ArrayList<>(); for (int i = 0; i < maxParamIndex; i++) { String mediaFormat = mediaFormats.length > i ? mediaFormats[i] : null; long width = widthStrings.length > i ? NumberUtils.toLong(widthStrings[i]) : 0; long height = heightStrings.length > i ? NumberUtils.toLong(heightStrings[i]) : 0; requests.add(new AssetRequest(assetPath, mediaFormat, width, height)); } return requests; }
/** * Returns a request parameter.<br> * In addition the method fixes problems with incorrect UTF-8 characters returned by the servlet engine. * All character data is converted from ISO-8859-1 to UTF-8. * @param requestMap Request Parameter map. * @param param Parameter name. * @return Parameter value or null if it is not set. */ public static @Nullable String get(@NotNull Map<String, String[]> requestMap, @NotNull String param) { String value = null; String[] valueArray = requestMap.get(param); if (valueArray != null && valueArray.length > 0) { value = valueArray[0]; } // convert encoding to UTF-8 if not form encoding parameter is set if (value != null && !hasFormEncodingParam(requestMap)) { value = convertISO88591toUTF8(value); } return value; }
/** * Detect integrator template modes - check selectors in current url. */ private void detectIntegratorTemplateModes() { // integrator mode cannot be active if no modes defined if (urlHandlerConfig.getIntegratorModes().isEmpty()) { return; } if (request != null && RequestPath.hasSelector(request, SELECTOR_INTEGRATORTEMPLATE_SECURE)) { integratorTemplateSecureMode = true; } else if (request != null && RequestPath.hasSelector(request, SELECTOR_INTEGRATORTEMPLATE)) { integratorTemplateMode = true; } }
@Override public Map<String, String> getOverrideMap() { if (this.enabled) { if (requestContext != null) { SlingHttpServletRequest request = requestContext.getThreadRequest(); if (request != null) { return buildMapFromHeaders(request); } } else { log.warn("RequestContext service not running - unable to inspect current request."); } } return ImmutableMap.<String, String>of(); }
/** * Add parameter to query string. * @param name Parameter name * @param value Parameter value. Will be converted to string. * If value is an array or {@link Iterable} the value items will be added as separate parameters. * @return this */ @SuppressWarnings("unchecked") public @NotNull QueryStringBuilder param(@NotNull String name, @Nullable Object value) { if (value instanceof Iterable) { Iterable<Object> valueItems = (Iterable)value; for (Object valueItem : valueItems) { params.add(new NameValuePair(name, valueItem)); } } else if (isArray(value)) { int length = Array.getLength(value); for (int i = 0; i < length; i++) { Object valueItem = Array.get(value, i); params.add(new NameValuePair(name, valueItem)); } } else { params.add(new NameValuePair(name, value)); } return this; }
/** * Build query string. * @return Query string or null if query string contains no parameters at all. */ public @Nullable String build() { StringBuilder queryString = new StringBuilder(); for (NameValuePair param : params) { if (queryString.length() > 0) { queryString.append(PARAM_SEPARATOR); } queryString.append(Escape.urlEncode(param.getName())); queryString.append(VALUE_SEPARATOR); queryString.append(Escape.urlEncode(param.getValue())); } if (queryString.length() > 0) { return queryString.toString(); } else { return null; } }
/** * Returns a request parameter as boolean. * @param request Request. * @param param Parameter name. * @return Parameter value or <code>false</code> if it does not exist or cannot be interpreted as boolean. */ public static boolean getBoolean(@NotNull ServletRequest request, @NotNull String param) { return getBoolean(request, param, false); }
/** * Returns a request parameter as float. * @param request Request. * @param param Parameter name. * @return Parameter value or 0 if it does not exist or is not a number. */ public static float getFloat(@NotNull ServletRequest request, @NotNull String param) { return getFloat(request, param, 0f); }
/** * Returns a request parameter as enum value. * @param <T> Enum type * @param request Request. * @param param Parameter name. * @param enumClass Enum class * @return Parameter value or null if it is not set or an invalid enum value. */ public static <T extends Enum> @Nullable T getEnum(@NotNull ServletRequest request, @NotNull String param, @NotNull Class<T> enumClass) { return getEnum(request, param, enumClass, null); }
/** * Add map of parameters to query string. * @param values Map with parameter names and values. Values will be converted to strings. * If a value is an array or {@link Iterable} the value items will be added as separate parameters. * @return this */ public @NotNull QueryStringBuilder params(@NotNull Map<String, Object> values) { for (Map.Entry<String, Object> entry : values.entrySet()) { param(entry.getKey(), entry.getValue()); } return this; }
/** * Determine root resource to list its children. (use resource for root page because root node does not have to be a * page but can be e.g. a nt:folder node) * @param request Request * @return Root resource or null if invalid resource was referenced */ protected final Resource getRootResource(SlingHttpServletRequest request) { String path = RequestParam.get(request, RP_PATH); if (StringUtils.isEmpty(path) && request.getResource() != null) { path = request.getResource().getPath(); } if (StringUtils.isNotEmpty(path)) { path = StringUtils.removeEnd(path, "/" + JcrConstants.JCR_CONTENT); return request.getResourceResolver().getResource(path); } return null; }
@Override protected PageFilter getPageFilter(SlingHttpServletRequest request) { Resource rootResource = getRootResource(request); String[] templates = RequestParam.getMultiple(request, RP_TEMPLATE); Set<String> pagePathsToFollow = getPagePathsForTemplate(templates, rootResource.getPath(), request); return new PathPageFilter(pagePathsToFollow); }
/** * Returns a request parameter.<br> * In addition the method fixes problems with incorrect UTF-8 characters returned by the servlet engine. * All character data is converted from ISO-8859-1 to UTF-8 if not '_charset_' parameter is provided. * @param request Request. * @param param Parameter name. * @param defaultValue Default value. * @return Parameter value or the default value if it is not set. */ public static @Nullable String get(@NotNull ServletRequest request, @NotNull String param, @Nullable String defaultValue) { String value = request.getParameter(param); if (value != null) { // convert encoding to UTF-8 if not form encoding parameter is set if (!hasFormEncodingParam(request)) { value = convertISO88591toUTF8(value); } return value; } else { return defaultValue; } }
/** * Checks whether resource URLs should be rendered in secure mode or not. * @return true if resource URLs should be rendered in secure mode */ private boolean isResourceUrlSecure(Page page) { ValueMap props = getPagePropertiesNullSafe(page); IntegratorMode mode = getIntegratorMode(props); if (mode.isDetectProtocol()) { IntegratorProtocol integratorProtocol = getIntegratorProtocol(props); if (integratorProtocol == IntegratorProtocol.HTTPS) { return true; } else if (integratorProtocol == IntegratorProtocol.AUTO) { return RequestPath.hasSelector(request, IntegratorHandler.SELECTOR_INTEGRATORTEMPLATE_SECURE); } } return false; }
@Override public @NotNull Collection<String> getOverrideStrings() { if (config.enabled()) { if (requestContext != null) { SlingHttpServletRequest request = requestContext.getThreadRequest(); if (request != null) { return buildMapFromHeaders(request); } } else { log.warn("RequestContext service not running - unable to inspect current request."); } } return ImmutableList.<String>of(); }
/** * Returns a request parameter array.<br> * The method fixes problems with incorrect UTF-8 characters returned by the servlet engine. * All character data is converted from ISO-8859-1 to UTF-8. * @param request Request. * @param param Parameter name. * @return Parameter value array value or null if it is not set. */ public static String @Nullable [] getMultiple(@NotNull ServletRequest request, @NotNull String param) { String[] values = request.getParameterValues(param); if (values == null) { return null; } // convert encoding to UTF-8 if not form encoding parameter is set if (!hasFormEncodingParam(request)) { String[] convertedValues = new String[values.length]; for (int i = 0; i < values.length; i++) { if (values[i] != null) { convertedValues[i] = convertISO88591toUTF8(values[i]); } } return convertedValues; } else { return values; } }