From 8588776eb0ecc5e65201603a25a2943650a88551 Mon Sep 17 00:00:00 2001 From: "adrian.f.cole" Date: Mon, 13 Jul 2009 22:44:32 +0000 Subject: [PATCH] added json support git-svn-id: http://jclouds.googlecode.com/svn/trunk@1621 3d8758e0-26b5-11de-8745-db77d3ebf521 --- core/pom.xml | 5 ++ .../org/jclouds/http/functions/ParseJson.java | 90 +++++++++++++++++++ 2 files changed, 95 insertions(+) create mode 100644 core/src/main/java/org/jclouds/http/functions/ParseJson.java diff --git a/core/pom.xml b/core/pom.xml index 814c603dea..10931a5d74 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -101,6 +101,11 @@ bcprov-jdk15 140 + + com.google.code.gson + gson + 1.3 + diff --git a/core/src/main/java/org/jclouds/http/functions/ParseJson.java b/core/src/main/java/org/jclouds/http/functions/ParseJson.java new file mode 100644 index 0000000000..d2502fe170 --- /dev/null +++ b/core/src/main/java/org/jclouds/http/functions/ParseJson.java @@ -0,0 +1,90 @@ +/** + * + * Copyright (C) 2009 Global Cloud Specialists, Inc. + * + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * ==================================================================== + */ +package org.jclouds.http.functions; + +import static org.jclouds.http.HttpConstants.PROPERTY_JSON_DEBUG; + +import java.io.InputStream; + +import javax.annotation.Resource; + +import org.apache.commons.io.IOUtils; +import org.jclouds.http.HttpResponse; +import org.jclouds.http.HttpResponseException; +import org.jclouds.logging.Logger; + +import com.google.common.base.Function; +import com.google.gson.Gson; +import com.google.inject.Inject; +import com.google.inject.name.Named; + +/** + * This object will parse the body of an HttpResponse and return the result of type back to the + * caller. + * + * @author Adrian Cole + */ +public abstract class ParseJson implements Function { + + @Inject(optional = true) + @Named(PROPERTY_JSON_DEBUG) + private boolean suckFirst = false; + @Resource + protected Logger logger = Logger.NULL; + protected final Gson gson; + + public ParseJson(Gson gson) { + this.gson = gson; + } + + /** + * parses the http response body to create a new {@code }. + */ + public T apply(HttpResponse from) { + InputStream gson = from.getContent(); + String response = null; + try { + if (suckFirst) { + response = IOUtils.toString(gson); + logger.trace("received content %n%s", response); + IOUtils.closeQuietly(gson); + gson = IOUtils.toInputStream(response); + } + return apply(gson); + } catch (Exception e) { + StringBuilder message = new StringBuilder(); + message.append("Error parsing input"); + if (response != null) { + message.append("\n").append(response); + } + logger.error(e, message.toString()); + throw new HttpResponseException(message.toString()+"\n"+from, null, from, e); + } finally { + IOUtils.closeQuietly(gson); + } + + } + + protected abstract T apply(InputStream stream); +} \ No newline at end of file