/*******************************************************************************
* Copyright 2013 butor.com
*
* 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.
******************************************************************************/
package org.butor.json.service;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;
import org.butor.json.JsonHelper;
import org.butor.json.JsonServiceRequest;
import org.butor.json.JsonStreamHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.api.client.http.AbstractHttpContent;
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpContent;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestFactory;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.HttpResponse;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
public class DefaultServiceCaller implements ServiceCaller {
private Logger logger = LoggerFactory.getLogger(this.getClass());
private static JsonHelper _jsh = new JsonHelper();
private String _namespace;
private String _url;
private int maxPayloadLengthToLog = -1;
public DefaultServiceCaller(String namespace_, String url_) {
_namespace = namespace_;
_url = url_;
logger.info("Created service caller: namespace={}, url={}", _namespace, _url);
}
public void setMaxPayloadLengthToLog(int maxPayloadLengthToLog) {
this.maxPayloadLengthToLog = maxPayloadLengthToLog;
}
public String serialize(Object args_) {
return _jsh.serialize(args_);
}
protected JsonServiceRequest createRequest(String service_,
Object serviceArgs_, String userId_, String sessionId_) {
JsonServiceRequest req = new JsonServiceRequest();
req.setSessionId(sessionId_);
req.setReqId("R-" +UUID.randomUUID().toString());
req.setUserId(userId_);
req.setNamespace(_namespace);
req.setService(service_);
req.setServiceArgsJson(serviceArgs_ instanceof String ?
(String)serviceArgs_ : serialize(serviceArgs_));
return req;
}
@Override
public void call(final JsonServiceRequest jsonServiceRequest_,
final ResponseHandler<?> handler_) throws ServiceCallException {
final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
try {
HttpRequestFactory requestFactory = HTTP_TRANSPORT
.createRequestFactory(new HttpRequestInitializer() {
@Override
public void initialize(HttpRequest request) {
}
});
GenericUrl url = new GenericUrl(_url);
HttpContent hc = new AbstractHttpContent("text/json") {
String content = serialize(jsonServiceRequest_);
@Override
public void writeTo(OutputStream out_) throws IOException {
String service = _url +jsonServiceRequest_.getService();
if (maxPayloadLengthToLog < 0 || content.length() <= maxPayloadLengthToLog) {
logger.info("calling service {} with payload {}", service, content);
} else {
logger.info("calling service {} with payload of {} chars (full payload in debug)", service, Integer.valueOf(content.length()));
logger.debug("payload {}", content);
}
out_.write(content.getBytes());
}
};
HttpRequest request = requestFactory.buildPostRequest(url, hc);
HttpResponse resp = request.execute();
InputStream is = resp.getContent();
JsonStreamHandler<?> jsh = new JsonStreamHandler(is, handler_);
} catch (IOException e) {
throw new ServiceCallException(e);
} finally {
// When HttpClient instance is no longer needed,
}
}
}