ec5661acd9ece2d86c377fa3e6b66c54173a4253
[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.dataprovider.http;
23
24 import java.io.IOException;
25 import javax.servlet.ServletException;
26 import javax.servlet.http.HttpServletRequest;
27 import javax.servlet.http.HttpServletResponse;
28
29 import org.onap.ccsdk.features.sdnr.wt.common.http.BaseServlet;
30 import org.onap.ccsdk.features.sdnr.wt.dataprovider.data.MediatorServerDataProvider;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 public class MsServlet extends BaseServlet {
35
36         /**
37          *
38          */
39         private static Logger LOG = LoggerFactory.getLogger(MsServlet.class);
40         private static final long serialVersionUID = -5361461082028405171L;
41         private static final String OFFLINE_RESPONSE_MESSAGE = "MediatorServer interface is offline";
42         private static boolean trustAll = false;
43         private static MediatorServerDataProvider entryProvider;
44         public MsServlet() {
45                 super();
46         }
47
48         @Override
49         protected void doOptions(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
50                 resp.setStatus(200);
51         }
52         @Override
53         protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
54                 super.doGet(req, resp);
55         }
56         @Override
57         protected String getOfflineResponse() {
58                 return OFFLINE_RESPONSE_MESSAGE;
59         }
60
61         public void triggerReloadDatabaseEntries() {
62                 LOG.debug("external reload triggered");
63                 entryProvider.triggerReloadSync();
64         }
65
66         @Override
67         protected boolean isOff() {
68                 return false;
69         }
70
71         @Override
72         protected String getRemoteUrl(String uri) {
73                 String dbServerId = "0";
74                 if (uri == null)
75                         uri = "";
76                 if (uri.length() > 0) {
77                         uri = uri.substring("/ms/".length());
78                         int idx= uri.indexOf("/");
79                         dbServerId = uri.substring(0,idx);
80                         uri=uri.substring(idx);
81                 }
82                 LOG.debug("request for ms server with id={}",dbServerId);
83                 String url= this.getBaseUrl(dbServerId) + uri;
84                 LOG.debug("dest-url: {}",url);
85                 return url;
86         }
87
88         protected String getBaseUrl(String dbServerId) {
89                 return entryProvider.getHostUrl(dbServerId);
90         }
91         @Override
92         protected boolean doTrustAll() {
93                 return trustAll;
94         }
95         @Override
96         protected void trustAll(boolean trust) {
97                 trustAll = trust;
98         }
99
100         public void setDataProvider(MediatorServerDataProvider mediatorServerDataProvider) {
101                 entryProvider = mediatorServerDataProvider;
102         }
103
104         @Override
105         protected boolean trustInsecure() {
106                 return trustAll;
107         }
108
109         @Override
110         protected boolean isCorsEnabled() {
111                 return false;
112         }
113 }