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