de07461c54076656ff09b32e0dd8a3a84e1e2a90
[dcaegen2/collectors/datafile.git] /
1 /*
2  * ============LICENSE_START======================================================================
3  * Copyright (C) 2018 NOKIA Intellectual Property, 2018 Nordix Foundation. All rights reserved.
4  * ===============================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
6  * in compliance with the License. You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software distributed under the License
11  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12  * or implied. See the License for the specific language governing permissions and limitations under
13  * the License.
14  * ============LICENSE_END========================================================================
15  */
16
17 package org.onap.dcaegen2.collectors.datafile.web;
18
19 import java.net.URI;
20 import org.apache.http.HttpRequest;
21 import org.apache.http.HttpResponse;
22 import org.apache.http.ProtocolException;
23 import org.apache.http.annotation.Contract;
24 import org.apache.http.annotation.ThreadingBehavior;
25 import org.apache.http.client.methods.HttpDelete;
26 import org.apache.http.client.methods.HttpGet;
27 import org.apache.http.client.methods.HttpHead;
28 import org.apache.http.client.methods.HttpPost;
29 import org.apache.http.client.methods.HttpPut;
30 import org.apache.http.client.methods.HttpUriRequest;
31 import org.apache.http.client.methods.RequestBuilder;
32 import org.apache.http.impl.client.DefaultRedirectStrategy;
33 import org.apache.http.protocol.HttpContext;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 /**
38  * PublishRedirectStrategy implementation
39  * that automatically redirects all HEAD, GET, POST, PUT, and DELETE requests.
40  * This strategy relaxes restrictions on automatic redirection of
41  * POST methods imposed by the HTTP specification.
42  *
43  */
44 @Contract(threading = ThreadingBehavior.IMMUTABLE)
45 public class PublishRedirectStrategy extends DefaultRedirectStrategy {
46
47     public static final PublishRedirectStrategy INSTANCE = new PublishRedirectStrategy();
48     private final Logger logger = LoggerFactory.getLogger(this.getClass());
49
50     /**
51      * Redirectable methods.
52      */
53     private static final String[] REDIRECT_METHODS = new String[] { //
54         HttpPut.METHOD_NAME, //
55         HttpGet.METHOD_NAME, //
56         HttpPost.METHOD_NAME, //
57         HttpHead.METHOD_NAME, //
58         HttpDelete.METHOD_NAME //
59     };
60
61     @Override
62     protected boolean isRedirectable(final String method) {
63         for (final String m : REDIRECT_METHODS) {
64             if (m.equalsIgnoreCase(method)) {
65                 return true;
66             }
67         }
68         return false;
69     }
70
71     @Override
72     public HttpUriRequest getRedirect(final HttpRequest request, final HttpResponse response, final HttpContext context)
73         throws ProtocolException {
74         final URI uri = getLocationURI(request, response, context);
75         logger.trace("getRedirect...: {}", request);
76         return RequestBuilder.copy(request).setUri(uri).build();
77     }
78
79 }