All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.apitrary.api.client.support.AbstractApitraryClient Maven / Gradle / Ivy

There is a newer version: 1.0.0
Show newest version
/*
 * Copyright 2012 Denis Neuling 
 * 
 * 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 com.apitrary.api.client.support;

import java.io.InputStream;

import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.log4j.Logger;

import com.apitrary.api.client.common.HttpStatus;
import com.apitrary.api.client.common.Timer;
import com.apitrary.api.client.exception.CommunicationErrorException;
import com.apitrary.api.client.util.HttpMethodUtil;
import com.apitrary.api.client.util.PathUtil;
import com.apitrary.api.client.util.RequestUtil;
import com.apitrary.api.common.HttpMethod;
import com.apitrary.api.request.Request;
import com.apitrary.api.response.Response;

/**
 * 

* Abstract AbstractApitraryClient class. *

* * @author Denis Neuling ([email protected]) * */ public abstract class AbstractApitraryClient { protected final Logger log = Logger.getLogger(this.getClass()); /** Constant apitraryUrl="api.apitrary.com" */ protected static final String apitraryUrl = "api.apitrary.com"; /** Constant protocol="http://" */ protected static final String protocol = "http://"; /** Constant apiAuthHeaderKey="X-Api-Key" */ protected static final String apiAuthHeaderKey = "X-Api-Key"; /** Constant contentType="application/json" */ protected static final String contentType = "application/json"; /** Constant DEFAULTCONNECTIONTIMEOUT=60000 */ protected static final int DEFAULTCONNECTIONTIMEOUT = 60000; /** Constant DEFAULTRECEIVETIMEOUT=60000 */ protected static final int DEFAULTRECEIVETIMEOUT = 60000; /** *

* dispatchByMethod. *

* * @param request * a {@link com.apitrary.api.request.Request} object. * @param * a T object. * @return a {@link com.apitrary.api.response.Response} object. */ protected Response dispatchByMethod(Request request) { HttpMethod method = HttpMethodUtil.retrieveMethod(request); WebClient webClient = instantiateWebClient(); switch (method) { case GET: return doGet(webClient, request); case POST: return doPost(webClient, request); case PUT: return doPut(webClient, request); case DELETE: return doDelete(webClient, request); default: throw new CommunicationErrorException(HttpStatus.Not_Implemented); } } /** *

* doGet. *

* * @param webClient * a {@link org.apache.cxf.jaxrs.client.WebClient} object. * @param request * a {@link com.apitrary.api.request.Request} object. * @param * a T object. * @return a {@link com.apitrary.api.response.Response} object. */ protected Response doGet(WebClient webClient, Request request) { webClient = webClient.path(inquirePath(request)); webClient = RequestUtil.resolveAndSetQueryPart(request, webClient); Timer timer = Timer.tic(); javax.ws.rs.core.Response cxfResponse = webClient.get(); timer.toc(); log.trace(cxfResponse.getStatus() + " " + webClient.getCurrentURI() + " took " + timer.getDifference() + "ms"); Response response = toResponse(timer, cxfResponse, request); return response; } /** *

* doPost. *

* * @param webClient * a {@link org.apache.cxf.jaxrs.client.WebClient} object. * @param request * a {@link com.apitrary.api.request.Request} object. * @param * a T object. * @return a {@link com.apitrary.api.response.Response} object. */ protected Response doPost(WebClient webClient, Request request) { webClient = webClient.path(inquirePath(request)); webClient = RequestUtil.resolveAndSetQueryPart(request, webClient); String payload = RequestUtil.getRequestPayload(request); Timer timer = Timer.tic(); javax.ws.rs.core.Response cxfResponse = webClient.post(payload); timer.toc(); log.trace(cxfResponse.getStatus() + " " + webClient.getCurrentURI() + " took " + timer.getDifference() + "ms"); Response response = toResponse(timer, cxfResponse, request); return response; } /** *

* doPut. *

* * @param webClient * a {@link org.apache.cxf.jaxrs.client.WebClient} object. * @param request * a {@link com.apitrary.api.request.Request} object. * @param * a T object. * @return a {@link com.apitrary.api.response.Response} object. */ protected Response doPut(WebClient webClient, Request request) { webClient = webClient.path(inquirePath(request)); webClient = RequestUtil.resolveAndSetQueryPart(request, webClient); String payload = RequestUtil.getRequestPayload(request); Timer timer = Timer.tic(); javax.ws.rs.core.Response cxfResponse = webClient.put(payload); timer.toc(); log.trace(cxfResponse.getStatus() + " " + webClient.getCurrentURI() + " took " + timer.getDifference() + "ms"); Response response = toResponse(timer, cxfResponse, request); return response; } /** *

* doDelete. *

* * @param webClient * a {@link org.apache.cxf.jaxrs.client.WebClient} object. * @param request * a {@link com.apitrary.api.request.Request} object. * @param * a T object. * @return a {@link com.apitrary.api.response.Response} object. */ protected Response doDelete(WebClient webClient, Request request) { webClient = webClient.path(inquirePath(request)); webClient = RequestUtil.resolveAndSetQueryPart(request, webClient); Timer timer = Timer.tic(); javax.ws.rs.core.Response cxfResponse = webClient.delete(); timer.toc(); log.trace(cxfResponse.getStatus() + " " + webClient.getCurrentURI() + " took " + timer.getDifference() + "ms"); Response response = toResponse(timer, cxfResponse, request); return response; } private Response toResponse(Timer timer, javax.ws.rs.core.Response cxfResponse, Request request) { InputStream inputStream = (InputStream) cxfResponse.getEntity(); Response response = null; HttpStatus status = HttpStatus.getStatus(cxfResponse.getStatus()); response = deserialize(inputStream, request); response.setStatusCode(status.getCode()); response.setResponseTime(timer.getDifference()); return response; } /** *

* inquirePath. *

* * @param request * a {@link com.apitrary.api.request.Request} object. * @param * a T object. * @return a {@link java.lang.String} object. */ protected String inquirePath(Request request) { return PathUtil.resolveResourcePath(request); } /** *

* inquireVHost. *

* * @param * a T object. * @return a {@link java.lang.String} object. */ protected abstract String inquireVHost(); /** *

* deserialize. *

* * @param response * a {@link java.lang.String} object. * @param request * a {@link com.apitrary.api.request.Request} object. * @param * a T object. * @return a {@link com.apitrary.api.response.Response} object. */ protected abstract Response deserialize(String response, Request request); /** *

* deserialize. *

* * @param inputStream * a {@link java.io.InputStream} object. * @param request * a {@link com.apitrary.api.request.Request} object. * @param * a T object. * @return a {@link com.apitrary.api.response.Response} object. */ protected abstract Response deserialize(InputStream inputStream, Request request); /** *

* instantiateWebClient. *

* * @return a {@link org.apache.cxf.jaxrs.client.WebClient} object. */ protected abstract WebClient instantiateWebClient(); }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy