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