1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package groovyx.net.http;
23
24 import groovy.lang.Closure;
25 import groovy.util.XmlSlurper;
26 import groovy.util.slurpersupport.GPathResult;
27 import groovyx.net.http.HTTPBuilder.SendDelegate;
28
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.io.InputStreamReader;
32 import java.io.Reader;
33 import java.io.UnsupportedEncodingException;
34 import java.nio.charset.Charset;
35 import java.util.HashMap;
36 import java.util.List;
37 import java.util.Map;
38
39 import javax.xml.parsers.ParserConfigurationException;
40
41 import net.sf.json.JSON;
42 import net.sf.json.groovy.JsonSlurper;
43
44 import org.apache.commons.logging.Log;
45 import org.apache.commons.logging.LogFactory;
46 import org.apache.http.HttpResponse;
47 import org.apache.http.NameValuePair;
48 import org.apache.http.client.utils.URLEncodedUtils;
49 import org.codehaus.groovy.runtime.DefaultGroovyMethods;
50 import org.codehaus.groovy.runtime.MethodClosure;
51 import org.cyberneko.html.parsers.SAXParser;
52 import org.xml.sax.SAXException;
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71 public class ParserRegistry {
72
73 protected Closure defaultParser = new MethodClosure( this, "parseStream" );
74 protected final Log log = LogFactory.getLog( getClass() );
75
76
77
78
79
80
81
82
83
84 public static String getCharset( HttpResponse resp ) {
85 NameValuePair charset = resp.getEntity().getContentType()
86 .getElements()[0].getParameterByName("charset");
87 return ( charset == null || charset.getValue().trim().equals("") ) ?
88 Charset.defaultCharset().name() : charset.getValue();
89 }
90
91
92
93
94
95
96 public static String getContentType( HttpResponse resp ) {
97
98
99 return resp.getEntity().getContentType()
100 .getElements()[0].getName();
101 }
102
103
104
105
106
107
108
109
110 public InputStream parseStream( HttpResponse resp ) throws IOException {
111 return resp.getEntity().getContent();
112 }
113
114
115
116
117
118
119
120
121
122
123
124 public Reader parseText( HttpResponse resp ) throws IOException {
125 return new InputStreamReader( resp.getEntity().getContent(),
126 ParserRegistry.getCharset( resp ) );
127 }
128
129
130
131
132
133
134
135 public Map<String,String> parseForm( HttpResponse resp ) throws IOException {
136 List<NameValuePair> params = URLEncodedUtils.parse( resp.getEntity() );
137 Map<String,String> paramMap = new HashMap<String,String>(params.size());
138 for ( NameValuePair param : params )
139 paramMap.put( param.getName(), param.getValue() );
140 return paramMap;
141 }
142
143
144
145
146
147
148
149
150
151
152 public GPathResult parseHTML( HttpResponse resp ) throws IOException, SAXException {
153 return new XmlSlurper( new org.cyberneko.html.parsers.SAXParser() )
154 .parse( parseText( resp ) );
155 }
156
157
158
159
160
161
162
163
164
165
166 public GPathResult parseXML( HttpResponse resp ) throws IOException, SAXException, ParserConfigurationException {
167 return new XmlSlurper().parse( parseText( resp ) );
168 }
169
170
171
172
173
174
175
176 public JSON parseJSON( HttpResponse resp ) throws IOException {
177
178 String jsonTxt = DefaultGroovyMethods.getText( parseText( resp ) );
179 return new JsonSlurper().parseText( jsonTxt );
180 }
181
182 protected Map<String,Closure> registeredParsers = buildDefaultParserMap();
183
184
185
186
187
188
189
190
191
192
193
194 public void register( String contentType, Closure closure ) {
195 registeredParsers.put( contentType, closure );
196 }
197
198
199
200
201
202
203
204
205
206 Closure get( String contentType ) {
207 Closure parser = registeredParsers.get(contentType);
208 if ( parser == null ) {
209 log.warn( "Cannot find parser for content-type: " + contentType
210 + " -- using default parser.");
211 parser = defaultParser;
212 }
213 return parser;
214 }
215
216
217
218
219
220
221
222 protected Map<String,Closure> buildDefaultParserMap() {
223 Map<String,Closure> parsers = new HashMap<String,Closure>();
224
225 parsers.put( ContentType.BINARY.toString(), new MethodClosure( this, "parseStream" ) );
226 parsers.put( ContentType.TEXT.toString(), new MethodClosure(this,"parseText") );
227 parsers.put( ContentType.URLENC.toString(), new MethodClosure(this,"parseForm") );
228 parsers.put( ContentType.HTML.toString(), new MethodClosure(this,"parseHTML") );
229
230 Closure pClosure = new MethodClosure(this,"parseXML");
231 for ( String ct : ContentType.XML.getContentTypeStrings() )
232 parsers.put( ct, pClosure );
233
234 pClosure = new MethodClosure(this,"parseJSON");
235 for ( String ct : ContentType.JSON.getContentTypeStrings() )
236 parsers.put( ct, pClosure );
237
238 return parsers;
239 }
240 }