Last active
December 20, 2017 13:26
-
-
Save abbath0767/25616aaea284006bdaf1e1fedad57eb5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ru.gorkapstroy.sensorlogger.api.mock; | |
import android.content.Context; | |
import android.util.Log; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.lang.reflect.Method; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
import okhttp3.Interceptor; | |
import okhttp3.MediaType; | |
import okhttp3.Protocol; | |
import okhttp3.Request; | |
import okhttp3.Response; | |
import okhttp3.ResponseBody; | |
import retrofit2.http.DELETE; | |
import retrofit2.http.GET; | |
import retrofit2.http.HEAD; | |
import retrofit2.http.OPTIONS; | |
import retrofit2.http.POST; | |
import retrofit2.http.PUT; | |
/** | |
* Created by NG on 23.03.17. | |
*/ | |
public class MockInterceptorReflect implements Interceptor { | |
private static final String JSON_SUFFIX = ".json"; | |
private static final MediaType MEDIA_JSON = MediaType.parse("application/json"); | |
private static final String NULL_STRING = "not_found"; | |
private boolean LOGGING = true; | |
private String logTag = "TAG"; | |
private Context context; | |
private static Class clazz; | |
private static Method[] methods; | |
private static Map<String, List<String>> annotationValues; | |
private static Map<String, String> filesMap; | |
public MockInterceptorReflect(Class clazz, Context context) { | |
this.clazz = clazz; | |
this.context = context; | |
initAnnotationValues(); | |
} | |
private void initAnnotationValues() { | |
//all methods in Service have annotation | |
methods = clazz.getMethods(); | |
annotationValues = new HashMap<>(); | |
filesMap = new HashMap<>(); | |
initMapWithPaths(); | |
initMethods(); | |
fillFilesMap(); | |
if (LOGGING) { | |
for (Map.Entry<String, List<String>> entry : annotationValues.entrySet()) { | |
Log.d(logTag, "value (path): " + entry.getKey()); | |
Log.d(logTag, "key (annotation) count - " + entry.getValue().size() + ": "); | |
if (entry.getValue().size() != 0) { | |
for (String method : entry.getValue()) { | |
Log.d(logTag, " method: " + method); | |
} | |
} | |
} | |
} | |
} | |
private void initMapWithPaths() { | |
for (int i = 0; i < methods.length; i++) { | |
if (methods[i].isAnnotationPresent(POST.class)) | |
annotationValues.put(methods[i].getAnnotation(POST.class).value(), new ArrayList<String>()); | |
else if (methods[i].isAnnotationPresent(GET.class)) | |
annotationValues.put(methods[i].getAnnotation(GET.class).value(), new ArrayList<String>()); | |
else if (methods[i].isAnnotationPresent(DELETE.class)) | |
annotationValues.put(methods[i].getAnnotation(DELETE.class).value(), new ArrayList<String>()); | |
else if (methods[i].isAnnotationPresent(PUT.class)) | |
annotationValues.put(methods[i].getAnnotation(PUT.class).value(), new ArrayList<String>()); | |
else if (methods[i].isAnnotationPresent(HEAD.class)) | |
annotationValues.put(methods[i].getAnnotation(HEAD.class).value(), new ArrayList<String>()); | |
else if (methods[i].isAnnotationPresent(OPTIONS.class)) | |
annotationValues.put(methods[i].getAnnotation(OPTIONS.class).value(), new ArrayList<String>()); | |
} | |
} | |
private void initMethods() { | |
for (int i = 0; i < methods.length; i++) { | |
if (methods[i].isAnnotationPresent(POST.class)) { | |
// parse(methods[i], POST.class); | |
String path = methods[i].getAnnotation(POST.class).value(); | |
annotationValues.get(path).add(POST.class.getSimpleName()); | |
} else if (methods[i].isAnnotationPresent(GET.class)) { | |
String path = methods[i].getAnnotation(GET.class).value(); | |
annotationValues.get(path).add(GET.class.getSimpleName()); | |
} else if (methods[i].isAnnotationPresent(DELETE.class)) { | |
String path = methods[i].getAnnotation(DELETE.class).value(); | |
annotationValues.get(path).add(DELETE.class.getSimpleName()); | |
} else if (methods[i].isAnnotationPresent(PUT.class)) { | |
String path = methods[i].getAnnotation(PUT.class).value(); | |
annotationValues.get(path).add(PUT.class.getSimpleName()); | |
} else if (methods[i].isAnnotationPresent(HEAD.class)) { | |
String path = methods[i].getAnnotation(HEAD.class).value(); | |
annotationValues.get(path).add(HEAD.class.getSimpleName()); | |
} else if (methods[i].isAnnotationPresent(OPTIONS.class)) { | |
String path = methods[i].getAnnotation(OPTIONS.class).value(); | |
annotationValues.get(path).add(OPTIONS.class.getSimpleName()); | |
} | |
} | |
} | |
private void fillFilesMap() { | |
for (Map.Entry<String, List<String>> entry: annotationValues.entrySet()) { | |
String path = entry.getKey(); | |
for (String method: entry.getValue()) { | |
String jsonPath = fromPathAndMethodToAssetName(path, method.toLowerCase()); | |
String jsonString = getJsonStringFromPotentialJsonName(jsonPath); | |
if (LOGGING && !jsonString.equals(NULL_STRING)) { | |
Log.d(logTag, "file.json found: " + jsonPath); | |
} else if (LOGGING) { | |
Log.d(logTag, "file.json not found for path: " + jsonPath); | |
} | |
filesMap.put(jsonPath, jsonString); | |
} | |
} | |
} | |
@Override | |
public Response intercept(Chain chain) throws IOException { | |
String path = chain.request().url().uri().getPath(); | |
String method = chain.request().method().toLowerCase(); | |
String jsonPath = fromPathAndMethodToAssetName(path, method); | |
if (LOGGING) { | |
Log.d(logTag, "MockInterceptorReflect. Path: " + path); | |
Log.d(logTag, "MockInterceptorReflect. Method: " + method); | |
Log.d(logTag, "json path in raw: " + jsonPath); | |
} | |
if (filesMap.get(jsonPath) != null && !filesMap.get(jsonPath).equals(NULL_STRING)) { | |
Log.d(logTag, "value: " + filesMap.get(jsonPath)); | |
return getMockedResponse(filesMap.get(jsonPath), chain.request()); | |
} | |
return chain.proceed(chain.request()); | |
} | |
private String getJsonStringFromPotentialJsonName(String potentialJsonName) { | |
String json = NULL_STRING; | |
try { | |
InputStream is = context.getAssets().open(potentialJsonName + JSON_SUFFIX); | |
int size = is.available(); | |
byte[] buffer = new byte[size]; | |
is.read(buffer); | |
is.close(); | |
json = new String(buffer, "UTF-8"); | |
} catch (IOException e) { | |
//..error.. | |
} | |
return json; | |
} | |
private String fromPathAndMethodToAssetName(String path, String method) { | |
return path.replace("/", "_").substring(1) + "_" + method; | |
} | |
private Response getMockedResponse(String jsonString, Request request) { | |
return new Response.Builder() | |
.code(200) | |
.request(request) | |
.body(ResponseBody.create(MEDIA_JSON, jsonString)) | |
.protocol(Protocol.HTTP_2) | |
.build(); | |
} | |
public boolean isLOGGING() { | |
return LOGGING; | |
} | |
public void setLOGGING(boolean LOGGING) { | |
this.LOGGING = LOGGING; | |
} | |
public String getLogTag() { | |
return logTag; | |
} | |
public void setLogTag(String logTag) { | |
this.logTag = logTag; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment