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.cocoon.webapps.session.components;
018:
019: import java.util.Enumeration;
020: import java.util.HashMap;
021: import java.util.Map;
022:
023: import org.apache.avalon.framework.component.Component;
024: import org.apache.avalon.framework.context.Context;
025: import org.apache.avalon.framework.context.ContextException;
026: import org.apache.avalon.framework.context.Contextualizable;
027: import org.apache.avalon.framework.logger.AbstractLogEnabled;
028: import org.apache.avalon.framework.service.ServiceException;
029: import org.apache.avalon.framework.service.ServiceManager;
030: import org.apache.avalon.framework.service.Serviceable;
031: import org.apache.avalon.framework.thread.ThreadSafe;
032: import org.apache.cocoon.ProcessingException;
033: import org.apache.cocoon.components.ContextHelper;
034: import org.apache.cocoon.environment.ObjectModelHelper;
035: import org.apache.cocoon.environment.Request;
036: import org.apache.cocoon.environment.Session;
037: import org.apache.cocoon.webapps.session.ContextManager;
038: import org.apache.cocoon.webapps.session.FormManager;
039: import org.apache.cocoon.webapps.session.SessionConstants;
040: import org.apache.cocoon.webapps.session.SessionManager;
041: import org.apache.cocoon.webapps.session.context.SessionContext;
042: import org.w3c.dom.DocumentFragment;
043:
044: /**
045: * Form handling
046: *
047: * @author <a href="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
048: * @deprecated This block is deprecated and will be removed in future versions.
049: * @version CVS $Id: DefaultFormManager.java 433543 2006-08-22 06:22:54Z crossley $
050: */
051: public final class DefaultFormManager extends AbstractLogEnabled
052: implements Serviceable, Component, FormManager, ThreadSafe,
053: Contextualizable {
054:
055: /** This session attribute is used to store the information for the inputxml tags */
056: private static final String ATTRIBUTE_INPUTXML_STORAGE = "org.apache.cocoon.webapps.session.InputXMLStorage";
057:
058: /** The <code>ServiceManager</code> */
059: private ServiceManager manager;
060:
061: /** The context */
062: private Context context;
063:
064: /**
065: * Get the context
066: */
067: private SessionContext getContext(String name)
068: throws ProcessingException {
069: ContextManager contextManager = null;
070: try {
071: contextManager = (ContextManager) this .manager
072: .lookup(ContextManager.ROLE);
073: return contextManager.getContext(name);
074: } catch (ServiceException ce) {
075: throw new ProcessingException(
076: "Unable to lookup context manager.", ce);
077: } finally {
078: this .manager.release(contextManager);
079: }
080: }
081:
082: private DocumentFragment getContextFragment(String context,
083: String path) throws ProcessingException {
084: SessionManager sessionManager = null;
085: try {
086: sessionManager = (SessionManager) this .manager
087: .lookup(SessionManager.ROLE);
088: return sessionManager.getContextFragment(context, path);
089: } catch (ServiceException ce) {
090: throw new ProcessingException(
091: "Unable to lookup session manager.", ce);
092: } finally {
093: this .manager.release(sessionManager);
094: }
095: }
096:
097: /**
098: * @see FormManager#registerInputField(String, String, String, String)
099: */
100: public DocumentFragment registerInputField(String contextName,
101: String path, String name, String formName)
102: throws ProcessingException {
103: // synchronized
104: if (this .getLogger().isDebugEnabled()) {
105: this .getLogger().debug(
106: "BEGIN registerInputField context=" + contextName
107: + ", path=" + path + ", name=" + name
108: + ", formName=" + formName);
109: }
110:
111: // test arguments
112: if (contextName == null) {
113: throw new ProcessingException(
114: "SessionManager.registerInputField: Context Name is required");
115: }
116: if (path == null) {
117: throw new ProcessingException(
118: "SessionManager.registerInputField: Path is required");
119: }
120: if (name == null) {
121: throw new ProcessingException(
122: "SessionManager.registerInputField: Name is required");
123: }
124: if (formName == null) {
125: throw new ProcessingException(
126: "SessionManager.registerInputField: Form is required");
127: }
128:
129: DocumentFragment value = null;
130: SessionContext context = this .getContext(contextName);
131: if (context == null) {
132: throw new ProcessingException(
133: "SessionManager.registerInputField: Context not found "
134: + contextName);
135: }
136: final Request request = ContextHelper.getRequest(this .context);
137: Session session = request.getSession(false);
138: if (session == null) {
139: throw new ProcessingException(
140: "SessionManager.registerInputField: Session is required for context "
141: + contextName);
142: }
143:
144: synchronized (session) {
145: Map inputFields = (Map) session
146: .getAttribute(ATTRIBUTE_INPUTXML_STORAGE);
147: if (inputFields == null) {
148: inputFields = new HashMap(10);
149: session.setAttribute(ATTRIBUTE_INPUTXML_STORAGE,
150: inputFields);
151: }
152: inputFields.put(name, new Object[] { context, path,
153: formName });
154: value = context.getXML(path);
155: }
156:
157: if (this .getLogger().isDebugEnabled()) {
158: this .getLogger().debug(
159: "END registerInputField value=" + value);
160: }
161: return value;
162: }
163:
164: /**
165: * Process all input fields.
166: * The fields are removed even if the request did not contain
167: * any values.
168: * This is a private method and should not be invoked directly.
169: */
170: private void processInputFields(Map objectModel) {
171: // we only want to invoke the testing once per request
172: if (objectModel.containsKey(this .getClass().getName())) {
173: return;
174: }
175: objectModel.put(this .getClass().getName(), "done");
176:
177: // synchronized
178: if (this .getLogger().isDebugEnabled()) {
179: this .getLogger().debug("BEGIN processInputFields");
180: }
181:
182: final Request request = ObjectModelHelper
183: .getRequest(objectModel);
184: final String formName = request
185: .getParameter(SessionConstants.SESSION_FORM_PARAMETER);
186: if (null != formName) {
187: final Session session = request.getSession(false);
188: if (session != null) {
189: synchronized (session) {
190: final Map inputFields = (Map) session
191: .getAttribute(ATTRIBUTE_INPUTXML_STORAGE);
192: if (inputFields != null) {
193: final Enumeration keys = request
194: .getParameterNames();
195: String currentKey;
196: Object[] contextAndPath;
197:
198: while (keys.hasMoreElements()) {
199: currentKey = (String) keys.nextElement();
200: if (inputFields.containsKey(currentKey)) {
201: contextAndPath = (Object[]) inputFields
202: .get(currentKey);
203: inputFields.remove(currentKey);
204:
205: SessionContext context = (SessionContext) contextAndPath[0];
206: String path = (String) contextAndPath[1];
207:
208: if (formName.equals(contextAndPath[2])) {
209: try {
210: context
211: .setXML(
212: path,
213: this
214: .getContextFragment(
215: SessionConstants.REQUEST_CONTEXT,
216: "/parameter/"
217: + currentKey));
218: } catch (ProcessingException ignore) {
219: this
220: .getLogger()
221: .warn(
222: "Exception during processing of input fields.",
223: ignore);
224: }
225: }
226: }
227: }
228: }
229: }
230: }
231: }
232:
233: if (this .getLogger().isDebugEnabled()) {
234: this .getLogger().debug("END processInputFields");
235: }
236: }
237:
238: /* (non-Javadoc)
239: * @see org.apache.cocoon.webapps.session.FormManager#processInputFields()
240: */
241: public void processInputFields() {
242: final Map objectModel = ContextHelper
243: .getObjectModel(this .context);
244: this .processInputFields(objectModel);
245: }
246:
247: /* (non-Javadoc)
248: * @see org.apache.avalon.framework.context.Contextualizable#contextualize(org.apache.avalon.framework.context.Context)
249: */
250: public void contextualize(Context context) throws ContextException {
251: this .context = context;
252: }
253:
254: /* (non-Javadoc)
255: * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
256: */
257: public void service(ServiceManager manager) throws ServiceException {
258: this.manager = manager;
259: }
260:
261: }
|