1 | /** |
2 | * Licensed to jclouds, Inc. (jclouds) under one or more |
3 | * contributor license agreements. See the NOTICE file |
4 | * distributed with this work for additional information |
5 | * regarding copyright ownership. jclouds licenses this file |
6 | * to you under the Apache License, Version 2.0 (the |
7 | * "License"); you may not use this file except in compliance |
8 | * with the License. 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, |
13 | * software distributed under the License is distributed on an |
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
15 | * KIND, either express or implied. See the License for the |
16 | * specific language governing permissions and limitations |
17 | * under the License. |
18 | */ |
19 | package org.jclouds.http.functions; |
20 | |
21 | import static org.jclouds.http.HttpUtils.releasePayload; |
22 | |
23 | import java.io.IOException; |
24 | import java.io.InputStream; |
25 | import java.lang.reflect.Type; |
26 | |
27 | import javax.annotation.Resource; |
28 | import javax.inject.Inject; |
29 | import javax.inject.Singleton; |
30 | |
31 | import org.jclouds.http.HttpResponse; |
32 | import org.jclouds.http.HttpResponseException; |
33 | import org.jclouds.json.Json; |
34 | import org.jclouds.logging.Logger; |
35 | import org.jclouds.util.Strings2; |
36 | |
37 | import com.google.common.base.Function; |
38 | import com.google.inject.TypeLiteral; |
39 | |
40 | /** |
41 | * This object will parse the body of an HttpResponse and return the result of |
42 | * type <T> back to the caller. |
43 | * |
44 | * @author Adrian Cole |
45 | */ |
46 | @Singleton |
47 | public class ParseJson<T> implements Function<HttpResponse, T> { |
48 | |
49 | @Resource |
50 | protected Logger logger = Logger.NULL; |
51 | protected final Json json; |
52 | protected final TypeLiteral<T> type; |
53 | |
54 | @Inject |
55 | public ParseJson(Json json, TypeLiteral<T> type) { |
56 | this.json = json; |
57 | this.type = type; |
58 | } |
59 | |
60 | /** |
61 | * parses the http response body to create a new {@code <T>}. |
62 | */ |
63 | public T apply(HttpResponse from) { |
64 | InputStream gson = from.getPayload().getInput(); |
65 | try { |
66 | return apply(gson); |
67 | } catch (Exception e) { |
68 | StringBuilder message = new StringBuilder(); |
69 | message.append("Error parsing input"); |
70 | logger.error(e, message.toString()); |
71 | throw new HttpResponseException(message.toString() + "\n" + from, null, from, e); |
72 | } finally { |
73 | releasePayload(from); |
74 | } |
75 | |
76 | } |
77 | |
78 | @SuppressWarnings("unchecked") |
79 | public T apply(InputStream stream) throws IOException { |
80 | return (T) apply(stream, type.getType()); |
81 | } |
82 | |
83 | @SuppressWarnings("unchecked") |
84 | public <V> V apply(InputStream stream, Type type) throws IOException { |
85 | try { |
86 | return (V) json.fromJson(Strings2.toStringAndClose(stream), type); |
87 | } finally { |
88 | if (stream != null) |
89 | stream.close(); |
90 | } |
91 | } |
92 | } |