Skip to content
This repository has been archived by the owner on Jun 8, 2018. It is now read-only.

Commit

Permalink
Implement codec interface
Browse files Browse the repository at this point in the history
- implement codec interface
- extend junit tests
- drop out annotations module and merge into core module

Conflicts:
	apitrary-orm/apitrary-orm-annotations/pom.xml
	apitrary-orm/apitrary-orm-codec/pom.xml
	apitrary-orm/apitrary-orm-core/pom.xml
  • Loading branch information
denisneuling committed Mar 4, 2013
2 parents 01c7170 + 47ba625 commit ce1341d
Show file tree
Hide file tree
Showing 43 changed files with 493 additions and 396 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@
import com.apitrary.api.client.exception.CommunicationErrorException;
import com.apitrary.api.client.exception.SerializationException;
import com.apitrary.api.client.support.AbstractApitraryClient;
import com.apitrary.api.client.util.NormalizationUtil;
import com.apitrary.api.client.util.RequestUtil;
import com.apitrary.api.request.Request;
import com.apitrary.api.response.Response;
import com.apitrary.api.response.normalized.Normalizer;

/**
* <p>
Expand All @@ -45,13 +47,17 @@ public class ApitraryClient extends AbstractApitraryClient {

private ApitraryApi api;

private int connectionTimeOut = DEFAULTCONNECTIONTIMEOUT;
private int connectionTimeout = DEFAULTCONNECTIONTIMEOUT;
private int receiveTimeout = DEFAULTRECEIVETIMEOUT;

private ApitraryClient(ApitraryApi api) {
protected ApitraryClient(ApitraryApi api) {
this.api = api;
}

protected ApitraryClient() {
throw new RuntimeException("Apitrary Client needs to connect to the targeted API. Hint: Use factory method #connectTo(ApitraryApi api)");
}

/**
* <p>
* connectTo.
Expand Down Expand Up @@ -99,6 +105,10 @@ protected <T> String inquireVHost() {
@Override
protected <T> Response<T> deserialize(String response, Request<T> request) {
Response<T> target = RequestUtil.getInstanceOfParameterizedType(request);

Normalizer normalizer = NormalizationUtil.getNormalizer(target);
response = normalizer.normalize(response);

target.setResult(response);
return target;
}
Expand Down Expand Up @@ -132,8 +142,8 @@ protected WebClient instantiateWebClient() {
params.setDisableCNCheck(true);

HTTPClientPolicy policy = new HTTPClientPolicy();
policy.setConnectionTimeout(connectionTimeOut);
policy.setReceiveTimeout(receiveTimeout);
policy.setConnectionTimeout(getConnectionTimeout());
policy.setReceiveTimeout(getReceiveTimeout());
policy.setAllowChunking(false);
conduit.setClient(policy);

Expand All @@ -142,25 +152,25 @@ protected WebClient instantiateWebClient() {

/**
* <p>
* Getter for the field <code>connectionTimeOut</code>.
* Getter for the field <code>connectionTimeout</code>.
* </p>
*
* @return a int.
*/
public int getConnectionTimeOut() {
return connectionTimeOut;
public int getConnectionTimeout() {
return connectionTimeout;
}

/**
* <p>
* Setter for the field <code>connectionTimeOut</code>.
* Setter for the field <code>connectionTimeout</code>.
* </p>
*
* @param connectionTimeOut
* @param connectionTimeout
* a int.
*/
public void setConnectionTimeOut(int connectionTimeOut) {
this.connectionTimeOut = connectionTimeOut;
public void setConnectionTimeout(int connectionTimeout) {
this.connectionTimeout = connectionTimeout;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,6 @@ public static HttpStatus getStatus(int code) {
return status;
}

/*
* (non-Javadoc)
*
* @see java.lang.Enum#toString()
*/
/** {@inheritDoc} */
@Override
public String toString() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,19 @@ public abstract class AbstractApitraryClient {

/** Constant <code>apitraryUrl="api.apitrary.com"</code> */
protected static final String apitraryUrl = "api.apitrary.com";

/** Constant <code>protocol="http://"</code> */
protected static final String protocol = "http://";
protected static final String protocol = "https://";

/** Constant <code>apiAuthHeaderKey="X-Api-Key"</code> */
protected static final String apiAuthHeaderKey = "X-Api-Key";

/** Constant <code>contentType="application/json"</code> */
protected static final String contentType = "application/json";

/** Constant <code>DEFAULTCONNECTIONTIMEOUT=60000</code> */
protected static final int DEFAULTCONNECTIONTIMEOUT = 60000;

/** Constant <code>DEFAULTRECEIVETIMEOUT=60000</code> */
protected static final int DEFAULTRECEIVETIMEOUT = 60000;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.logging.Logger;

/**
* <p>
Expand All @@ -32,6 +33,7 @@
*
*/
public class ClassUtil {
private static Logger log = Logger.getLogger(NormalizationUtil.class.getName());

/**
* <p>
Expand All @@ -58,6 +60,7 @@ public static <T> T getClassAnnotationValue(Class source, Class annotation, Stri
try {
value = (T) instance.annotationType().getMethod(attributeName).invoke(instance);
} catch (Exception ex) {
log.warning(ex.getClass().getSimpleName()+": "+ex.getMessage());
}
}
return value;
Expand Down Expand Up @@ -195,6 +198,7 @@ public static <T> T getValueOf(Field field, Object reference, Class<?> reference
}
return (T) toReturn;
} catch (Exception e) {
log.warning(e.getClass().getSimpleName()+": "+e.getMessage());
return null;
}
}
Expand Down Expand Up @@ -227,6 +231,7 @@ public static <T> T getValueOf(String fieldName, Object reference, Class<?> refe
T toReturn = (T) field.get(reference);
return toReturn;
} catch (Exception e) {
log.warning(e.getClass().getSimpleName()+": "+e.getMessage());
return null;
}
}
Expand All @@ -248,7 +253,9 @@ public static Object getValueOfField(Field field, Object ref) {
try {
value = field.get(ref);
} catch (IllegalArgumentException e) {
log.warning(e.getClass().getSimpleName()+": "+e.getMessage());
} catch (IllegalAccessException e) {
log.warning(e.getClass().getSimpleName()+": "+e.getMessage());
}
return value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package com.apitrary.api.client.util;

import java.util.logging.Logger;

import com.apitrary.api.annotation.Normalized;
import com.apitrary.api.response.Response;
import com.apitrary.api.response.normalized.Normalizer;
Expand All @@ -28,6 +30,7 @@
*
*/
public class NormalizationUtil {
private static Logger log = Logger.getLogger(NormalizationUtil.class.getName());

/**
* <p>
Expand All @@ -43,12 +46,14 @@ public class NormalizationUtil {
@SuppressWarnings("unchecked")
public static <T> Normalizer getNormalizer(Response<T> response) {
Normalizer normalizer = new Normalizer();
Class<Normalizer> standardizerClazz = ClassUtil.getClassAnnotationValue(response.getClass(), Normalized.class, "value", Class.class);
if (standardizerClazz != null) {
Class<Normalizer> normalizerClazz = ClassUtil.getClassAnnotationValue(response.getClass(), Normalized.class, "value", Class.class);
if (normalizerClazz != null) {
try {
normalizer = standardizerClazz.newInstance();
normalizer = normalizerClazz.newInstance();
} catch (InstantiationException e) {
log.warning(e.getClass().getSimpleName()+": "+e.getMessage());
} catch (IllegalAccessException e) {
log.warning(e.getClass().getSimpleName()+": "+e.getMessage());
}
}
return normalizer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package com.apitrary.api.request;

import java.io.Serializable;

import com.apitrary.api.annotation.Method;
import com.apitrary.api.annotation.Path;
import com.apitrary.api.common.HttpMethod;
Expand All @@ -30,7 +32,7 @@
*/
@Method(HttpMethod.GET)
@Path("/")
public class APIStateRequest extends Request<APIStateResponse> {
public class APIStateRequest extends Request<APIStateResponse> implements Serializable{
private static final long serialVersionUID = -2599282917840158769L;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package com.apitrary.api.request;

import java.io.Serializable;

import com.apitrary.api.annotation.Method;
import com.apitrary.api.annotation.Path;
import com.apitrary.api.annotation.PathVariable;
Expand All @@ -32,7 +34,7 @@
*/
@Method(HttpMethod.DELETE)
@Path("/${entity}/${id}")
public class DeleteRequest extends Request<DeleteResponse> {
public class DeleteRequest extends Request<DeleteResponse> implements Serializable{
private static final long serialVersionUID = -8265792383976749317L;

@Required
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package com.apitrary.api.request;

import java.io.Serializable;

import com.apitrary.api.annotation.Default;
import com.apitrary.api.annotation.Method;
import com.apitrary.api.annotation.Path;
Expand All @@ -33,7 +35,7 @@
*/
@Method(HttpMethod.GET)
@Path("/${entity}/${id}")
public class GetRequest extends Request<GetResponse> {
public class GetRequest extends Request<GetResponse> implements Serializable{
private static final long serialVersionUID = 626610613858401470L;

@Required
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package com.apitrary.api.request;

import java.io.Serializable;

import com.apitrary.api.annotation.Body;
import com.apitrary.api.annotation.Method;
import com.apitrary.api.annotation.Path;
Expand All @@ -33,7 +35,7 @@
*/
@Method(HttpMethod.POST)
@Path("/${entity}")
public class PostRequest extends Request<PostResponse> {
public class PostRequest extends Request<PostResponse> implements Serializable{
private static final long serialVersionUID = 7454490450438849781L;

@Required
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package com.apitrary.api.request;

import java.io.Serializable;

import com.apitrary.api.annotation.Body;
import com.apitrary.api.annotation.Method;
import com.apitrary.api.annotation.Path;
Expand All @@ -33,7 +35,7 @@
*/
@Method(HttpMethod.PUT)
@Path("/${entity}/${id}")
public class PutRequest extends Request<PutResponse> {
public class PutRequest extends Request<PutResponse> implements Serializable{
private static final long serialVersionUID = 7880320624981198826L;

@Required
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package com.apitrary.api.request;

import java.io.Serializable;

import com.apitrary.api.annotation.Method;
import com.apitrary.api.annotation.Path;
import com.apitrary.api.annotation.PathVariable;
Expand All @@ -34,7 +36,7 @@
@Method(HttpMethod.GET)
@Path("/${entity}")
/* a/id || a?q= */
public class QueriedGetRequest extends Request<QueriedGetResponse> {
public class QueriedGetRequest extends Request<QueriedGetResponse> implements Serializable{
private static final long serialVersionUID = 7586680719718414828L;

@Required
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@
*/
@Normalized
public class APIStateResponse extends Response<APIStateResponse> {
private static final long serialVersionUID = 3881869281324737452L;

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@
*/
@Normalized
public class DeleteResponse extends Response<DeleteResponse> {
private static final long serialVersionUID = -4818084375753188141L;

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package com.apitrary.api.response;

import java.io.Serializable;

/**
* <p>
* Abstract Response class.
Expand All @@ -23,8 +25,9 @@
* @author Denis Neuling ([email protected])
*
*/
public abstract class Response<T> {

public abstract class Response<T> implements Serializable{
private static final long serialVersionUID = 5050233742143155805L;

protected String result;
protected String statusMessage;
protected int statusCode;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2012-2013 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.common.status;

import java.io.Serializable;

import junit.framework.Assert;

import org.junit.Test;

/**
* @author Denis Neuling ([email protected])
*
*/
public class GeneralCommonStatusTest {

@Test
public void test_isSerializable(){
Assert.assertTrue(Serializable.class.isAssignableFrom(API.class));
Assert.assertTrue(Serializable.class.isAssignableFrom(APIState.class));
Assert.assertTrue(Serializable.class.isAssignableFrom(Info.class));
Assert.assertTrue(Serializable.class.isAssignableFrom(Status.class));
}
}
Loading

0 comments on commit ce1341d

Please sign in to comment.