1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.jclouds.http.functions;
20
21 import static com.google.common.base.Preconditions.checkArgument;
22 import static com.google.common.base.Preconditions.checkNotNull;
23 import static com.google.common.io.Closeables.closeQuietly;
24
25 import java.io.InputStream;
26 import java.io.StringReader;
27
28 import javax.inject.Inject;
29
30 import org.jclouds.http.HttpRequest;
31 import org.jclouds.http.HttpResponse;
32 import org.jclouds.rest.InvocationContext;
33 import org.jclouds.rest.internal.GeneratedHttpRequest;
34 import org.jclouds.util.Strings2;
35 import org.xml.sax.InputSource;
36 import org.xml.sax.SAXParseException;
37 import org.xml.sax.XMLReader;
38 import org.xml.sax.helpers.DefaultHandler;
39
40 import com.google.common.base.Function;
41 import com.google.common.base.Throwables;
42
43
44
45
46
47
48
49 public class ParseSax<T> implements Function<HttpResponse, T>, InvocationContext<ParseSax<T>> {
50
51 private final XMLReader parser;
52 private final HandlerWithResult<T> handler;
53 private HttpRequest request;
54
55 public static interface Factory {
56 <T> ParseSax<T> create(HandlerWithResult<T> handler);
57 }
58
59 @Inject
60 public ParseSax(XMLReader parser, HandlerWithResult<T> handler) {
61 this.parser = checkNotNull(parser, "parser");
62 this.handler = checkNotNull(handler, "handler");
63 }
64
65 public T apply(HttpResponse from) {
66 try {
67 checkNotNull(from, "http response");
68 checkNotNull(from.getPayload(), "payload in " + from);
69 } catch (NullPointerException e) {
70 return addDetailsAndPropagate(from, e);
71 }
72 if (from.getStatusCode() >= 300)
73 return convertStreamToStringAndParse(from);
74 return parse(from.getPayload().getInput());
75 }
76
77 private T convertStreamToStringAndParse(HttpResponse from) {
78 try {
79 return parse(Strings2.toStringAndClose(from.getPayload().getInput()));
80 } catch (Exception e) {
81 return addDetailsAndPropagate(from, e);
82 }
83 }
84
85 public T parse(String from) {
86 try {
87 checkNotNull(from, "xml string");
88 checkArgument(from.indexOf('<') >= 0, String.format("not an xml document [%s] ", from));
89 } catch (RuntimeException e) {
90 return addDetailsAndPropagate(null, e);
91 }
92 return parse(new InputSource(new StringReader(from)));
93 }
94
95 public T parse(InputStream from) {
96 try {
97 return parse(new InputSource(from));
98 } finally {
99 closeQuietly(from);
100 }
101 }
102
103 public T parse(InputSource from) {
104 try {
105 checkNotNull(from, "xml inputsource");
106 from.setEncoding("UTF-8");
107 parser.setContentHandler(getHandler());
108
109 parser.parse(from);
110 return getHandler().getResult();
111 } catch (Exception e) {
112 return addDetailsAndPropagate(null, e);
113 }
114 }
115
116 public T addDetailsAndPropagate(HttpResponse response, Exception e) {
117 StringBuilder message = new StringBuilder();
118 if (request != null) {
119 message.append("request: ").append(request.getRequestLine());
120 }
121 if (response != null) {
122 if (message.length() != 0)
123 message.append("; ");
124 message.append("response: ").append(response.getStatusLine());
125 }
126 if (e instanceof SAXParseException) {
127 SAXParseException parseException = (SAXParseException) e;
128 String systemId = parseException.getSystemId();
129 if (systemId == null) {
130 systemId = "";
131 }
132 if (message.length() != 0)
133 message.append("; ");
134 message.append(String.format("error at %d:%d in document %s", parseException.getColumnNumber(),
135 parseException.getLineNumber(), systemId));
136 }
137 if (message.length() != 0) {
138 message.append("; cause: ").append(e.toString());
139 throw new RuntimeException(message.toString(), e);
140 } else {
141 Throwables.propagate(e);
142 return null;
143 }
144
145 }
146
147 public HandlerWithResult<T> getHandler() {
148 return handler;
149 }
150
151
152
153
154
155
156 public abstract static class HandlerWithResult<T> extends DefaultHandler implements
157 InvocationContext<HandlerWithResult<T>> {
158 private HttpRequest request;
159
160 protected HttpRequest getRequest() {
161 return request;
162 }
163
164 public abstract T getResult();
165
166 @Override
167 public HandlerWithResult<T> setContext(HttpRequest request) {
168 this.request = request;
169 return this;
170 }
171 }
172
173 public abstract static class HandlerForGeneratedRequestWithResult<T> extends HandlerWithResult<T> {
174 @Override
175 protected GeneratedHttpRequest<?> getRequest() {
176 return (GeneratedHttpRequest<?>) super.getRequest();
177 }
178
179 @Override
180 public HandlerForGeneratedRequestWithResult<T> setContext(HttpRequest request) {
181 checkArgument(request instanceof GeneratedHttpRequest<?>, "note this handler requires a GeneratedHttpRequest");
182 super.setContext(request);
183 return this;
184 }
185 }
186
187 @Override
188 public ParseSax<T> setContext(HttpRequest request) {
189 handler.setContext(request);
190 this.request = request;
191 return this;
192 }
193 }