Removing jackson to mitigate cve-2017-4995
[vfc/nfvo/driver/vnfm/svnfm.git] / nokiav2 / driver / src / test / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / nokia / vnfm / HttpTestServer.java
1 /*
2  * Copyright 2016-2017, Nokia Corporation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm;
18
19 import com.google.common.io.ByteStreams;
20 import java.io.IOException;
21 import java.nio.file.Path;
22 import java.nio.file.Paths;
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.concurrent.ExecutorService;
26 import java.util.concurrent.Executors;
27 import java.util.concurrent.Future;
28 import java.util.concurrent.TimeUnit;
29 import javax.servlet.ServletException;
30 import javax.servlet.http.HttpServletRequest;
31 import javax.servlet.http.HttpServletResponse;
32 import org.eclipse.jetty.server.NetworkTrafficServerConnector;
33 import org.eclipse.jetty.server.Server;
34 import org.eclipse.jetty.server.handler.AbstractHandler;
35 import org.eclipse.jetty.util.ssl.SslContextFactory;
36
37 public class HttpTestServer {
38     public Server _server;
39     public volatile List<String> requests = new ArrayList<>();
40     public volatile List<Integer> codes = new ArrayList<>();
41     public volatile List<String> respones = new ArrayList<>();
42     ExecutorService executorService = Executors.newCachedThreadPool();
43
44     public void start() throws Exception {
45         configureServer();
46         startServer();
47     }
48
49     //the server starts up asynchronously there is no other way to wait for the server to be up and running
50     @SuppressWarnings("squid:S2925")
51     private void startServer() throws Exception {
52         requests.clear();
53         codes.clear();
54         _server.start();
55         Future<?> serverStarted = executorService.submit(() -> {
56             while (true) {
57                 try {
58                     Thread.sleep(10);
59                     if (_server.isStarted()) {
60                         return;
61                     }
62                 } catch (InterruptedException e) {
63                 }
64             }
65         });
66         serverStarted.get(30, TimeUnit.SECONDS);
67     }
68
69     protected void configureServer() throws Exception {
70         Path jksPath = Paths.get(TestCbamTokenProvider.class.getResource("/unittests/localhost.jks").toURI());
71         String path = jksPath.normalize().toAbsolutePath().toUri().toString();
72         _server = new Server();
73         SslContextFactory factory = new SslContextFactory(path);
74         factory.setKeyStorePassword("changeit");
75         NetworkTrafficServerConnector connector = new NetworkTrafficServerConnector(_server, factory);
76         connector.setHost("127.0.0.1");
77         _server.addConnector(connector);
78         _server.setHandler(new AbstractHandler() {
79             @Override
80             public void handle(String target, org.eclipse.jetty.server.Request request, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException, ServletException {
81                 requests.add(new String(ByteStreams.toByteArray(request.getInputStream())));
82                 httpServletResponse.getWriter().write(respones.remove(0));
83                 httpServletResponse.setStatus(codes.remove(0));
84                 request.setHandled(true);
85             }
86         });
87     }
88
89     public void stop() throws Exception {
90         _server.stop();
91     }
92 }