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