KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > faces > FacesAction


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.faces;
17
18 import org.apache.avalon.framework.activity.Initializable;
19 import org.apache.avalon.framework.configuration.Configurable;
20 import org.apache.avalon.framework.configuration.Configuration;
21 import org.apache.avalon.framework.configuration.ConfigurationException;
22 import org.apache.avalon.framework.context.ContextException;
23 import org.apache.avalon.framework.context.Contextualizable;
24 import org.apache.avalon.framework.logger.AbstractLogEnabled;
25 import org.apache.avalon.framework.parameters.Parameters;
26 import org.apache.avalon.framework.thread.ThreadSafe;
27
28 import org.apache.cocoon.CascadingIOException;
29 import org.apache.cocoon.Constants;
30 import org.apache.cocoon.ProcessingException;
31 import org.apache.cocoon.acting.Action;
32 import org.apache.cocoon.environment.Context;
33 import org.apache.cocoon.environment.ObjectModelHelper;
34 import org.apache.cocoon.environment.Redirector;
35 import org.apache.cocoon.environment.Request;
36 import org.apache.cocoon.environment.Response;
37 import org.apache.cocoon.environment.SourceResolver;
38 import org.apache.cocoon.environment.portlet.ActionResponse;
39 import org.apache.cocoon.environment.portlet.PortletResponse;
40
41 import javax.faces.FacesException;
42 import javax.faces.FactoryFinder;
43 import javax.faces.application.Application;
44 import javax.faces.application.ApplicationFactory;
45 import javax.faces.context.FacesContext;
46 import javax.faces.context.FacesContextFactory;
47 import javax.faces.lifecycle.Lifecycle;
48 import javax.faces.lifecycle.LifecycleFactory;
49 import javax.faces.webapp.FacesServlet;
50 import java.io.IOException JavaDoc;
51 import java.util.Map JavaDoc;
52
53 /**
54  * @author <a HREF="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
55  * @version CVS $Id: FacesAction.java 46253 2004-09-17 14:36:29Z vgritsenko $
56  */

57 public class FacesAction extends AbstractLogEnabled
58                          implements Action, ThreadSafe, Contextualizable,
59                                     Configurable, Initializable {
60
61     public static final String JavaDoc REQUEST_REDIRECTOR_ATTRIBUTE = "org.apache.cocoon.faces.REDIRECTOR";
62
63     private Context context;
64
65     private String JavaDoc cutPrefix;
66     private String JavaDoc cutSuffix;
67     private String JavaDoc addPrefix;
68     private String JavaDoc addSuffix;
69
70     private FacesContextFactory facesContextFactory;
71     private Application application;
72     private Lifecycle lifecycle;
73
74
75     /**
76      *
77      */

78     class RedirectorImpl implements FacesRedirector {
79         private Redirector redirector;
80         private Request request;
81         private Response response;
82
83         RedirectorImpl (Redirector redirector, Request request, Response response) {
84             this.redirector = redirector;
85             this.request = request;
86             this.response = response;
87         }
88
89         public void dispatch(String JavaDoc url) throws IOException {
90             // System.err.println("INFO: Dispatching to " + url);
91
try {
92                 // TODO: HACK: Dependency on ActionResponse
93
if (response instanceof ActionResponse) {
94                     // Can't render response. Redirect to another face.
95
redirector.redirect(true, url);
96                 } else {
97                     // Need to render face. Convert face URL to view URL.
98
int begin = 0;
99                     int end = url.length();
100
101                     if (cutPrefix != null && url.startsWith(cutPrefix)) {
102                         begin = cutPrefix.length();
103                     }
104                     if (cutSuffix != null && url.endsWith(cutSuffix)) {
105                         end = end - cutSuffix.length();
106                     }
107
108                     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
109                     if (addPrefix != null) {
110                         buffer.append(addPrefix);
111                     }
112                     if (begin < end) {
113                         buffer.append(url.substring(begin, end));
114                     }
115                     if (addSuffix != null) {
116                         buffer.append(addSuffix);
117                     }
118                     url = buffer.toString();
119
120                     // System.err.println("INFO: Dispatching to view " + url);
121
redirector.redirect(true, "cocoon:/" + url);
122                 }
123             } catch (Exception JavaDoc e) {
124                 throw new CascadingIOException(e);
125             }
126         }
127
128         public void redirect(String JavaDoc url) throws IOException {
129             // System.err.println("redirect: " + url);
130
try {
131                 redirector.redirect(true, url);
132             } catch (Exception JavaDoc e) {
133                 throw new CascadingIOException(e);
134             }
135         }
136
137         public String JavaDoc encodeActionURL(String JavaDoc url) {
138             // System.err.println("encodeActionURL: " + url);
139
// TODO: HACK: Dependency on PortletResponse
140
if (response instanceof PortletResponse) {
141                 final String JavaDoc context = request.getContextPath();
142
143                 if (url.startsWith(context)) {
144                     url = url.substring(context.length());
145                     // System.err.println("encodeActionURL: cut: " + url);
146
}
147
148                 return "portlet:action:" + response.encodeURL(url);
149             } else {
150                 return response.encodeURL(url);
151             }
152         }
153
154         public String JavaDoc encodeResourceURL(String JavaDoc url) {
155             // System.err.println("encodeResourceURL: " + url);
156
return response.encodeURL(url);
157         }
158     }
159
160
161     public void contextualize(org.apache.avalon.framework.context.Context avalonContext) throws ContextException {
162         context = (Context) avalonContext.get(Constants.CONTEXT_ENVIRONMENT_CONTEXT);
163     }
164
165     public void configure(Configuration configuration) throws ConfigurationException {
166         this.cutPrefix = configuration.getChild("cut-prefix").getValue(null);
167         this.cutSuffix = configuration.getChild("cut-suffix").getValue(".faces");
168         this.addPrefix = configuration.getChild("add-prefix").getValue(null);
169         this.addSuffix = configuration.getChild("add-suffix").getValue(".view");
170     }
171
172     public void initialize() throws Exception JavaDoc {
173         if (getLogger().isDebugEnabled()) {
174             getLogger().debug("Initializing FacesAction");
175         }
176
177         facesContextFactory = (FacesContextFactory) FactoryFinder.getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
178         // facesContextFactory = new FacesContextFactoryImpl();
179

180         ApplicationFactory applicationFactory = (ApplicationFactory) FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY);
181         application = applicationFactory.getApplication();
182         // application.setDefaultRenderKitId("COCOON_BASIC_XHTML");
183

184         LifecycleFactory lifecycleFactory = (LifecycleFactory) FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY);
185         String JavaDoc lifecycleID = context.getInitParameter(FacesServlet.LIFECYCLE_ID_ATTR);
186         if (lifecycleID == null) {
187             lifecycleID = "DEFAULT";
188         }
189         lifecycle = lifecycleFactory.getLifecycle(lifecycleID);
190
191         if (getLogger().isDebugEnabled()) {
192             getLogger().debug("Faces context factory is " + facesContextFactory.getClass().getName());
193             getLogger().debug("Faces application factory is " + applicationFactory.getClass().getName());
194             getLogger().debug("Faces lifecycle factory is " + lifecycleFactory.getClass().getName());
195         }
196     }
197
198     public Map JavaDoc act(Redirector redirector,
199                    SourceResolver resolver,
200                    Map JavaDoc objectModel,
201                    String JavaDoc source,
202                    Parameters parameters)
203     throws Exception JavaDoc {
204         Request request = ObjectModelHelper.getRequest(objectModel);
205         Response response = ObjectModelHelper.getResponse(objectModel);
206
207         // Pass FacesRedirector to the FacesContext implementation.
208
request.setAttribute(REQUEST_REDIRECTOR_ATTRIBUTE, new RedirectorImpl(redirector, request, response));
209
210         FacesContext context = facesContextFactory.getFacesContext(this.context, request, response, lifecycle);
211         try {
212             lifecycle.execute(context);
213             lifecycle.render(context);
214
215             if (getLogger().isDebugEnabled()) {
216                 getLogger().debug("Request processed; View root ID: " + context.getViewRoot().getId());
217             }
218         } catch (FacesException e) {
219             throw new ProcessingException("Failed to process faces request", e);
220         } finally {
221             context.release();
222         }
223
224         return null;
225     }
226 }
227
Popular Tags