Beyond PrettyFaces - Einführung in Rewrite

  • 498 views
Uploaded on

W-JAX 2013 talk on Rewrite

W-JAX 2013 talk on Rewrite

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
498
On Slideshare
0
From Embeds
0
Number of Embeds
3

Actions

Shares
Downloads
2
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Christian Kaltepoth | ingenit GmbH & Co. KG Beyond PrettyFaces Einführung in Rewrite
  • 2. URL-Rewriting
  • 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. Beispiel http://www.onlineshop.de/b/ref=sa_m enu_desk3?ie=UTF8&node=2193272340 http://www.onlineshop.de/elektronik
  • 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. Wozu das Ganze?
  • 7. Vorteile • Adressierbare Informationen – Wo bin ich hier? – "Vertrauen" • Reload und Bookmarks • Einfache HTML Links – Lose Kopplung • Technologieneutralität
  • 8. SEO • Keywords in URL • Optimierung des Rankings http://www.amazon.com/JavaServerFaces-2-0-CompleteReference/dp/0071625097
  • 9. PrettyFaces • • • • • • JSF URL-Rewriting De-facto-Standard RESTful URLs Page Actions Einfache Rewrite Engine Dynamic Views Integration mit JSF Navigation
  • 10. Warum Rewrite?
  • 11. JSF-spezifisch Servlet-spezifisch PrettyFaces API Abhängigkeiten
  • 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. Der Neuanfang
  • 14. Was ist Rewrite?
  • 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. Begriffe • Configuration: – Sortierte Liste von Rules • Rule: – Conditions – Operations – Priority
  • 17. Rewriting Types Inbound Outbound
  • 18. Inbound GET /faces/home.xhtml HTTP/1.1 Host: www.acme.com Connection: keep-alive [....]
  • 19. Outbound <a href="/faces/home.xhtml"> Getting started </a>
  • 20. Java DSL
  • 21. Warum? • • • • • Typensichere Konfiguration Code Assist durch IDE Geführte Konfiguration Erweiterbar „Plain Java“
  • 22. Java DSL public class RewriteConfig extends HttpConfigurationProvider { @Override public Configuration getConfiguration(ServletContext ctx) { // Konfiguration „bauen“ } @Override public int priority() { return 10; } }
  • 23. ConfigurationBuilder return ConfigurationBuilder.begin() // Variante 1 .addRule() .when( /* condition */ ) .perform( /* operation */ ) // Variante 2 .addRule( /* rule */ ) ;
  • 24. Initial Redirect http://www.acme.com/ Redirect http://www.acme.com/faces/home.xhtml
  • 25. Beispiel: Initial Redirect .addRule() .when( Direction.isInbound().and(Path.matches("/")) ) .perform( Redirect.permanent("/faces/home.xhtml") )
  • 26. Der erste Rewrite http://www.acme.com/faces/home.xhtml http://www.acme.com/home
  • 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. Einfacher: Joins .addRule( Join.path("/home") .to("/faces/home.xhtml") )
  • 29. Parameter /faces/products.xhtml?category=books /products/books
  • 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. Join mit Parametern .addRule( Join.path("/products/{category}") .to("/faces/products.xhtml") )
  • 32. Demo
  • 33. Annotations?
  • 34. Einfacher Join @Named @RequestScoped @Join(path = "/home", to = "/faces/home.xhtml") public class HomePage { /* your code */ }
  • 35. Parameter /faces/products.xhtml?category=books /products/books
  • 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. Ohne View-Parameter @Named @RequestScoped @Join(path = "/products/{category}", to = "/faces/products.xhtml") public class ProductListPage { @Parameter private String category; /* ... */ }
  • 38. Validierung @Named @RequestScoped @Join(path = "/products/{category}", to = "/faces/products.xhtml") public class ProductListPage { @Parameter @Matches("[a-zA-Z-]+") private String category; /* ... */ }
  • 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. Request Actions
  • 41. Request Actions @Named @RequestScoped @Join(path = "/home", to = "/faces/home.xhtml") public class HomePage { @RequestAction public void init() { /* your code */ } }
  • 42. Ignore Postbacks @Named @RequestScoped @Join(path = "/home", to = "/faces/home.xhtml") public class HomePage { @RequestAction @IgnorePostback public void init() { /* your code */ } }
  • 43. Deferral @Named @ViewScoped @Join(path = "/home", to = "/faces/home.xhtml") public class HomePage { @RequestAction @Deferred public void init() { /* your code */ } }
  • 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. Navigation
  • 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. Navigation public class SomePage { public String actionMethod() { /* do something */ return "/products.xhtml?category=books" + "&faces-redirect=true"; } }
  • 48. Navigation public class SomePage { public Navigate actionMethod() { /* do something */ return Navigate.to(ProductListPage.class) .with("category", "books"); } }
  • 49. Was kann Rewrite noch?
  • 50. Content Delivery Networks (CDN)
  • 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. CDN URL Relocation .addRule( CDN.relocate("/faces/javax.faces.resource/jquery.js") .to("http://dh8sm43.cloudfront.net/jquery.js") )
  • 53. Resource Transformation
  • 54. HTTP Response Rewrite Transformation Pipeline
  • 55. Usecases • Minification – JavaScript, CSS • Compression – GZIP, Deflate • Rendering – SASS, SCSS, Markdown, Textile, ... • Custom Processing
  • 56. JavaScript Minify .addRule() .when( Direction.isInbound().and(Path.matches( "/faces/javax.faces.resource/{*}.js")) ) .perform( Transform.with(Minify.js()) )
  • 57. Rendering
  • 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. Beispiel: Sass .addRule() .when( Direction.isInbound().and( Path.matches("/styles/{*}.sass")) ) .perform( Response.setContentType("text/css").and( Transform.with(Sass.compiler())) )
  • 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. 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. Wie migriere ich meine PrettyFaces Anwendung?
  • 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. Thank you! http://ocpsoft.org/rewrite/ Christian Kaltepoth christian@kaltepoth.de @chkal