Second part of onap rename
[appc.git] / appc-adapters / appc-rest-adapter / appc-rest-adapter-bundle / src / main / java / org / onap / appc / adapter / rest / RequestFactory.java
1 /*
2  * ============LICENSE_START=============================================================================================================
3  * Copyright (c) 2017 Intel Corp.  All rights reserved.
4  * ===================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
6  *
7  *        http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
10  * OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
11  * ============LICENSE_END===============================================================================================================
12  *
13  */
14
15 package org.onap.appc.adapter.rest;
16
17 import com.att.eelf.configuration.EELFLogger;
18 import com.att.eelf.configuration.EELFManager;
19 import org.apache.http.client.methods.HttpDelete;
20 import org.apache.http.client.methods.HttpGet;
21 import org.apache.http.client.methods.HttpPost;
22 import org.apache.http.client.methods.HttpPut;
23 import org.apache.http.client.methods.HttpRequestBase;
24 import org.apache.http.client.utils.URIBuilder;
25
26 import java.net.URI;
27 import java.net.URISyntaxException;
28 import java.util.HashMap;
29 import java.util.Map;
30 import java.util.function.Supplier;
31
32 public class RequestFactory {
33     private final EELFLogger logger = EELFManager.getInstance().getLogger(RequestFactory.class);
34
35     final static Map<String, Supplier<HttpRequestBase>> map = new HashMap<>();
36
37     static {
38         map.put("GET", HttpGet::new);
39         map.put("POST", HttpPost::new);
40         map.put("PUT", HttpPut::new);
41         map.put("DELETE", HttpDelete::new);
42     }
43
44     public HttpRequestBase getHttpRequest(String method, String tUrl) {
45         Supplier<HttpRequestBase> httpRequestSupplier = map.get(method.toUpperCase());
46         URI uri = null;
47         if (httpRequestSupplier != null ) {
48             try {
49                 uri = new URIBuilder(tUrl).build();
50             } catch (URISyntaxException ex) {
51                 logger.error("URI Syntax Incorrect: " + tUrl, ex);
52             }
53             HttpRequestBase httpRequest = httpRequestSupplier.get();
54             httpRequest.setURI(uri);
55             return httpRequest;
56
57         }
58         throw new IllegalArgumentException("No method named: " + method.toUpperCase());
59     }
60 }