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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
14 * ============LICENSE_END========================================================================
17 package org.onap.dcaegen2.collectors.datafile.web;
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;
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.
46 @Contract(threading = ThreadingBehavior.IMMUTABLE)
47 public class PublishRedirectStrategy extends DefaultRedirectStrategy {
49 private final Logger logger = LoggerFactory.getLogger(this.getClass());
50 private final Map<String, String> contextMap;
53 * Constructor PublishRedirectStrategy.
55 * @param contextMap - MDC context map
57 public PublishRedirectStrategy(Map<String, String> contextMap) {
58 this.contextMap = contextMap;
62 * Redirectable methods.
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 //
73 protected boolean isRedirectable(final String method) {
74 for (final String m : REDIRECT_METHODS) {
75 if (m.equalsIgnoreCase(method)) {
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();