Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*******************************************************************************
* Copyright (c) 2011 Sonatype, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
* The Apache License v2.0 is available at
* http://www.apache.org/licenses/LICENSE-2.0.html
* You may elect to redistribute this code under either of these licenses.
*******************************************************************************/
package org.sitebricks.client.easy.internal;
import com.google.sitebricks.At;
import com.google.sitebricks.client.Web;
import com.google.sitebricks.client.WebClient;
import com.google.sitebricks.client.WebResponse;
import com.google.sitebricks.client.transport.Json;
import com.google.sitebricks.http.Delete;
import com.google.sitebricks.http.Get;
import com.google.sitebricks.http.Patch;
import com.google.sitebricks.http.Post;
import com.google.sitebricks.http.Put;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.type.TypeFactory;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Map;
public class DefaultRestClient implements InvocationHandler {
private static final HttpMethod[] METHODS = new HttpMethod[] { new GetHttpMethod(), new PutHttpMethod(), new PatchHttpMethod(), new DeleteHttpMethod(), new PostHttpMethod() };
private final URL baseUrl;
private final Web web;
private final Map bindings;
private final Class> serviceInterface;
private final ObjectMapper mapper;
private final TypeFactory typeFactory;
public DefaultRestClient(final Web web, final ObjectMapper mapper, final Class> serviceInterface, final Map bindings, final URL baseUrl) {
this.mapper = mapper;
this.serviceInterface = serviceInterface;
this.baseUrl = baseUrl;
this.web = web;
this.bindings = bindings;
typeFactory = TypeFactory.defaultInstance();
}
@Override
public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
final HttpMethod httpMethod = getHttpMethod(method);
final String url = getUrl(method, args);
final Class extends Object> transportType = getTransportType(method);
final WebClient extends Object> webClient = web.clientOf(url).transports(transportType).over(Json.class);
try {
@SuppressWarnings("unchecked")
final WebResponse restResponse = httpMethod.invoke((WebClient