All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.brutusin.jsonsrv.impl.JsonsrvHttpServletRequest Maven / Gradle / Ivy

/*
 * Copyright 2015 Ignacio del Valle Alles [email protected].
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.brutusin.jsonsrv.impl;

import java.util.Arrays;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

/**
 *
 * @author Ignacio del Valle Alles [email protected]
 */
public class JsonsrvHttpServletRequest extends HttpServletRequestWrapper {

    private final Map map;

    public JsonsrvHttpServletRequest(HttpServletRequest request, Map parameterMap) {
        super(request);
        if (request.getParameterMap() != null) {
            this.map = new HashMap(request.getParameterMap());
        } else {
            this.map = new HashMap();
        }
        if (parameterMap != null) {
            for (Map.Entry entry : parameterMap.entrySet()) {
                String param = entry.getKey();
                String[] values = entry.getValue();
                if (map.containsKey(param)) {
                    String[] reqValues = map.get(param);
                    List list = Arrays.asList(reqValues);
                    for (int i = 0; i < values.length; i++) {
                        list.add(values[i]);
                    }
                    map.put(param, list.toArray(new String[list.size()]));
                } else {
                    map.put(param, values);
                }
            }
        }
    }

    @Override
    public Map getParameterMap() {
        return map;
    }

    @Override
    public String getParameter(String name) {
        String[] array = getParameterValues(name);
        if (array == null || array.length == 0) {
            return null;
        }
        return array[0];
    }

    @Override
    public String[] getParameterValues(String name) {
        if (map == null) {
            return null;
        }
        String[] array = map.get(name);
        if (array == null) {
            return null;
        }
        return array;
    }

    @Override
    public Enumeration getParameterNames() {
        if (map == null) {
            return null;
        }
        final Iterator it = map.keySet().iterator();
        return new Enumeration() {
            public boolean hasMoreElements() {
                return it.hasNext();
            }

            public String nextElement() {
                return it.next();
            }
        };
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy