3ceec62704191c5efe9901d527a18c08457eb8fd
[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 java.util.Map;
21 import org.apache.http.HttpRequest;
22 import org.apache.http.HttpResponse;
23 import org.apache.http.ProtocolException;
24 import org.apache.http.annotation.Contract;
25 import org.apache.http.annotation.ThreadingBehavior;
26 import org.apache.http.client.methods.HttpDelete;
27 import org.apache.http.client.methods.HttpGet;
28 import org.apache.http.client.methods.HttpHead;
29 import org.apache.http.client.methods.HttpPost;
30 import org.apache.http.client.methods.HttpPut;
31 import org.apache.http.client.methods.HttpUriRequest;
32 import org.apache.http.client.methods.RequestBuilder;
33 import org.apache.http.impl.client.DefaultRedirectStrategy;
34 import org.apache.http.protocol.HttpContext;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import org.slf4j.MDC;
38
39 /**
40  * PublishRedirectStrategy implementation
41  * that automatically redirects all HEAD, GET, POST, PUT, and DELETE requests.
42  * This strategy relaxes restrictions on automatic redirection of
43  * POST methods imposed by the HTTP specification.
44  *
45  */
46 @Contract(threading = ThreadingBehavior.IMMUTABLE)
47 public class PublishRedirectStrategy extends DefaultRedirectStrategy {
48
49     private final Logger logger = LoggerFactory.getLogger(this.getClass());
50     private final Map<String, String> contextMap;
51
52     /**
53      * Constructor PublishRedirectStrategy.
54      *
55      * @param contextMap - MDC context map
56      */
57     public PublishRedirectStrategy(Map<String, String> contextMap) {
58         this.contextMap = contextMap;
59     }
60
61     /**
62      * Redirectable methods.
63      */
64     private static final String[] REDIRECT_METHODS = new String[] { //
65         HttpPut.METHOD_NAME, //
66         HttpGet.METHOD_NAME, //
67         HttpPost.METHOD_NAME, //
68         HttpHead.METHOD_NAME, //
69         HttpDelete.METHOD_NAME //
70     };
71
72     @Override
73     protected boolean isRedirectable(final String method) {
74         for (final String m : REDIRECT_METHODS) {
75             if (m.equalsIgnoreCase(method)) {
76                 return true;
77             }
78         }
79         return false;
80     }
81
82     @Override
83     public HttpUriRequest getRedirect(final HttpRequest request, final HttpResponse response, final HttpContext context)
84         throws ProtocolException {
85         MDC.setContextMap(contextMap);
86         final URI uri = getLocationURI(request, response, context);
87         logger.trace("getRedirect...: {}", request);
88         return RequestBuilder.copy(request).setUri(uri).build();
89     }
90
91 }