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: */
18: package org.apache.lenya.cms.site.usecases;
19:
20: import java.util.Map;
21:
22: import org.apache.avalon.framework.context.Context;
23: import org.apache.cocoon.components.ContextHelper;
24: import org.apache.cocoon.environment.ObjectModelHelper;
25: import org.apache.cocoon.environment.Request;
26: import org.apache.cocoon.environment.Session;
27:
28: /**
29: * Helper class for clipboard handling.
30: *
31: * @version $Id: ClipboardHelper.java 532646 2007-04-26 08:04:08Z andreas $
32: */
33: public class ClipboardHelper {
34:
35: /**
36: * Returns the clipboard attachted to the session.
37: * @param context The context containing the session.
38: * @return A clipboard or <code>null</code> if no clipboard is attached to
39: * the session.
40: */
41: public Clipboard getClipboard(Context context) {
42: Session session = getSession(context);
43: Clipboard clipboard = (Clipboard) session
44: .getAttribute(getSessionAttributeName());
45: return clipboard;
46: }
47:
48: /**
49: * @return The name of the session attribute to hold the clipboard.
50: */
51: protected String getSessionAttributeName() {
52: return Clipboard.class.getName();
53: }
54:
55: /**
56: * @param context The context.
57: * @return The session of the context.
58: */
59: protected Session getSession(Context context) {
60: Map objectModel = ContextHelper.getObjectModel(context);
61: Request request = ObjectModelHelper.getRequest(objectModel);
62: Session session = request.getSession(true);
63: return session;
64: }
65:
66: /**
67: * Saves the clipboard to the session.
68: * @param context The context.
69: * @param clipboard The clipboard.
70: */
71: public void saveClipboard(Context context, Clipboard clipboard) {
72: Session session = getSession(context);
73: session.setAttribute(getSessionAttributeName(), clipboard);
74: }
75:
76: /**
77: * Removes the clipboard from the session.
78: * @param context The context.
79: */
80: public void removeClipboard(Context context) {
81: Session session = getSession(context);
82: session.removeAttribute(getSessionAttributeName());
83: }
84:
85: }
|