Replaced all tabs with spaces in java and pom.xml
[so.git] / cloudify-client / src / main / java / org / onap / so / 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.onap.so.cloudify.connector.http;
22
23 import java.net.URI;
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;
36
37 /**
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).
41  * 
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.
44  */
45 @Immutable
46 public class HttpClientRedirectStrategy extends DefaultRedirectStrategy {
47
48     /**
49      * Redirectable methods.
50      */
51     private static final String[] REDIRECT_METHODS =
52             new String[] {HttpGet.METHOD_NAME, HttpDelete.METHOD_NAME, HttpHead.METHOD_NAME};
53
54     /**
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.
57      */
58     @Override
59     protected boolean isRedirectable(final String method) {
60         for (final String m : REDIRECT_METHODS) {
61             if (m.equalsIgnoreCase(method)) {
62                 return true;
63             }
64         }
65         return false;
66     }
67
68     /**
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.
71      */
72     @Override
73     public HttpUriRequest getRedirect(final HttpRequest request, final HttpResponse response, final HttpContext context)
74             throws ProtocolException {
75
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);
82         } else {
83
84             final int status = response.getStatusLine().getStatusCode();
85
86             HttpUriRequest newRequest = null;
87             if (status == HttpStatus.SC_TEMPORARY_REDIRECT || status == HttpStatus.SC_MOVED_TEMPORARILY) {
88                 newRequest = RequestBuilder.copy(request).setUri(uri).build();
89             } else {
90                 newRequest = new HttpGet(uri);
91             }
92             return newRequest;
93         }
94     }
95 }