9dcf133ab41fb9c1c5cbbb7e96b953f8e194c957
[dcaegen2/collectors/datafile.git] /
1 /*-
2  * ============LICENSE_START======================================================================
3  * Copyright (C) 2018 NOKIA Intellectual Property, 2018-2019 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 that automatically redirects all HEAD, GET, POST, PUT, and DELETE requests.
41  * This strategy relaxes restrictions on automatic redirection of POST methods imposed by the HTTP specification.
42  *
43  */
44 @Contract(threading = ThreadingBehavior.IMMUTABLE)
45 public class PublishRedirectStrategy extends DefaultRedirectStrategy {
46
47     private final Logger logger = LoggerFactory.getLogger(this.getClass());
48     private final Map<String, String> contextMap;
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     /**
62      * Constructor PublishRedirectStrategy.
63      *
64      * @param contextMap - MDC context map
65      */
66     public PublishRedirectStrategy(Map<String, String> contextMap) {
67         this.contextMap = contextMap;
68     }
69
70     @Override
71     protected boolean isRedirectable(final String method) {
72         for (final String m : REDIRECT_METHODS) {
73             if (m.equalsIgnoreCase(method)) {
74                 return true;
75             }
76         }
77         return false;
78     }
79
80     @Override
81     public HttpUriRequest getRedirect(final HttpRequest request, final HttpResponse response, final HttpContext context)
82             throws ProtocolException {
83         MDC.setContextMap(contextMap);
84         final URI uri = getLocationURI(request, response, context);
85         logger.trace("getRedirect...: {}", request);
86         return RequestBuilder.copy(request).setUri(uri).build();
87     }
88
89 }