001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.wicket.request.target.component;
018:
019: import org.apache.wicket.Component;
020: import org.apache.wicket.Page;
021: import org.apache.wicket.PageParameters;
022: import org.apache.wicket.RequestCycle;
023: import org.apache.wicket.RequestListenerInterface;
024: import org.apache.wicket.WicketRuntimeException;
025: import org.apache.wicket.protocol.http.request.WebRequestCodingStrategy;
026: import org.apache.wicket.util.string.AppendingStringBuffer;
027: import org.apache.wicket.util.string.Strings;
028:
029: /**
030: * Request target for bookmarkable page links that also contain component path
031: * and interface name. This is used for stateless forms and stateless links.
032: *
033: * @author Matej Knopp
034: */
035: public class BookmarkableListenerInterfaceRequestTarget extends
036: BookmarkablePageRequestTarget {
037: private final String componentPath;
038: private final String interfaceName;
039:
040: /**
041: * This constructor is called when a stateless link is clicked on but the
042: * page wasn't found in the session. Then this class will recreate the page
043: * and call the interface method on the component that is targetted with the
044: * component path.
045: *
046: * @param pageMapName
047: * @param pageClass
048: * @param pageParameters
049: * @param componentPath
050: * @param interfaceName
051: */
052: public BookmarkableListenerInterfaceRequestTarget(
053: String pageMapName, Class pageClass,
054: PageParameters pageParameters, String componentPath,
055: String interfaceName) {
056: super (pageMapName, pageClass, pageParameters);
057: this .componentPath = componentPath;
058: this .interfaceName = interfaceName;
059: }
060:
061: /**
062: * This constructor is called for generating the urls
063: * (RequestCycle.urlFor()) So it will alter the PageParameters to include
064: * the 2 org.apache.wicket params
065: * {@link WebRequestCodingStrategy#BOOKMARKABLE_PAGE_PARAMETER_NAME} and
066: * {@link WebRequestCodingStrategy#INTERFACE_PARAMETER_NAME}
067: *
068: * @param pageMapName
069: * @param pageClass
070: * @param pageParameters
071: * @param component
072: * @param listenerInterface
073: */
074: public BookmarkableListenerInterfaceRequestTarget(
075: String pageMapName, Class pageClass,
076: PageParameters pageParameters, Component component,
077: RequestListenerInterface listenerInterface) {
078: this (pageMapName, pageClass, pageParameters, component
079: .getPath(), listenerInterface.getName());
080:
081: int version = component.getPage().getCurrentVersionNumber();
082:
083: // add the wicket:interface param to the params.
084: // pagemap:(pageid:componenta:componentb:...):version:interface:behavior:urlDepth
085: AppendingStringBuffer param = new AppendingStringBuffer(4
086: + componentPath.length() + interfaceName.length());
087: if (pageMapName != null) {
088: param.append(pageMapName);
089: }
090: param.append(Component.PATH_SEPARATOR);
091: param.append(getComponentPath());
092: param.append(Component.PATH_SEPARATOR);
093: if (version != 0) {
094: param.append(version);
095: }
096: // Interface
097: param.append(Component.PATH_SEPARATOR);
098: param.append(getInterfaceName());
099:
100: // Behavior (none)
101: param.append(Component.PATH_SEPARATOR);
102:
103: // URL depth (not required)
104: param.append(Component.PATH_SEPARATOR);
105:
106: pageParameters.put(
107: WebRequestCodingStrategy.INTERFACE_PARAMETER_NAME,
108: param.toString());
109: }
110:
111: public void processEvents(RequestCycle requestCycle) {
112: Page page = getPage(requestCycle);
113: final String pageRelativeComponentPath = Strings
114: .afterFirstPathComponent(componentPath,
115: Component.PATH_SEPARATOR);
116: Component component = page.get(pageRelativeComponentPath);
117: if (component == null) {
118: throw new WicketRuntimeException(
119: "unable to find component with path "
120: + pageRelativeComponentPath + " on page "
121: + page);
122: }
123: RequestListenerInterface listenerInterface = RequestListenerInterface
124: .forName(interfaceName);
125: if (listenerInterface == null) {
126: throw new WicketRuntimeException(
127: "unable to find listener interface "
128: + interfaceName);
129: }
130: listenerInterface.invoke(page, component);
131: }
132:
133: public void respond(RequestCycle requestCycle) {
134: getPage(requestCycle).renderPage();
135: }
136:
137: /**
138: * @return The component path.
139: */
140: public String getComponentPath() {
141: return componentPath;
142: }
143:
144: /**
145: * @return The interface name
146: */
147: public String getInterfaceName() {
148: return interfaceName;
149: }
150: }
|