Skip to content

Commit db2c980

Browse files
committed
First version of JAXBPrettyConfigParser (currently only supports rewrite rules)
1 parent 1538285 commit db2c980

6 files changed

Lines changed: 308 additions & 0 deletions

File tree

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
package com.ocpsoft.pretty.faces.config;
2+
3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
6+
import javax.xml.bind.JAXBContext;
7+
import javax.xml.bind.JAXBElement;
8+
import javax.xml.bind.JAXBException;
9+
import javax.xml.bind.Unmarshaller;
10+
import javax.xml.parsers.ParserConfigurationException;
11+
import javax.xml.parsers.SAXParserFactory;
12+
import javax.xml.transform.sax.SAXSource;
13+
14+
import org.xml.sax.Attributes;
15+
import org.xml.sax.InputSource;
16+
import org.xml.sax.SAXException;
17+
import org.xml.sax.XMLFilter;
18+
import org.xml.sax.XMLReader;
19+
import org.xml.sax.helpers.XMLFilterImpl;
20+
21+
import com.ocpsoft.pretty.faces.config.types.PrettyConfigElement;
22+
23+
/**
24+
* Implementation of {@link PrettyConfigParser} that uses the JAXB API to parse
25+
* the PrettyFaces configuration.
26+
*
27+
* @author Christian Kaltepoth <christian@kaltepoth.de>
28+
*/
29+
public class JAXBPrettyConfigParser implements PrettyConfigParser
30+
{
31+
32+
private JAXBContext jaxbContext;
33+
34+
private SAXParserFactory saxParserFactory;
35+
36+
/**
37+
* Constructor
38+
*/
39+
public JAXBPrettyConfigParser()
40+
{
41+
try
42+
{
43+
// build JAXBContext using the package we generate the classes to
44+
jaxbContext = JAXBContext.newInstance("com.ocpsoft.pretty.faces.config.types");
45+
46+
// create a namespace aware SAXParserFactory
47+
saxParserFactory = SAXParserFactory.newInstance();
48+
saxParserFactory.setNamespaceAware(true);
49+
50+
}
51+
catch (JAXBException e)
52+
{
53+
throw new IllegalStateException(e);
54+
}
55+
}
56+
57+
/*
58+
* @see com.ocpsoft.pretty.faces.config.PrettyConfigParser#parse(com.ocpsoft.pretty.faces.config.PrettyConfigBuilder, java.io.InputStream)
59+
*/
60+
public void parse(PrettyConfigBuilder builder, InputStream resource) throws IOException, SAXException
61+
{
62+
63+
try
64+
{
65+
66+
// create the JAXB unmarshaller
67+
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
68+
69+
// use a special XMLFilter to do the namespace replacement
70+
XMLReader xmlReader = saxParserFactory.newSAXParser().getXMLReader();
71+
XMLFilter namespaceFilter = new NamespaceFilter(xmlReader);
72+
xmlReader.setContentHandler(unmarshaller.getUnmarshallerHandler());
73+
SAXSource source = new SAXSource(namespaceFilter, new InputSource(resource));
74+
75+
// parse the document and get the PrettyConfigElement
76+
JAXBElement<?> e = (JAXBElement<?>) unmarshaller.unmarshal(source);
77+
PrettyConfigElement prettyConfigElement = (PrettyConfigElement) e.getValue();
78+
79+
// build the PrettyConfig and append it to the builder
80+
PrettyConfig config = new PrettyConfig(prettyConfigElement);
81+
builder.addFromConfig(config);
82+
83+
}
84+
catch (JAXBException e)
85+
{
86+
throw new IOException(e);
87+
}
88+
catch (ParserConfigurationException e)
89+
{
90+
throw new IOException(e);
91+
}
92+
}
93+
94+
/**
95+
* This class is used to replace older PrettyFaces namespace URIs with the
96+
* most current one. This is required so that the JAXB classes generated by
97+
* the most current schema work correctly.
98+
*
99+
* @author Christian Kaltepoth <christian@kaltepoth.de>
100+
*/
101+
private class NamespaceFilter extends XMLFilterImpl
102+
{
103+
104+
/**
105+
* The prefix of namespaces that have to be replaced
106+
*/
107+
private final static String EXPECTED_URI_PREFIX = "http://ocpsoft.com/prettyfaces/";
108+
109+
/**
110+
* The most current namespace
111+
*/
112+
private final static String CURRENT_NAMESPACE_URI = "http://ocpsoft.com/prettyfaces/3.2.1";
113+
114+
public NamespaceFilter(XMLReader reader)
115+
{
116+
super(reader);
117+
}
118+
119+
public void startPrefixMapping(String prefix, String uri) throws SAXException
120+
{
121+
super.startPrefixMapping(prefix, replaceNamespace(uri));
122+
}
123+
124+
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException
125+
{
126+
127+
super.startElement(replaceNamespace(uri), localName, qName, attributes);
128+
}
129+
130+
public void endElement(String uri, String localName, String qName) throws SAXException
131+
{
132+
super.endElement(replaceNamespace(uri), localName, qName);
133+
}
134+
135+
private String replaceNamespace(String uri)
136+
{
137+
if (uri != null)
138+
{
139+
return uri.startsWith(EXPECTED_URI_PREFIX) ? CURRENT_NAMESPACE_URI : uri;
140+
}
141+
return null;
142+
}
143+
144+
}
145+
146+
}

‎core/src/main/java/com/ocpsoft/pretty/faces/config/PrettyConfig.java‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import com.ocpsoft.pretty.PrettyFilter;
2727
import com.ocpsoft.pretty.faces.config.mapping.UrlMapping;
2828
import com.ocpsoft.pretty.faces.config.rewrite.RewriteRule;
29+
import com.ocpsoft.pretty.faces.config.types.PrettyConfigElement;
30+
import com.ocpsoft.pretty.faces.config.types.RewriteElement;
2931
import com.ocpsoft.pretty.faces.url.URL;
3032

3133
public class PrettyConfig
@@ -36,6 +38,28 @@ public class PrettyConfig
3638
private String dynaviewId = "";
3739
private Map<String, UrlMapping> cachedMappings = new HashMap<String, UrlMapping>();
3840

41+
/**
42+
* Creates an empty {@link PrettyConfig} object
43+
*/
44+
public PrettyConfig()
45+
{
46+
// nothing
47+
}
48+
49+
/**
50+
* Initializes the instance using the supplied JAXB object
51+
*
52+
* @param prettyConfigElement
53+
* The JAXB object
54+
*/
55+
public PrettyConfig(PrettyConfigElement prettyConfigElement)
56+
{
57+
for (RewriteElement rewriteElement : prettyConfigElement.getRewrite())
58+
{
59+
globalRewriteRules.add(new RewriteRule(rewriteElement));
60+
}
61+
}
62+
3963
/**
4064
* Set the current DynaView ID. This is used when calculating dynamic viewIds specified in pretty-config.xml (Do not
4165
* change unless you know what you are doing - this maps directly to your Faces Servlet mapping and is discovered

‎core/src/main/java/com/ocpsoft/pretty/faces/config/rewrite/RewriteRule.java‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import java.util.regex.Matcher;
2020
import java.util.regex.Pattern;
2121

22+
import com.ocpsoft.pretty.faces.config.types.RewriteElement;
23+
2224
/**
2325
* @author Lincoln Baxter, III <lincoln@ocpsoft.com>
2426
*/
@@ -36,6 +38,56 @@ public class RewriteRule
3638

3739
private Pattern pattern = Pattern.compile("");
3840

41+
/**
42+
* Creates an empty {@link RewriteRule}
43+
*/
44+
public RewriteRule()
45+
{
46+
// nothing
47+
}
48+
49+
/**
50+
* Creates an rewrite rule from the supplied JAXB object
51+
*
52+
* @param element
53+
* The JAXB object
54+
*/
55+
public RewriteRule(RewriteElement element)
56+
{
57+
match = element.getMatch();
58+
substitute = element.getSubstitute();
59+
processor = element.getProcessor();
60+
url = element.getUrl();
61+
if (element.isInbound() != null)
62+
{
63+
inbound = element.isInbound();
64+
}
65+
if (element.isOutbound() != null)
66+
{
67+
outbound = element.isOutbound();
68+
}
69+
if (element.getRedirect() != null)
70+
{
71+
if ("301".equals(element.getRedirect()))
72+
{
73+
redirect = Redirect.PERMANENT;
74+
}
75+
else if ("302".equals(element.getRedirect()))
76+
{
77+
redirect = Redirect.TEMPORARY;
78+
}
79+
redirect = Redirect.valueOf(element.getRedirect().toUpperCase());
80+
}
81+
if (element.getToCase() != null)
82+
{
83+
toCase = Case.valueOf(element.getToCase().name().toUpperCase());
84+
}
85+
if (element.getTrailingSlash() != null)
86+
{
87+
trailingSlash = TrailingSlash.valueOf(element.getTrailingSlash().name().toUpperCase());
88+
}
89+
}
90+
3991
/**
4092
* Return true if the <b>match</b> field contains a regex that matches some
4193
* or all of the given URL, false if no match is found.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.ocpsoft.pretty.faces.config;
2+
3+
import static junit.framework.Assert.assertEquals;
4+
import static junit.framework.Assert.assertNotNull;
5+
6+
import java.io.InputStream;
7+
8+
import org.junit.Before;
9+
import org.junit.Test;
10+
11+
import com.ocpsoft.pretty.faces.config.rewrite.Case;
12+
import com.ocpsoft.pretty.faces.config.rewrite.Redirect;
13+
import com.ocpsoft.pretty.faces.config.rewrite.RewriteRule;
14+
import com.ocpsoft.pretty.faces.config.rewrite.TrailingSlash;
15+
16+
public class JAXBPrettyConfigParserTest
17+
{
18+
19+
private PrettyConfig config;
20+
21+
@Before
22+
public void setUp() throws Exception
23+
{
24+
InputStream xml = getClass().getClassLoader().getResourceAsStream("complete-pretty-config.xml");
25+
assertNotNull("Cannot find pretty-config.xml file for test!", xml);
26+
PrettyConfigBuilder builder = new PrettyConfigBuilder();
27+
new DigesterPrettyConfigParser().parse(builder, xml);
28+
config = builder.build();
29+
}
30+
31+
@Test
32+
public void testParseRewriteRules()
33+
{
34+
35+
assertNotNull("PrettyConfig has not been created!", config);
36+
assertNotNull("No rewrite rules parsed", config.getGlobalRewriteRules());
37+
assertEquals(2, config.getGlobalRewriteRules().size());
38+
39+
// no attributes specified in the XML file. Check for default values
40+
RewriteRule minimalRule = config.getGlobalRewriteRules().get(0);
41+
assertEquals("", minimalRule.getMatch());
42+
assertEquals("", minimalRule.getSubstitute());
43+
assertEquals("", minimalRule.getUrl());
44+
assertEquals(Redirect.PERMANENT, minimalRule.getRedirect());
45+
assertEquals(Case.IGNORE, minimalRule.getToCase());
46+
assertEquals(TrailingSlash.IGNORE, minimalRule.getTrailingSlash());
47+
assertEquals(true, minimalRule.isInbound());
48+
assertEquals(true, minimalRule.isOutbound());
49+
assertEquals("", minimalRule.getProcessor());
50+
51+
// all attributes specified. Verify values from XML file.
52+
RewriteRule fullRule = config.getGlobalRewriteRules().get(1);
53+
assertEquals("/something", fullRule.getMatch());
54+
assertEquals("/new", fullRule.getSubstitute());
55+
assertEquals("http://www.ocpsoft.com/", fullRule.getUrl());
56+
assertEquals(Redirect.TEMPORARY, fullRule.getRedirect());
57+
assertEquals(Case.UPPERCASE, fullRule.getToCase());
58+
assertEquals(TrailingSlash.REMOVE, fullRule.getTrailingSlash());
59+
assertEquals(false, fullRule.isInbound());
60+
assertEquals(false, fullRule.isOutbound());
61+
assertEquals("com.ocpsoft.MyProcessor", fullRule.getProcessor());
62+
63+
}
64+
65+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<pretty-config xmlns="http://ocpsoft.com/prettyfaces/3.1.1"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://ocpsoft.com/prettyfaces/3.1.1 http://ocpsoft.com/xml/ns/prettyfaces/ocpsoft-pretty-faces-3.1.1.xsd">
5+
6+
<!-- minimal configuration -->
7+
<rewrite />
8+
9+
<!-- complete rule specifying all possible attributes -->
10+
<rewrite inbound="false" match="/something" outbound="false"
11+
processor="com.ocpsoft.MyProcessor" redirect="302" substitute="/new"
12+
toCase="uppercase" trailingSlash="remove" url="http://www.ocpsoft.com/" />
13+
14+
</pretty-config>

‎pom.xml‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,13 @@
129129
<scope>provided</scope>
130130
</dependency>
131131

132+
<dependency>
133+
<groupId>javax.xml</groupId>
134+
<artifactId>jaxb-api</artifactId>
135+
<version>2.0</version>
136+
<scope>provided</scope>
137+
</dependency>
138+
132139
<dependency>
133140
<groupId>junit</groupId>
134141
<artifactId>junit</artifactId>

0 commit comments

Comments
 (0)