e00cd1a03d6d26c3fc76d81096c6abe21fef4882
[ccsdk/features.git] /
1 /*******************************************************************************
2  * ============LICENSE_START========================================================================
3  * ONAP : ccsdk feature sdnr wt
4  * =================================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6  * =================================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software distributed under the License
13  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14  * or implied. See the License for the specific language governing permissions and limitations under
15  * the License.
16  * ============LICENSE_END==========================================================================
17  ******************************************************************************/
18 package org.onap.ccsdk.features.sdnr.wt.common.database.requests;
19
20 import java.io.UnsupportedEncodingException;
21 import java.net.URLEncoder;
22 import java.nio.charset.StandardCharsets;
23
24 import org.elasticsearch.client.Request;
25 import org.json.JSONObject;
26 import org.onap.ccsdk.features.sdnr.wt.common.database.queries.QueryBuilder;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 public abstract class BaseRequest {
31
32         private static final Logger LOG = LoggerFactory.getLogger(BaseRequest.class);
33
34         protected final Request request;
35         private String query;
36         public BaseRequest(String method, String endpoint) {
37                 LOG.debug("create request {} {}", method, endpoint);
38                 this.request = new Request(method, endpoint);
39                 query=null;
40         }
41
42         public Request getInner() {
43
44                 return this.request;
45         }
46
47         public static String urlEncodeValue(String value) {
48                 if (value == null)
49                         return null;
50                 try {
51                         return URLEncoder.encode(value, StandardCharsets.UTF_8.toString()).replace("+", "%20");
52                 } catch (UnsupportedEncodingException ex) {
53                         LOG.warn("encoding problem: {}", ex.getMessage());
54                 }
55                 return value;
56         }
57         @Override
58         public String toString() {
59                 return this.request.getMethod() + " "+this.request.getEndpoint()+ " : "+(this.query!=null?this.query:"no query");
60         }
61
62         protected void setQuery(QueryBuilder query) {
63                 this.setQuery(query.toJSON());
64         }
65
66         public void setQuery(JSONObject o) {
67                 this.setQuery(o.toString());
68         }
69
70         public void setQuery(String content) {
71                 this.query=content;
72                 LOG.trace("query={}",content);
73                 this.request.setJsonEntity(this.query);
74         }
75 }