2008-09-01 15:37:34 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2008 Google Inc.
|
|
|
|
*
|
|
|
|
* Licensed 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.
|
|
|
|
*/
|
2009-12-28 10:44:47 +01:00
|
|
|
package com.google.gson.webservice.client;
|
2008-09-01 15:37:34 +02:00
|
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
import java.io.Reader;
|
|
|
|
import java.lang.reflect.Type;
|
|
|
|
import java.net.HttpURLConnection;
|
|
|
|
import java.util.Map;
|
2010-01-20 14:59:59 +01:00
|
|
|
import java.util.logging.Level;
|
|
|
|
import java.util.logging.Logger;
|
2008-09-01 15:37:34 +02:00
|
|
|
|
|
|
|
import com.google.gson.Gson;
|
2009-12-28 10:44:47 +01:00
|
|
|
import com.google.gson.webservice.definition.HeaderMap;
|
|
|
|
import com.google.gson.webservice.definition.HeaderMapSpec;
|
|
|
|
import com.google.gson.webservice.definition.ResponseBody;
|
|
|
|
import com.google.gson.webservice.definition.ResponseBodySpec;
|
|
|
|
import com.google.gson.webservice.definition.ResponseSpec;
|
|
|
|
import com.google.gson.webservice.definition.WebServiceResponse;
|
2010-02-16 00:24:48 +01:00
|
|
|
import com.google.gson.webservice.definition.WebServiceSystemException;
|
2008-09-01 15:37:34 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Receives a response coming on an {@link HttpURLConnection}.
|
|
|
|
*
|
|
|
|
* @author inder
|
|
|
|
*/
|
|
|
|
public final class ResponseReceiver {
|
|
|
|
private final Gson gson;
|
|
|
|
private final ResponseSpec spec;
|
2010-01-20 14:59:59 +01:00
|
|
|
private final Logger logger;
|
|
|
|
private final Level logLevel;
|
2008-09-01 15:37:34 +02:00
|
|
|
|
|
|
|
public ResponseReceiver(Gson gson, ResponseSpec spec) {
|
2010-01-20 14:59:59 +01:00
|
|
|
this(gson, spec, null);
|
|
|
|
}
|
|
|
|
public ResponseReceiver(Gson gson, ResponseSpec spec, Level logLevel) {
|
2008-09-01 15:37:34 +02:00
|
|
|
this.gson = gson;
|
|
|
|
this.spec = spec;
|
2010-01-20 14:59:59 +01:00
|
|
|
this.logger = logLevel == null ? null : Logger.getLogger(ResponseReceiver.class.getName());
|
|
|
|
this.logLevel = logLevel;
|
2008-09-01 15:37:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public WebServiceResponse receive(HttpURLConnection conn) {
|
|
|
|
try {
|
|
|
|
HeaderMapSpec paramSpec = spec.getHeadersSpec();
|
|
|
|
ResponseBodySpec bodySpec = spec.getBodySpec();
|
|
|
|
// read response
|
|
|
|
HeaderMap responseParams = readResponseHeaders(conn, paramSpec);
|
|
|
|
ResponseBody responseBody = readResponseBody(conn, bodySpec);
|
|
|
|
return new WebServiceResponse(responseParams, responseBody);
|
|
|
|
} catch (IOException e) {
|
2010-02-16 00:24:48 +01:00
|
|
|
throw new WebServiceSystemException(e);
|
2008-09-01 15:37:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private HeaderMap readResponseHeaders(HttpURLConnection conn, HeaderMapSpec paramsSpec) {
|
2008-09-20 15:55:52 +02:00
|
|
|
HeaderMap.Builder paramsBuilder = new HeaderMap.Builder(paramsSpec);
|
2008-09-01 15:37:34 +02:00
|
|
|
for (Map.Entry<String, Type> entry : paramsSpec.entrySet()) {
|
|
|
|
String paramName = entry.getKey();
|
|
|
|
String json = conn.getHeaderField(paramName);
|
|
|
|
if (json != null) {
|
2010-01-20 14:59:59 +01:00
|
|
|
if (logger != null) {
|
|
|
|
logger.log(logLevel, String.format("Response Header: %s:%s\n", paramName, json));
|
|
|
|
}
|
2008-09-01 15:37:34 +02:00
|
|
|
Type typeOfT = paramsSpec.getTypeFor(paramName);
|
|
|
|
Object value = gson.fromJson(json, typeOfT);
|
2008-09-20 15:55:52 +02:00
|
|
|
paramsBuilder.put(paramName, value, typeOfT);
|
2008-09-01 15:37:34 +02:00
|
|
|
}
|
|
|
|
}
|
2010-01-19 13:58:52 +01:00
|
|
|
return paramsBuilder.build();
|
2008-09-01 15:37:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private ResponseBody readResponseBody(HttpURLConnection conn, ResponseBodySpec bodySpec)
|
|
|
|
throws IOException {
|
|
|
|
if (bodySpec.size() == 0) {
|
2010-01-19 13:58:52 +01:00
|
|
|
return new ResponseBody.Builder(bodySpec).build();
|
2008-09-01 15:37:34 +02:00
|
|
|
}
|
|
|
|
String connContentType = conn.getContentType();
|
2010-01-24 16:02:17 +01:00
|
|
|
Preconditions.checkArgument(connContentType.contains(bodySpec.getContentType()), conn);
|
2008-09-01 15:37:34 +02:00
|
|
|
Reader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
|
|
|
|
ResponseBody body = gson.fromJson(reader, ResponseBody.class);
|
|
|
|
return body;
|
|
|
|
}
|
|
|
|
}
|