001// Copyright 2004, 2005 The Apache Software Foundation
002//
003// Licensed under the Apache License, Version 2.0 (the "License");
004// you may not use this file except in compliance with the License.
005// You may obtain a copy of the License at
006//
007//     http://www.apache.org/licenses/LICENSE-2.0
008//
009// Unless required by applicable law or agreed to in writing, software
010// distributed under the License is distributed on an "AS IS" BASIS,
011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012// See the License for the specific language governing permissions and
013// limitations under the License.
014
015package org.apache.tapestry.engine;
016
017import java.util.Map;
018
019import org.apache.hivemind.util.Defense;
020import org.apache.tapestry.util.QueryParameterMap;
021
022/**
023 * Implementation of {@link org.apache.tapestry.engine.ServiceEncoding}, which adds the ability to
024 * determine when the encoding has been modified.
025 * 
026 * @author Howard M. Lewis Ship
027 * @since 4.0
028 */
029public class ServiceEncodingImpl implements ServiceEncoding
030{
031    private String _servletPath;
032
033    private String _pathInfo;
034
035    /**
036     * Map of query parameter values; key is string name, value is either a string, an array of
037     * strings, or null. Could have done this with subclassing rather than delegation.
038     */
039
040    private final QueryParameterMap _parameters;
041
042    private boolean _modified;
043
044    public boolean isModified()
045    {
046        return _modified;
047    }
048
049    public void resetModified()
050    {
051        _modified = false;
052    }
053
054    /**
055     * Creates a new instance with a new map of parameters.
056     */
057
058    public ServiceEncodingImpl(String servletPath)
059    {
060        this(servletPath, null, new QueryParameterMap());
061    }
062
063    public ServiceEncodingImpl(String servletPath, Map parametersMap)
064    {
065        this(servletPath, null, new QueryParameterMap(parametersMap));
066    }
067
068    public ServiceEncodingImpl(String servletPath, String pathInfo, QueryParameterMap parameters)
069    {
070        Defense.notNull(servletPath, "servletPath");
071        Defense.notNull(parameters, "parameters");
072
073        _servletPath = servletPath;
074        _pathInfo = pathInfo;
075
076        _parameters = parameters;
077    }
078
079    public String getParameterValue(String name)
080    {
081        return _parameters.getParameterValue(name);
082    }
083
084    public String[] getParameterValues(String name)
085    {
086        return _parameters.getParameterValues(name);
087    }
088
089    public void setServletPath(String servletPath)
090    {
091        Defense.notNull(servletPath, "servletPath");
092
093        _servletPath = servletPath;
094        _modified = true;
095    }
096
097    public void setParameterValue(String name, String value)
098    {
099        _parameters.setParameterValue(name, value);
100
101        _modified = true;
102    }
103
104    public void setParameterValues(String name, String[] values)
105    {
106        _parameters.setParameterValues(name, values);
107
108        _modified = true;
109    }
110
111    public String getServletPath()
112    {
113        return _servletPath;
114    }
115
116    public String[] getParameterNames()
117    {
118        return _parameters.getParameterNames();
119    }
120
121    public String getPathInfo()
122    {
123        return _pathInfo;
124    }
125}