added json support

git-svn-id: http://jclouds.googlecode.com/svn/trunk@1621 3d8758e0-26b5-11de-8745-db77d3ebf521
This commit is contained in:
adrian.f.cole 2009-07-13 22:44:32 +00:00
parent f9cd7e19d9
commit 8588776eb0
2 changed files with 95 additions and 0 deletions

View File

@ -101,6 +101,11 @@
<artifactId>bcprov-jdk15</artifactId>
<version>140</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>1.3</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,90 @@
/**
*
* Copyright (C) 2009 Global Cloud Specialists, Inc. <info@globalcloudspecialists.com>
*
* ====================================================================
* 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 <T> back to the
* caller.
*
* @author Adrian Cole
*/
public abstract class ParseJson<T> implements Function<HttpResponse, T> {
@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 <T>}.
*/
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);
}