-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[NR-277770] Response interception support in mule server (#298)
* NR-277770 : Response interception in mule server
- Loading branch information
1 parent
d8aa936
commit 3873293
Showing
13 changed files
with
424 additions
and
149 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
46 changes: 46 additions & 0 deletions
46
...c/main/java/org/mule/module/http/internal/domain/ByteArrayHttpEntity_Instrumentation.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,46 @@ | ||
package org.mule.module.http.internal.domain; | ||
|
||
import com.newrelic.agent.security.instrumentation.mule36.MuleHelper; | ||
import com.newrelic.api.agent.security.NewRelicSecurity; | ||
import com.newrelic.api.agent.security.instrumentation.helpers.GenericHelper; | ||
import com.newrelic.api.agent.security.schema.SecurityMetaData; | ||
import com.newrelic.api.agent.security.utils.logging.LogLevel; | ||
import com.newrelic.api.agent.weaver.Weave; | ||
import com.newrelic.api.agent.weaver.Weaver; | ||
import org.apache.commons.io.Charsets; | ||
import org.mule.util.IOUtils; | ||
|
||
import java.io.IOException; | ||
import java.util.Objects; | ||
|
||
@Weave(originalName = "org.mule.module.http.internal.domain.ByteArrayHttpEntity") | ||
public class ByteArrayHttpEntity_Instrumentation { | ||
|
||
public byte[] getContent() { | ||
byte[] content = Weaver.callOriginal(); | ||
try { | ||
extractResponseBody(content); | ||
} catch (Exception e) { | ||
NewRelicSecurity.getAgent().log(LogLevel.SEVERE, String.format(GenericHelper.ERROR_PARSING_HTTP_RESPONSE_BODY, MuleHelper.MULE_36, e.getMessage()), e, this.getClass().getName()); | ||
NewRelicSecurity.getAgent().reportIncident(LogLevel.SEVERE , String.format(GenericHelper.ERROR_PARSING_HTTP_RESPONSE_BODY, MuleHelper.MULE_36, e.getMessage()), e, this.getClass().getName()); | ||
} | ||
return content; | ||
} | ||
|
||
private void extractResponseBody(byte[] content) throws IOException { | ||
if (NewRelicSecurity.isHookProcessingActive()){ | ||
String encoding = NewRelicSecurity.getAgent().getSecurityMetaData().getCustomAttribute(MuleHelper.MULE_ENCODING, String.class); | ||
if (encoding == null || encoding.isEmpty()){ | ||
encoding = Charsets.UTF_8.name(); | ||
} | ||
String body = IOUtils.toString(content, encoding); | ||
|
||
SecurityMetaData securityMetaData = NewRelicSecurity.getAgent().getSecurityMetaData(); | ||
if (MuleHelper.preprocessStream(this.hashCode(), MuleHelper.RESPONSE_ENTITY_STREAM)) { | ||
securityMetaData.getResponse().getResponseBody().append(body); | ||
} else if (MuleHelper.preprocessStream(this.hashCode(), MuleHelper.REQUEST_ENTITY_STREAM)) { | ||
securityMetaData.getRequest().getBody().append(body); | ||
} | ||
} | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
...main/java/org/mule/module/http/internal/domain/InputStreamHttpEntity_Instrumentation.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,41 @@ | ||
package org.mule.module.http.internal.domain; | ||
|
||
import com.newrelic.agent.security.instrumentation.mule36.MuleHelper; | ||
import com.newrelic.api.agent.security.NewRelicSecurity; | ||
import com.newrelic.api.agent.security.instrumentation.helpers.GenericHelper; | ||
import com.newrelic.api.agent.security.schema.SecurityMetaData; | ||
import com.newrelic.api.agent.security.utils.logging.LogLevel; | ||
import com.newrelic.api.agent.weaver.Weave; | ||
import com.newrelic.api.agent.weaver.Weaver; | ||
|
||
import java.io.InputStream; | ||
import java.util.Objects; | ||
|
||
@Weave(originalName = "org.mule.module.http.internal.domain.InputStreamHttpEntity") | ||
public class InputStreamHttpEntity_Instrumentation { | ||
|
||
public InputStream getInputStream() { | ||
InputStream stream = Weaver.callOriginal(); | ||
try { | ||
extractResponseBody(stream); | ||
} catch (Exception e) { | ||
NewRelicSecurity.getAgent().log(LogLevel.SEVERE, String.format(GenericHelper.ERROR_PARSING_HTTP_RESPONSE_BODY, MuleHelper.MULE_36, e.getMessage()), e, this.getClass().getName()); | ||
NewRelicSecurity.getAgent().reportIncident(LogLevel.SEVERE , String.format(GenericHelper.ERROR_PARSING_HTTP_RESPONSE_BODY, MuleHelper.MULE_36, e.getMessage()), e, this.getClass().getName()); | ||
} | ||
return stream; | ||
} | ||
|
||
private void extractResponseBody(InputStream stream) { | ||
if (NewRelicSecurity.isHookProcessingActive() && stream != null) { | ||
// check if it is an input or output stream | ||
// outputBody stream | ||
if (MuleHelper.preprocessStream(this.hashCode(), MuleHelper.RESPONSE_ENTITY_STREAM)) { | ||
MuleHelper.registerStreamHashIfNeeded(stream.hashCode(), MuleHelper.RESPONSE_OUTPUTSTREAM_HASH); | ||
} | ||
// inputBody stream | ||
else if (MuleHelper.preprocessStream(this.hashCode(), MuleHelper.REQUEST_ENTITY_STREAM)) { | ||
MuleHelper.registerStreamHashIfNeeded(stream.hashCode(), MuleHelper.REQUEST_INPUTSTREAM_HASH); | ||
} | ||
} | ||
} | ||
} |
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
Oops, something went wrong.