1 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 ; 51 import java.util.Map ; 52 53 57 public class FacesAction extends AbstractLogEnabled 58 implements Action, ThreadSafe, Contextualizable, 59 Configurable, Initializable { 60 61 public static final String REQUEST_REDIRECTOR_ATTRIBUTE = "org.apache.cocoon.faces.REDIRECTOR"; 62 63 private Context context; 64 65 private String cutPrefix; 66 private String cutSuffix; 67 private String addPrefix; 68 private String addSuffix; 69 70 private FacesContextFactory facesContextFactory; 71 private Application application; 72 private Lifecycle lifecycle; 73 74 75 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 url) throws IOException { 90 try { 92 if (response instanceof ActionResponse) { 94 redirector.redirect(true, url); 96 } else { 97 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 buffer = new StringBuffer (); 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 redirector.redirect(true, "cocoon:/" + url); 122 } 123 } catch (Exception e) { 124 throw new CascadingIOException(e); 125 } 126 } 127 128 public void redirect(String url) throws IOException { 129 try { 131 redirector.redirect(true, url); 132 } catch (Exception e) { 133 throw new CascadingIOException(e); 134 } 135 } 136 137 public String encodeActionURL(String url) { 138 if (response instanceof PortletResponse) { 141 final String context = request.getContextPath(); 142 143 if (url.startsWith(context)) { 144 url = url.substring(context.length()); 145 } 147 148 return "portlet:action:" + response.encodeURL(url); 149 } else { 150 return response.encodeURL(url); 151 } 152 } 153 154 public String encodeResourceURL(String url) { 155 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 { 173 if (getLogger().isDebugEnabled()) { 174 getLogger().debug("Initializing FacesAction"); 175 } 176 177 facesContextFactory = (FacesContextFactory) FactoryFinder.getFactory(FactoryFinder.FACES_CONTEXT_FACTORY); 178 180 ApplicationFactory applicationFactory = (ApplicationFactory) FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY); 181 application = applicationFactory.getApplication(); 182 184 LifecycleFactory lifecycleFactory = (LifecycleFactory) FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY); 185 String 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 act(Redirector redirector, 199 SourceResolver resolver, 200 Map objectModel, 201 String source, 202 Parameters parameters) 203 throws Exception { 204 Request request = ObjectModelHelper.getRequest(objectModel); 205 Response response = ObjectModelHelper.getResponse(objectModel); 206 207 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 |