3 * ============LICENSE_START=======================================================
5 * ================================================================================
6 * Copyright (C) 2017 AT&T Intellectual Property. 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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.dcae.restapi;
24 import java.io.IOException;
27 import javax.servlet.ServletException;
29 import org.apache.tomcat.util.codec.binary.Base64;
30 import org.onap.dcae.commonFunction.CommonStartup;
31 import org.onap.dcae.commonFunction.VESLogger;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 import com.att.nsa.apiServer.CommonServlet;
36 import com.att.nsa.configs.ConfigDbException;
37 import com.att.nsa.drumlin.service.framework.DrumlinErrorHandler;
38 import com.att.nsa.drumlin.service.framework.context.DrumlinRequestContext;
39 import com.att.nsa.drumlin.service.framework.routing.DrumlinRequestRouter;
40 import com.att.nsa.drumlin.service.framework.routing.playish.DrumlinPlayishRoutingFileSource;
41 import com.att.nsa.drumlin.service.standards.HttpStatusCodes;
42 import com.att.nsa.drumlin.till.nv.rrNvReadable;
43 import com.att.nsa.drumlin.till.nv.rrNvReadable.loadException;
44 import com.att.nsa.drumlin.till.nv.rrNvReadable.missingReqdSetting;
45 import com.att.nsa.security.NsaAuthenticator;
47 import com.att.nsa.security.authenticators.SimpleAuthenticator;
48 import com.att.nsa.security.db.simple.NsaSimpleApiKey;
51 public class RestfulCollectorServlet extends CommonServlet
55 public String authlist;
57 public RestfulCollectorServlet ( rrNvReadable settings ) throws loadException, missingReqdSetting
59 super ( settings, "collector", false );
60 authlist = settings.getString(CommonStartup.kSetting_authlist,null);
67 * This is called once at server start. Use it to init any shared objects and setup the route mapping.
70 protected void servletSetup () throws rrNvReadable.missingReqdSetting, rrNvReadable.invalidSettingValue, ServletException
72 super.servletSetup ();
76 // the base class provides a bunch of things like API authentication and ECOMP compliant
77 // logging. The Restful Collector likely doesn't need API authentication, so for now,
78 // we init the base class services with an in-memory (and empty!) config DB.
79 commonServletSetup ( ConfigDbType.MEMORY );
81 VESLogger.setUpEcompLogging();
83 // setup the servlet routing and error handling
84 final DrumlinRequestRouter drr = getRequestRouter ();
86 // you can tell the request router what to do when a particular kind of exception is thrown.
87 drr.setHandlerForException( IllegalArgumentException.class, new DrumlinErrorHandler()
90 public void handle ( DrumlinRequestContext ctx, Throwable cause )
92 sendJsonReply ( ctx, HttpStatusCodes.k400_badRequest, cause.getMessage() );
96 // load the routes from the config file
97 final URL routes = findStream ( "routes.conf" );
98 if ( routes == null ) throw new rrNvReadable.missingReqdSetting ( "No routing configuration." );
99 final DrumlinPlayishRoutingFileSource drs = new DrumlinPlayishRoutingFileSource ( routes );
100 drr.addRouteSource ( drs );
102 if (CommonStartup.authflag > 0) {
103 NsaAuthenticator<NsaSimpleApiKey> NsaAuth;
104 NsaAuth = AuthlistHandler(authlist);
106 this.getSecurityManager().addAuthenticator(NsaAuth);
109 log.info ( "Restful Collector Servlet is up." );
111 catch ( SecurityException e )
113 throw new ServletException ( e );
115 catch ( IOException e )
117 throw new ServletException ( e );
119 catch ( ConfigDbException e )
121 throw new ServletException ( e );
125 public NsaAuthenticator<NsaSimpleApiKey> AuthlistHandler (String authlist)
127 NsaAuthenticator<NsaSimpleApiKey> NsaAuth = new SimpleAuthenticator ();
128 if (authlist != null)
130 String authpair[] = authlist.split("\\|");
131 for (String pair: authpair) {
132 String lineid[] = pair.split(",");
133 String listauthid = lineid[0];
134 String listauthpwd = new String(Base64.decodeBase64(lineid[1]));
135 ((SimpleAuthenticator) NsaAuth).add(listauthid,listauthpwd);
139 else if (authid != null)
141 ((SimpleAuthenticator) NsaAuth).add(authid,authpwd);
145 //add a default test account
146 ((SimpleAuthenticator) NsaAuth).add("admin","collectorpasscode");
153 private static final long serialVersionUID = 1L;
154 private static final Logger log = LoggerFactory.getLogger ( RestfulCollectorServlet.class );