Beyond PrettyFaces - Einführung in Rewrite

828 views
721 views

Published on

W-JAX 2013 talk on Rewrite

Published in: Technology, News & Politics
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
828
On SlideShare
0
From Embeds
0
Number of Embeds
60
Actions
Shares
0
Downloads
6
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Beyond PrettyFaces - Einführung in Rewrite

  1. 1. Christian Kaltepoth | ingenit GmbH & Co. KG Beyond PrettyFaces Einführung in Rewrite
  2. 2. URL-Rewriting
  3. 3. Wikipedia A rewrite engine is software [...] that modifies a web URL's appearance. This modification is called URL rewriting. http://en.wikipedia.org/wiki/Rewrite_engine
  4. 4. Beispiel http://www.onlineshop.de/b/ref=sa_m enu_desk3?ie=UTF8&node=2193272340 http://www.onlineshop.de/elektronik
  5. 5. Sprechende RESTful URLs http://www.javaserverfaces.org/news http://jax.de/wjax2013/sessions https://github.com/ocpsoft/rewrite/issues/87 http://stackoverflow.com/questions/tagged/jsf
  6. 6. Wozu das Ganze?
  7. 7. Vorteile • Adressierbare Informationen – Wo bin ich hier? – "Vertrauen" • Reload und Bookmarks • Einfache HTML Links – Lose Kopplung • Technologieneutralität
  8. 8. SEO • Keywords in URL • Optimierung des Rankings http://www.amazon.com/JavaServerFaces-2-0-CompleteReference/dp/0071625097
  9. 9. PrettyFaces • • • • • • JSF URL-Rewriting De-facto-Standard RESTful URLs Page Actions Einfache Rewrite Engine Dynamic Views Integration mit JSF Navigation
  10. 10. Warum Rewrite?
  11. 11. JSF-spezifisch Servlet-spezifisch PrettyFaces API Abhängigkeiten
  12. 12. Einschränkungen Mapping nur via Request Path Eingeschränkte Konfiguration via XML Annotation API „verbesserungswürdig“ Konvertierung nur eingeschränkt möglich • Nicht besonders erweiterbar • • • •
  13. 13. Der Neuanfang
  14. 14. Was ist Rewrite?
  15. 15. Key Features • Servlet basiertes Rewriting auf Basis einer Rule-Engine • Framework Integration – JSF, CDI, Spring, Shiro, etc. • Konfiguration: Java DSL + Annotations • Fokus auf Erweiterbarkeit • Open Source (Apache 2.0)
  16. 16. Begriffe • Configuration: – Sortierte Liste von Rules • Rule: – Conditions – Operations – Priority
  17. 17. Rewriting Types Inbound Outbound
  18. 18. Inbound GET /faces/home.xhtml HTTP/1.1 Host: www.acme.com Connection: keep-alive [....]
  19. 19. Outbound <a href="/faces/home.xhtml"> Getting started </a>
  20. 20. Java DSL
  21. 21. Warum? • • • • • Typensichere Konfiguration Code Assist durch IDE Geführte Konfiguration Erweiterbar „Plain Java“
  22. 22. Java DSL public class RewriteConfig extends HttpConfigurationProvider { @Override public Configuration getConfiguration(ServletContext ctx) { // Konfiguration „bauen“ } @Override public int priority() { return 10; } }
  23. 23. ConfigurationBuilder return ConfigurationBuilder.begin() // Variante 1 .addRule() .when( /* condition */ ) .perform( /* operation */ ) // Variante 2 .addRule( /* rule */ ) ;
  24. 24. Initial Redirect http://www.acme.com/ Redirect http://www.acme.com/faces/home.xhtml
  25. 25. Beispiel: Initial Redirect .addRule() .when( Direction.isInbound().and(Path.matches("/")) ) .perform( Redirect.permanent("/faces/home.xhtml") )
  26. 26. Der erste Rewrite http://www.acme.com/faces/home.xhtml http://www.acme.com/home
  27. 27. Der erste Rewrite .addRule() .when(Direction.isInbound().and( Path.matches("/home"))) .perform(Forward.to("/faces/home.xhtml")) .addRule() .when(Direction.isOutbound().and( Path.matches("/faces/home.xhtml"))) .perform(Substitute.with("/home"))
  28. 28. Einfacher: Joins .addRule( Join.path("/home") .to("/faces/home.xhtml") )
  29. 29. Parameter /faces/products.xhtml?category=books /products/books
  30. 30. JSF 2.0 View Parameter <f:metadata> <f:viewParam name="category" value="#{productListPage.category}" /> </f:metadata> @Named @RequestScoped public class ProductListPage { private String category; }
  31. 31. Join mit Parametern .addRule( Join.path("/products/{category}") .to("/faces/products.xhtml") )
  32. 32. Demo
  33. 33. Annotations?
  34. 34. Einfacher Join @Named @RequestScoped @Join(path = "/home", to = "/faces/home.xhtml") public class HomePage { /* your code */ }
  35. 35. Parameter /faces/products.xhtml?category=books /products/books
  36. 36. Mit View-Parametern @Named @RequestScoped @Join(path = "/products/{category}", to = "/faces/products.xhtml") public class ProductListPage { // <f:viewParam name=“category“ ...> private String category; /* ... */ }
  37. 37. Ohne View-Parameter @Named @RequestScoped @Join(path = "/products/{category}", to = "/faces/products.xhtml") public class ProductListPage { @Parameter private String category; /* ... */ }
  38. 38. Validierung @Named @RequestScoped @Join(path = "/products/{category}", to = "/faces/products.xhtml") public class ProductListPage { @Parameter @Matches("[a-zA-Z-]+") private String category; /* ... */ }
  39. 39. JSF Validators @Named @RequestScoped @Join(path = "/products/{category}", to = "/faces/products.xhtml") public class ProductListPage { @Parameter @Validate(with = CategoryValidator.class) private String category; /* ... */ }
  40. 40. Request Actions
  41. 41. Request Actions @Named @RequestScoped @Join(path = "/home", to = "/faces/home.xhtml") public class HomePage { @RequestAction public void init() { /* your code */ } }
  42. 42. Ignore Postbacks @Named @RequestScoped @Join(path = "/home", to = "/faces/home.xhtml") public class HomePage { @RequestAction @IgnorePostback public void init() { /* your code */ } }
  43. 43. Deferral @Named @ViewScoped @Join(path = "/home", to = "/faces/home.xhtml") public class HomePage { @RequestAction @Deferred public void init() { /* your code */ } }
  44. 44. Deferral @Named @ViewScoped @Join(path = "/home", to = "/faces/home.xhtml") public class HomePage { @RequestAction @Deferred(before = Phase.RENDER_RESPONSE) public void init() { /* your code */ } }
  45. 45. Navigation
  46. 46. Navigation <h:link outcome="/products.xhtml"> <f:param name="category" value="books"/> Bücher </h:link> <a href="/products/books"> Bücher </a>
  47. 47. Navigation public class SomePage { public String actionMethod() { /* do something */ return "/products.xhtml?category=books" + "&faces-redirect=true"; } }
  48. 48. Navigation public class SomePage { public Navigate actionMethod() { /* do something */ return Navigate.to(ProductListPage.class) .with("category", "books"); } }
  49. 49. Was kann Rewrite noch?
  50. 50. Content Delivery Networks (CDN)
  51. 51. JSF Resources <h:outputScript name="jquery.js" /> Erzeugt <script type="text/javascript" src="/faces/javax.faces.resource/jquery.js" /> Gewünscht <script type="text/javascript" src="http://dh8sm43.cloudfront.net/jquery.js" />
  52. 52. CDN URL Relocation .addRule( CDN.relocate("/faces/javax.faces.resource/jquery.js") .to("http://dh8sm43.cloudfront.net/jquery.js") )
  53. 53. Resource Transformation
  54. 54. HTTP Response Rewrite Transformation Pipeline
  55. 55. Usecases • Minification – JavaScript, CSS • Compression – GZIP, Deflate • Rendering – SASS, SCSS, Markdown, Textile, ... • Custom Processing
  56. 56. JavaScript Minify .addRule() .when( Direction.isInbound().and(Path.matches( "/faces/javax.faces.resource/{*}.js")) ) .perform( Transform.with(Minify.js()) )
  57. 57. Rendering
  58. 58. Beispiel: Sass $blue: #3bbfce; $margin: 16px; .content-navigation { border-color: $blue; color: darken($blue, 9%); } .border { padding: $margin / 2; margin: $margin / 2; border-color: $blue; } .content-navigation { border-color: #3bbfce; color: #2b9eab; } .border { padding: 8px; margin: 8px; border-color: #3bbfce; }
  59. 59. Beispiel: Sass .addRule() .when( Direction.isInbound().and( Path.matches("/styles/{*}.sass")) ) .perform( Response.setContentType("text/css").and( Transform.with(Sass.compiler())) )
  60. 60. Precompile with Maven <plugin> <groupId>org.jasig.maven</groupId> <artifactId>sass-maven-plugin</artifactId> <version>1.1.1</version> <executions> <execution> <phase>prepare-package</phase> <goals> <goal>update-stylesheets</goal> </goals> </execution> </executions> </plugin>
  61. 61. Transform on demand .addRule() .when( Direction.isInbound() .and(Path.matches("/styles/{name}.css")) .and(Resource.exists("/styles/{name}.scss")) .and(Not.any( Resource.exists("/styles/{name}.css"))) ) .perform( Forward.to("/styles/{name}.scss") .and(Response.setContentType("text/css")) .and(Transform.with(Sass.compiler())) )
  62. 62. Wie migriere ich meine PrettyFaces Anwendung?
  63. 63. Rewrite PrettyFaces Module • Drop-In Replacement für PrettyFaces • „Sanfte“ Migration <dependency> <groupId>org.ocpsoft.rewrite</groupId> <artifactId>rewrite-config-prettyfaces</artifactId> <version>2.0.8.Final</version> </dependency>
  64. 64. Thank you! http://ocpsoft.org/rewrite/ Christian Kaltepoth christian@kaltepoth.de @chkal

×