Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions Parse/src/main/java/com/parse/ParseNetworkInterceptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@
/** package */ interface ParseNetworkInterceptor {

/**
* Intercept the {@link ParseHttpRequest} with the help of
* {@link com.parse.ParseNetworkInterceptor.Chain}, proceed the {@link ParseHttpRequest} and get
* the {@link ParseHttpResponse}, intercept the {@link ParseHttpResponse} and return the
* intercepted {@link ParseHttpResponse}.
* @param chain
* The helper chain we used to get the request, proceed the request and receive the
* response.
* @return The intercepted response.
* The helper chain we used to get the {@link ParseHttpRequest}, proceed the
* {@link ParseHttpRequest} and receive the {@link ParseHttpResponse}.
* @return The intercepted {@link ParseHttpResponse}.
* @throws IOException
*/
ParseHttpResponse intercept(Chain chain) throws IOException;
Expand Down
89 changes: 89 additions & 0 deletions ParseNetworkLogger/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
apply plugin: 'com.android.library'

group = 'com.parse'
version = VERSION_NAME

buildscript {
repositories {
mavenCentral()
}
}

android {
compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion

defaultConfig {
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 1
versionName VERSION_NAME
}

lintOptions {
abortOnError false
}

buildTypes {
debug {
testCoverageEnabled = true
}
}
}

dependencies {
compile project(':Parse')

provided 'com.facebook.stetho:stetho:1.1.1'

testCompile 'junit:junit:4.12'
testCompile "org.json:json:20140107"
}

android.libraryVariants.all { variant ->
def name = variant.buildType.name
def jar = project.tasks.create(name: "jar${name.capitalize()}", type: Jar) {
dependsOn variant.javaCompile
from variant.javaCompile.destinationDir

manifest {
attributes(
"Bundle-Name": 'parse-network-logger',
"Bundle-Version": project.version
)
}

exclude '**/R.class'
exclude '**/R\$*.class'
exclude '**/Manifest.class'
exclude '**/Manifest\$*.class'
exclude '**/BuildConfig.class'
}

def javadoc = task("javadoc${variant.name.capitalize()}", type: Javadoc) {
description "Generates Javadoc for $variant.name."
source = variant.javaCompile.source
ext.androidJar = "${android.sdkDirectory}/platforms/${android.compileSdkVersion}/android.jar"
classpath = files(variant.javaCompile.classpath.files) + files(ext.androidJar)

options.docletpath = [rootProject.file("./gradle/ExcludeDoclet.jar")]
options.doclet = "me.grantland.doclet.ExcludeDoclet"

options.linksOffline("http://d.android.com/reference", "${android.sdkDirectory}/docs/reference")
options.links("http://boltsframework.github.io/docs/android/")

exclude '**/BuildConfig.java'
exclude '**/R.java'
exclude '**/internal/**'
}

def javadocJar = task("javadocJar${variant.name.capitalize()}", type: Jar, dependsOn: "javadoc${variant.name.capitalize()}") {
classifier = 'javadoc'
from javadoc.destinationDir
}

if (name.equals(com.android.builder.core.BuilderConstants.RELEASE)) {
artifacts.add('archives', jar);
artifacts.add('archives', javadocJar);
}
}
12 changes: 12 additions & 0 deletions ParseNetworkLogger/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (c) 2015-present, Parse, LLC.
~ All rights reserved.
~
~ This source code is licensed under the BSD-style license found in the
~ LICENSE file in the root directory of this source tree. An additional grant
~ of patent rights can be found in the PATENTS file in the same directory.
-->
<manifest package="com.parse.networklogger">
<application />
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
* {@code ParseLogInterceptor} is used to log the request and response information to the given
* logger.
*/
/** package */ class ParseLogInterceptor implements ParseNetworkInterceptor {
public class ParseLogInterceptor implements ParseNetworkInterceptor {

private final static String TAG = "ParseLogNetworkInterceptor";
private final static String LOG_PARAGRAPH_BREAKER = "--------------";
Expand All @@ -58,7 +58,7 @@
* The {@code Logger} to log the request and response information.
*/
public static abstract class Logger {
public static String NEW_LINE = "\n";
private static String NEW_LINE = System.getProperty("line.separator");

// The reason we need a lock here is since multiple network threads may write to the
// logger concurrently, the message of different threads may intertwined. We need this
Expand All @@ -69,25 +69,58 @@ public Logger() {
lock = new ReentrantLock();
}

/**
* Write the given string through {@code Logger}.
* @param str
* The string to be written.
*/
public abstract void write(String str);

/**
* Lock the {@code Logger}. Since multiple threads may use a same {@code Logger}, we need to
* to lock the {@code Logger} to make sure content written by different threads does not
* intertwined.
*/
public void lock() {
lock.lock();
}

/**
* Unlock the {@code Logger}.
*/
public void unlock() {
lock.unlock();
}

/**
* Write a key-value pair through {@code Logger}. The name and value are separated by :.
* @param name
* The key of the key-value pair to be written.
* @param value
* The value of the key-value pair to be written.
*/
public void write(String name, String value) {
write(name + " : " + value);
}

/**
* Write the given string followed by a newline through {@code Logger}.
* @param str
* The string to be written.
*/
public void writeLine(String str) {
write(str);
write(NEW_LINE);
}

/**
* Write a key-value pair followed by a newline through {@code Logger}. The name and value are
* separated by :.
* @param name
* The key of the key-value pair to be written.
* @param value
* The value of the key-value pair to be written.
*/
public void writeLine(String name, String value) {
writeLine(name + " : " + value);
}
Expand All @@ -100,6 +133,11 @@ private static class LogcatLogger extends Logger {

private static int MAX_MESSAGE_LENGTH = 4000;

/**
* Write the given string to Android logcat.
* @param str
* The string to be written.
*/
@Override
public void write(String str) {
// Logcat can only print the limited number of characters in one line, so when we have a long
Expand All @@ -112,6 +150,13 @@ public void write(String str) {
}
}

/**
* Write the given string to Android logcat followed by a newline. Since Android {@link Log}
* actually adds newline for each write, we need to override this method to avoid adding
* additional newlines.
* @param str
* The string to be written.
*/
@Override
public void writeLine(String str) {
// Logcat actually writes in a new line every time, so we need to rewrite it
Expand Down Expand Up @@ -231,6 +276,14 @@ private Logger getLogger() {
// Request Id generator
private final AtomicInteger nextRequestId = new AtomicInteger(0);

/**
* Intercept the {@link ParseHttpRequest} and {@link ParseHttpResponse}.
* @param chain
* The helper chain we use to get the {@link ParseHttpRequest}, proceed the
* {@link ParseHttpRequest} and receive the {@link ParseHttpResponse}.
* @return The intercept {@link ParseHttpResponse}.
* @throws IOException
*/
@Override
public ParseHttpResponse intercept(Chain chain) throws IOException {
// Intercept request
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* {@code ParseStethoInterceptor} is used to log the request and response through Stetho to chrome
* browser debugger.
*/
/** package */ class ParseStethoInterceptor implements ParseNetworkInterceptor {
public class ParseStethoInterceptor implements ParseNetworkInterceptor {

private static final String CONTENT_LENGTH_HEADER = "Content-Length";
private static final String CONTENT_TYPE_HEADER = "Content-Type";
Expand Down Expand Up @@ -225,6 +225,14 @@ public String firstHeaderValue(String name) {
// Request Id generator
private final AtomicInteger nextRequestId = new AtomicInteger(0);

/**
* Intercept the {@link ParseHttpRequest} and {@link ParseHttpResponse}.
* @param chain
* The helper chain we use to get the {@link ParseHttpRequest}, proceed the
* {@link ParseHttpRequest} and receive the {@link ParseHttpResponse}.
* @return The intercepted {@link ParseHttpResponse}.
* @throws IOException
*/
@Override
public ParseHttpResponse intercept(Chain chain) throws IOException {
// Intercept request
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricGradleTestRunner;
import org.robolectric.annotation.Config;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
Expand All @@ -26,9 +23,6 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

// For GZIPOutputStream
@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 21)
public class ParseLogInterceptorTest {

@Rule
Expand Down
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
include ':Parse'
include ':ParseStarterProject'

include ':ParseNetworkLogger'