f9aedf569ae015ad19355062d5b7433c38068229
[so/libs.git] /
1 /*-\r
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
6  *\r
7  *      http://www.apache.org/licenses/LICENSE-2.0\r
8  *\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
15  */\r
16 \r
17 /*\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
25  *\r
26  *        http://www.apache.org/licenses/LICENSE-2.0\r
27  *\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
34  *\r
35  * ECOMP and OpenECOMP are trademarks\r
36  * and service marks of AT&T Intellectual Property.\r
37  *\r
38  */\r
39 \r
40 package com.woorea.openstack.connector;\r
41 \r
42 import java.net.URI;\r
43 \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
56 \r
57 /**\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
63  * \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
67  */\r
68 @Immutable\r
69 public class HttpClientRedirectStrategy extends DefaultRedirectStrategy {\r
70 \r
71     /**\r
72      * Redirectable methods.\r
73      */\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
78     };\r
79 \r
80     /**\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
84      */\r
85     @Override\r
86     protected boolean isRedirectable(final String method) {\r
87         for (final String m: REDIRECT_METHODS) {\r
88             if (m.equalsIgnoreCase(method)) {\r
89                 return true;\r
90             }\r
91         }\r
92         return false;\r
93     }\r
94 \r
95     /**\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
99      */\r
100     @Override\r
101     public HttpUriRequest getRedirect(\r
102             final HttpRequest request,\r
103             final HttpResponse response,\r
104             final HttpContext context) throws ProtocolException {\r
105         \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
112         } else {\r
113 \r
114             final int status = response.getStatusLine().getStatusCode();\r
115             \r
116             HttpUriRequest newRequest;\r
117             if (status == HttpStatus.SC_TEMPORARY_REDIRECT || status == HttpStatus.SC_MOVED_TEMPORARILY) {\r
118                 newRequest = RequestBuilder.copy(request).setUri(uri).build();\r
119             } else {\r
120                 newRequest =  new HttpGet(uri);\r
121             }\r
122             return newRequest;\r
123         }\r
124     }\r
125 }\r