2 * ============LICENSE_START=======================================================
\r
3 * Licensed under the Apache License, Version 2.0 (the "License");
\r
4 * you may not use this file except in compliance with the License.
\r
5 * You may obtain a copy of the License at
\r
7 * http://www.apache.org/licenses/LICENSE-2.0
\r
9 * Unless required by applicable law or agreed to in writing, software
\r
10 * distributed under the License is distributed on an "AS IS" BASIS,
\r
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
12 * See the License for the specific language governing permissions and
\r
13 * limitations under the License.
\r
14 * ============LICENSE_END=========================================================
\r
18 * ============LICENSE_START==========================================
\r
19 * ===================================================================
\r
20 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
\r
21 * ===================================================================
\r
22 * Licensed under the Apache License, Version 2.0 (the "License");
\r
23 * you may not use this file except in compliance with the License.
\r
24 * You may obtain a copy of the License at
\r
26 * http://www.apache.org/licenses/LICENSE-2.0
\r
28 * Unless required by applicable law or agreed to in writing, software
\r
29 * distributed under the License is distributed on an "AS IS" BASIS,
\r
30 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
31 * See the License for the specific language governing permissions and
\r
32 * limitations under the License.
\r
33 * ============LICENSE_END============================================
\r
35 * ECOMP and OpenECOMP are trademarks
\r
36 * and service marks of AT&T Intellectual Property.
\r
40 package com.woorea.openstack.connector;
\r
42 import java.net.URI;
\r
44 import org.apache.http.HttpRequest;
\r
45 import org.apache.http.HttpResponse;
\r
46 import org.apache.http.HttpStatus;
\r
47 import org.apache.http.ProtocolException;
\r
48 import org.apache.http.annotation.Immutable;
\r
49 import org.apache.http.client.methods.HttpGet;
\r
50 import org.apache.http.client.methods.HttpHead;
\r
51 import org.apache.http.client.methods.HttpDelete;
\r
52 import org.apache.http.client.methods.HttpUriRequest;
\r
53 import org.apache.http.client.methods.RequestBuilder;
\r
54 import org.apache.http.impl.client.DefaultRedirectStrategy;
\r
55 import org.apache.http.protocol.HttpContext;
\r
58 * Custom {@link org.apache.http.client.RedirectStrategy} implementation
\r
59 * that automatically redirects all HEAD, GET and DELETE requests.
\r
60 * The {@link org.apache.http.client.DefaultRedirectStrategy} only
\r
61 * redirects GET and HEAD automatically, per the HTTP specification
\r
62 * (POST and PUT typically have bodies and thus cannot be redirected).
\r
64 * A custom strategy is needed for the Openstack API, which can also send
\r
65 * 302 on a DELETE (by name) request, expecting the client to follow the
\r
66 * redirect to perform the actual deletion.
\r
69 public class HttpClientRedirectStrategy extends DefaultRedirectStrategy {
\r
72 * Redirectable methods.
\r
74 private static final String[] REDIRECT_METHODS = new String[] {
\r
75 HttpGet.METHOD_NAME,
\r
76 HttpDelete.METHOD_NAME,
\r
77 HttpHead.METHOD_NAME
\r
81 * Determine if the request should be redirected.
\r
82 * This may not actually be needed, since the REDIRECT_METHODS
\r
83 * array has been updated with the DELETE.
\r
86 protected boolean isRedirectable(final String method) {
\r
87 for (final String m: REDIRECT_METHODS) {
\r
88 if (m.equalsIgnoreCase(method)) {
\r
96 * Override the default redirect handling method. As implemented
\r
97 * in HttpClient, it does not preserve the method on 301 or 302
\r
98 * responses, always redirecting to a GET.
\r
101 public HttpUriRequest getRedirect(
\r
102 final HttpRequest request,
\r
103 final HttpResponse response,
\r
104 final HttpContext context) throws ProtocolException {
\r
106 final URI uri = getLocationURI(request, response, context);
\r
107 final String method = request.getRequestLine().getMethod();
\r
108 if (method.equalsIgnoreCase(HttpHead.METHOD_NAME)) {
\r
109 return new HttpHead(uri);
\r
110 } else if (method.equalsIgnoreCase(HttpGet.METHOD_NAME)) {
\r
111 return new HttpGet(uri);
\r
114 final int status = response.getStatusLine().getStatusCode();
\r
116 HttpUriRequest newRequest = null;
\r
117 if (status == HttpStatus.SC_TEMPORARY_REDIRECT || status == HttpStatus.SC_MOVED_TEMPORARILY) {
\r
118 newRequest = RequestBuilder.copy(request).setUri(uri).build();
\r
120 newRequest = new HttpGet(uri);
\r