001: /*
002: * $Id: org.eclipse.jdt.ui.prefs 5004 2006-03-17 20:47:08 -0800 (Fri, 17 Mar 2006) eelco12 $
003: * $Revision: 5004 $
004: * $Date: 2006-03-17 20:47:08 -0800 (Fri, 17 Mar 2006) $
005: *
006: * ==============================================================================
007: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
008: * use this file except in compliance with the License. You may obtain a copy of
009: * the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
015: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
016: * License for the specific language governing permissions and limitations under
017: * the License.
018: */
019: package wicket.protocol.http.request.urlcompressing;
020:
021: import org.apache.commons.logging.Log;
022: import org.apache.commons.logging.LogFactory;
023:
024: import wicket.Component;
025: import wicket.IRedirectListener;
026: import wicket.IRequestTarget;
027: import wicket.Page;
028: import wicket.RequestCycle;
029: import wicket.RequestListenerInterface;
030: import wicket.WicketRuntimeException;
031: import wicket.authorization.UnauthorizedActionException;
032: import wicket.markup.html.INewBrowserWindowListener;
033: import wicket.markup.html.WebPage;
034: import wicket.protocol.http.WebApplication;
035: import wicket.protocol.http.request.urlcompressing.URLCompressor.ComponentAndInterface;
036: import wicket.request.RequestParameters;
037: import wicket.request.compound.DefaultRequestTargetResolverStrategy;
038: import wicket.request.target.component.listener.RedirectPageRequestTarget;
039: import wicket.util.string.Strings;
040:
041: /**
042: * Use this ResolverStrategy with the {@link WebURLCompressingCodingStrategy} to
043: * minimize the wicket:interface urls. The component path and the interface name
044: * will be removed from the url and only an uid will be inserted into the url.
045: *
046: * To use this url compressing behaviour you must override the
047: * {@link WebApplication} newRequestCycleProcessor() method. To make a request cycle
048: * processor with this ResolverStrategy and the {@link WebURLCompressingCodingStrategy}
049: *
050: * <pre>
051: * protected IRequestCycleProcessor newRequestCycleProcessor()
052: * {
053: * return new CompoundRequestCycleProcessor(new WebURLCompressingCodingStrategy(),new WebURLCompressingTargetResolverStrategy(),null,null,null);
054: * }
055: * </pre>
056: *
057: * @author jcompagner
058: *
059: * @since 1.2
060: */
061: public class WebURLCompressingTargetResolverStrategy extends
062: DefaultRequestTargetResolverStrategy {
063: /** log. */
064: private static final Log log = LogFactory
065: .getLog(WebURLCompressingTargetResolverStrategy.class);
066:
067: /**
068: * Resolves the RequestTarget for the given interface. This method can be
069: * overriden if some special interface needs to resolve to its own target.
070: *
071: * @param requestCycle
072: * The current RequestCycle object
073: * @param page
074: * The page object which holds the component for which this
075: * interface is called on.
076: * @param componentPath
077: * The component path for looking up the component in the page.
078: * @param interfaceName
079: * The interface to resolve.
080: * @param requestParameters
081: * @return The RequestTarget that was resolved
082: */
083: protected IRequestTarget resolveListenerInterfaceTarget(
084: final RequestCycle requestCycle, final Page page,
085: final String componentPath, String interfaceName,
086: final RequestParameters requestParameters) {
087: String pageRelativeComponentPath = Strings
088: .afterFirstPathComponent(componentPath,
089: Component.PATH_SEPARATOR);
090: Component component = null;
091: if (page instanceof WebPage
092: && !"IResourceListener".equals(interfaceName)) {
093: ComponentAndInterface cai = ((WebPage) page)
094: .getUrlCompressor().getComponentAndInterfaceForUID(
095: pageRelativeComponentPath);
096: if (cai != null) {
097: interfaceName = cai.getInterfaceName();
098: component = cai.getComponent();
099: }
100: }
101:
102: if (interfaceName.equals(IRedirectListener.INTERFACE.getName())) {
103: return new RedirectPageRequestTarget(page);
104: } else if (interfaceName
105: .equals(INewBrowserWindowListener.INTERFACE.getName())) {
106: return INewBrowserWindowListener.INTERFACE
107: .newRequestTarget(page, page,
108: INewBrowserWindowListener.INTERFACE,
109: requestParameters);
110: } else {
111: // Get the listener interface we need to call
112: final RequestListenerInterface listener = RequestListenerInterface
113: .forName(interfaceName);
114: if (listener == null) {
115: throw new WicketRuntimeException(
116: "Attempt to access unknown request listener interface "
117: + interfaceName);
118: }
119:
120: // Get component
121: if (component == null) {
122: if (Strings.isEmpty(pageRelativeComponentPath)) {
123: // We have an interface that is not a redirect, but no
124: // component... that must be wrong
125: throw new WicketRuntimeException(
126: "When trying to call " + listener
127: + ", a component must be provided");
128: }
129: component = page.get(pageRelativeComponentPath);
130: }
131: if (!component.isEnableAllowed()) {
132: throw new UnauthorizedActionException(component,
133: Component.ENABLE);
134: }
135:
136: // Ask the request listener interface object to create a request target
137: return listener.newRequestTarget(page, component, listener,
138: requestParameters);
139: }
140: }
141: }
|