01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.cocoon.ajax;
18:
19: import java.util.Collections;
20: import java.util.Map;
21:
22: import org.apache.avalon.framework.activity.Disposable;
23: import org.apache.avalon.framework.parameters.Parameters;
24: import org.apache.avalon.framework.service.ServiceException;
25: import org.apache.avalon.framework.service.ServiceManager;
26: import org.apache.avalon.framework.thread.ThreadSafe;
27: import org.apache.cocoon.acting.ServiceableAction;
28: import org.apache.cocoon.components.flow.ContinuationsManager;
29: import org.apache.cocoon.components.flow.FlowHelper;
30: import org.apache.cocoon.components.flow.WebContinuation;
31: import org.apache.cocoon.environment.ObjectModelHelper;
32: import org.apache.cocoon.environment.Redirector;
33: import org.apache.cocoon.environment.SourceResolver;
34: import org.apache.cocoon.sitemap.SitemapParameters;
35:
36: /**
37: * Get a continuation knowing its identifier, and set it as the current continuation
38: * in the object model, in the same manner as <code>cocoon.sendPageAndWait()</code> does.
39: * This action is useful when an Ajax request is made that needs to access data related
40: * to the continuation that originally displayed the current page.
41: * <p>
42: * The continuation id is either the <code>src</code> attribute in <code><map:act></code>
43: * or, if not specified, the <code>continuation-id</code> request parameter.
44: * <p>
45: * This action is successful if the continuation exists and is still valid.
46: *
47: * @since 2.1.8
48: * @version $Id: GetContinuationAction.java 448479 2006-09-21 07:33:36Z crossley $
49: */
50: public class GetContinuationAction extends ServiceableAction implements
51: ThreadSafe, Disposable {
52: ContinuationsManager contManager;
53:
54: public void service(ServiceManager manager) throws ServiceException {
55: super .service(manager);
56: this .contManager = (ContinuationsManager) manager
57: .lookup(ContinuationsManager.ROLE);
58: }
59:
60: public Map act(Redirector redirector, SourceResolver resolver,
61: Map objectModel, String source, Parameters parameters)
62: throws Exception {
63: String continuationId;
64: if (source == null) {
65: continuationId = ObjectModelHelper.getRequest(objectModel)
66: .getParameter("continuation-id");
67: } else {
68: continuationId = source;
69: }
70:
71: // The interpreter id is the sitemap's URI
72: String interpreterId = SitemapParameters
73: .getLocation(parameters).getURI();
74: WebContinuation wk = this .contManager.lookupWebContinuation(
75: continuationId, interpreterId);
76: if (wk == null || wk.disposed()) {
77: return null;
78: } else {
79: // Getting the continuation updates the last access time
80: wk.getContinuation();
81: FlowHelper.setWebContinuation(objectModel, wk);
82: return Collections.EMPTY_MAP;
83: }
84: }
85:
86: public void dispose() {
87: this.manager.release(this.contManager);
88: }
89: }
|