Checkstyle fixes for datarouter prov
[dmaap/datarouter.git] / datarouter-prov / src / main / java / org / onap / dmaap / datarouter / provisioning / ProxyServlet.java
1 /*******************************************************************************
2  * ============LICENSE_START==================================================
3  * * org.onap.dmaap
4  * * ===========================================================================
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * * ===========================================================================
7  * * Licensed under the Apache License, Version 2.0 (the "License");
8  * * you may not use this file except in compliance with the License.
9  * * You may obtain a copy of the License at
10  * *
11  *  *      http://www.apache.org/licenses/LICENSE-2.0
12  * *
13  *  * Unless required by applicable law or agreed to in writing, software
14  * * distributed under the License is distributed on an "AS IS" BASIS,
15  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * * See the License for the specific language governing permissions and
17  * * limitations under the License.
18  * * ============LICENSE_END====================================================
19  * *
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  * *
22  ******************************************************************************/
23
24
25 package org.onap.dmaap.datarouter.provisioning;
26
27 import static org.onap.dmaap.datarouter.provisioning.utils.HttpServletUtils.sendResponseError;
28
29 import java.io.File;
30 import java.io.FileInputStream;
31 import java.io.FileNotFoundException;
32 import java.io.IOException;
33 import java.io.InputStream;
34 import java.net.URI;
35 import java.security.KeyStore;
36 import java.security.KeyStoreException;
37 import java.util.Collections;
38 import java.util.List;
39 import java.util.Properties;
40 import javax.servlet.ServletConfig;
41 import javax.servlet.ServletException;
42 import javax.servlet.http.HttpServletRequest;
43 import javax.servlet.http.HttpServletResponse;
44 import org.apache.commons.io.IOUtils;
45 import org.apache.http.Header;
46 import org.apache.http.HttpEntity;
47 import org.apache.http.HttpResponse;
48 import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
49 import org.apache.http.client.methods.HttpGet;
50 import org.apache.http.client.methods.HttpRequestBase;
51 import org.apache.http.conn.scheme.Scheme;
52 import org.apache.http.conn.ssl.SSLSocketFactory;
53 import org.apache.http.entity.BasicHttpEntity;
54 import org.apache.http.impl.client.AbstractHttpClient;
55 import org.apache.http.impl.client.DefaultHttpClient;
56 import org.onap.dmaap.datarouter.provisioning.utils.DB;
57 import org.onap.dmaap.datarouter.provisioning.utils.URLUtilities;
58
59 /**
60  * This class is the base class for those servlets that need to proxy their requests from the standby to active server.
61  * Its methods perform the proxy function to the active server. If the active server is not reachable, a 503
62  * (SC_SERVICE_UNAVAILABLE) is returned.  Only DELETE/GET/PUT/POST are supported.
63  *
64  * @author Robert Eby
65  * @version $Id: ProxyServlet.java,v 1.3 2014/03/24 18:47:10 eby Exp $
66  */
67 @SuppressWarnings("serial")
68
69 public class ProxyServlet extends BaseServlet {
70
71     private boolean inited = false;
72     private Scheme sch;
73
74     /**
75      * Initialize this servlet, by setting up SSL.
76      */
77     @SuppressWarnings("deprecation")
78     @Override
79     public void init(ServletConfig config) throws ServletException {
80         super.init(config);
81         try {
82             // Set up keystore
83             Properties props = (new DB()).getProperties();
84             String store = props.getProperty(Main.KEYSTORE_PATH_PROPERTY);
85             String pass = props.getProperty(Main.KEYSTORE_PASS_PROPERTY);
86             store = props.getProperty(Main.TRUSTSTORE_PATH_PROPERTY);
87             pass = props.getProperty(Main.TRUSTSTORE_PASS_PROPERTY);
88             if (store == null || store.length() == 0) {
89                 store = Main.DEFAULT_TRUSTSTORE;
90                 pass = "changeit";
91             }
92             KeyStore trustStore = readStore(store, pass, KeyStore.getDefaultType());
93
94             // We are connecting with the node name, but the certificate will have the CNAME
95             // So we need to accept a non-matching certificate name
96             String type = props.getProperty(Main.KEYSTORE_TYPE_PROPERTY, "jks");
97             KeyStore keyStore = readStore(store, pass, type);
98             SSLSocketFactory socketFactory = new SSLSocketFactory(keyStore,
99                     props.getProperty(Main.KEYSTORE_PASS_PROPERTY), trustStore);
100             socketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
101             sch = new Scheme("https", 443, socketFactory);
102             inited = true;
103         } catch (Exception e) {
104             intlogger.error("ProxyServlet.init: " + e.getMessage(), e);
105         }
106         intlogger.info("ProxyServlet: inited = " + inited);
107     }
108
109     private KeyStore readStore(String store, String pass, String type) throws KeyStoreException {
110         KeyStore ks = KeyStore.getInstance(type);
111         try (FileInputStream instream = new FileInputStream(new File(store))) {
112             ks.load(instream, pass.toCharArray());
113         } catch (FileNotFoundException fileNotFoundException) {
114             intlogger.error("ProxyServlet.readStore: " + fileNotFoundException.getMessage(), fileNotFoundException);
115         } catch (Exception x) {
116             intlogger.error("READING TRUSTSTORE: " + x);
117         }
118         return ks;
119     }
120
121     /**
122      * Return <i>true</i> if the requester has NOT set the <i>noproxy</i> CGI variable. If they have, this indicates
123      * they want to forcibly turn the proxy off.
124      *
125      * @param req the HTTP request
126      * @return true or false
127      */
128     boolean isProxyOK(final HttpServletRequest req) {
129         String str = req.getQueryString();
130         if (str != null) {
131             str = str.replaceAll("&amp;", "&");
132             for (String s : str.split("&")) {
133                 if ("noproxy".equals(s) || s.startsWith("noproxy=")) {
134                     return false;
135                 }
136             }
137         }
138         return true;
139     }
140
141     /**
142      * Is this the standby server?  If it is, the proxy functions can be used. If not, the proxy functions should not be
143      * called, and will send a response of 500 (Internal Server Error).
144      *
145      * @return true if this server is the standby (and hence a proxy server).
146      */
147     boolean isProxyServer() {
148         SynchronizerTask st = SynchronizerTask.getSynchronizer();
149         return st.getPodState() == SynchronizerTask.STANDBY_POD;
150     }
151
152     /**
153      * Issue a proxy DELETE to the active provisioning server.
154      */
155     @Override
156     public void doDelete(HttpServletRequest req, HttpServletResponse resp) {
157         doProxy(req, resp, "DELETE");
158     }
159
160     /**
161      * Issue a proxy GET to the active provisioning server.
162      */
163     @Override
164     public void doGet(HttpServletRequest req, HttpServletResponse resp) {
165         doProxy(req, resp, "GET");
166     }
167
168     /**
169      * Issue a proxy PUT to the active provisioning server.
170      */
171     @Override
172     public void doPut(HttpServletRequest req, HttpServletResponse resp) {
173         doProxy(req, resp, "PUT");
174     }
175
176     /**
177      * Issue a proxy POST to the active provisioning server.
178      */
179     @Override
180     public void doPost(HttpServletRequest req, HttpServletResponse resp) {
181         doProxy(req, resp, "POST");
182     }
183
184     /**
185      * Issue a proxy GET to the active provisioning server.  Unlike doGet() above, this method will allow the caller to
186      * fall back to other code if the remote server is unreachable.
187      *
188      * @return true if the proxy succeeded
189      */
190     boolean doGetWithFallback(HttpServletRequest req, HttpServletResponse resp) {
191         boolean rv = false;
192         if (inited) {
193             String url = buildUrl(req);
194             intlogger.info("ProxyServlet: proxying with fallback GET " + url);
195             try (AbstractHttpClient httpclient = new DefaultHttpClient()) {
196                 HttpRequestBase proxy = new HttpGet(url);
197                 try {
198                     httpclient.getConnectionManager().getSchemeRegistry().register(sch);
199
200                     // Copy request headers and request body
201                     copyRequestHeaders(req, proxy);
202
203                     // Execute the request
204                     HttpResponse pxyResponse = httpclient.execute(proxy);
205
206                     // Get response headers and body
207                     int code = pxyResponse.getStatusLine().getStatusCode();
208                     resp.setStatus(code);
209                     copyResponseHeaders(pxyResponse, resp);
210                     copyEntityContent(pxyResponse, resp);
211                     rv = true;
212
213                 } catch (IOException e) {
214                     intlogger.error("ProxyServlet.doGetWithFallback: " + e.getMessage(), e);
215                 } finally {
216                     proxy.releaseConnection();
217                     httpclient.getConnectionManager().shutdown();
218                 }
219             }
220         } else {
221             intlogger.warn("ProxyServlet: proxy disabled");
222         }
223         return rv;
224     }
225
226     private void doProxy(HttpServletRequest req, HttpServletResponse resp, final String method) {
227         if (inited && isProxyServer()) {
228             String url = buildUrl(req);
229             intlogger.info("ProxyServlet: proxying " + method + " " + url);
230             try (AbstractHttpClient httpclient = new DefaultHttpClient()) {
231                 ProxyHttpRequest proxy = new ProxyHttpRequest(method, url);
232                 try {
233                     httpclient.getConnectionManager().getSchemeRegistry().register(sch);
234
235                     // Copy request headers and request body
236                     copyRequestHeaders(req, proxy);
237
238                     handlePutOrPost(req, method, proxy);
239
240                     // Execute the request
241                     HttpResponse pxyResponse = httpclient.execute(proxy);
242
243                     // Get response headers and body
244                     int code = pxyResponse.getStatusLine().getStatusCode();
245                     resp.setStatus(code);
246                     copyResponseHeaders(pxyResponse, resp);
247                     copyEntityContent(pxyResponse, resp);
248                 } catch (IOException e) {
249                     intlogger.warn("ProxyServlet.doProxy: " + e.getMessage(), e);
250                     sendResponseError(resp, HttpServletResponse.SC_SERVICE_UNAVAILABLE, "", intlogger);
251                 } finally {
252                     proxy.releaseConnection();
253                     httpclient.getConnectionManager().shutdown();
254                 }
255             }
256         } else {
257             intlogger.warn("ProxyServlet: proxy disabled");
258             sendResponseError(resp, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, DB_PROBLEM_MSG, intlogger);
259         }
260     }
261
262     private void handlePutOrPost(HttpServletRequest req, String method, ProxyHttpRequest proxy) throws IOException {
263         if ("POST".equals(method) || "PUT".equals(method)) {
264             BasicHttpEntity body = new BasicHttpEntity();
265             body.setContent(req.getInputStream());
266             body.setContentLength(-1);    // -1 = unknown
267             proxy.setEntity(body);
268         }
269     }
270
271     private String buildUrl(HttpServletRequest req) {
272         StringBuilder sb = new StringBuilder("https://");
273         sb.append(URLUtilities.getPeerPodName());
274         sb.append(req.getRequestURI());
275         String query = req.getQueryString();
276         if (query != null) {
277             sb.append("?").append(query);
278         }
279         return sb.toString();
280     }
281
282     private void copyRequestHeaders(HttpServletRequest from, HttpRequestBase to) {
283         List<String> list = Collections.list(from.getHeaderNames());
284         for (String name : list) {
285             // Proxy code will add this one
286             if (!"Content-Length".equalsIgnoreCase(name)) {
287                 to.addHeader(name, from.getHeader(name));
288             }
289         }
290     }
291
292     void copyResponseHeaders(HttpResponse from, HttpServletResponse to) {
293         for (Header hdr : from.getAllHeaders()) {
294             // Don't copy Date: our Jetty will add another Date header
295             if (!"Date".equals(hdr.getName())) {
296                 to.addHeader(hdr.getName(), hdr.getValue());
297             }
298         }
299     }
300
301     void copyEntityContent(HttpResponse pxyResponse, HttpServletResponse resp) {
302         HttpEntity entity = pxyResponse.getEntity();
303         if (entity != null) {
304             try (InputStream in = entity.getContent()) {
305                 IOUtils.copy(in, resp.getOutputStream());
306             } catch (Exception e) {
307                 intlogger.error("ProxyServlet.copyEntityContent: " + e.getMessage(), e);
308             }
309         }
310     }
311
312     public static class ProxyHttpRequest extends HttpEntityEnclosingRequestBase {
313
314         private final String method;
315
316         ProxyHttpRequest(final String method, final String uri) {
317             super();
318             this.method = method;
319             setURI(URI.create(uri));
320         }
321
322         @Override
323         public String getMethod() {
324             return method;
325         }
326     }
327 }