1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
package com.mattunderscore.filter.contentnegotiation.variantsource; |
27 | |
|
28 | |
import java.io.ByteArrayInputStream; |
29 | |
import java.io.FileInputStream; |
30 | |
import java.io.FileNotFoundException; |
31 | |
import java.util.Enumeration; |
32 | |
import java.util.HashMap; |
33 | |
import java.util.List; |
34 | |
import java.util.Map; |
35 | |
|
36 | |
import javax.servlet.FilterConfig; |
37 | |
import javax.servlet.ServletContext; |
38 | |
|
39 | |
import com.mattunderscore.filter.contentnegotiation.parser.Variant; |
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
public class FilterVariantXMLSource implements VariantSourceForFilter |
54 | |
{ |
55 | |
|
56 | |
|
57 | |
|
58 | |
public static final String SAX_HANDLER = "saxHandlerClass"; |
59 | |
public static final String XML_INPUT_FILE = "xmlInputFile"; |
60 | |
public static final String XML_INPUT_STRING = "xmlInputString"; |
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
public static final String XML_INPUT_RESOURCE = "xmlInputResource"; |
66 | |
|
67 | 8 | private final Map<String, String> parameterMap = new HashMap<String, String>(); |
68 | 8 | private final BaseVariantXMLSource source = new BaseVariantXMLSource(); |
69 | |
|
70 | |
public FilterVariantXMLSource() |
71 | 8 | { |
72 | 8 | } |
73 | |
|
74 | |
@Override |
75 | |
public void configureSource(FilterConfig config) throws VariantSourceConfigurationException |
76 | |
{ |
77 | 8 | final ServletContext context = config.getServletContext(); |
78 | |
@SuppressWarnings("unchecked") |
79 | 8 | final Enumeration<String> parameterNames = config.getInitParameterNames(); |
80 | 14 | while (parameterNames.hasMoreElements()) |
81 | |
{ |
82 | 6 | final String parameterName = parameterNames.nextElement(); |
83 | 6 | final String parameterValue = config.getInitParameter(parameterName); |
84 | 6 | parameterMap.put(parameterName, parameterValue); |
85 | 6 | } |
86 | |
|
87 | 8 | if (parameterMap.containsKey(SAX_HANDLER)) |
88 | |
{ |
89 | |
try |
90 | |
{ |
91 | 4 | final Class<?> handlerClass = Class.forName(parameterMap.get(SAX_HANDLER)); |
92 | 4 | final Object handlerObject = handlerClass.newInstance(); |
93 | 4 | source.setHandler((VariantSAXHandler) handlerObject); |
94 | |
} |
95 | 0 | catch (final InstantiationException ex) |
96 | |
{ |
97 | 0 | throw new VariantSourceConfigurationException("Unable to construct SAX handler", |
98 | |
ex); |
99 | |
} |
100 | 0 | catch (final IllegalAccessException ex) |
101 | |
{ |
102 | 0 | throw new VariantSourceConfigurationException("Unable to construct SAX handler", |
103 | |
ex); |
104 | |
} |
105 | 0 | catch (final ClassNotFoundException ex) |
106 | |
{ |
107 | 0 | throw new VariantSourceConfigurationException("Unable to construct SAX handler", |
108 | |
ex); |
109 | 2 | } |
110 | |
} |
111 | 6 | if (parameterMap.containsKey(XML_INPUT_RESOURCE)) |
112 | |
{ |
113 | 0 | source.setInputStream(context.getResourceAsStream( |
114 | 0 | parameterMap.get(XML_INPUT_RESOURCE))); |
115 | |
} |
116 | 6 | else if (parameterMap.containsKey(XML_INPUT_FILE)) |
117 | |
{ |
118 | |
try |
119 | |
{ |
120 | 0 | source.setInputStream(new FileInputStream(parameterMap.get(XML_INPUT_FILE))); |
121 | |
} |
122 | 0 | catch (final FileNotFoundException exception) |
123 | |
{ |
124 | 0 | throw new VariantSourceConfigurationException("Unable get XML file", exception); |
125 | 0 | } |
126 | |
} |
127 | 6 | else if (parameterMap.containsKey(XML_INPUT_STRING)) |
128 | |
{ |
129 | 0 | source.setInputStream(new ByteArrayInputStream(parameterMap.get(XML_INPUT_STRING) |
130 | 0 | .getBytes())); |
131 | |
} |
132 | |
else |
133 | |
{ |
134 | 6 | throw new VariantSourceConfigurationException("Input stream not set"); |
135 | |
} |
136 | 0 | } |
137 | |
|
138 | |
@Override |
139 | |
public List<Variant> getVariants() throws VariantSourceException |
140 | |
{ |
141 | 0 | return source.getVariants(); |
142 | |
} |
143 | |
} |