001: // Copyright 2006, 2007 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:
015: package org.apache.tapestry.internal.services;
016:
017: import static org.apache.tapestry.ioc.internal.util.CollectionFactory.newMap;
018: import static org.apache.tapestry.ioc.internal.util.Defense.notBlank;
019:
020: import java.util.List;
021: import java.util.Map;
022:
023: import org.apache.tapestry.internal.TapestryInternalUtils;
024: import org.apache.tapestry.ioc.internal.util.InternalUtils;
025: import org.apache.tapestry.test.PageTester;
026:
027: /**
028: * Represents an invocation for a page or a component in the current application. This information
029: * is extracted from incoming URLs for a running application (or created by the {@link PageTester}.
030: * Each invocation may provide a context (Object[]) and parameters to the invocation target.
031: */
032: public class ComponentInvocation {
033: private final String[] _context;
034:
035: private final InvocationTarget _target;
036:
037: private final String[] _activationContext;
038:
039: private Map<String, String> _parameters;
040:
041: /**
042: * @param target
043: * identifies the target of the event: a component with a page
044: * @param context
045: * context information supplied by the component to be provided back when the event
046: * on the component is triggered, or contains the activation context when the
047: * invocation is for a page render request
048: * @param activationContext
049: * page activation context for the page containing the component, supplied via a
050: * passivate event to the page's root component (used when an action component
051: * invocation is for a page with an activation context)
052: */
053: public ComponentInvocation(InvocationTarget target,
054: String[] context, String[] activationContext) {
055: _target = target;
056: _context = context;
057: _activationContext = activationContext;
058: }
059:
060: /**
061: * @return A path taking the format <em>target-path</em>/e1/e2?&q1=v1&q2=v2. where the
062: * <em>target-path</em> is the path provided by the invocation target; e1 and e2 are
063: * elements of the context; q1 and q2 are the parameters.
064: */
065: public String buildURI(boolean isForm) {
066: String path = getPath();
067: if (isForm || _parameters == null)
068: return path;
069:
070: StringBuilder builder = new StringBuilder();
071:
072: builder.append(path);
073:
074: String sep = "?";
075:
076: for (String name : getParameterNames()) {
077: String value = _parameters.get(name);
078:
079: builder.append(sep);
080:
081: // We assume that the name is URL safe and that the value will already have been URL
082: // encoded if it is not known to be URL safe.
083:
084: builder.append(name);
085: builder.append("=");
086: builder.append(value);
087:
088: sep = "&";
089: }
090:
091: return builder.toString();
092: }
093:
094: /**
095: * @return Just like the return value of {@link #buildURI(boolean)} except that parameters are
096: * not included.
097: */
098: private String getPath() {
099: StringBuilder builder = new StringBuilder();
100: builder.append(_target.getPath());
101:
102: for (String id : _context) {
103: builder.append("/");
104:
105: builder.append(TapestryInternalUtils.urlEncode(id));
106: }
107:
108: return builder.toString();
109: }
110:
111: public String[] getContext() {
112: return _context;
113: }
114:
115: public String[] getActivationContext() {
116: return _activationContext;
117: }
118:
119: public void addParameter(String parameterName, String value) {
120: notBlank(parameterName, "parameterName");
121: notBlank(value, "value");
122:
123: if (_parameters == null)
124: _parameters = newMap();
125:
126: if (_parameters.containsKey(parameterName))
127: throw new IllegalArgumentException(ServicesMessages
128: .parameterNameMustBeUnique(parameterName,
129: _parameters.get(parameterName)));
130:
131: _parameters.put(parameterName, value);
132: }
133:
134: public List<String> getParameterNames() {
135: return InternalUtils.sortedKeys(_parameters);
136: }
137:
138: public String getParameterValue(String name) {
139: return InternalUtils.get(_parameters, name);
140: }
141:
142: public InvocationTarget getTarget() {
143: return _target;
144: }
145: }
|