Search in sources :

Example 1 with EmptyEnumeration

use of com.helger.commons.collection.iterate.EmptyEnumeration in project ph-web by phax.

the class RequestWebScope method initScope.

public final void initScope() {
    // Avoid double initialization of a scope, because for file uploads, the
    // parameters can only be extracted once!
    // As the parameters are stored directly in the HTTP request, we're not
    // loosing any data here!
    final IAttributeContainerAny<String> aAttrs = attrs();
    if (aAttrs.getAndSetFlag(REQUEST_ATTR_SCOPE_INITED)) {
        if (LOGGER.isWarnEnabled())
            LOGGER.warn("Scope was already inited: " + toString());
        return;
    }
    final IRequestParamContainer aParams = params();
    // where some extra items (like file items) handled?
    final boolean bAddedSpecialRequestParams = addSpecialRequestParams().isChanged();
    // Retrieve once (because locked)
    final IParamValueCleanser aParamValueCleanser = getParamValueCleanser();
    // set parameters as attributes (handles GET and POST parameters)
    // This may throw an exception, if the payload is invalid
    Enumeration<String> aEnum;
    try {
        aEnum = m_aHttpRequest.getParameterNames();
    } catch (final Exception ex) {
        aEnum = new EmptyEnumeration<>();
    }
    while (aEnum.hasMoreElements()) {
        final String sParamName = aEnum.nextElement();
        // String again
        if (bAddedSpecialRequestParams && aParams.containsKey(sParamName))
            continue;
        // Check if it is a single value or not
        final String[] aParamValues = m_aHttpRequest.getParameterValues(sParamName);
        final int nParamValues = aParamValues.length;
        if (nParamValues == 1) {
            // Convert from String[] to String
            String sValue = aParamValues[0];
            if (aParamValueCleanser != null) {
                // Adopt value if needed
                sValue = aParamValueCleanser.getCleanedValue(sParamName, 0, sValue);
            }
            aParams.putIn(sParamName, sValue);
        } else {
            // Use String[] as is
            final String[] aPreProcessedValues = new String[nParamValues];
            for (int i = 0; i < nParamValues; ++i) {
                String sValue = aParamValues[i];
                if (aParamValueCleanser != null) {
                    // Adopt value if needed
                    sValue = aParamValueCleanser.getCleanedValue(sParamName, i, sValue);
                }
                aPreProcessedValues[i] = sValue;
            }
            aParams.putIn(sParamName, aPreProcessedValues);
        }
    }
    // done initialization
    if (ScopeHelper.isDebugRequestScopeLifeCycle(LOGGER))
        if (LOGGER.isInfoEnabled())
            LOGGER.info("Initialized request web scope '" + getID() + "' of class " + ClassHelper.getClassLocalName(this), ScopeHelper.getDebugStackTrace());
}
Also used : IRequestParamContainer(com.helger.web.scope.IRequestParamContainer) EmptyEnumeration(com.helger.commons.collection.iterate.EmptyEnumeration)

Aggregations

EmptyEnumeration (com.helger.commons.collection.iterate.EmptyEnumeration)1 IRequestParamContainer (com.helger.web.scope.IRequestParamContainer)1