AT&T 1712 and 1802 release code
[so.git] / cloudify-client / src / main / java / org / openecomp / mso / cloudify / connector / http / HttpClientRedirectStrategy.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.openecomp.mso.cloudify.connector.http;
22
23 import java.net.URI;
24
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;
37
38 /**
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).
44  * 
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. 
48  */
49 @Immutable
50 public class HttpClientRedirectStrategy extends DefaultRedirectStrategy {
51
52     /**
53      * Redirectable methods.
54      */
55     private static final String[] REDIRECT_METHODS = new String[] {
56         HttpGet.METHOD_NAME,
57         HttpDelete.METHOD_NAME,
58         HttpHead.METHOD_NAME
59     };
60
61     /**
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.
65      */
66     @Override
67     protected boolean isRedirectable(final String method) {
68         for (final String m: REDIRECT_METHODS) {
69             if (m.equalsIgnoreCase(method)) {
70                 return true;
71             }
72         }
73         return false;
74     }
75
76     /**
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.
80      */
81     @Override
82     public HttpUriRequest getRedirect(
83             final HttpRequest request,
84             final HttpResponse response,
85             final HttpContext context) throws ProtocolException {
86         
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);
93         } else {
94
95             final int status = response.getStatusLine().getStatusCode();
96             
97                 HttpUriRequest newRequest = null;
98                 if (status == HttpStatus.SC_TEMPORARY_REDIRECT || status == HttpStatus.SC_MOVED_TEMPORARILY) {
99                 newRequest = RequestBuilder.copy(request).setUri(uri).build();
100             } else {
101                 newRequest =  new HttpGet(uri);
102             }
103                 return newRequest;
104         }
105     }
106 }