-
Notifications
You must be signed in to change notification settings - Fork 362
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sdk/java: backport Java SDK 1.1.1 to 1.1-stable
- Loading branch information
1 parent
3e42a33
commit e600c7f
Showing
5 changed files
with
195 additions
and
12 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
76 changes: 76 additions & 0 deletions
76
sdk/java/src/main/java/com/chain/http/LoggingInterceptor.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package com.chain.http; | ||
|
||
import com.squareup.okhttp.Interceptor; | ||
import com.squareup.okhttp.Request; | ||
import com.squareup.okhttp.Response; | ||
import okio.BufferedSink; | ||
import okio.BufferedSource; | ||
|
||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.nio.charset.Charset; | ||
|
||
/** | ||
* The LoggingInterceptor object logs http requests given | ||
* an output stream. | ||
*/ | ||
public class LoggingInterceptor implements Interceptor { | ||
private Level level; | ||
private OutputStream logger; | ||
|
||
public enum Level { | ||
ALL, | ||
ERRORS, | ||
NONE, | ||
} | ||
|
||
public LoggingInterceptor(OutputStream logger, Level logAllRequests) { | ||
this.logger = logger; | ||
this.level = logAllRequests; | ||
} | ||
|
||
@Override | ||
public Response intercept(Interceptor.Chain chain) throws IOException { | ||
Request request = chain.request(); | ||
Response response = chain.proceed(request); | ||
|
||
boolean isError = (response.code() / 100) == 5 || (response.code() / 100) == 4; | ||
if ((isError && level == level.ERRORS) || level == level.ALL) { | ||
logRequestData(request, response); | ||
} | ||
|
||
return response; | ||
} | ||
|
||
public void logRequestData(Request request, Response response) throws IOException { | ||
String reqid = response.header("Chain-Request-Id"); | ||
|
||
String requestBody; | ||
try { | ||
BufferedSink reqBodySink = new okio.Buffer(); | ||
request.body().writeTo(reqBodySink); | ||
requestBody = new String(reqBodySink.buffer().readByteArray()); | ||
} catch (IOException e) { | ||
requestBody = "Unable to read request body."; | ||
} | ||
|
||
String label = "chain-request"; | ||
if (response.code() / 100 == 5) { | ||
label = "chain-error"; | ||
} | ||
|
||
BufferedSource source = response.body().source(); | ||
source.request(Long.MAX_VALUE); | ||
|
||
logger.write( | ||
String.format( | ||
"%s:\n\treqid=%s\n\turl=%s\n\tcode=%d\n\trequest=%s\n\tresponse=%s\n", | ||
label, | ||
reqid, | ||
request.urlString(), | ||
response.code(), | ||
requestBody, | ||
source.buffer().clone().readString(Charset.forName("UTF-8"))) | ||
.getBytes()); | ||
} | ||
} |