001/*
002 * (C) Copyright 2006-2007 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Nuxeo - initial API and implementation
018 *
019 * $Id: JOOoConvertPluginImpl.java 18651 2007-05-13 20:28:53Z sfermigier $
020 */
021
022package org.nuxeo.ecm.platform.ui.web.rest;
023
024import java.util.Collections;
025import java.util.Enumeration;
026import java.util.HashMap;
027import java.util.Map;
028
029import javax.servlet.http.HttpServletRequest;
030import javax.servlet.http.HttpServletRequestWrapper;
031
032import org.nuxeo.ecm.platform.url.api.DocumentView;
033
034/**
035 * TODO: document me. Used in get methods to get request params to the filter. Encapuslates the request into a wrapper
036 * to do so.
037 *
038 * @author tiry
039 */
040public class FancyURLRequestWrapper extends HttpServletRequestWrapper {
041
042    private DocumentView docView;
043
044    public FancyURLRequestWrapper(HttpServletRequest request) {
045        super(request);
046    }
047
048    public FancyURLRequestWrapper(HttpServletRequest request, DocumentView docView) {
049        super(request);
050        this.docView = docView;
051    }
052
053    @Override
054    @SuppressWarnings("unchecked")
055    public Map<String, String[]> getParameterMap() {
056        Map<String, String[]> result = super.getParameterMap();
057        if (result == null) {
058            result = new HashMap<String, String[]>();
059        } else {
060            // copy parameter map from parent class to avoid modifying the
061            // original
062            result = new HashMap<String, String[]>(result);
063        }
064        if (docView != null) {
065            for (Map.Entry<String, String> param : docView.getParameters().entrySet()) {
066                result.put(param.getKey(), new String[] { param.getValue() });
067            }
068        }
069        return result;
070    }
071
072    @Override
073    public String[] getParameterValues(String name) {
074        if (docView != null) {
075            String value = docView.getParameter(name);
076            if (value != null) {
077                return new String[] { value };
078            }
079        }
080        return super.getParameterValues(name);
081    }
082
083    @Override
084    public String getParameter(String name) {
085        if (docView != null) {
086            String value = docView.getParameter(name);
087            if (value != null) {
088                return value;
089            }
090        }
091        return super.getParameter(name);
092    }
093
094    @Override
095    public Enumeration<String> getParameterNames() {
096        return Collections.enumeration(getParameterMap().keySet());
097    }
098
099}