3 * ============LICENSE_START=======================================================
\r
5 * ================================================================================
\r
6 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
\r
7 * ================================================================================
\r
8 * Licensed under the Apache License, Version 2.0 (the "License");
\r
9 * you may not use this file except in compliance with the License.
\r
10 * You may obtain a copy of the License at
\r
12 * http://www.apache.org/licenses/LICENSE-2.0
\r
14 * Unless required by applicable law or agreed to in writing, software
\r
15 * distributed under the License is distributed on an "AS IS" BASIS,
\r
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
17 * See the License for the specific language governing permissions and
\r
18 * limitations under the License.
\r
19 * ============LICENSE_END=========================================================
\r
22 package org.onap.dcae.restapi;
\r
24 import java.io.IOException;
\r
25 import java.net.URL;
\r
27 import javax.servlet.ServletException;
\r
29 import org.apache.tomcat.util.codec.binary.Base64;
\r
30 import org.onap.dcae.commonFunction.CommonStartup;
\r
31 import org.onap.dcae.commonFunction.VESLogger;
\r
32 import org.slf4j.Logger;
\r
33 import org.slf4j.LoggerFactory;
\r
35 import com.att.nsa.apiServer.CommonServlet;
\r
36 import com.att.nsa.configs.ConfigDbException;
\r
37 import com.att.nsa.drumlin.service.framework.DrumlinErrorHandler;
\r
38 import com.att.nsa.drumlin.service.framework.context.DrumlinRequestContext;
\r
39 import com.att.nsa.drumlin.service.framework.routing.DrumlinRequestRouter;
\r
40 import com.att.nsa.drumlin.service.framework.routing.playish.DrumlinPlayishRoutingFileSource;
\r
41 import com.att.nsa.drumlin.service.standards.HttpStatusCodes;
\r
42 import com.att.nsa.drumlin.till.nv.rrNvReadable;
\r
43 import com.att.nsa.drumlin.till.nv.rrNvReadable.loadException;
\r
44 import com.att.nsa.drumlin.till.nv.rrNvReadable.missingReqdSetting;
\r
45 import com.att.nsa.security.NsaAuthenticator;
\r
47 import com.att.nsa.security.authenticators.SimpleAuthenticator;
\r
48 import com.att.nsa.security.db.simple.NsaSimpleApiKey;
\r
50 public class RestfulCollectorServlet extends CommonServlet
\r
53 public static String authlist;
\r
55 public RestfulCollectorServlet ( rrNvReadable settings ) throws loadException, missingReqdSetting
\r
57 super ( settings, "collector", false );
\r
58 authlist = settings.getString(CommonStartup.KSETTING_AUTHLIST,null);
\r
65 * This is called once at server start. Use it to init any shared objects and setup the route mapping.
\r
68 protected void servletSetup () throws rrNvReadable.missingReqdSetting, rrNvReadable.invalidSettingValue, ServletException
\r
70 super.servletSetup ();
\r
74 // the base class provides a bunch of things like API authentication and ECOMP compliant
\r
75 // logging. The Restful Collector likely doesn't need API authentication, so for now,
\r
76 // we init the base class services with an in-memory (and empty!) config DB.
\r
77 commonServletSetup ( ConfigDbType.MEMORY );
\r
79 VESLogger.setUpEcompLogging();
\r
81 // setup the servlet routing and error handling
\r
82 final DrumlinRequestRouter drr = getRequestRouter ();
\r
84 // you can tell the request router what to do when a particular kind of exception is thrown.
\r
85 drr.setHandlerForException( IllegalArgumentException.class, new DrumlinErrorHandler()
\r
88 public void handle ( DrumlinRequestContext ctx, Throwable cause )
\r
90 sendJsonReply ( ctx, HttpStatusCodes.k400_badRequest, cause.getMessage() );
\r
94 // load the routes from the config file
\r
95 final URL routes = findStream ( "routes.conf" );
\r
96 if ( routes == null ) throw new rrNvReadable.missingReqdSetting ( "No routing configuration." );
\r
97 final DrumlinPlayishRoutingFileSource drs = new DrumlinPlayishRoutingFileSource ( routes );
\r
98 drr.addRouteSource ( drs );
\r
100 if (CommonStartup.authflag > 0) {
\r
101 NsaAuthenticator<NsaSimpleApiKey> NsaAuth;
\r
102 NsaAuth = AuthlistHandler(authlist);
\r
104 this.getSecurityManager().addAuthenticator(NsaAuth);
\r
107 log.info ( "Restful Collector Servlet is up." );
\r
109 catch ( SecurityException e )
\r
111 throw new ServletException ( e );
\r
113 catch ( IOException e )
\r
115 throw new ServletException ( e );
\r
117 catch ( ConfigDbException e )
\r
119 throw new ServletException ( e );
\r
123 public NsaAuthenticator<NsaSimpleApiKey> AuthlistHandler (String authlist)
\r
125 NsaAuthenticator<NsaSimpleApiKey> NsaAuth = new SimpleAuthenticator ();
\r
126 if (authlist != null)
\r
128 String authpair[] = authlist.split("\\|");
\r
129 for (String pair: authpair) {
\r
130 String lineid[] = pair.split(",");
\r
131 String listauthid = lineid[0];
\r
132 String listauthpwd = new String(Base64.decodeBase64(lineid[1]));
\r
133 ((SimpleAuthenticator) NsaAuth).add(listauthid,listauthpwd);
\r
139 //add a default test account
\r
140 ((SimpleAuthenticator) NsaAuth).add("admin","collectorpasscode");
\r
147 private static final long serialVersionUID = 1L;
\r
148 private static final Logger log = LoggerFactory.getLogger ( RestfulCollectorServlet.class );
\r