Skip to content

Commit

Permalink
Fix for issue Azure#458.
Browse files Browse the repository at this point in the history
  • Loading branch information
avranju committed Jan 14, 2015
1 parent f6320b3 commit 0fa83ae
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
*/
package com.microsoft.windowsazure.core;

import com.microsoft.windowsazure.core.pipeline.apache.*;
import com.microsoft.windowsazure.core.pipeline.filter.ServiceRequestFilter;
import com.microsoft.windowsazure.core.pipeline.apache.HttpRequestInterceptorAdapter;
import com.microsoft.windowsazure.core.pipeline.filter.ServiceResponseFilter;
import com.microsoft.windowsazure.core.pipeline.apache.HttpResponseInterceptorAdapter;

import java.io.Closeable;
import java.io.IOException;
import java.util.concurrent.ExecutorService;
Expand Down Expand Up @@ -57,6 +57,10 @@ public CloseableHttpClient getHttpClient() {
}

private final HttpClientBuilder httpClientBuilder;
private HttpRequestInterceptorFirstAdapter requestInterceptorAdapterFirst;
private HttpRequestInterceptorLastAdapter requestInterceptorAdapterLast;
private HttpResponseInterceptorFirstAdapter responseInterceptorAdapterFirst;
private HttpResponseInterceptorLastAdapter responseInterceptorAdapterLast;

protected ServiceClient(HttpClientBuilder httpClientBuilder,
ExecutorService executorService) {
Expand All @@ -71,35 +75,48 @@ protected abstract TClient newInstance(HttpClientBuilder httpClientBuilder,
@Override
public TClient withRequestFilterFirst(
ServiceRequestFilter serviceRequestFilter) {
httpClientBuilder
.addInterceptorFirst(new HttpRequestInterceptorAdapter(
serviceRequestFilter));
if(requestInterceptorAdapterFirst == null) {
requestInterceptorAdapterFirst = new HttpRequestInterceptorFirstAdapter();
httpClientBuilder.addInterceptorFirst(requestInterceptorAdapterFirst);
}

requestInterceptorAdapterFirst.add(serviceRequestFilter);
return this.newInstance(httpClientBuilder, executorService);
}

@Override
public TClient withRequestFilterLast(
ServiceRequestFilter serviceRequestFilter) {
httpClientBuilder.addInterceptorLast(new HttpRequestInterceptorAdapter(
serviceRequestFilter));
if(requestInterceptorAdapterLast == null) {
requestInterceptorAdapterLast = new HttpRequestInterceptorLastAdapter();
httpClientBuilder.addInterceptorLast(requestInterceptorAdapterLast);
}

requestInterceptorAdapterLast.add(serviceRequestFilter);
return this.newInstance(httpClientBuilder, executorService);
}

@Override
public TClient withResponseFilterFirst(
ServiceResponseFilter serviceResponseFilter) {
httpClientBuilder
.addInterceptorFirst(new HttpResponseInterceptorAdapter(
serviceResponseFilter));
if(responseInterceptorAdapterFirst == null) {
responseInterceptorAdapterFirst = new HttpResponseInterceptorFirstAdapter();
httpClientBuilder.addInterceptorFirst(responseInterceptorAdapterFirst);
}

responseInterceptorAdapterFirst.add(serviceResponseFilter);
return this.newInstance(httpClientBuilder, executorService);
}

@Override
public TClient withResponseFilterLast(
ServiceResponseFilter serviceResponseFilter) {
httpClientBuilder
.addInterceptorLast(new HttpResponseInterceptorAdapter(
serviceResponseFilter));
if(responseInterceptorAdapterLast == null) {
responseInterceptorAdapterLast = new HttpResponseInterceptorLastAdapter();
httpClientBuilder.addInterceptorLast(responseInterceptorAdapterLast);
}

responseInterceptorAdapterLast.add(serviceResponseFilter);
return this.newInstance(httpClientBuilder, executorService);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,24 @@
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.protocol.HttpContext;

import java.util.ArrayList;
import java.util.List;

public class HttpRequestInterceptorAdapter implements HttpRequestInterceptor {
private ServiceRequestFilter filter;
private List<ServiceRequestFilter> filters = new ArrayList<ServiceRequestFilter>();

public HttpRequestInterceptorAdapter() {
}

public HttpRequestInterceptorAdapter(ServiceRequestFilter filter) {
this.filter = filter;
public void add(ServiceRequestFilter filter) {
filters.add(filter);
}

@Override
public void process(HttpRequest request, HttpContext context) {
filter.filter(new HttpServiceRequestContext(request, context));
HttpServiceRequestContext serviceRequestContext = new HttpServiceRequestContext(request, context);
for(ServiceRequestFilter filter : filters) {
filter.filter(serviceRequestContext);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
*
* Copyright (c) Microsoft and contributors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.microsoft.windowsazure.core.pipeline.apache;

public class HttpRequestInterceptorFirstAdapter extends HttpRequestInterceptorAdapter {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
*
* Copyright (c) Microsoft and contributors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.microsoft.windowsazure.core.pipeline.apache;

public class HttpRequestInterceptorLastAdapter extends HttpRequestInterceptorAdapter {
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,24 @@
import org.apache.http.HttpResponseInterceptor;
import org.apache.http.protocol.HttpContext;

import java.util.ArrayList;
import java.util.List;

public class HttpResponseInterceptorAdapter implements HttpResponseInterceptor {
private ServiceResponseFilter filter;
private List<ServiceResponseFilter> filters = new ArrayList<ServiceResponseFilter>();

public HttpResponseInterceptorAdapter() {
}

public HttpResponseInterceptorAdapter(ServiceResponseFilter filter) {
this.filter = filter;
public void add(ServiceResponseFilter filter) {
filters.add(filter);
}

@Override
public void process(HttpResponse response, HttpContext context) {
filter.filter(null, new HttpServiceResponseContext(response, context));
HttpServiceResponseContext serviceResponseContext = new HttpServiceResponseContext(response, context);
for(ServiceResponseFilter filter : filters) {
filter.filter(null, serviceResponseContext);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
*
* Copyright (c) Microsoft and contributors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.microsoft.windowsazure.core.pipeline.apache;

public class HttpResponseInterceptorFirstAdapter extends HttpResponseInterceptorAdapter {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
*
* Copyright (c) Microsoft and contributors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.microsoft.windowsazure.core.pipeline.apache;

public class HttpResponseInterceptorLastAdapter extends HttpResponseInterceptorAdapter {
}

0 comments on commit 0fa83ae

Please sign in to comment.