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.resource;
018:
019: import javax.servlet.http.HttpServletResponse;
020:
021: import org.apache.wicket.Application;
022: import org.apache.wicket.RequestCycle;
023: import org.apache.wicket.Resource;
024: import org.apache.wicket.Response;
025: import org.apache.wicket.SharedResources;
026: import org.apache.wicket.WicketRuntimeException;
027: import org.apache.wicket.application.IClassResolver;
028: import org.apache.wicket.markup.html.PackageResource;
029: import org.apache.wicket.protocol.http.WebResponse;
030: import org.apache.wicket.request.RequestParameters;
031: import org.slf4j.Logger;
032: import org.slf4j.LoggerFactory;
033:
034: /**
035: * Default implementation of {@link ISharedResourceRequestTarget}. Target that
036: * denotes a shared {@link org.apache.wicket.Resource}.
037: *
038: * @author Eelco Hillenius
039: */
040: public class SharedResourceRequestTarget implements
041: ISharedResourceRequestTarget {
042: /** Logging object */
043: private static final Logger log = LoggerFactory
044: .getLogger(SharedResourceRequestTarget.class);
045:
046: private final RequestParameters requestParameters;
047:
048: /**
049: * Construct.
050: *
051: * @param requestParameters
052: * the request parameters
053: */
054: public SharedResourceRequestTarget(
055: RequestParameters requestParameters) {
056: this .requestParameters = requestParameters;
057: if (requestParameters == null) {
058: throw new IllegalArgumentException(
059: "requestParameters may not be null");
060: } else if (requestParameters.getResourceKey() == null) {
061: throw new IllegalArgumentException(
062: "requestParameters.getResourceKey() "
063: + "may not be null");
064: }
065: }
066:
067: /**
068: * @see org.apache.wicket.IRequestTarget#detach(org.apache.wicket.RequestCycle)
069: */
070: public void detach(RequestCycle requestCycle) {
071: }
072:
073: /**
074: * @see java.lang.Object#equals(java.lang.Object)
075: */
076: public boolean equals(Object obj) {
077: if (obj instanceof SharedResourceRequestTarget) {
078: SharedResourceRequestTarget that = (SharedResourceRequestTarget) obj;
079: return getRequestParameters().getResourceKey().equals(
080: that.getRequestParameters().getResourceKey());
081: }
082: return false;
083: }
084:
085: /**
086: * @see org.apache.wicket.request.target.resource.ISharedResourceRequestTarget#getRequestParameters()
087: */
088: public final RequestParameters getRequestParameters() {
089: return requestParameters;
090: }
091:
092: /**
093: * @see org.apache.wicket.request.target.resource.ISharedResourceRequestTarget#getResourceKey()
094: */
095: public final String getResourceKey() {
096: return requestParameters.getResourceKey();
097: }
098:
099: /**
100: * @see java.lang.Object#hashCode()
101: */
102: public int hashCode() {
103: int result = "SharedResourceRequestTarget".hashCode();
104: result += getRequestParameters().getResourceKey().hashCode();
105: return 17 * result;
106: }
107:
108: /**
109: * Respond by looking up the shared resource and delegating the actual
110: * response to that resource.
111: *
112: * @see org.apache.wicket.IRequestTarget#respond(org.apache.wicket.RequestCycle)
113: */
114: public void respond(RequestCycle requestCycle) {
115: Application application = requestCycle.getApplication();
116: SharedResources sharedResources = application
117: .getSharedResources();
118: final String resourceKey = getRequestParameters()
119: .getResourceKey();
120: Resource resource = sharedResources.get(resourceKey);
121:
122: // try to lazily register
123: if (resource == null) {
124: int ix = resourceKey.indexOf('/');
125: if (ix != -1) {
126: String className = resourceKey.substring(0, ix);
127: IClassResolver resolver = application
128: .getApplicationSettings().getClassResolver();
129: Class scope = null;
130: try {
131: // First try to match mounted resources.
132: scope = Application.get().getSharedResources()
133: .getAliasClass(className);
134:
135: // If that fails, resolve it as a fully qualified class
136: // name.
137: if (scope == null) {
138: scope = resolver.resolveClass(className);
139: }
140: String path = resourceKey.substring(ix + 1);
141:
142: PackageResource packageResource = PackageResource
143: .get(scope, path);
144: if (sharedResources.get(resourceKey) == null) {
145: sharedResources.add(resourceKey,
146: packageResource);
147: }
148: resource = packageResource;
149: } catch (Exception e) {
150: // besides logging, ignore exception; after this an error
151: // will be returned that the resource could not be retrieved
152: log.error(
153: "unable to lazily register shared resource "
154: + resourceKey, e);
155: }
156: }
157: }
158:
159: // if resource is still null, it doesn't exist
160: if (resource == null) {
161: Response response = requestCycle.getResponse();
162: if (response instanceof WebResponse) {
163: ((WebResponse) response).getHttpServletResponse()
164: .setStatus(HttpServletResponse.SC_NOT_FOUND);
165: log.error("shared resource " + resourceKey
166: + " not found");
167: return;
168: } else {
169: throw new WicketRuntimeException("shared resource "
170: + resourceKey + " not found");
171: }
172: }
173:
174: // set request parameters if there are any
175: if (requestParameters != null) {
176: resource.setParameters(requestParameters.getParameters());
177: }
178:
179: // let the resource handle the request
180: resource.onResourceRequested();
181: }
182:
183: /**
184: * @see java.lang.Object#toString()
185: */
186: public String toString() {
187: return "[SharedResourceRequestTarget@" + hashCode()
188: + ", resourceKey="
189: + getRequestParameters().getResourceKey() + "]";
190: }
191: }
|