2 * ============LICENSE_START=======================================================
3 * ONAP : ccsdk features
4 * ================================================================================
5 * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property.
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.ccsdk.features.sdnr.wt.common.database.requests;
24 import java.io.UnsupportedEncodingException;
25 import java.net.URLEncoder;
26 import java.nio.charset.StandardCharsets;
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;
35 public abstract class BaseRequest {
37 private static final Logger LOG = LoggerFactory.getLogger(BaseRequest.class);
39 public static final int DEFAULT_RETRIES = 3;
41 protected final Request request;
43 private final boolean refresh;
45 public BaseRequest(String method, String endpoint) {
46 LOG.debug("create request {} {}", method, endpoint);
48 this.request = new Request(method, endpoint);
52 public BaseRequest(String method, String endpoint, boolean refresh) {
53 LOG.debug("create request {} {} with refresh={}", method, endpoint, refresh);
54 this.refresh = refresh;
55 this.request = new Request(method, String.format("%s?refresh=%s", endpoint, String.valueOf(refresh)));
59 public BaseRequest(String method, String endpoint, boolean refresh, int retries) {
60 LOG.debug("create request {} {} with refresh={}", method, endpoint, refresh);
61 this.refresh = refresh;
62 this.request = new Request(method,
63 String.format("%s?refresh=%s&retry_on_conflict=%d", endpoint, String.valueOf(refresh), retries));
67 public Request getInner() {
72 public static String urlEncodeValue(String value) {
76 return URLEncoder.encode(value, StandardCharsets.UTF_8.toString()).replace("+", "%20");
77 } catch (UnsupportedEncodingException ex) {
78 LOG.warn("encoding problem: {}", ex.getMessage());
84 public String toString() {
85 return this.request.getMethod() + " " + this.request.getEndpoint() + " : "
86 + (this.query != null ? this.query : "no query");
89 protected void setQuery(QueryBuilder query) {
90 this.setQuery(query.toJSON());
93 public void setQuery(JSONObject o) {
94 this.setQuery(o.toString());
97 public void setQuery(String content) {
99 LOG.trace("query={}", content);
100 this.request.setJsonEntity(this.query);
103 protected String getQuery() {
107 protected boolean doRefresh() {