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.openecomp.mso.cloudify.connector.http;
25 import org.apache.http.HttpRequest;
26 import org.apache.http.HttpResponse;
27 import org.apache.http.HttpStatus;
28 import org.apache.http.ProtocolException;
29 import org.apache.http.annotation.Immutable;
30 import org.apache.http.client.methods.HttpGet;
31 import org.apache.http.client.methods.HttpHead;
32 import org.apache.http.client.methods.HttpDelete;
33 import org.apache.http.client.methods.HttpUriRequest;
34 import org.apache.http.client.methods.RequestBuilder;
35 import org.apache.http.impl.client.DefaultRedirectStrategy;
36 import org.apache.http.protocol.HttpContext;
39 * Custom {@link org.apache.http.client.RedirectStrategy} implementation
40 * that automatically redirects all HEAD, GET and DELETE requests.
41 * The {@link org.apache.http.client.DefaultRedirectStrategy} only
42 * redirects GET and HEAD automatically, per the HTTP specification
43 * (POST and PUT typically have bodies and thus cannot be redirected).
45 * A custom strategy is needed for the Openstack API, which can also send
46 * 302 on a DELETE (by name) request, expecting the client to follow the
47 * redirect to perform the actual deletion.
50 public class HttpClientRedirectStrategy extends DefaultRedirectStrategy {
53 * Redirectable methods.
55 private static final String[] REDIRECT_METHODS = new String[] {
57 HttpDelete.METHOD_NAME,
62 * Determine if the request should be redirected.
63 * This may not actually be needed, since the REDIRECT_METHODS
64 * array has been updated with the DELETE.
67 protected boolean isRedirectable(final String method) {
68 for (final String m: REDIRECT_METHODS) {
69 if (m.equalsIgnoreCase(method)) {
77 * Override the default redirect handling method. As implemented
78 * in HttpClient, it does not preserve the method on 301 or 302
79 * responses, always redirecting to a GET.
82 public HttpUriRequest getRedirect(
83 final HttpRequest request,
84 final HttpResponse response,
85 final HttpContext context) throws ProtocolException {
87 final URI uri = getLocationURI(request, response, context);
88 final String method = request.getRequestLine().getMethod();
89 if (method.equalsIgnoreCase(HttpHead.METHOD_NAME)) {
90 return new HttpHead(uri);
91 } else if (method.equalsIgnoreCase(HttpGet.METHOD_NAME)) {
92 return new HttpGet(uri);
95 final int status = response.getStatusLine().getStatusCode();
97 HttpUriRequest newRequest = null;
98 if (status == HttpStatus.SC_TEMPORARY_REDIRECT || status == HttpStatus.SC_MOVED_TEMPORARILY) {
99 newRequest = RequestBuilder.copy(request).setUri(uri).build();
101 newRequest = new HttpGet(uri);