org.talend.daikon.http.HttpRequestContext.java Source code

Java tutorial

Introduction

Here is the source code for org.talend.daikon.http.HttpRequestContext.java

Source

//  ============================================================================
//
//  Copyright (C) 2006-2016 Talend Inc. - www.talend.com
//
//  This source code is available under agreement available at
//  https://github.com/Talend/data-prep/blob/master/LICENSE
//
//  You should have received a copy of the agreement
//  along with this program; if not, write to Talend SA
//  9 rue Pages 92150 Suresnes, France
//
//  ============================================================================

package org.talend.daikon.http;

import java.util.Collections;
import java.util.Enumeration;

import org.apache.commons.lang.StringUtils;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * A helper class around {@link RequestContextHolder}: allow simple modifications on HTTP context without worrying
 * whether code is called in a web context or not.
 */
public class HttpRequestContext {

    private HttpRequestContext() {
    }

    /**
     * @return All the current HTTP parameters in current HTTP request or empty enumeration if not in an HTTP context.
     */
    public static Enumeration<String> parameters() {
        final RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
        if (attributes != null && attributes instanceof ServletRequestAttributes) {
            return ((ServletRequestAttributes) attributes).getRequest().getParameterNames();
        }
        return Collections.emptyEnumeration();
    }

    /**
     * @param parameterName The HTTP parameter name.
     * @return All the requested HTTP parameter value in current HTTP request or empty string if not in an HTTP context.
     */
    public static String parameter(String parameterName) {
        final RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
        if (attributes != null && attributes instanceof ServletRequestAttributes) {
            return ((ServletRequestAttributes) attributes).getRequest().getParameter(parameterName);
        }
        return StringUtils.EMPTY;
    }

}