Skip to content

Commit

Permalink
Merge pull request Netflix#150 from freddd/updated-simple-example-new…
Browse files Browse the repository at this point in the history
…-httpclient

Updated httpclient and cleaned up example
  • Loading branch information
NiteshKant committed Sep 15, 2015
2 parents d567a42 + c9dabb7 commit cddd3e7
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 204 deletions.
4 changes: 2 additions & 2 deletions zuul-simple-webapp/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ apply plugin: 'jetty'

dependencies {
compile project(":zuul-core")
compile 'org.apache.httpcomponents:httpclient:4.3.2'

compile 'org.apache.httpcomponents:httpclient:4.5'
providedCompile 'javax.servlet:servlet-api:2.5'
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package filters.post

import com.netflix.config.DynamicBooleanProperty
import com.netflix.config.DynamicIntProperty
Expand All @@ -23,22 +24,22 @@ import com.netflix.zuul.constants.ZuulConstants
import com.netflix.zuul.constants.ZuulHeaders
import com.netflix.zuul.context.Debug
import com.netflix.zuul.context.RequestContext
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mockito
import org.mockito.runners.MockitoJUnitRunner
import org.slf4j.Logger
import org.slf4j.LoggerFactory

import javax.servlet.http.HttpServletResponse
import java.nio.charset.Charset
import java.util.zip.GZIPInputStream
import javax.servlet.http.HttpServletResponse
import java.util.zip.ZipException

class SendResponseFilter extends ZuulFilter {
private static final Logger LOG = LoggerFactory.getLogger(SendResponseFilter.class);

static DynamicBooleanProperty INCLUDE_DEBUG_HEADER =
DynamicPropertyFactory.getInstance().getBooleanProperty(ZuulConstants.ZUUL_INCLUDE_DEBUG_HEADER, false);
DynamicPropertyFactory.getInstance().getBooleanProperty(ZuulConstants.ZUUL_INCLUDE_DEBUG_HEADER, false);

static DynamicIntProperty INITIAL_STREAM_BUFFER_SIZE =
DynamicPropertyFactory.getInstance().getIntProperty(ZuulConstants.ZUUL_INITIAL_STREAM_BUFFER_SIZE, 1024);
DynamicPropertyFactory.getInstance().getIntProperty(ZuulConstants.ZUUL_INITIAL_STREAM_BUFFER_SIZE, 1024);

static DynamicBooleanProperty SET_CONTENT_LENGTH = DynamicPropertyFactory.getInstance().getBooleanProperty(ZuulConstants.ZUUL_SET_CONTENT_LENGTH, false);

Expand All @@ -53,9 +54,9 @@ class SendResponseFilter extends ZuulFilter {
}

boolean shouldFilter() {
return !RequestContext.currentContext.getZuulResponseHeaders().isEmpty() ||
RequestContext.currentContext.getResponseDataStream() != null ||
RequestContext.currentContext.responseBody != null
return !RequestContext.getCurrentContext().getZuulResponseHeaders().isEmpty() ||
RequestContext.getCurrentContext().getResponseDataStream() != null ||
RequestContext.getCurrentContext().responseBody != null
}

Object run() {
Expand All @@ -64,19 +65,20 @@ class SendResponseFilter extends ZuulFilter {
}

void writeResponse() {
RequestContext context = RequestContext.currentContext
RequestContext context = RequestContext.getCurrentContext()

// there is no body to send
if (context.getResponseBody() == null && context.getResponseDataStream() == null) return;
if (context.getResponseBody() == null && context.getResponseDataStream() == null) {
return
};

HttpServletResponse servletResponse = context.getResponse()
servletResponse.setCharacterEncoding("UTF-8")

OutputStream outStream = servletResponse.getOutputStream();
InputStream is = null
try {
if (RequestContext.currentContext.responseBody != null) {
String body = RequestContext.currentContext.responseBody
if (RequestContext.getCurrentContext().responseBody != null) {
String body = RequestContext.getCurrentContext().responseBody
writeResponse(new ByteArrayInputStream(body.getBytes(Charset.forName("UTF-8"))), outStream)
return;
}
Expand All @@ -90,31 +92,26 @@ class SendResponseFilter extends ZuulFilter {
InputStream inputStream = is
if (is != null) {
if (context.sendZuulResponse()) {
// if origin response is gzipped, and client has not requested gzip, decompress stream
// before sending to client
// else, stream gzip directly to client
if (context.getResponseGZipped() && !isGzipRequested)
try {
inputStream = new GZIPInputStream(is);

} catch (java.util.zip.ZipException e) {
println("gzip expected but not received assuming unencoded response" + RequestContext.currentContext.getRequest().getRequestURL().toString())
} catch (ZipException e) {
LOG.error("gzip expected but not received assuming unencoded response" + RequestContext.getCurrentContext().getRequest().getRequestURL().toString())
inputStream = is
}
else if (context.getResponseGZipped() && isGzipRequested)
else if (context.getResponseGZipped() && isGzipRequested) {
servletResponse.setHeader(ZuulHeaders.CONTENT_ENCODING, "gzip")
}
writeResponse(inputStream, outStream)
}
}

} finally {
try {
is?.close();

outStream.flush()
outStream.close()
} catch (IOException e) {

}
}
}
Expand All @@ -123,15 +120,11 @@ class SendResponseFilter extends ZuulFilter {
byte[] bytes = new byte[INITIAL_STREAM_BUFFER_SIZE.get()];
int bytesRead = -1;
while ((bytesRead = zin.read(bytes)) != -1) {
// if (Debug.debugRequest() && !Debug.debugRequestHeadersOnly()) {
// Debug.addRequestDebug("OUTBOUND: < " + new String(bytes, 0, bytesRead));
// }

try {
out.write(bytes, 0, bytesRead);
out.flush();
} catch (IOException e) {
//ignore
e.printStackTrace()
}

Expand All @@ -155,13 +148,6 @@ class SendResponseFilter extends ZuulFilter {
debugHeader += "[[[${it}]]]";
}

/*
rd = (List<String>) RequestContext.getCurrentContext().get("requestDebug");
rd?.each {
debugHeader += "[[[REQUEST_DEBUG::${it}]]]";
}
*/

if (INCLUDE_DEBUG_HEADER.get()) servletResponse.addHeader("X-Zuul-Debug-Header", debugHeader)

if (Debug.debugRequest()) {
Expand All @@ -185,4 +171,4 @@ class SendResponseFilter extends ZuulFilter {
}
}

}
}
11 changes: 1 addition & 10 deletions zuul-simple-webapp/src/main/groovy/filters/post/Stats.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package scripts.postProcess
package filters.post

import com.netflix.zuul.ZuulFilter
import com.netflix.zuul.context.RequestContext
import org.junit.Assert
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mock
import org.mockito.runners.MockitoJUnitRunner

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

/**
* @author Mikey Cohen
* Date: 2/3/12
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package scripts.preProcess
package filters.pre

import com.netflix.config.DynamicBooleanProperty
import com.netflix.config.DynamicPropertyFactory
Expand All @@ -38,9 +38,11 @@ class DebugFilter extends ZuulFilter {
}

boolean shouldFilter() {
if ("true".equals(RequestContext.getCurrentContext().getRequest().getParameter(debugParameter.get()))) return true;
return routingDebug.get();
if ("true".equals(RequestContext.getCurrentContext().getRequest().getParameter(debugParameter.get()))) {
return true
}

return routingDebug.get();
}

Object run() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package scripts.preProcess
package filters.pre

import com.netflix.zuul.ZuulFilter
import com.netflix.zuul.context.Debug
import com.netflix.zuul.context.RequestContext
import org.junit.Assert
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mock
import org.mockito.Mockito
import org.mockito.runners.MockitoJUnitRunner

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

/**
* @author Mikey Cohen
* Date: 3/12/12
Expand Down Expand Up @@ -62,7 +54,6 @@ class DebugRequest extends ZuulFilter {
String name = (String) headerIt.next()
String value = req.getHeader(name)
Debug.addRequestDebug("REQUEST:: > " + name + ":" + value)

}

final RequestContext ctx = RequestContext.getCurrentContext()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package filters.pre

import com.netflix.zuul.ZuulFilter
import com.netflix.zuul.context.RequestContext
Expand Down
Loading

0 comments on commit cddd3e7

Please sign in to comment.