📄 webrequestcodingstrategy.java
字号:
{ IRequestTargetUrlCodingStrategy strategy = mountsOnPath.strategyForPath(path); if (strategy != null) { return strategy; } } } return null; } /** * @see org.apache.wicket.request.IRequestTargetMounter#mount( * org.apache.wicket.request.target.coding.IRequestTargetUrlCodingStrategy) */ public final void mount(IRequestTargetUrlCodingStrategy encoder) { if (encoder == null) { throw new IllegalArgumentException("Argument encoder must not be null"); } String path = encoder.getMountPath(); if (path == null) { throw new IllegalArgumentException("Argument path must not be null"); } if (path.equals("/") || path.equals("")) { throw new IllegalArgumentException( "The mount path '/' is reserved for the application home page"); } // Sanity check in case someone doesn't read the javadoc while // implementing IRequestTargetUrlCodingStrategy if (path.startsWith("/")) { path = path.substring(1); } synchronized (mountsOnPath) { if (mountsOnPath.strategyForMount(path) != null) { throw new WicketRuntimeException(path + " is already mounted for " + mountsOnPath.strategyForMount(path)); } mountsOnPath.mount(path, encoder); } } /** * @see org.apache.wicket.request.IRequestCodingStrategy#pathForTarget(org.apache.wicket.IRequestTarget) */ public final CharSequence pathForTarget(IRequestTarget requestTarget) { // first check whether the target was mounted IRequestTargetUrlCodingStrategy encoder = getMountEncoder(requestTarget); if (encoder != null) { return encoder.encode(requestTarget); } return null; } /** * @see org.apache.wicket.request.IRequestCodingStrategy#targetForRequest(org.apache.wicket.request.RequestParameters) */ public final IRequestTarget targetForRequest(RequestParameters requestParameters) { IRequestTargetUrlCodingStrategy encoder = urlCodingStrategyForPath(requestParameters .getPath()); if (encoder == null) { return null; } return encoder.decode(requestParameters); } /** * @see org.apache.wicket.request.IRequestCodingStrategy#unmount(java.lang.String) */ public final void unmount(String path) { if (path == null) { throw new IllegalArgumentException("Argument path must be not-null"); } // sanity check if (path.startsWith("/")) { path = path.substring(1); } synchronized (mountsOnPath) { mountsOnPath.unmount(path); } } /** * Adds bookmarkable page related parameters (page alias and optionally page parameters). Any * bookmarkable page alias mount will override this method; hence if a mount is found, this * method will not be called. * * If you override this method to behave differently then * {@link #encode(RequestCycle, IBookmarkablePageRequestTarget)} should also be overridden to be * in sync with that behavior. * * @param request * the incoming request * @param parameters * the parameters object to set the found values on */ protected void addBookmarkablePageParameters(final Request request, final RequestParameters parameters) { final String requestString = request .getParameter(WebRequestCodingStrategy.BOOKMARKABLE_PAGE_PARAMETER_NAME); if (requestString != null) { final String[] components = Strings.split(requestString, Component.PATH_SEPARATOR); if (components.length != 2) { throw new WicketRuntimeException("Invalid bookmarkablePage parameter: " + requestString + ", expected: 'pageMapName:pageClassName'"); } // Extract any pagemap name final String pageMapName = components[0]; parameters.setPageMapName(pageMapName.length() == 0 ? PageMap.DEFAULT_NAME : pageMapName); // Extract bookmarkable page class name final String pageClassName = components[1]; parameters.setBookmarkablePageClass(pageClassName); } } /** * Adds page related parameters (path and pagemap and optionally version and interface). * * If you override this method to behave different then also * {@link #encode(RequestCycle, IListenerInterfaceRequestTarget)} should be overridden to be in * sync with that behavior. * * @param request * the incoming request * @param parameters * the parameters object to set the found values on */ protected void addInterfaceParameters(final Request request, final RequestParameters parameters) { addInterfaceParameters(request.getParameter(INTERFACE_PARAMETER_NAME), parameters); } /** * Analyzes the passed in interfaceParameter for the relevant parts and puts the parts as * parameters in the provided request parameters object. * * @param interfaceParameter * The format of the interfaceParameter is: <code> * page-map-name:path:version:interface:behaviourId:urlDepth * </code> * @param parameters * parameters object to set the found parts in */ public static void addInterfaceParameters(final String interfaceParameter, final RequestParameters parameters) { if (interfaceParameter == null) { return; } // Split into array of strings String[] pathComponents = Strings.split(interfaceParameter, Component.PATH_SEPARATOR); // There must be 6 components // pagemap:(pageid:componenta:componentb:...):version:interface:behavior:depth if (pathComponents.length < 6) { throw new WicketRuntimeException("Internal error parsing " + INTERFACE_PARAMETER_NAME + " = " + interfaceParameter); } // Extract version String versionNumberString = null; try { versionNumberString = pathComponents[pathComponents.length - 4]; final int versionNumber = Strings.isEmpty(versionNumberString) ? 0 : Integer .parseInt(versionNumberString); parameters.setVersionNumber(versionNumber); } catch (NumberFormatException e) { throw new WicketRuntimeException("Internal error parsing " + INTERFACE_PARAMETER_NAME + " = " + interfaceParameter + "; wrong format for page version argument. Expected a number but was '" + versionNumberString + "'", e); } // Set pagemap name final String pageMapName = pathComponents[0]; parameters.setPageMapName(pageMapName.length() == 0 ? PageMap.DEFAULT_NAME : pageMapName); // Extract URL depth after last colon final String urlDepthString = pathComponents[pathComponents.length - 1]; final int urlDepth; try { urlDepth = Strings.isEmpty(urlDepthString) ? -1 : Integer.parseInt(urlDepthString); } catch (NumberFormatException e) { throw new WicketRuntimeException("Internal error parsing " + INTERFACE_PARAMETER_NAME + " = " + interfaceParameter + "; wrong format for url depth argument. Expected a number but was '" + urlDepthString + "'", e); } parameters.setUrlDepth(urlDepth); // Extract behavior ID after last colon final String behaviourId = pathComponents[pathComponents.length - 2]; parameters.setBehaviorId(behaviourId.length() != 0 ? behaviourId : null); // Extract interface name after second-to-last colon final String interfaceName = pathComponents[pathComponents.length - 3]; parameters.setInterfaceName(interfaceName.length() != 0 ? interfaceName : IRedirectListener.INTERFACE.getName()); // Component path is everything after pageMapName and before version final int start = pageMapName.length() + 1; final int end = interfaceParameter.length() - behaviourId.length() - interfaceName.length() - versionNumberString.length() - urlDepthString.length() - 4; final String componentPath = interfaceParameter.substring(start, end); parameters.setComponentPath(componentPath); } /** * Adds (shared) resource related parameters (resource key). Any shared resource key mount will * override this method; hence if a mount is found, this method will not be called. * * If you override this method to behave different then also * {@link #encode(RequestCycle, ISharedResourceRequestTarget)} should be overridden to be in * sync with that behavior. * * @param request * the incoming request * @param parameters * the parameters object to set the found values on */ protected void addResourceParameters(Request request, RequestParameters parameters) { String pathInfo = request.getPath(); if (pathInfo != null && pathInfo.startsWith(RESOURCES_PATH_PREFIX)) { int ix = RESOURCES_PATH_PREFIX.length(); if (pathInfo.length() > ix) { StringBuffer path = new StringBuffer(pathInfo.substring(ix)); int ixSemiColon = path.indexOf(";"); // strip off any jsession id if (ixSemiColon != -1) { int ixEnd = path.indexOf("?"); if (ixEnd == -1) { ixEnd = path.length(); } path.delete(ixSemiColon, ixEnd); } parameters.setResourceKey(path.toString()); } } } /** * In case you are using custom targets that are not part of the default target hierarchy, you * need to override this method, which will be called after the defaults have been tried. When * this doesn't provide a url either (returns null), an exception will be thrown by the encode * method saying that encoding could not be done. * * @param requestCycle * the current request cycle (for efficient access) * * @param requestTarget * the request target * @return the url to the provided target, as a relative path from the filter root. */ protected String doEncode(RequestCycle requestCycle, IRequestTarget requestTarget) { return null; } /** * Encode a page class target. * * If you override this method to behave different then also * {@link #addBookmarkablePageParameters(Request, RequestParameters)} should be overridden to be * in sync with that behavior. * * @param requestCycle * the current request cycle * @param requestTarget * the target to encode * @return the encoded url */ protected CharSequence encode(RequestCycle requestCycle, IBookmarkablePageRequestTarget requestTarget) { // Begin encoding URL final AppendingStringBuffer url = new AppendingStringBuffer(64); // Get page Class final Class pageClass = requestTarget.getPageClass(); final Application application = Application.get(); // Find pagemap name String pageMapName = requestTarget.getPageMapName(); if (pageMapName == null) { IRequestTarget currentTarget = requestCycle.getRequestTarget(); if (currentTarget instanceof IPageRequestTarget) { Page currentPage = ((IPageRequestTarget)currentTarget).getPage(); final IPageMap pageMap = currentPage.getPageMap(); if (pageMap.isDefault()) { pageMapName = ""; } else { pageMapName = pageMap.getName(); } } else { pageMapName = ""; } } WebRequestEncoder encoder = new WebRequestEncoder(url); if (!application.getHomePage().equals(pageClass) || !"".equals(pageMapName) || (application.getHomePage().equals(pageClass) && requestTarget instanceof BookmarkableListenerInterfaceRequestTarget)) { /* * Add <page-map-name>:<bookmarkable-page-class> * * Encode the url so it is correct even for class names containing non ASCII characters, * like ä, æ, ø, å etc. * * The reason for this is that when redirecting to these bookmarkable pages, we need to * have the url encoded correctly because we can't rely on the browser to interpret the * unencoded url correctly. */ encoder.addValue(WebRequestCodingStrategy.BOOKMARKABLE_PAGE_PARAMETER_NAME, pageMapName + Component.PATH_SEPARATOR + pageClass.getName()); } // Get page parameters final PageParameters parameters = requestTarget.getPageParameters(); if (parameters != null) { final Iterator iterator; if (UnitTestSettings.getSortUrlParameters()) { iterator = new TreeSet(parameters.keySet()).iterator(); } else { iterator = parameters.keySet().iterator(); } while (iterator.hasNext()) { final String key = (String)iterator.next(); final String values[] = parameters.getStringArray(key); if (values != null) { for (int i = 0; i < values.length; i++) { encoder.addValue(key, values[i]); } } } } return url; } /** * Encode a shared resource target.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -