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.coding;
018:
019: import org.apache.wicket.IRequestTarget;
020: import org.apache.wicket.request.RequestParameters;
021: import org.apache.wicket.request.target.resource.ISharedResourceRequestTarget;
022: import org.apache.wicket.request.target.resource.SharedResourceRequestTarget;
023: import org.apache.wicket.util.string.AppendingStringBuffer;
024: import org.apache.wicket.util.value.ValueMap;
025:
026: /**
027: * Encodes and decodes mounts for a single resource class.
028: *
029: * @author Gili Tzabari
030: */
031: public class SharedResourceRequestTargetUrlCodingStrategy extends
032: AbstractRequestTargetUrlCodingStrategy {
033: private final String resourceKey;
034:
035: /**
036: * Construct.
037: *
038: * @param mountPath
039: * @param resourceKey
040: */
041: public SharedResourceRequestTargetUrlCodingStrategy(
042: final String mountPath, final String resourceKey) {
043: super (mountPath);
044: this .resourceKey = resourceKey;
045: }
046:
047: /**
048: * @see org.apache.wicket.request.target.coding.IRequestTargetUrlCodingStrategy#decode(org.apache.wicket.request.RequestParameters)
049: */
050: public IRequestTarget decode(RequestParameters requestParameters) {
051: final String parametersFragment = requestParameters.getPath()
052: .substring(getMountPath().length());
053: final ValueMap parameters = decodeParameters(
054: parametersFragment, requestParameters.getParameters());
055:
056: requestParameters.setParameters(parameters);
057: requestParameters.setResourceKey(resourceKey);
058: return new SharedResourceRequestTarget(requestParameters);
059: }
060:
061: /**
062: * @see org.apache.wicket.request.target.coding.IRequestTargetUrlCodingStrategy#encode(org.apache.wicket.IRequestTarget)
063: */
064: public CharSequence encode(IRequestTarget requestTarget) {
065: if (!(requestTarget instanceof ISharedResourceRequestTarget)) {
066: throw new IllegalArgumentException(
067: "This encoder can only be used with "
068: + "instances of "
069: + ISharedResourceRequestTarget.class
070: .getName());
071: }
072: final AppendingStringBuffer url = new AppendingStringBuffer(40);
073: url.append(getMountPath());
074: final ISharedResourceRequestTarget target = (ISharedResourceRequestTarget) requestTarget;
075:
076: RequestParameters requestParameters = target
077: .getRequestParameters();
078: appendParameters(url, requestParameters.getParameters());
079: return url;
080: }
081:
082: /**
083: * @see org.apache.wicket.request.target.coding.IRequestTargetUrlCodingStrategy#matches(org.apache.wicket.IRequestTarget)
084: */
085: public boolean matches(IRequestTarget requestTarget) {
086: if (requestTarget instanceof ISharedResourceRequestTarget) {
087: ISharedResourceRequestTarget target = (ISharedResourceRequestTarget) requestTarget;
088: return target.getRequestParameters().getResourceKey()
089: .equals(resourceKey);
090: } else {
091: return false;
092: }
093: }
094:
095: /**
096: * @see java.lang.Object#toString()
097: */
098: public String toString() {
099: return "SharedResourceEncoder[key=" + resourceKey + "]";
100: }
101: }
|