EMMA Coverage Report (generated Wed Jun 22 19:47:49 EDT 2011)
[all classes][org.jclouds.http.functions]

COVERAGE SUMMARY FOR SOURCE FILE [ParseSax.java]

nameclass, %method, %block, %line, %
ParseSax.java67%  (2/3)33%  (5/15)43%  (122/281)43%  (27/63)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class ParseSax$HandlerForGeneratedRequestWithResult0%   (0/1)0%   (0/3)0%   (0/17)0%   (0/5)
ParseSax$HandlerForGeneratedRequestWithResult (): void 0%   (0/1)0%   (0/3)0%   (0/1)
getRequest (): GeneratedHttpRequest 0%   (0/1)0%   (0/4)0%   (0/1)
setContext (HttpRequest): ParseSax$HandlerForGeneratedRequestWithResult 0%   (0/1)0%   (0/10)0%   (0/3)
     
class ParseSax100% (1/1)33%  (3/9)45%  (114/253)44%  (24/54)
apply (HttpResponse): Object 0%   (0/1)0%   (0/37)0%   (0/8)
convertStreamToStringAndParse (HttpResponse): Object 0%   (0/1)0%   (0/13)0%   (0/3)
getHandler (): ParseSax$HandlerWithResult 0%   (0/1)0%   (0/3)0%   (0/1)
parse (InputSource): Object 0%   (0/1)0%   (0/26)0%   (0/7)
parse (InputStream): Object 0%   (0/1)0%   (0/16)0%   (0/2)
parse (String): Object 0%   (0/1)0%   (0/37)0%   (0/6)
addDetailsAndPropagate (HttpResponse, Exception): Object 100% (1/1)93%  (89/96)85%  (17/20)
ParseSax (XMLReader, ParseSax$HandlerWithResult): void 100% (1/1)100% (15/15)100% (4/4)
setContext (HttpRequest): ParseSax 100% (1/1)100% (10/10)100% (3/3)
     
class ParseSax$HandlerWithResult100% (1/1)67%  (2/3)73%  (8/11)75%  (3/4)
getRequest (): HttpRequest 0%   (0/1)0%   (0/3)0%   (0/1)
ParseSax$HandlerWithResult (): void 100% (1/1)100% (3/3)100% (1/1)
setContext (HttpRequest): ParseSax$HandlerWithResult 100% (1/1)100% (5/5)100% (2/2)

1/**
2 *
3 * Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com>
4 *
5 * ====================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 * ====================================================================
18 */
19package org.jclouds.http.functions;
20 
21import static com.google.common.base.Preconditions.checkArgument;
22import static com.google.common.base.Preconditions.checkNotNull;
23import static com.google.common.io.Closeables.closeQuietly;
24 
25import java.io.InputStream;
26import java.io.StringReader;
27 
28import javax.inject.Inject;
29 
30import org.jclouds.http.HttpRequest;
31import org.jclouds.http.HttpResponse;
32import org.jclouds.rest.InvocationContext;
33import org.jclouds.rest.internal.GeneratedHttpRequest;
34import org.jclouds.util.Strings2;
35import org.xml.sax.InputSource;
36import org.xml.sax.SAXParseException;
37import org.xml.sax.XMLReader;
38import org.xml.sax.helpers.DefaultHandler;
39 
40import com.google.common.base.Function;
41import com.google.common.base.Throwables;
42 
43/**
44 * This object will parse the body of an HttpResponse and return the result of type <T> back to the
45 * caller.
46 * 
47 * @author Adrian Cole
48 */
49public 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         // This method should accept documents with a BOM (Byte-order mark)
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    * Handler that produces a useable domain object accessible after parsing completes.
153    * 
154    * @author Adrian Cole
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}

[all classes][org.jclouds.http.functions]
EMMA 2.0.5312 (C) Vladimir Roubtsov