2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.so.cloudify.connector.http;
24 import org.apache.http.HttpRequest;
25 import org.apache.http.HttpResponse;
26 import org.apache.http.HttpStatus;
27 import org.apache.http.ProtocolException;
28 import org.apache.http.annotation.Immutable;
29 import org.apache.http.client.methods.HttpGet;
30 import org.apache.http.client.methods.HttpHead;
31 import org.apache.http.client.methods.HttpDelete;
32 import org.apache.http.client.methods.HttpUriRequest;
33 import org.apache.http.client.methods.RequestBuilder;
34 import org.apache.http.impl.client.DefaultRedirectStrategy;
35 import org.apache.http.protocol.HttpContext;
38 * Custom {@link org.apache.http.client.RedirectStrategy} implementation that automatically redirects all HEAD, GET and
39 * DELETE requests. The {@link org.apache.http.client.DefaultRedirectStrategy} only redirects GET and HEAD
40 * automatically, per the HTTP specification (POST and PUT typically have bodies and thus cannot be redirected).
42 * A custom strategy is needed for the Openstack API, which can also send 302 on a DELETE (by name) request, expecting
43 * the client to follow the redirect to perform the actual deletion.
46 public class HttpClientRedirectStrategy extends DefaultRedirectStrategy {
49 * Redirectable methods.
51 private static final String[] REDIRECT_METHODS =
52 new String[] {HttpGet.METHOD_NAME, HttpDelete.METHOD_NAME, HttpHead.METHOD_NAME};
55 * Determine if the request should be redirected. This may not actually be needed, since the REDIRECT_METHODS array
56 * has been updated with the DELETE.
59 protected boolean isRedirectable(final String method) {
60 for (final String m : REDIRECT_METHODS) {
61 if (m.equalsIgnoreCase(method)) {
69 * Override the default redirect handling method. As implemented in HttpClient, it does not preserve the method on
70 * 301 or 302 responses, always redirecting to a GET.
73 public HttpUriRequest getRedirect(final HttpRequest request, final HttpResponse response, final HttpContext context)
74 throws ProtocolException {
76 final URI uri = getLocationURI(request, response, context);
77 final String method = request.getRequestLine().getMethod();
78 if (method.equalsIgnoreCase(HttpHead.METHOD_NAME)) {
79 return new HttpHead(uri);
80 } else if (method.equalsIgnoreCase(HttpGet.METHOD_NAME)) {
81 return new HttpGet(uri);
84 final int status = response.getStatusLine().getStatusCode();
86 HttpUriRequest newRequest = null;
87 if (status == HttpStatus.SC_TEMPORARY_REDIRECT || status == HttpStatus.SC_MOVED_TEMPORARILY) {
88 newRequest = RequestBuilder.copy(request).setUri(uri).build();
90 newRequest = new HttpGet(uri);