Re: URL rewriting with PrettyFaces, URLMapping and pretty:link

Splash Forums PrettyFaces Users URL rewriting with PrettyFaces, URLMapping and pretty:link Re: URL rewriting with PrettyFaces, URLMapping and pretty:link

#22904

balteo
Participant

Hi Lincoln,

Let me provide the full configuration:

com.ocpsoft.pretty.faces.spi.ConfigurationPostProcessor:

package com.jeanbaptistemartin.rewrite;

import com.ocpsoft.pretty.faces.config.PrettyConfig;

import com.ocpsoft.pretty.faces.config.mapping.UrlAction;

import com.ocpsoft.pretty.faces.config.mapping.UrlMapping;

import com.ocpsoft.pretty.faces.spi.ConfigurationPostProcessor;

import javax.servlet.ServletContext;

import org.apache.log4j.Logger;

/**

*

* @author julien

*/

public class PrettyFacesPostProcessor implements ConfigurationPostProcessor {

private static transient Logger log = Logger.getLogger(PrettyFacesPostProcessor.class);

@Override

public PrettyConfig processConfiguration(ServletContext sc, PrettyConfig pc) {

log.debug("processConfiguration");

UrlAction rootAction = new UrlAction("#{accueilView.changeLocale}");

UrlMapping biographieMapping = (UrlMapping) pc.getMappingById("biographieAction");

biographieMapping.addAction(rootAction);

UrlMapping commandesPersonnaliseesMapping = (UrlMapping) pc.getMappingById("commandesPersonnaliseesAction");

commandesPersonnaliseesMapping.addAction(rootAction);

UrlMapping contactMapping = (UrlMapping) pc.getMappingById("contactAction");

contactMapping.getActions().add(0, rootAction);

UrlMapping galerieMapping = (UrlMapping) pc.getMappingById("galerieAction");

galerieMapping.getActions().add(0, rootAction);

UrlMapping liensMapping = (UrlMapping) pc.getMappingById("liensAction");

liensMapping.getActions().add(0, rootAction);

UrlMapping sculptureMapping = (UrlMapping) pc.getMappingById("sculptureAction");

sculptureMapping.getActions().add(0, rootAction);

UrlMapping inscriptionNewsletterMapping = (UrlMapping) pc.getMappingById("inscriptionNewsletterAction");

inscriptionNewsletterMapping.getActions().add(0, rootAction);

return pc;

}

}

org.ocpsoft.rewrite.config.ConfigurationProvider:

package com.jeanbaptistemartin.rewrite;

import javax.servlet.ServletContext;

import org.apache.log4j.Logger;

import org.ocpsoft.rewrite.config.Configuration;

import org.ocpsoft.rewrite.config.ConfigurationBuilder;

import org.ocpsoft.rewrite.servlet.config.HttpConfigurationProvider;

import org.ocpsoft.rewrite.servlet.config.rule.Join;

public class I18nProvider extends HttpConfigurationProvider {

private static final Logger log = Logger.getLogger(I18nProvider.class);

@Override

public Configuration getConfiguration(ServletContext context) {

log.debug("getConfiguration");

return ConfigurationBuilder.begin()

.addRule(Join.path("/lang/en/gallery").to("/lang/en/galerie").withInboundCorrection())

.addRule(Join.path("/lang/en/biography").to("/lang/en/biographie").withInboundCorrection())

.addRule(Join.path("/lang/en/newsletter-registration").to("/lang/en/inscription-newsletter").withInboundCorrection())

//.addRule(Join.path("/lang/en/contact").to("/lang/en/contact").withInboundCorrection())

.addRule(Join.path("/lang/en/works-made-to-order").to("/lang/en/commandes-personnalisees").withInboundCorrection())

.addRule(Join.path("/lang/en/links").to("/lang/en/liens").withInboundCorrection());

}

@Override

public int priority() {

return 10;

}

}

pretty-config.xml

<?xml version="1.0" encoding="UTF-8"?>

<pretty-config xmlns="http://ocpsoft.com/prettyfaces/3.3.3"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://ocpsoft.com/prettyfaces/3.3.3

http://ocpsoft.com/xml/ns/prettyfaces/ocpsoft-pretty-faces-3.3.3.xsd">

<url-mapping id="desinscription">

<pattern value="/lang/#{accueilView.language}/desinscription" />

<view-id>/desinscription.jsf</view-id>

</url-mapping>

<rewrite match="^/desinscriptionAbonne/([w-]+)$" substitute="/DesinscriptionAbonneServlet?abonneToken=$1" redirect="chain"/>

<rewrite match="^/redirigeVersSculpture/(fr|en)/(d+)$" substitute="/RedirigeVersSculptureServlet?abonneLocale=$1&sculptureID=$2" redirect="chain"/>

<rewrite match="/lang/(fr|en)/sculpture/(.+)-(d+)" substitute="/lang/$1/galerie/sculpture/$2-$3" redirect="301"/>

</pretty-config>

web.xml

<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"

version="3.0" metadata-complete="false">

<display-name>Site Jean-Baptiste Martin ${pom.version}</display-name>

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:root-application-config.xml</param-value>

</context-param>

<context-param>

<param-name>javax.faces.DEFAULT_SUFFIX</param-name>

<param-value>.xhtml</param-value>

</context-param>

<context-param>

<param-name>javax.faces.STATE_SAVING_METHOD</param-name>

<param-value>server</param-value>

</context-param>

<context-param>

<!-- todo change to false in prod -->

<param-name>facelets.DEVELOPMENT</param-name>

<param-value>true</param-value>

</context-param>

<context-param>

<param-name>javax.faces.FACELETS_LIBRARIES</param-name>

<param-value>/WEB-INF/jbm.taglib.xml</param-value>

</context-param>

<context-param>

<param-name>org.richfaces.LoadStyleStrategy</param-name>

<param-value>NONE</param-value>

</context-param>

<context-param>

<param-name>org.richfaces.enableControlSkinning</param-name>

<param-value>false</param-value>

</context-param>

<context-param>

<param-name>com.jeanbaptistemartin.static.url</param-name>

<param-value>http://static.jeanbaptistemartin.com/sculptures</param-value>

</context-param>

<context-param>

<param-name>com.jeanbaptistemartin.version</param-name>

<param-value>${pom.version}</param-value>

</context-param>

<context-param>

<param-name>javax.faces.FACELETS_SKIP_COMMENTS</param-name>

<param-value>true</param-value>

</context-param>

<context-param>

<!-- todo change to Production in prod -->

<param-name>javax.faces.PROJECT_STAGE</param-name>

<param-value>Production</param-value>

</context-param>

<!-- todo comment in prod -->

<!-- <context-param> <param-name>javax.faces.FACELETS_REFRESH_PERIOD</param-name>

<param-value>1</param-value> </context-param> -->

<context-param>

<param-name>com.ocpsoft.pretty.BASE_PACKAGES</param-name>

<param-value>com.jeanbaptistemartin.view,com.ocpsoft</param-value>

</context-param>

<listener>

<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>

</listener>

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

<listener>

<listener-class>com.sun.faces.config.ConfigureListener</listener-class>

</listener>

<listener>

<listener-class>com.sun.faces.application.WebappLifecycleListener</listener-class>

</listener>

<servlet>

<servlet-name>Faces Servlet</servlet-name>

<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>

<load-on-startup>1</load-on-startup>

</servlet>

<!-- <servlet> <servlet-name>CaptchaServlet</servlet-name> <servlet-class>com.jeanbaptistemartin.util.CaptchaServlet</servlet-class>

</servlet> -->

<servlet>

<servlet-name>DesinscriptionAbonneServlet</servlet-name>

<servlet-class>com.jeanbaptistemartin.util.DesinscriptionAbonneServlet</servlet-class>

</servlet>

<servlet>

<servlet-name>RedirigeVersSculptureServlet</servlet-name>

<servlet-class>com.jeanbaptistemartin.util.RedirigeVersSculptureServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>Faces Servlet</servlet-name>

<url-pattern>*.jsf</url-pattern>

</servlet-mapping>

<context-param>

<param-name>org.richfaces.CONTROL_SKINNING</param-name>

<param-value>disable</param-value>

</context-param>

<filter>

<filter-name>Spring OpenEntityManagerInViewFilter</filter-name>

<filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>

</filter>

<filter>

<filter-name>Pretty Filter</filter-name>

<filter-class>com.ocpsoft.pretty.PrettyFilter</filter-class>

</filter>

<filter-mapping>

<filter-name>Spring OpenEntityManagerInViewFilter</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

<filter-mapping>

<filter-name>Pretty Filter</filter-name>

<url-pattern>/*</url-pattern>

<dispatcher>REQUEST</dispatcher>

<dispatcher>FORWARD</dispatcher>

<dispatcher>ERROR</dispatcher>

</filter-mapping>

<servlet-mapping>

<servlet-name>DesinscriptionAbonneServlet</servlet-name>

<url-pattern>/DesinscriptionAbonneServlet</url-pattern>

</servlet-mapping>

<servlet-mapping>

<servlet-name>RedirigeVersSculptureServlet</servlet-name>

<url-pattern>/RedirigeVersSculptureServlet</url-pattern>

</servlet-mapping>

<session-config>

<session-timeout>30</session-timeout>

<tracking-mode>COOKIE</tracking-mode>

</session-config>

<welcome-file-list>

<welcome-file>accueil.xhtml</welcome-file>

</welcome-file-list>

<error-page>

<exception-type>javax.faces.application.ViewExpiredException</exception-type>

<location>/WEB-INF/include/error-pages/viewExpired.html</location>

</error-page>

<error-page>

<error-code>404</error-code>

<location>/WEB-INF/include/error-pages/404.html</location>

</error-page>

<error-page>

<!-- todo: change to 500 in production -->

<error-code>500</error-code>

<location>/WEB-INF/include/error-pages/500.html</location>

</error-page>

<security-constraint>

<display-name>jbmConstraint</display-name>

<web-resource-collection>

<web-resource-name>jbmAll</web-resource-name>

<description />

<url-pattern>/*</url-pattern>

<http-method>GET</http-method>

<http-method>POST</http-method>

<http-method>HEAD</http-method>

<http-method>PUT</http-method>

<http-method>OPTIONS</http-method>

<http-method>TRACE</http-method>

<http-method>DELETE</http-method>

</web-resource-collection>

</security-constraint>

<security-constraint>

<display-name>jbmConstraint</display-name>

<web-resource-collection>

<web-resource-name>jbmAll</web-resource-name>

<description />

<url-pattern>/*</url-pattern>

<http-method>GET</http-method>

<http-method>POST</http-method>

<http-method>HEAD</http-method>

<http-method>PUT</http-method>

<http-method>OPTIONS</http-method>

<http-method>TRACE</http-method>

<http-method>DELETE</http-method>

</web-resource-collection>

</security-constraint>

<login-config>

<auth-method>BASIC</auth-method>

<realm-name>jbmRealm</realm-name>

</login-config>

</web-app>

I can provide more info if required.

Regards,

Julien.